From 0156ef12fa7e8f7db48a6f4f2ee8b9568425d5ab Mon Sep 17 00:00:00 2001 From: mjclements Date: Sat, 18 Sep 2021 14:21:57 -0400 Subject: [PATCH 1/5] Max working. --- library/core/src/num/nonzero.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index dd9b9330aee2b..2ef5754416f48 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -895,3 +895,23 @@ macro_rules! nonzero_unsigned_is_power_of_two { } nonzero_unsigned_is_power_of_two! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroU128 NonZeroUsize } + +macro_rules! nonzero_max { + ( $( $Ty: ident($Int: ty) )+ ) => { + $( + + impl $Ty { + #[unstable(feature = "nonzero_max", issue = "89065")] + #[doc = concat!("The maximum value for a`", stringify!($Ty), "` is the same as `", stringify!($Int), "`")] + #[doc = concat!("assert_eq!(", stringify!($Ty), "::MAX, ", stringify!($Int), ">::MAX);")] + //SAFETY: Since the MAX value, for any supported integer type, is greater than 0, + // the MAX will always be non-zero. + pub const MAX : $Ty = unsafe { $Ty::new_unchecked(<$Int>::MAX) }; + } + )+ + } +} + +nonzero_max! { NonZeroU8(u8) NonZeroI8(i8) NonZeroU16(u16) NonZeroI16(i16) NonZeroU32(u32) NonZeroI32(i32) + NonZeroU64(u64) NonZeroI64(i64) NonZeroUsize(usize) NonZeroIsize(isize) +} From b5c1fe19ccfe2af2cedcd0f8fea75bf2bb92de15 Mon Sep 17 00:00:00 2001 From: mjclements Date: Sat, 18 Sep 2021 15:03:31 -0400 Subject: [PATCH 2/5] nonzero min and nonzero max working. --- library/core/src/num/nonzero.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 2ef5754416f48..969eaac1e8b48 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -896,22 +896,41 @@ macro_rules! nonzero_unsigned_is_power_of_two { nonzero_unsigned_is_power_of_two! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroU128 NonZeroUsize } -macro_rules! nonzero_max { - ( $( $Ty: ident($Int: ty) )+ ) => { +macro_rules! nonzero_min_max { + ( $( $min:expr , $Ty: ident($Int: ty); )+ ) => { $( impl $Ty { #[unstable(feature = "nonzero_max", issue = "89065")] #[doc = concat!("The maximum value for a`", stringify!($Ty), "` is the same as `", stringify!($Int), "`")] - #[doc = concat!("assert_eq!(", stringify!($Ty), "::MAX, ", stringify!($Int), ">::MAX);")] + #[doc = concat!("assert_eq!(", stringify!($Ty), "::MAX, ", stringify!($Int), "::MAX);")] //SAFETY: Since the MAX value, for any supported integer type, is greater than 0, // the MAX will always be non-zero. pub const MAX : $Ty = unsafe { $Ty::new_unchecked(<$Int>::MAX) }; - } + #[unstable(feature = "nonzero_min", issue = "89065")] + #[doc = concat!("The minimum value for a`", stringify!($Ty), "`.")] + /// # Examples + /// + #[doc = concat!("assert_eq!(", stringify!($Ty), "::MIN, ", stringify!($min), ";")] + //SAFETY: In the signed case, the minimum integer is negative, and therefore non-zero. + // In the unsignedd case, we use one, which is non-zero. + pub const MIN : $Ty = unsafe { $Ty::new_unchecked($min)}; + } )+ } } -nonzero_max! { NonZeroU8(u8) NonZeroI8(i8) NonZeroU16(u16) NonZeroI16(i16) NonZeroU32(u32) NonZeroI32(i32) - NonZeroU64(u64) NonZeroI64(i64) NonZeroUsize(usize) NonZeroIsize(isize) +nonzero_min_max! { + 1 , NonZeroU8(u8); + 1 , NonZeroU16(u16); + 1 , NonZeroU32(u32); + 1 , NonZeroU64(u64); + 1 , NonZeroU128(u128); + 1 , NonZeroUsize(usize); + i8::MIN , NonZeroI8(i8); + i16::MIN , NonZeroI16(i16); + i32::MIN , NonZeroI32(i32); + i64::MIN , NonZeroI64(i64); + i128::MIN , NonZeroI128(i128); + isize::MIN , NonZeroIsize(isize); } From 84d88f2ae9085800d958d1c46a522cebfa72f63f Mon Sep 17 00:00:00 2001 From: mjclements Date: Sat, 18 Sep 2021 16:11:09 -0400 Subject: [PATCH 3/5] Fixed some formatting issues. --- library/core/src/num/nonzero.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 969eaac1e8b48..a27ec0e11674b 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -899,23 +899,20 @@ nonzero_unsigned_is_power_of_two! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 N macro_rules! nonzero_min_max { ( $( $min:expr , $Ty: ident($Int: ty); )+ ) => { $( - impl $Ty { - #[unstable(feature = "nonzero_max", issue = "89065")] + #[unstable(feature = "nonzero_max", issue = "89065")] #[doc = concat!("The maximum value for a`", stringify!($Ty), "` is the same as `", stringify!($Int), "`")] #[doc = concat!("assert_eq!(", stringify!($Ty), "::MAX, ", stringify!($Int), "::MAX);")] - //SAFETY: Since the MAX value, for any supported integer type, is greater than 0, - // the MAX will always be non-zero. + // SAFETY: Since the MAX value, for any supported integer type, is greater than 0, the MAX will always be non-zero. pub const MAX : $Ty = unsafe { $Ty::new_unchecked(<$Int>::MAX) }; #[unstable(feature = "nonzero_min", issue = "89065")] #[doc = concat!("The minimum value for a`", stringify!($Ty), "`.")] - /// # Examples - /// + /// # Examples #[doc = concat!("assert_eq!(", stringify!($Ty), "::MIN, ", stringify!($min), ";")] - //SAFETY: In the signed case, the minimum integer is negative, and therefore non-zero. - // In the unsignedd case, we use one, which is non-zero. + // SAFETY: In the signed case, the minimum integer is negative, and therefore non-zero. + // SAFETY: In the unsignedd case, we use one, which is non-zero. pub const MIN : $Ty = unsafe { $Ty::new_unchecked($min)}; - } + } )+ } } From b2740229f302618b08b4830123b49529970a3724 Mon Sep 17 00:00:00 2001 From: mjclements Date: Sat, 18 Sep 2021 16:11:56 -0400 Subject: [PATCH 4/5] Made a macro name consistent. --- library/core/src/num/nonzero.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index a27ec0e11674b..ea882a098e12e 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -897,7 +897,7 @@ macro_rules! nonzero_unsigned_is_power_of_two { nonzero_unsigned_is_power_of_two! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroU128 NonZeroUsize } macro_rules! nonzero_min_max { - ( $( $min:expr , $Ty: ident($Int: ty); )+ ) => { + ( $( $MinVal:expr , $Ty: ident($Int: ty); )+ ) => { $( impl $Ty { #[unstable(feature = "nonzero_max", issue = "89065")] From 0f84d19b65d973f2514027615d23b30a253da6b6 Mon Sep 17 00:00:00 2001 From: mjclements Date: Sat, 18 Sep 2021 16:23:33 -0400 Subject: [PATCH 5/5] Fixed variable name _properly_ --- library/core/src/num/nonzero.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index ea882a098e12e..436e3eb708ad4 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -908,10 +908,10 @@ macro_rules! nonzero_min_max { #[unstable(feature = "nonzero_min", issue = "89065")] #[doc = concat!("The minimum value for a`", stringify!($Ty), "`.")] /// # Examples - #[doc = concat!("assert_eq!(", stringify!($Ty), "::MIN, ", stringify!($min), ";")] + #[doc = concat!("assert_eq!(", stringify!($Ty), "::MIN, ", stringify!($Min), ";")] // SAFETY: In the signed case, the minimum integer is negative, and therefore non-zero. // SAFETY: In the unsignedd case, we use one, which is non-zero. - pub const MIN : $Ty = unsafe { $Ty::new_unchecked($min)}; + pub const MIN : $Ty = unsafe { $Ty::new_unchecked($Min)}; } )+ }