Skip to content
Closed
72 changes: 72 additions & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this and the below example need a ``` ... ``` code block, for formatting (though they're not very interesting as doctests).

I think including some blank lines as done above for nonzero_unsigned_signed_operations (such as ``` above and below the Examples header, and a normal blank line between the MAX and MIN definitions) but before would also make the macro code more readable even if it doesn't affect the documentation output.

/// ```
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation mismatch here.

NonZeroU16(u16);
NonZeroU32(u32);
NonZeroU64(u64);
NonZeroU128(u128);
NonZeroUsize(usize);
}