Skip to content

Commit fe71c74

Browse files
committed
Implement MIN/MAX constants for non-zero integers
1 parent e7825f2 commit fe71c74

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
@@ -988,3 +988,104 @@ macro_rules! nonzero_unsigned_is_power_of_two {
988988
}
989989

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

0 commit comments

Comments
 (0)