Skip to content

Commit

Permalink
Fix diverging doc regarding signedness.
Browse files Browse the repository at this point in the history
  • Loading branch information
iago-lito committed Jun 9, 2021
1 parent 3c168b0 commit d442c10
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions library/core/src/num/nonzero.rs
Expand Up @@ -360,8 +360,7 @@ macro_rules! nonzero_unsigned_operations {
/// Overflow is unchecked, and it is undefined behaviour to overflow
/// *even if the result would wrap to a non-zero value*.
/// The behaviour is undefined as soon as
#[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`")]
#[doc = concat!(" or `self + rhs < ", stringify!($Int), "::MIN`.")]
#[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`.")]
///
/// # Examples
///
Expand Down Expand Up @@ -650,7 +649,7 @@ nonzero_signed_operations! {

// A bunch of methods for both signed and unsigned nonzero types.
macro_rules! nonzero_unsigned_signed_operations {
( $( $Ty: ident($Int: ty); )+ ) => {
( $( $signedness:ident $Ty: ident($Int: ty); )+ ) => {
$(
impl $Ty {
/// Multiply two non-zero integers together.
Expand Down Expand Up @@ -723,8 +722,16 @@ macro_rules! nonzero_unsigned_signed_operations {
/// Overflow is unchecked, and it is undefined behaviour to overflow
/// *even if the result would wrap to a non-zero value*.
/// The behaviour is undefined as soon as
#[doc = concat!("`self * rhs > ", stringify!($Int), "::MAX`, ")]
#[doc = concat!("or `self * rhs < ", stringify!($Int), "::MIN`.")]
#[doc = sign_dependent_expr!{
$signedness ?
if signed {
concat!("`self * rhs > ", stringify!($Int), "::MAX`, ",
"or `self * rhs < ", stringify!($Int), "::MIN`.")
}
if unsigned {
concat!("`self * rhs > ", stringify!($Int), "::MAX`.")
}
}]
///
/// # Examples
///
Expand Down Expand Up @@ -783,7 +790,16 @@ macro_rules! nonzero_unsigned_signed_operations {
}

/// Raise non-zero value to an integer power.
#[doc = concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")]
#[doc = sign_dependent_expr!{
$signedness ?
if signed {
concat!("Return [`", stringify!($Int), "::MIN`] ",
"or [`", stringify!($Int), "::MAX`] on overflow.")
}
if unsigned {
concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")
}
}]
///
/// # Examples
///
Expand Down Expand Up @@ -815,19 +831,29 @@ macro_rules! nonzero_unsigned_signed_operations {
}
}

// Use this when the generated code should differ between signed and unsigned types.
macro_rules! sign_dependent_expr {
(signed ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => {
$signed_case
};
(unsigned ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => {
$unsigned_case
};
}

nonzero_unsigned_signed_operations! {
NonZeroU8(u8);
NonZeroU16(u16);
NonZeroU32(u32);
NonZeroU64(u64);
NonZeroU128(u128);
NonZeroUsize(usize);
NonZeroI8(i8);
NonZeroI16(i16);
NonZeroI32(i32);
NonZeroI64(i64);
NonZeroI128(i128);
NonZeroIsize(isize);
unsigned NonZeroU8(u8);
unsigned NonZeroU16(u16);
unsigned NonZeroU32(u32);
unsigned NonZeroU64(u64);
unsigned NonZeroU128(u128);
unsigned NonZeroUsize(usize);
signed NonZeroI8(i8);
signed NonZeroI16(i16);
signed NonZeroI32(i32);
signed NonZeroI64(i64);
signed NonZeroI128(i128);
signed NonZeroIsize(isize);
}

macro_rules! nonzero_unsigned_is_power_of_two {
Expand Down

0 comments on commit d442c10

Please sign in to comment.