diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index dd9b9330aee2b..d643f2bea9d95 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -895,3 +895,75 @@ macro_rules! nonzero_unsigned_is_power_of_two { } nonzero_unsigned_is_power_of_two! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroU128 NonZeroUsize } + +macro_rules! nonzero_constants_signed { + ( $( $Ty: ident($Int: ty); )+ ) => { + $( + impl $Ty { + #[unstable(feature = "nonzero_min_max", issue = "89065")] + #[doc = concat!("The maximum value for a `", stringify!($Ty), "` is the same as `", stringify!($Int), "`.")] + /// # Examples + /// + /// ``` + #[doc = concat!("assert_eq!(", stringify!($Ty), "::MAX, ", stringify!($Int), "::MAX);")] + /// ``` + pub const MAX : $Ty = $Ty::new(<$Int>::MAX).unwrap(); + #[unstable(feature = "nonzero_min_max", issue = "89065")] + #[doc = concat!("The minimum value for a `", stringify!($Ty), " `.")] + /// # Examples + /// + /// ``` + #[doc = concat!("assert_eq!(", stringify!($Ty), "::MIN, ", stringify!($Int), "::MIN);")] + /// ``` + pub const MIN : $Ty = $Ty::new(<$Int>::MIN).unwrap(); + } + )+ + } +} + +nonzero_constants_signed! { + NonZeroI8(i8); + NonZeroI16(i16); + NonZeroI32(i32); + NonZeroI64(i64); + NonZeroI128(i128); + NonZeroIsize(isize); +} + +macro_rules! nonzero_constants_unsigned{ + ( $( $Ty: ident($Int: ty); )+ ) => { + $( + impl $Ty { + #[unstable(feature = "nonzero_min_max", issue = "89065")] + #[doc = concat!("The maximum value for a `", stringify!($Ty), "` is the same as `", stringify!($Int), "`.")] + /// Note: While most integer types are defined for every whole number between MIN and + /// MAX, signed non-zero integers are a special case. They have a 'gap' at 0. + /// # Examples + /// + /// ``` + #[doc = concat!("assert_eq!(", stringify!($Ty), "::MAX, ", stringify!($Int), "::MAX);")] + /// ``` + pub const MAX : $Ty = $Ty::new(<$Int>::MAX).unwrap() ; + #[unstable(feature = "nonzero_min_max", issue = "89065")] + #[doc = concat!("The minimum value for a `", stringify!($Ty), "`.")] + /// Note: While most integer types are defined for every whole number between MIN and + /// MAX, signed non-zero integers are a special case. They have a 'gap' at 0. + /// # Examples + /// + /// ``` + #[doc = concat!("assert_eq!(", stringify!($Ty), "::MIN, ", stringify!($Int), "::MIN);")] + /// ``` + pub const MIN : $Ty = $Ty::new(1).unwrap(); + } + )+ + } +} + +nonzero_constants_unsigned! { + NonZeroU8(u8); + NonZeroU16(u16); + NonZeroU32(u32); + NonZeroU64(u64); + NonZeroU128(u128); + NonZeroUsize(usize); +}