about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-06-25 17:37:02 +0000
committerbors <bors@rust-lang.org>2020-06-25 17:37:02 +0000
commit50fc24d8a172a853b5dfe40702d6550e3b8562ba (patch)
tree76deb7c9dc12ef5ae3500c5292d23161da098de4
parent9f3c96b869b48ecd0bb556c6ad9cd603b4dacfb9 (diff)
parent6c8d8d6a6055d19dc33cc8b33b082d17583a0e47 (diff)
downloadrust-50fc24d8a172a853b5dfe40702d6550e3b8562ba.tar.gz
rust-50fc24d8a172a853b5dfe40702d6550e3b8562ba.zip
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.
-rw-r--r--src/libcore/convert/num.rs39
-rw-r--r--src/libcore/tests/nonzero.rs19
2 files changed, 58 insertions, 0 deletions
diff --git a/src/libcore/convert/num.rs b/src/libcore/convert/num.rs
index 5ff52a9a11b..46ba0a279b7 100644
--- a/src/libcore/convert/num.rs
+++ b/src/libcore/convert/num.rs
@@ -445,3 +445,42 @@ 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! { usize, NonZeroUsize, #[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")] }
+nzint_impl_try_from_int! { isize, NonZeroIsize, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
diff --git a/src/libcore/tests/nonzero.rs b/src/libcore/tests/nonzero.rs
index 0227a66b863..48aec6d718d 100644
--- a/src/libcore/tests/nonzero.rs
+++ b/src/libcore/tests/nonzero.rs
@@ -1,3 +1,4 @@
+use core::convert::TryFrom;
 use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
 use core::option::Option::{self, None, Some};
 use std::mem::size_of;
@@ -176,3 +177,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).unwrap()));
+    assert_eq!(NonZeroU32::try_from(5), Ok(NonZeroU32::new(5).unwrap()));
+
+    assert_eq!(NonZeroI8::try_from(-5), Ok(NonZeroI8::new(-5).unwrap()));
+    assert_eq!(NonZeroI32::try_from(-5), Ok(NonZeroI32::new(-5).unwrap()));
+}
+
+#[test]
+fn test_nonzero_from_int_on_err() {
+    assert!(NonZeroU8::try_from(0).is_err());
+    assert!(NonZeroU32::try_from(0).is_err());
+
+    assert!(NonZeroI8::try_from(0).is_err());
+    assert!(NonZeroI32::try_from(0).is_err());
+}