Conversation
src/saturating.rs
Outdated
|
|
||
| #[inline(always)] | ||
| fn max_value() -> Self { | ||
| Saturating(<$t>::min_value()) |
|
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 innumit 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 traitSaturatingwhich is unfortunate. I suspect that this type will need a name change but at the moment I do not have any suggestions.