Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

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

Merged
homu merged 5 commits into
rust-num:masterfrom
yoanlcq:impl-num-for-wrapping
Apr 21, 2017
Merged

homu merged 5 commits into
rust-num:masterfrom
yoanlcq:impl-num-for-wrapping

Conversation

@yoanlcq

@yoanlcq yoanlcq commented Apr 21, 2017

Copy link
Copy Markdown
Contributor

Attempts to fix issue #278

Comment thread traits/src/lib.rs

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

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

@cuviper cuviper left a comment

Copy link
Copy Markdown
Member

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.

Comment thread traits/src/identities.rs Outdated
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
Copy Markdown
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.

Comment thread traits/src/identities.rs Outdated

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
Copy Markdown
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.

Comment thread traits/src/lib.rs Outdated
{
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
Copy Markdown
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
Copy Markdown
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!

Comment thread traits/src/lib.rs

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

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

Comment thread traits/src/identities.rs Outdated
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
Copy Markdown
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
Copy Markdown
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

cuviper commented Apr 21, 2017

Copy link
Copy Markdown
Member

Thanks!

@homu r+ 2172a93

@homu

homu commented Apr 21, 2017

Copy link
Copy Markdown
Contributor

⚡ 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