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 checked shifts #21

Merged
merged 2 commits into from
Jan 18, 2018
Merged

Add checked shifts #21

merged 2 commits into from
Jan 18, 2018

Conversation

fabianschuiki
Copy link
Contributor

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.

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.
Copy link
Member

@cuviper cuviper left a 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>
Copy link
Member

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.

@@ -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> {
Copy link
Member

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.
@fabianschuiki
Copy link
Contributor Author

How about now?

@cuviper
Copy link
Member

cuviper commented Jan 18, 2018

Looks good, thanks!

bors r+

bors bot added a commit that referenced this pull request Jan 18, 2018
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.
@bors
Copy link
Contributor

bors bot commented Jan 18, 2018

Build succeeded

@bors bors bot merged commit 809ccff into rust-num:master Jan 18, 2018
@fabianschuiki fabianschuiki deleted the checked-shift branch January 18, 2018 11:50
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.

None yet

2 participants