-
Notifications
You must be signed in to change notification settings - Fork 135
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
Implement Bounded, ToPrimitive, NumCast, FromPrimitive for NonZero* #334
base: master
Are you sure you want to change the base?
Conversation
03bb963
to
eee8ac0
Compare
@acrrd |
eee8ac0
to
7f6c0e5
Compare
Curious about https://doc.rust-lang.org/stable/core/num/struct.NonZero.html as well (might need an MSRV-preserving feature-gate) |
done |
This needs nightly, |
@acrrd though the https://doc.rust-lang.org/stable/core/num/trait.ZeroablePrimitive.html#implementors Quite a bit of the functionality is already stable, as is noted in the feature gates in the rustdoc. Here's a small example: |
The old types are just aliases now, literally |
I'm torn on So if we add that, shouldn't we also do it for |
It was requested in #335, they have a use case for it. Let me know if you want to keep it.
We can't. The trait requires |
I'm going to respond there, because I think that use is also problematic, even aside from
I wasn't talking about any other traits, only |
7f6c0e5
to
acb1a26
Compare
done
I see it now, sorry |
To me, a big part of the value of add-on crates like the
For example, the concept you mentioned of "types where you could literally write x as T" is not expressible using any combination of traits from the standard library.
I think the difference is that Whereas |
This seems consistent to me -- Anyway, let me give this PR an actual review... |
src/bounds.rs
Outdated
const $i: $t = { | ||
let arr = [$v]; | ||
let idx = if $v != 0 { 0 } else { 1 }; | ||
|
||
unsafe { <$t>::new_unchecked(arr[idx]) } | ||
}; |
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 can be done safely -- NonZero::new
is const
since 1.47, and const panic!
since 1.57. (Our MSRV is 1.60.)
const $i: $t = { | |
let arr = [$v]; | |
let idx = if $v != 0 { 0 } else { 1 }; | |
unsafe { <$t>::new_unchecked(arr[idx]) } | |
}; | |
const $i: $t = match <$t>::new($v) { | |
Some(nz) => nz, | |
None => panic!("bad nonzero bound!"), | |
}; |
And once our MSRV is 1.70 we should use the inherent consts, so please leave a comment for that.
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.
Nice, done.
src/cast.rs
Outdated
macro_rules! impl_to_primitive_nonzero_to_method { | ||
($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$( | ||
#[inline] | ||
$(#[$cfg])* |
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 guess you copied the cfg
from the other macros, but it's vestigial, from the days when i128
was conditional. We definitely shouldn't propagate that, but could you clean up the others too?
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 wasn't sure what it was so I just kept it. Removed here and from other macros
src/cast.rs
Outdated
|
||
#[inline] | ||
fn to_f32(&self) -> Option<f32> { | ||
Some(self.get() as f32) |
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.
Why isn't this forwarding to the inner to_f32
as well?
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, I think I copied from the other macro and didn't notice here I could put everything together.
src/cast.rs
Outdated
@@ -572,6 +631,85 @@ impl_from_primitive!(u128, to_u128); | |||
impl_from_primitive!(f32, to_f32); | |||
impl_from_primitive!(f64, to_f64); | |||
|
|||
macro_rules! impl_from_primitive_nonzero { | |||
($T:ty, $to_ty:ident) => { | |||
#[allow(deprecated)] |
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 this is also vestigial, way back from commit 03db5c9 when it was copied from its deprecated state in the standard library.
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.
Done
I have a use case where I convert to and from primitive, and I need to involve
NonZero*
types as well.Currently, I have a function that uses these traits for primitive types, and I have additional functions for each
NonZero*
that I need. It would be nice to get rid of them and have only one.I can create on PR per trait implementation if it's preferred. This PR has one commit per trait implementation.