-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 Saturating type (based on Wrapping type) #87921
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
i'm pretty sure the implementation of while the methods can have a separate library feature, the trait impls (BitAnd, BitOr, BitXor, Shl, Shr) must be stabilized together with |
I added naive shift (and remainder) impls. What needs to ben done next? |
i don't think it makes sense to |
Well... you are actually right about that 😅 |
I think this is wrong in case of |
This comment has been minimized.
This comment has been minimized.
/// # Examples | ||
/// | ||
/// Basic usage: | ||
/// | ||
/// ``` | ||
/// #![feature(saturating_int_impl, saturating_div)] | ||
/// use std::num::Saturating; | ||
/// | ||
#[doc = concat!("assert_eq!(Saturating(2", stringify!($t), "), Saturating(5", stringify!($t), ") / Saturating(2));")] | ||
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MAX), Saturating(", stringify!($t), "::MAX) / Saturating(1));")] | ||
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN), Saturating(", stringify!($t), "::MIN) / Saturating(1));")] | ||
/// ``` | ||
/// | ||
/// ```should_panic | ||
/// #![feature(saturating_int_impl, saturating_div)] | ||
/// use std::num::Saturating; | ||
/// | ||
#[doc = concat!("let _ = Saturating(0", stringify!($t), ") / Saturating(0);")] | ||
/// ``` | ||
#[unstable(feature = "saturating_int_impl", issue = "87920")] |
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.
Not sure whether to keep this doc(-tests)
What are the next steps? :) |
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.
rest LGTM
library/core/src/num/saturating.rs
Outdated
if other < 0 { | ||
Saturating(self.0.shr((-other & self::shift_max::$t as $f) as u32)) | ||
} else { | ||
Saturating(self.0.shl((other & self::shift_max::$t as $f) as u32)) | ||
} |
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.
🤔 this implementation is strange.
For other >= 0
, the implementation is the same as self.0 << other
. This is fine.
For other < 0
, however, this becomes self.0 >> -other
, which is never possible for the <<
operation.
I expected this either performs Ⓐ self.0 << other
, or Ⓑ self.0.wrapping_shl(other)
, or Ⓒ self.0 << other.clamp(0, self::shift_max)
, or Ⓓ don't implement Shl<signed>
at all.
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.
I've gone for Ⓓ because it does not seem clear (to me) what the correct impl should look like. I'd rather have someone figure it out later, than implementing it wrong now.
thanks! @bors r+ |
📌 Commit ce636f2 has been approved by |
☀️ Test successful - checks-actions |
Stabilize feature `saturating_div` for rust 1.58.0 The tracking issue is rust-lang#89381 This seems like a reasonable simple change(?). The feature `saturating_div` was added as part of the ongoing effort to implement a `Saturating` integer type (see rust-lang#87921). The implementation has been discussed [here](rust-lang#87921 (comment)) and [here](rust-lang#87921 (comment)). It extends the list of saturating operations on integer types (like `saturating_add`, `saturating_sub`, `saturating_mul`, ...) by the function `fn saturating_div(self, rhs: Self) -> Self`. The stabilization of the feature `saturating_int_impl` (for the `Saturating` type) needs to have this stabilized first. Closes rust-lang#89381
Stabilize feature `saturating_div` for rust 1.58.0 The tracking issue is rust-lang#89381 This seems like a reasonable simple change(?). The feature `saturating_div` was added as part of the ongoing effort to implement a `Saturating` integer type (see rust-lang#87921). The implementation has been discussed [here](rust-lang#87921 (comment)) and [here](rust-lang#87921 (comment)). It extends the list of saturating operations on integer types (like `saturating_add`, `saturating_sub`, `saturating_mul`, ...) by the function `fn saturating_div(self, rhs: Self) -> Self`. The stabilization of the feature `saturating_int_impl` (for the `Saturating` type) needs to have this stabilized first. Closes rust-lang#89381
Tracking #87920
Unresolved Questions
impl Div for Saturating<T>
falls back on inner integer division - which seems alright?saturating_div
? (to respect division by-1
)::saturating_shl
and::saturating_shr
. (How to) implementShl
,ShlAssign
,Shr
andShrAssign
?saturating_neg
is only implemented on signed integer typesWrapping
-type correct forSaturating
?Saturating::rotate_left
Saturating::rotate_right
Not
BitXorOr
andBitXorOrAssign
BitOr
andBitOrAssign
BitAnd
andBitAndAssign
Saturating::swap_bytes
Saturating::reverse_bits