-
Notifications
You must be signed in to change notification settings - Fork 148
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
Saturating<T> wrapper type #175
Conversation
|
||
#[inline(always)] | ||
fn max_value() -> Self { | ||
Saturating(<$t>::min_value()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh this doesn't look right...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops
I'm not sure that I agree that implementing |
@cuviper I guess it depends on the number of people who think it would be useful, but it does make more sense to provide both As for usefulness in the generic case I have some code which is read from external input where I do not mind if the values are saturated if they are out of bounds, here it is useful with a saturating datatype since I do not have to manually perform checks to make sure that the data I read is within the range of the datatype. |
Would be nice to have traits for wrapping_add(), saturating_add(), etc, and have a Wrapping, Saturating, etc impl for them. But that's another issue. |
@m4rw3r then will you propose this for the standard library instead? |
@cuviper Submitted as an RFC here: rust-lang/rfcs#1534 this also includes the always-checked counterpart too. |
☔ The latest upstream changes (presumably #164) made this pull request unmergeable. Please resolve the merge conflicts. |
If this is ever revived, it will need to be on the separate repo: |
Rust's stdlib has a
Wrapping<T>
wrapper type for primitive integer types. The type provides intentionally wrapping semantics to all operations performed on it.This pull-request introduces a
Saturating<T>
wrapper type which is the corresponding saturating wrapper type for primitive integer types. This type provides saturating semantics on arithmetic and bit-shifting operations for both signed and unsigned integers.The goal is to provide a type which is easy to slot in instead of either
Wrapping<T>
or any basic integer type. Combined with the traits innum
it would be possible to make functions/methods generic over the type of integer and if it should have wrapping or saturating semantics.The name
Saturating<T>
collides with the traitSaturating
which is unfortunate. I suspect that this type will need a name change but at the moment I do not have any suggestions.