Skip to content

Implemented Zero, One and Num for std::num::Wrapping<T> #279

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

Merged
merged 5 commits into from
Apr 21, 2017
Merged

Implemented Zero, One and Num for std::num::Wrapping<T> #279

merged 5 commits into from
Apr 21, 2017

Conversation

yoanlcq
Copy link
Contributor

@yoanlcq yoanlcq commented Apr 21, 2017

Attempts to fix issue #278

@@ -243,9 +256,9 @@ float_trait_impl!(Num for f32 f64);

/// A value bounded by a minimum and a maximum
///
/// If input is less than min then this returns min.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what caused these changes - didn't do these on purpose anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess your editor is set to strip trailing whitespace. That's OK.

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.

Just a couple minor nits.

@@ -50,6 +51,16 @@ zero_impl!(i64, 0i64);
zero_impl!(f32, 0.0f32);
zero_impl!(f64, 0.0f64);

impl<T: Zero + PartialEq> Zero for Wrapping<T> where Wrapping<T>: Add<Output=Wrapping<T>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I was worried that a generic impl would be a breaking change, in case someone had already implemented it for their own Foo and Wrapping<Foo>, but it looks like they couldn't do that:

    |
115 | impl Zero for Wrapping<Foo> {}
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use types inside crate
    |
    = note: the impl does not reference any types defined in this crate
    = note: define and implement a trait or new type instead

... even though Foo is defined on the line right before. I always forget exactly how coherence works. I think you can have this overlap with users if the custom type were a trait parameter instead.

Anyway, looks like this is OK.

@@ -50,6 +51,16 @@ zero_impl!(i64, 0i64);
zero_impl!(f32, 0.0f32);
zero_impl!(f64, 0.0f64);

impl<T: Zero + PartialEq> Zero for Wrapping<T> where Wrapping<T>: Add<Output=Wrapping<T>> {
fn is_zero(&self) -> bool {
self.0 == T::zero()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use self.0.is_zero() here.

{
type FromStrRadixErr = T::FromStrRadixErr;
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
T::from_str_radix(str, radix).map(|x| Wrapping(x))
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 this can just be map(Wrapping), but it doesn't make much difference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's a pretty nice trick!

@@ -243,9 +256,9 @@ float_trait_impl!(Num for f32 f64);

/// A value bounded by a minimum and a maximum
///
/// If input is less than min then this returns min.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess your editor is set to strip trailing whitespace. That's OK.

@@ -94,6 +105,11 @@ one_impl!(i64, 1i64);
one_impl!(f32, 1.0f32);
one_impl!(f64, 1.0f64);

impl<T: One> One for Wrapping<T> where Wrapping<T>: Add<Output=Wrapping<T>> + Mul<Output=Wrapping<T>> {
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 this only needs Mul, not Add.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vaguely remember that I had to put it for some reason, but now it should indeed be removed.

@cuviper
Copy link
Member

cuviper commented Apr 21, 2017

Thanks!

@homu r+ 2172a93

@homu
Copy link
Contributor

homu commented Apr 21, 2017

⚡ Test exempted - status

@homu homu merged commit 2172a93 into rust-num:master Apr 21, 2017
homu added a commit that referenced this pull request Apr 21, 2017
Implemented Zero, One and Num for std::num::Wrapping<T>

Attempts to fix issue #278
@yoanlcq yoanlcq deleted the impl-num-for-wrapping branch April 21, 2017 17:19
homu referenced this pull request May 7, 2017
impl remaining num-traits for std::num::Wrapping<T>

This is a (late) follow-up for [https://github.com/rust-num/num/pull/279](https://github.com/rust-num/num/pull/279) since I realized that implementing `Num` for `Wrapping<T>` was merely half of the work.

This PR makes `Wrapping<T>` implement the remaining appropriate traits, granting it the ability to really be used a complete substitute for its primitive integer counterparts.
Some benefits are :
- Less refactoring for users using `num`'s traits replacing some primitives by their `Wrapping` counterpart (same for the opposite);
- Since `Wrapping<T>` is from `std`, nobody except us can `impl` our traits for it, so people don't have to create their own.
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.

3 participants