Skip to content

Commit acb1a26

Browse files
committed
Implement FromPrimitive for NonZero*
1 parent 397037d commit acb1a26

File tree

2 files changed

+97
-12
lines changed

2 files changed

+97
-12
lines changed

src/cast.rs

+79
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,85 @@ impl_from_primitive!(u128, to_u128);
631631
impl_from_primitive!(f32, to_f32);
632632
impl_from_primitive!(f64, to_f64);
633633

634+
macro_rules! impl_from_primitive_nonzero {
635+
($T:ty, $to_ty:ident) => {
636+
#[allow(deprecated)]
637+
impl FromPrimitive for $T {
638+
#[inline]
639+
fn from_isize(n: isize) -> Option<$T> {
640+
n.$to_ty().and_then(Self::new)
641+
}
642+
#[inline]
643+
fn from_i8(n: i8) -> Option<$T> {
644+
n.$to_ty().and_then(Self::new)
645+
}
646+
#[inline]
647+
fn from_i16(n: i16) -> Option<$T> {
648+
n.$to_ty().and_then(Self::new)
649+
}
650+
#[inline]
651+
fn from_i32(n: i32) -> Option<$T> {
652+
n.$to_ty().and_then(Self::new)
653+
}
654+
#[inline]
655+
fn from_i64(n: i64) -> Option<$T> {
656+
n.$to_ty().and_then(Self::new)
657+
}
658+
#[inline]
659+
fn from_i128(n: i128) -> Option<$T> {
660+
n.$to_ty().and_then(Self::new)
661+
}
662+
663+
#[inline]
664+
fn from_usize(n: usize) -> Option<$T> {
665+
n.$to_ty().and_then(Self::new)
666+
}
667+
#[inline]
668+
fn from_u8(n: u8) -> Option<$T> {
669+
n.$to_ty().and_then(Self::new)
670+
}
671+
#[inline]
672+
fn from_u16(n: u16) -> Option<$T> {
673+
n.$to_ty().and_then(Self::new)
674+
}
675+
#[inline]
676+
fn from_u32(n: u32) -> Option<$T> {
677+
n.$to_ty().and_then(Self::new)
678+
}
679+
#[inline]
680+
fn from_u64(n: u64) -> Option<$T> {
681+
n.$to_ty().and_then(Self::new)
682+
}
683+
#[inline]
684+
fn from_u128(n: u128) -> Option<$T> {
685+
n.$to_ty().and_then(Self::new)
686+
}
687+
688+
#[inline]
689+
fn from_f32(n: f32) -> Option<$T> {
690+
n.$to_ty().and_then(Self::new)
691+
}
692+
#[inline]
693+
fn from_f64(n: f64) -> Option<$T> {
694+
n.$to_ty().and_then(Self::new)
695+
}
696+
}
697+
};
698+
}
699+
700+
impl_from_primitive_nonzero!(NonZeroIsize, to_isize);
701+
impl_from_primitive_nonzero!(NonZeroI8, to_i8);
702+
impl_from_primitive_nonzero!(NonZeroI16, to_i16);
703+
impl_from_primitive_nonzero!(NonZeroI32, to_i32);
704+
impl_from_primitive_nonzero!(NonZeroI64, to_i64);
705+
impl_from_primitive_nonzero!(NonZeroI128, to_i128);
706+
impl_from_primitive_nonzero!(NonZeroUsize, to_usize);
707+
impl_from_primitive_nonzero!(NonZeroU8, to_u8);
708+
impl_from_primitive_nonzero!(NonZeroU16, to_u16);
709+
impl_from_primitive_nonzero!(NonZeroU32, to_u32);
710+
impl_from_primitive_nonzero!(NonZeroU64, to_u64);
711+
impl_from_primitive_nonzero!(NonZeroU128, to_u128);
712+
634713
macro_rules! impl_to_primitive_wrapping {
635714
($( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
636715
#[inline]

tests/cast.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,23 @@ fn cast_to_int_checks_overflow() {
118118
assert_eq!(Some(normal_f as i64), cast::<f64, i64>(normal_f));
119119

120120
assert_eq!(
121-
NonZeroIsize::new(normal_f as isize),
121+
NonZeroIsize::from_isize(normal_f as isize),
122122
cast::<f64, NonZeroIsize>(normal_f)
123123
);
124124
assert_eq!(
125-
NonZeroI8::new(normal_f as i8),
125+
NonZeroI8::from_i8(normal_f as i8),
126126
cast::<f64, NonZeroI8>(normal_f)
127127
);
128128
assert_eq!(
129-
NonZeroI16::new(normal_f as i16),
129+
NonZeroI16::from_i16(normal_f as i16),
130130
cast::<f64, NonZeroI16>(normal_f)
131131
);
132132
assert_eq!(
133-
NonZeroI32::new(normal_f as i32),
133+
NonZeroI32::from_i32(normal_f as i32),
134134
cast::<f64, NonZeroI32>(normal_f)
135135
);
136136
assert_eq!(
137-
NonZeroI64::new(normal_f as i64),
137+
NonZeroI64::from_i64(normal_f as i64),
138138
cast::<f64, NonZeroI64>(normal_f)
139139
);
140140

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

177177
assert_eq!(
178-
NonZeroUsize::new(normal_f as usize),
178+
NonZeroUsize::from_usize(normal_f as usize),
179179
cast::<f64, NonZeroUsize>(normal_f)
180180
);
181181
assert_eq!(
182-
NonZeroU8::new(normal_f as u8),
182+
NonZeroU8::from_u8(normal_f as u8),
183183
cast::<f64, NonZeroU8>(normal_f)
184184
);
185185
assert_eq!(
186-
NonZeroU16::new(normal_f as u16),
186+
NonZeroU16::from_u16(normal_f as u16),
187187
cast::<f64, NonZeroU16>(normal_f)
188188
);
189189
assert_eq!(
190-
NonZeroU32::new(normal_f as u32),
190+
NonZeroU32::from_u32(normal_f as u32),
191191
cast::<f64, NonZeroU32>(normal_f)
192192
);
193193
assert_eq!(
194-
NonZeroU64::new(normal_f as u64),
194+
NonZeroU64::from_u64(normal_f as u64),
195195
cast::<f64, NonZeroU64>(normal_f)
196196
);
197197

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

225225
assert_eq!(
226-
NonZeroI128::new(normal_f as i128),
226+
NonZeroI128::from_i128(normal_f as i128),
227227
cast::<f64, NonZeroI128>(normal_f)
228228
);
229229
assert_eq!(
230-
NonZeroU128::new(normal_f as u128),
230+
NonZeroU128::from_u128(normal_f as u128),
231231
cast::<f64, NonZeroU128>(normal_f)
232232
);
233233

@@ -577,6 +577,9 @@ fn newtype_from_primitive() {
577577
}
578578
check!(i8 i16 i32 i64 isize);
579579
check!(u8 u16 u32 u64 usize);
580+
581+
check!(NonZeroI8 NonZeroI16 NonZeroI32 NonZeroI64 NonZeroIsize);
582+
check!(NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroUsize);
580583
}
581584

582585
#[test]
@@ -615,4 +618,7 @@ fn newtype_to_primitive() {
615618
}
616619
check!(i8 i16 i32 i64 isize);
617620
check!(u8 u16 u32 u64 usize);
621+
622+
check!(NonZeroI8 NonZeroI16 NonZeroI32 NonZeroI64 NonZeroIsize);
623+
check!(NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroUsize);
618624
}

0 commit comments

Comments
 (0)