Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rsl::StrongType #87

Merged
merged 1 commit into from
Nov 10, 2023
Merged

Add rsl::StrongType #87

merged 1 commit into from
Nov 10, 2023

Conversation

ChrisThrasher
Copy link
Collaborator

@ChrisThrasher ChrisThrasher commented May 25, 2023

Strong type aliases are a useful pattern but they require just enough boilerplate that you don't typically see codebases implement them and they're just small and non-specific enough that libraries don't provide them. This type aims to fill that gap so that we can actually use strong type aliases in our code.

Motivating example:

using JointID = rsl::StrongType<std::uint64_t, struct JointIDTag>;

struct Joint {
    JointID id;
    int count;
};

auto joint = Joint{JointID(4), 2};
auto joint = Joint{2, JointID(4)}; // Won't compile

The presence of a strong type alias ensures that the order of arguments is correct. Without that strong alias, it would be easy to accidentally swap the two arguments without realizing it.

@codecov
Copy link

codecov bot commented May 25, 2023

Codecov Report

Merging #87 (9296fdc) into main (5c0d774) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main      #87      +/-   ##
==========================================
+ Coverage   99.05%   99.06%   +0.01%     
==========================================
  Files          10       11       +1     
  Lines         211      215       +4     
==========================================
+ Hits          209      213       +4     
  Misses          2        2              
Flag Coverage Δ
humble 99.06% <100.00%> (+0.01%) ⬆️
rolling 99.06% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
include/rsl/strong_type.hpp 100.00% <100.00%> (ø)

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

include/rsl/type.hpp Outdated Show resolved Hide resolved
include/rsl/type.hpp Outdated Show resolved Hide resolved
@ChrisThrasher
Copy link
Collaborator Author

I added constexpr support along with tests with non-constexpr types to confirm everything still works.

@ChrisThrasher ChrisThrasher force-pushed the rsl_type branch 3 times, most recently from 743b547 to 0ed47ec Compare November 10, 2023 21:26
@ChrisThrasher ChrisThrasher changed the title Add rsl::Type Add rsl::StrongType Nov 10, 2023
@ChrisThrasher ChrisThrasher marked this pull request as ready for review November 10, 2023 21:27
@ChrisThrasher ChrisThrasher force-pushed the rsl_type branch 2 times, most recently from 8cb1c03 to 8d97fdb Compare November 10, 2023 21:40
@ChrisThrasher
Copy link
Collaborator Author

Added missing Doxygen comments

@ChrisThrasher ChrisThrasher merged commit 9a69d04 into main Nov 10, 2023
35 checks passed
@delete-merged-branch delete-merged-branch bot deleted the rsl_type branch November 10, 2023 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants