Skip to content

Commit

Permalink
Add cast_signed and cast_unsigned methods for NonZero types
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtriplett authored and gitbot committed Feb 20, 2025
1 parent b57fbff commit ebbccd8
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,29 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
// results will be sqrt(1), which is 1, so a result can't be zero.
unsafe { Self::new_unchecked(result) }
}

/// Returns the bit pattern of `self` reinterpreted as a signed integer of the same size.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(integer_sign_cast)]
/// # use std::num::NonZero;
///
#[doc = concat!("let n = NonZero::<", stringify!($Int), ">::MAX;")]
///
#[doc = concat!("assert_eq!(n.cast_signed(), NonZero::new(-1", stringify!($Sint), ").unwrap());")]
/// ```
#[unstable(feature = "integer_sign_cast", issue = "125882")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_signed(self) -> NonZero<$Sint> {
// SAFETY: `self.get()` can't be zero
unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
}
};

// Associated items for signed nonzero types only.
Expand Down Expand Up @@ -2042,6 +2065,30 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
// SAFETY: negation of nonzero cannot yield zero values.
unsafe { Self::new_unchecked(result) }
}

/// Returns the bit pattern of `self` reinterpreted as an unsigned integer of the same size.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(integer_sign_cast)]
/// # use std::num::NonZero;
///
#[doc = concat!("let n = NonZero::new(-1", stringify!($Int), ").unwrap();")]
///
#[doc = concat!("assert_eq!(n.cast_unsigned(), NonZero::<", stringify!($Uint), ">::MAX);")]
/// ```
#[unstable(feature = "integer_sign_cast", issue = "125882")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn cast_unsigned(self) -> NonZero<$Uint> {
// SAFETY: `self.get()` can't be zero
unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
}

};
}

Expand Down

0 comments on commit ebbccd8

Please sign in to comment.