Skip to content

Commit

Permalink
Implement FromPrimitive for NonZero*
Browse files Browse the repository at this point in the history
  • Loading branch information
acrrd committed Aug 30, 2024
1 parent 54c1e0b commit eee8ac0
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
79 changes: 79 additions & 0 deletions src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,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)]
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]
Expand Down
30 changes: 18 additions & 12 deletions tests/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,23 @@ fn cast_to_int_checks_overflow() {
assert_eq!(Some(normal_f as i64), cast::<f64, i64>(normal_f));

assert_eq!(
NonZeroIsize::new(normal_f as isize),
NonZeroIsize::from_isize(normal_f as isize),
cast::<f64, NonZeroIsize>(normal_f)
);
assert_eq!(
NonZeroI8::new(normal_f as i8),
NonZeroI8::from_i8(normal_f as i8),
cast::<f64, NonZeroI8>(normal_f)
);
assert_eq!(
NonZeroI16::new(normal_f as i16),
NonZeroI16::from_i16(normal_f as i16),
cast::<f64, NonZeroI16>(normal_f)
);
assert_eq!(
NonZeroI32::new(normal_f as i32),
NonZeroI32::from_i32(normal_f as i32),
cast::<f64, NonZeroI32>(normal_f)
);
assert_eq!(
NonZeroI64::new(normal_f as i64),
NonZeroI64::from_i64(normal_f as i64),
cast::<f64, NonZeroI64>(normal_f)
);

Expand Down Expand Up @@ -175,23 +175,23 @@ fn cast_to_unsigned_int_checks_overflow() {
assert_eq!(Some(normal_f as u64), cast::<f64, u64>(normal_f));

assert_eq!(
NonZeroUsize::new(normal_f as usize),
NonZeroUsize::from_usize(normal_f as usize),
cast::<f64, NonZeroUsize>(normal_f)
);
assert_eq!(
NonZeroU8::new(normal_f as u8),
NonZeroU8::from_u8(normal_f as u8),
cast::<f64, NonZeroU8>(normal_f)
);
assert_eq!(
NonZeroU16::new(normal_f as u16),
NonZeroU16::from_u16(normal_f as u16),
cast::<f64, NonZeroU16>(normal_f)
);
assert_eq!(
NonZeroU32::new(normal_f as u32),
NonZeroU32::from_u32(normal_f as u32),
cast::<f64, NonZeroU32>(normal_f)
);
assert_eq!(
NonZeroU64::new(normal_f as u64),
NonZeroU64::from_u64(normal_f as u64),
cast::<f64, NonZeroU64>(normal_f)
);

Expand Down Expand Up @@ -223,11 +223,11 @@ fn cast_to_i128_checks_overflow() {
assert_eq!(Some(normal_f as u128), cast::<f64, u128>(normal_f));

assert_eq!(
NonZeroI128::new(normal_f as i128),
NonZeroI128::from_i128(normal_f as i128),
cast::<f64, NonZeroI128>(normal_f)
);
assert_eq!(
NonZeroU128::new(normal_f as u128),
NonZeroU128::from_u128(normal_f as u128),
cast::<f64, NonZeroU128>(normal_f)
);

Expand Down Expand Up @@ -577,6 +577,9 @@ fn newtype_from_primitive() {
}
check!(i8 i16 i32 i64 isize);
check!(u8 u16 u32 u64 usize);

check!(NonZeroI8 NonZeroI16 NonZeroI32 NonZeroI64 NonZeroIsize);
check!(NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroUsize);
}

#[test]
Expand Down Expand Up @@ -615,4 +618,7 @@ fn newtype_to_primitive() {
}
check!(i8 i16 i32 i64 isize);
check!(u8 u16 u32 u64 usize);

check!(NonZeroI8 NonZeroI16 NonZeroI32 NonZeroI64 NonZeroIsize);
check!(NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroUsize);
}

0 comments on commit eee8ac0

Please sign in to comment.