From 2031e99d772091854d716df5bdf227f8be9b6397 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Fri, 29 May 2020 03:05:03 +0200 Subject: [PATCH 1/5] Added implementations for TryFrom<{int}> for NonZero{int} --- src/libcore/convert/num.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/libcore/convert/num.rs b/src/libcore/convert/num.rs index 6dd0522f7f610..22a2804678a4c 100644 --- a/src/libcore/convert/num.rs +++ b/src/libcore/convert/num.rs @@ -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::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")] } From d8b51f180a9c6ced4397ce5568fa8ab553a7143e Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Fri, 29 May 2020 03:05:25 +0200 Subject: [PATCH 2/5] Added tests for the implementations --- src/libcore/tests/nonzero.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/libcore/tests/nonzero.rs b/src/libcore/tests/nonzero.rs index 0227a66b8633a..2500099853237 100644 --- a/src/libcore/tests/nonzero.rs +++ b/src/libcore/tests/nonzero.rs @@ -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; @@ -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(()))); +} From 0dcb392e9889ab6976973a2f7140d18a80178202 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Mon, 8 Jun 2020 16:15:39 +0200 Subject: [PATCH 3/5] Added implementations for NonZero(U|I)size --- src/libcore/convert/num.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcore/convert/num.rs b/src/libcore/convert/num.rs index 22a2804678a4c..dc1de55a9feea 100644 --- a/src/libcore/convert/num.rs +++ b/src/libcore/convert/num.rs @@ -477,8 +477,10 @@ nzint_impl_try_from_int! { u16, NonZeroU16, #[stable(feature = "nzint_try_from_i 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")] } From 2b8e2f836f59db23630a06554b8e1a33f6c502f5 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Mon, 8 Jun 2020 17:48:36 +0200 Subject: [PATCH 4/5] Correctly format the tests and import TryFrom --- src/libcore/tests/nonzero.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libcore/tests/nonzero.rs b/src/libcore/tests/nonzero.rs index 2500099853237..0a48d3d0ca9bc 100644 --- a/src/libcore/tests/nonzero.rs +++ b/src/libcore/tests/nonzero.rs @@ -1,4 +1,5 @@ -use core::num::{TryFromIntError, IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8}; +use core::convert::TryFrom; +use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8, TryFromIntError}; use core::option::Option::{self, None, Some}; use std::mem::size_of; From 6c8d8d6a6055d19dc33cc8b33b082d17583a0e47 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Mon, 8 Jun 2020 19:17:05 +0200 Subject: [PATCH 5/5] Fix test call for error case --- src/libcore/tests/nonzero.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libcore/tests/nonzero.rs b/src/libcore/tests/nonzero.rs index 0a48d3d0ca9bc..48aec6d718d3d 100644 --- a/src/libcore/tests/nonzero.rs +++ b/src/libcore/tests/nonzero.rs @@ -1,5 +1,5 @@ use core::convert::TryFrom; -use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8, TryFromIntError}; +use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8}; use core::option::Option::{self, None, Some}; use std::mem::size_of; @@ -180,18 +180,18 @@ fn test_nonzero_bitor_assign() { #[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!(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))); - assert_eq!(NonZeroI32::try_from(-5), Ok(NonZeroI32::new(-5))); + 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_eq!(NonZeroU8::try_from(0), Err(TryFromIntError(()))); - assert_eq!(NonZeroU32::try_from(0), Err(TryFromIntError(()))); + assert!(NonZeroU8::try_from(0).is_err()); + assert!(NonZeroU32::try_from(0).is_err()); - assert_eq!(NonZeroI8::try_from(0), Err(TryFromIntError(()))); - assert_eq!(NonZeroI32::try_from(0), Err(TryFromIntError(()))); + assert!(NonZeroI8::try_from(0).is_err()); + assert!(NonZeroI32::try_from(0).is_err()); }