-
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?
Changes from 4 commits
22ce2dc
2b9af3d
397037d
acb1a26
542884d
d4e5b0a
b43a252
2ce0fb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
use core::mem::size_of; | ||
use core::num::Wrapping; | ||
use core::num::{ | ||
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128, | ||
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, | ||
}; | ||
use core::{f32, f64}; | ||
use core::{i128, i16, i32, i64, i8, isize}; | ||
use core::{u128, u16, u32, u64, u8, usize}; | ||
|
@@ -266,6 +270,61 @@ impl_to_primitive_uint!(u32); | |
impl_to_primitive_uint!(u64); | ||
impl_to_primitive_uint!(u128); | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I guess you copied the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
fn $method(&self) -> Option<$DstT> { | ||
self.get().$method() | ||
} | ||
)*} | ||
} | ||
|
||
macro_rules! impl_to_primitive_nonzero { | ||
($T:ident) => { | ||
impl ToPrimitive for $T { | ||
impl_to_primitive_nonzero_to_method! { $T: | ||
fn to_isize -> isize; | ||
fn to_i8 -> i8; | ||
fn to_i16 -> i16; | ||
fn to_i32 -> i32; | ||
fn to_i64 -> i64; | ||
fn to_i128 -> i128; | ||
|
||
fn to_usize -> usize; | ||
fn to_u8 -> u8; | ||
fn to_u16 -> u16; | ||
fn to_u32 -> u32; | ||
fn to_u64 -> u64; | ||
fn to_u128 -> u128; | ||
} | ||
|
||
#[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 commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't this forwarding to the inner There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
#[inline] | ||
fn to_f64(&self) -> Option<f64> { | ||
Some(self.get() as f64) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_to_primitive_nonzero!(NonZeroUsize); | ||
impl_to_primitive_nonzero!(NonZeroU8); | ||
impl_to_primitive_nonzero!(NonZeroU16); | ||
impl_to_primitive_nonzero!(NonZeroU32); | ||
impl_to_primitive_nonzero!(NonZeroU64); | ||
impl_to_primitive_nonzero!(NonZeroU128); | ||
|
||
impl_to_primitive_nonzero!(NonZeroIsize); | ||
impl_to_primitive_nonzero!(NonZeroI8); | ||
impl_to_primitive_nonzero!(NonZeroI16); | ||
impl_to_primitive_nonzero!(NonZeroI32); | ||
impl_to_primitive_nonzero!(NonZeroI64); | ||
impl_to_primitive_nonzero!(NonZeroI128); | ||
|
||
macro_rules! impl_to_primitive_float_to_float { | ||
($SrcT:ident : $( fn $method:ident -> $DstT:ident ; )*) => {$( | ||
#[inline] | ||
|
@@ -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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
impl FromPrimitive for $T { | ||
#[inline] | ||
fn from_isize(n: isize) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_i8(n: i8) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_i16(n: i16) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_i32(n: i32) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_i64(n: i64) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_i128(n: i128) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
|
||
#[inline] | ||
fn from_usize(n: usize) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_u8(n: u8) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_u16(n: u16) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_u32(n: u32) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_u64(n: u64) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_u128(n: u128) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
|
||
#[inline] | ||
fn from_f32(n: f32) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
#[inline] | ||
fn from_f64(n: f64) -> Option<$T> { | ||
n.$to_ty().and_then(Self::new) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_from_primitive_nonzero!(NonZeroIsize, to_isize); | ||
impl_from_primitive_nonzero!(NonZeroI8, to_i8); | ||
impl_from_primitive_nonzero!(NonZeroI16, to_i16); | ||
impl_from_primitive_nonzero!(NonZeroI32, to_i32); | ||
impl_from_primitive_nonzero!(NonZeroI64, to_i64); | ||
impl_from_primitive_nonzero!(NonZeroI128, to_i128); | ||
impl_from_primitive_nonzero!(NonZeroUsize, to_usize); | ||
impl_from_primitive_nonzero!(NonZeroU8, to_u8); | ||
impl_from_primitive_nonzero!(NonZeroU16, to_u16); | ||
impl_from_primitive_nonzero!(NonZeroU32, to_u32); | ||
impl_from_primitive_nonzero!(NonZeroU64, to_u64); | ||
impl_from_primitive_nonzero!(NonZeroU128, to_u128); | ||
|
||
macro_rules! impl_to_primitive_wrapping { | ||
($( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$( | ||
#[inline] | ||
|
@@ -695,6 +833,34 @@ impl_num_cast!(isize, to_isize); | |
impl_num_cast!(f32, to_f32); | ||
impl_num_cast!(f64, to_f64); | ||
|
||
macro_rules! impl_num_cast_nonzero { | ||
($T:ty, $conv:ident) => { | ||
impl NumCast for $T { | ||
#[inline] | ||
#[allow(deprecated)] | ||
fn from<N: ToPrimitive>(n: N) -> Option<$T> { | ||
// `$conv` could be generated using `concat_idents!`, but that | ||
// macro seems to be broken at the moment | ||
n.$conv().and_then(Self::new) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_num_cast_nonzero!(NonZeroUsize, to_usize); | ||
impl_num_cast_nonzero!(NonZeroU8, to_u8); | ||
impl_num_cast_nonzero!(NonZeroU16, to_u16); | ||
impl_num_cast_nonzero!(NonZeroU32, to_u32); | ||
impl_num_cast_nonzero!(NonZeroU64, to_u64); | ||
impl_num_cast_nonzero!(NonZeroU128, to_u128); | ||
|
||
impl_num_cast_nonzero!(NonZeroIsize, to_isize); | ||
impl_num_cast_nonzero!(NonZeroI8, to_i8); | ||
impl_num_cast_nonzero!(NonZeroI16, to_i16); | ||
impl_num_cast_nonzero!(NonZeroI32, to_i32); | ||
impl_num_cast_nonzero!(NonZeroI64, to_i64); | ||
impl_num_cast_nonzero!(NonZeroI128, to_i128); | ||
|
||
impl<T: NumCast> NumCast for Wrapping<T> { | ||
fn from<U: ToPrimitive>(n: U) -> Option<Self> { | ||
T::from(n).map(Wrapping) | ||
|
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
isconst
since 1.47, andconst panic!
since 1.57. (Our MSRV is 1.60.)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.