Skip to content

Commit d58c69a

Browse files
authored
Rollup merge of #93293 - nvzqz:nonzero-min-max, r=joshtriplett
Implement `MIN`/`MAX` constants for non-zero integers This adds the associated [`MIN`](https://doc.rust-lang.org/stable/std/primitive.usize.html#associatedconstant.MIN)/[`MAX`](https://doc.rust-lang.org/stable/std/primitive.usize.html#associatedconstant.MAX) constants to `NonZero{U,I}{8,16,32,64,128,size}`, requested in #89065. This reimplements #89077 due that PR being stagnant for 4 months. I am fine with closing this in favor of that one if the author revisits it. If so, I'd like to see that PR have the docs link to the `$Int`'s constants.
2 parents 352e621 + ecb7927 commit d58c69a

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

library/core/src/num/nonzero.rs

+101
Original file line numberDiff line numberDiff line change
@@ -989,3 +989,104 @@ macro_rules! nonzero_unsigned_is_power_of_two {
989989
}
990990

991991
nonzero_unsigned_is_power_of_two! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroU128 NonZeroUsize }
992+
993+
macro_rules! nonzero_min_max_unsigned {
994+
( $( $Ty: ident($Int: ident); )+ ) => {
995+
$(
996+
impl $Ty {
997+
/// The smallest value that can be represented by this non-zero
998+
/// integer type, 1.
999+
///
1000+
/// # Examples
1001+
///
1002+
/// ```
1003+
/// #![feature(nonzero_min_max)]
1004+
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
1005+
///
1006+
#[doc = concat!("assert_eq!(", stringify!($Ty), "::MIN.get(), 1", stringify!($Int), ");")]
1007+
/// ```
1008+
#[unstable(feature = "nonzero_min_max", issue = "89065")]
1009+
pub const MIN: Self = Self::new(1).unwrap();
1010+
1011+
/// The largest value that can be represented by this non-zero
1012+
/// integer type,
1013+
#[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1014+
///
1015+
/// # Examples
1016+
///
1017+
/// ```
1018+
/// #![feature(nonzero_min_max)]
1019+
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
1020+
///
1021+
#[doc = concat!("assert_eq!(", stringify!($Ty), "::MAX.get(), ", stringify!($Int), "::MAX);")]
1022+
/// ```
1023+
#[unstable(feature = "nonzero_min_max", issue = "89065")]
1024+
pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1025+
}
1026+
)+
1027+
}
1028+
}
1029+
1030+
macro_rules! nonzero_min_max_signed {
1031+
( $( $Ty: ident($Int: ident); )+ ) => {
1032+
$(
1033+
impl $Ty {
1034+
/// The smallest value that can be represented by this non-zero
1035+
/// integer type,
1036+
#[doc = concat!("equal to [`", stringify!($Int), "::MIN`].")]
1037+
///
1038+
/// Note: While most integer types are defined for every whole
1039+
/// number between `MIN` and `MAX`, signed non-zero integers are
1040+
/// a special case. They have a "gap" at 0.
1041+
///
1042+
/// # Examples
1043+
///
1044+
/// ```
1045+
/// #![feature(nonzero_min_max)]
1046+
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
1047+
///
1048+
#[doc = concat!("assert_eq!(", stringify!($Ty), "::MIN.get(), ", stringify!($Int), "::MIN);")]
1049+
/// ```
1050+
#[unstable(feature = "nonzero_min_max", issue = "89065")]
1051+
pub const MIN: Self = Self::new(<$Int>::MIN).unwrap();
1052+
1053+
/// The largest value that can be represented by this non-zero
1054+
/// integer type,
1055+
#[doc = concat!("equal to [`", stringify!($Int), "::MAX`].")]
1056+
///
1057+
/// Note: While most integer types are defined for every whole
1058+
/// number between `MIN` and `MAX`, signed non-zero integers are
1059+
/// a special case. They have a "gap" at 0.
1060+
///
1061+
/// # Examples
1062+
///
1063+
/// ```
1064+
/// #![feature(nonzero_min_max)]
1065+
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
1066+
///
1067+
#[doc = concat!("assert_eq!(", stringify!($Ty), "::MAX.get(), ", stringify!($Int), "::MAX);")]
1068+
/// ```
1069+
#[unstable(feature = "nonzero_min_max", issue = "89065")]
1070+
pub const MAX: Self = Self::new(<$Int>::MAX).unwrap();
1071+
}
1072+
)+
1073+
}
1074+
}
1075+
1076+
nonzero_min_max_unsigned! {
1077+
NonZeroU8(u8);
1078+
NonZeroU16(u16);
1079+
NonZeroU32(u32);
1080+
NonZeroU64(u64);
1081+
NonZeroU128(u128);
1082+
NonZeroUsize(usize);
1083+
}
1084+
1085+
nonzero_min_max_signed! {
1086+
NonZeroI8(i8);
1087+
NonZeroI16(i16);
1088+
NonZeroI32(i32);
1089+
NonZeroI64(i64);
1090+
NonZeroI128(i128);
1091+
NonZeroIsize(isize);
1092+
}

0 commit comments

Comments
 (0)