Add MIN/MAX associated constants to the integer types

This commit is contained in:
Linus Färnstrand 2020-01-17 22:16:52 +01:00
parent e0bbe7915e
commit b5ff8064a4

View File

@ -245,42 +245,54 @@ macro_rules! int_impl {
$reversed:expr, $le_bytes:expr, $be_bytes:expr, $reversed:expr, $le_bytes:expr, $be_bytes:expr,
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => { $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => {
doc_comment! { doc_comment! {
concat!("Returns the smallest value that can be represented by this integer type. concat!("The smallest value that can be represented by this integer type.
# Examples # Examples
Basic usage: Basic usage:
``` ```
", $Feature, "assert_eq!(", stringify!($SelfT), "::min_value(), ", stringify!($Min), ");", ", $Feature, "assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");",
$EndFeature, " $EndFeature, "
```"), ```"),
#[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")]
pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self;
}
doc_comment! {
concat!("The largest value that can be represented by this integer type.
# Examples
Basic usage:
```
", $Feature, "assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");",
$EndFeature, "
```"),
#[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")]
pub const MAX: Self = !Self::MIN;
}
doc_comment! {
"Returns the smallest value that can be represented by this integer type.",
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[inline(always)] #[inline(always)]
#[rustc_promotable] #[rustc_promotable]
#[rustc_const_stable(feature = "const_min_value", since = "1.32.0")] #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
pub const fn min_value() -> Self { pub const fn min_value() -> Self {
!0 ^ ((!0 as $UnsignedT) >> 1) as Self Self::MIN
} }
} }
doc_comment! { doc_comment! {
concat!("Returns the largest value that can be represented by this integer type. "Returns the largest value that can be represented by this integer type.",
# Examples
Basic usage:
```
", $Feature, "assert_eq!(", stringify!($SelfT), "::max_value(), ", stringify!($Max), ");",
$EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[inline(always)] #[inline(always)]
#[rustc_promotable] #[rustc_promotable]
#[rustc_const_stable(feature = "const_max_value", since = "1.32.0")] #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
pub const fn max_value() -> Self { pub const fn max_value() -> Self {
!Self::min_value() Self::MAX
} }
} }
@ -2342,38 +2354,50 @@ macro_rules! uint_impl {
$reversed:expr, $le_bytes:expr, $be_bytes:expr, $reversed:expr, $le_bytes:expr, $be_bytes:expr,
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => { $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => {
doc_comment! { doc_comment! {
concat!("Returns the smallest value that can be represented by this integer type. concat!("The smallest value that can be represented by this integer type.
# Examples # Examples
Basic usage: Basic usage:
``` ```
", $Feature, "assert_eq!(", stringify!($SelfT), "::min_value(), 0);", $EndFeature, " ", $Feature, "assert_eq!(", stringify!($SelfT), "::MIN, 0);", $EndFeature, "
```"), ```"),
#[stable(feature = "rust1", since = "1.0.0")] #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")]
#[rustc_promotable] pub const MIN: Self = 0;
#[inline(always)]
#[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
pub const fn min_value() -> Self { 0 }
} }
doc_comment! { doc_comment! {
concat!("Returns the largest value that can be represented by this integer type. concat!("The largest value that can be represented by this integer type.
# Examples # Examples
Basic usage: Basic usage:
``` ```
", $Feature, "assert_eq!(", stringify!($SelfT), "::max_value(), ", ", $Feature, "assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($MaxV), ");",
stringify!($MaxV), ");", $EndFeature, " $EndFeature, "
```"), ```"),
#[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")]
pub const MAX: Self = !0;
}
doc_comment! {
"Returns the smallest value that can be represented by this integer type.",
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_promotable] #[rustc_promotable]
#[inline(always)] #[inline(always)]
#[rustc_const_stable(feature = "const_max_value", since = "1.32.0")] #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
pub const fn max_value() -> Self { !0 } pub const fn min_value() -> Self { Self::MIN }
}
doc_comment! {
"Returns the largest value that can be represented by this integer type.",
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_promotable]
#[inline(always)]
#[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
pub const fn max_value() -> Self { Self::MAX }
} }
doc_comment! { doc_comment! {