Skip to content

Commit 50fc24d

Browse files
committed
Auto merge of #72717 - poliorcetics:try-from-int-to-nzint, r=dtolnay
Add TryFrom<{int}> for NonZero{int} Adds `TryFrom<{int}> for NonZero{int}`. It uses the existing `NonZero{int}::new()` and `Option::ok_or()` functions, meaning the checks are not repeated. I also added tests, I tried to follow the convention I saw in the test file. I also used `#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]`, but I have no idea if the feature/version are correctly named or even correct.
2 parents 9f3c96b + 6c8d8d6 commit 50fc24d

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/libcore/convert/num.rs

+39
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,42 @@ nzint_impl_from! { NonZeroU16, NonZeroI128, #[stable(feature = "nz_int_conv", si
445445
nzint_impl_from! { NonZeroU32, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
446446
nzint_impl_from! { NonZeroU32, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
447447
nzint_impl_from! { NonZeroU64, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
448+
449+
macro_rules! nzint_impl_try_from_int {
450+
($Int: ty, $NonZeroInt: ty, #[$attr:meta], $doc: expr) => {
451+
#[$attr]
452+
#[doc = $doc]
453+
impl TryFrom<$Int> for $NonZeroInt {
454+
type Error = TryFromIntError;
455+
456+
#[inline]
457+
fn try_from(value: $Int) -> Result<Self, Self::Error> {
458+
Self::new(value).ok_or(TryFromIntError(()))
459+
}
460+
}
461+
};
462+
($Int: ty, $NonZeroInt: ty, #[$attr:meta]) => {
463+
nzint_impl_try_from_int!($Int,
464+
$NonZeroInt,
465+
#[$attr],
466+
concat!("Attempts to convert `",
467+
stringify!($Int),
468+
"` to `",
469+
stringify!($NonZeroInt),
470+
"`."));
471+
}
472+
}
473+
474+
// Int -> Non-zero Int
475+
nzint_impl_try_from_int! { u8, NonZeroU8, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
476+
nzint_impl_try_from_int! { u16, NonZeroU16, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
477+
nzint_impl_try_from_int! { u32, NonZeroU32, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
478+
nzint_impl_try_from_int! { u64, NonZeroU64, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
479+
nzint_impl_try_from_int! { u128, NonZeroU128, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
480+
nzint_impl_try_from_int! { usize, NonZeroUsize, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
481+
nzint_impl_try_from_int! { i8, NonZeroI8, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
482+
nzint_impl_try_from_int! { i16, NonZeroI16, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
483+
nzint_impl_try_from_int! { i32, NonZeroI32, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
484+
nzint_impl_try_from_int! { i64, NonZeroI64, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
485+
nzint_impl_try_from_int! { i128, NonZeroI128, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
486+
nzint_impl_try_from_int! { isize, NonZeroIsize, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }

src/libcore/tests/nonzero.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::convert::TryFrom;
12
use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
23
use core::option::Option::{self, None, Some};
34
use std::mem::size_of;
@@ -176,3 +177,21 @@ fn test_nonzero_bitor_assign() {
176177
target |= 0;
177178
assert_eq!(target.get(), 0b1011_1111);
178179
}
180+
181+
#[test]
182+
fn test_nonzero_from_int_on_success() {
183+
assert_eq!(NonZeroU8::try_from(5), Ok(NonZeroU8::new(5).unwrap()));
184+
assert_eq!(NonZeroU32::try_from(5), Ok(NonZeroU32::new(5).unwrap()));
185+
186+
assert_eq!(NonZeroI8::try_from(-5), Ok(NonZeroI8::new(-5).unwrap()));
187+
assert_eq!(NonZeroI32::try_from(-5), Ok(NonZeroI32::new(-5).unwrap()));
188+
}
189+
190+
#[test]
191+
fn test_nonzero_from_int_on_err() {
192+
assert!(NonZeroU8::try_from(0).is_err());
193+
assert!(NonZeroU32::try_from(0).is_err());
194+
195+
assert!(NonZeroI8::try_from(0).is_err());
196+
assert!(NonZeroI32::try_from(0).is_err());
197+
}

0 commit comments

Comments
 (0)