Skip to content
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

Add TryFrom<{int}> for NonZero{int} #72717

Merged
merged 5 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/libcore/convert/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,40 @@ nzint_impl_from! { NonZeroU16, NonZeroI128, #[stable(feature = "nz_int_conv", si
nzint_impl_from! { NonZeroU32, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
nzint_impl_from! { NonZeroU32, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
nzint_impl_from! { NonZeroU64, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }

macro_rules! nzint_impl_try_from_int {
($Int: ty, $NonZeroInt: ty, #[$attr:meta], $doc: expr) => {
#[$attr]
#[doc = $doc]
impl TryFrom<$Int> for $NonZeroInt {
type Error = TryFromIntError;

#[inline]
fn try_from(value: $Int) -> Result<Self, Self::Error> {
Self::new(value).ok_or(TryFromIntError(()))
}
}
};
($Int: ty, $NonZeroInt: ty, #[$attr:meta]) => {
nzint_impl_try_from_int!($Int,
$NonZeroInt,
#[$attr],
concat!("Attempts to convert `",
stringify!($Int),
"` to `",
stringify!($NonZeroInt),
"`."));
}
}

// Int -> Non-zero Int
nzint_impl_try_from_int! { u8, NonZeroU8, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
nzint_impl_try_from_int! { u16, NonZeroU16, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
nzint_impl_try_from_int! { u32, NonZeroU32, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
nzint_impl_try_from_int! { u64, NonZeroU64, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
nzint_impl_try_from_int! { u128, NonZeroU128, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
nzint_impl_try_from_int! { i8, NonZeroI8, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
nzint_impl_try_from_int! { i16, NonZeroI16, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
nzint_impl_try_from_int! { i32, NonZeroI32, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
nzint_impl_try_from_int! { i64, NonZeroI64, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
nzint_impl_try_from_int! { i128, NonZeroI128, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
poliorcetics marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 19 additions & 1 deletion src/libcore/tests/nonzero.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
use core::num::{TryFromIntError, IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
use core::option::Option::{self, None, Some};
use std::mem::size_of;

Expand Down Expand Up @@ -176,3 +176,21 @@ fn test_nonzero_bitor_assign() {
target |= 0;
assert_eq!(target.get(), 0b1011_1111);
}

#[test]
fn test_nonzero_from_int_on_success() {
assert_eq!(NonZeroU8::try_from(5), Ok(NonZeroU8::new(5)));
assert_eq!(NonZeroU32::try_from(5), Ok(NonZeroU32::new(5)));

assert_eq!(NonZeroI8::try_from(-5), Ok(NonZeroI8::new(-5)));
assert_eq!(NonZeroI32::try_from(-5), Ok(NonZeroI32::new(-5)));
}

#[test]
fn test_nonzero_from_int_on_err() {
assert_eq!(NonZeroU8::try_from(0), Err(TryFromIntError(())));
assert_eq!(NonZeroU32::try_from(0), Err(TryFromIntError(())));

assert_eq!(NonZeroI8::try_from(0), Err(TryFromIntError(())));
assert_eq!(NonZeroI32::try_from(0), Err(TryFromIntError(())));
}