-
Notifications
You must be signed in to change notification settings - Fork 140
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 checked shifts #21
Conversation
Add traits `CheckedShl` and `CheckedShr` that correspond to the standard library's `checked_shl` and `checked_shr` functions. Implement the trait on all primitive integer types by default, akin to what the standard library does. The stdlib is somewhat inconsistent when it comes to the type of the shift amount. The `checked_*` functions have a `u32` shift amount, but the `std::ops::{Shl,Shr}` traits are generic over the shift amount. Also the stdlib implements these traits for all primitive integer types as right-hand sides. Our implementation mimics this behaviour.
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'd like to scale this back a little bit as described in other comments, and we also need a little testing please!
src/int.rs
Outdated
@@ -21,6 +21,8 @@ pub trait PrimInt | |||
+ CheckedSub<Output=Self> | |||
+ CheckedMul<Output=Self> | |||
+ CheckedDiv<Output=Self> | |||
+ CheckedShl<usize, Output=Self> | |||
+ CheckedShr<usize, Output=Self> |
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.
Unfortunately, this is a breaking change. We don't control all implementations of PrimInt
, so adding new constraints would break others, however theoretical such others may be.
src/ops/checked.rs
Outdated
@@ -90,3 +90,54 @@ checked_impl!(CheckedDiv, checked_div, i32); | |||
checked_impl!(CheckedDiv, checked_div, i64); | |||
checked_impl!(CheckedDiv, checked_div, isize); | |||
|
|||
/// Performs a left shift that returns `None` on overflow. | |||
pub trait CheckedShl<RHS>: Sized + Shl<RHS, Output=Self> { |
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 think for now, I'd rather keep these consistent with the other Checked
traits that are not parameterized, especially since the standard library methods are only for u32
. That also moots your comment below about needing checked conversions to u32
.
(We could decide to expand it later using a default, like CheckedShl<RHS = u32>
.)
Make the checked left and right shifts take a `u32` as right-hand side, which is more consistent with the other checked operations. Also drop `CheckedShl` and `CheckedShr` from the `PrimInt` trait, to not break existing code. Add doctests for the two traits.
How about now? |
Looks good, thanks! bors r+ |
17: Add AsPrimitive trait for generic casting with `as` r=cuviper a=Enet4 This is my personal attempt at #7. It is fairly similar to what can be found in `asprim`, although implemented from scratch. Please let me know of what you think. Could it use more tests? Should I also leave a safety notice that some conversions with `as` are currently UB (rust-lang/rust#10184)? 21: Add checked shifts r=cuviper a=fabianschuiki Add traits `CheckedShl` and `CheckedShr` that correspond to the standard library's `checked_shl` and `checked_shr` functions. Implement the trait on all primitive integer types by default, akin to what the standard library does. The stdlib is somewhat inconsistent when it comes to the type of the shift amount. The `checked_*` functions have a `u32` shift amount, but the `std::ops::{Shl,Shr}` traits are generic over the shift amount. Also the stdlib implements these traits for all primitive integer types as right-hand sides. Our implementation mimics this behaviour.
Build succeeded |
Add traits
CheckedShl
andCheckedShr
that correspond to the standardlibrary's
checked_shl
andchecked_shr
functions. Implement the traiton all primitive integer types by default, akin to what the standard
library does.
The stdlib is somewhat inconsistent when it comes to the type of the
shift amount. The
checked_*
functions have au32
shift amount, butthe
std::ops::{Shl,Shr}
traits are generic over the shift amount. Alsothe stdlib implements these traits for all primitive integer types as
right-hand sides. Our implementation mimics this behaviour.