Rollup merge of #143282 - nxsaken:strict_sub_signed, r=jhpratt

Add `uX::strict_sub_signed`

rust-lang/rust#116090 missed `strict_sub_signed`, adding it here.
Part of rust-lang/rust#118260.

r? ``@m-ou-se``
This commit is contained in:
Guillaume Gomez 2025-07-20 15:34:04 +02:00 committed by GitHub
commit 0162cc50e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -556,7 +556,7 @@ macro_rules! uint_impl {
pub const fn strict_add(self, rhs: Self) -> Self {
let (a, b) = self.overflowing_add(rhs);
if b { overflow_panic::add() } else { a }
}
}
/// Unchecked integer addition. Computes `self + rhs`, assuming overflow
/// cannot occur.
@ -653,7 +653,7 @@ macro_rules! uint_impl {
pub const fn strict_add_signed(self, rhs: $SignedT) -> Self {
let (a, b) = self.overflowing_add_signed(rhs);
if b { overflow_panic::add() } else { a }
}
}
/// Checked integer subtraction. Computes `self - rhs`, returning
/// `None` if overflow occurred.
@ -713,7 +713,7 @@ macro_rules! uint_impl {
pub const fn strict_sub(self, rhs: Self) -> Self {
let (a, b) = self.overflowing_sub(rhs);
if b { overflow_panic::sub() } else { a }
}
}
/// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
/// cannot occur.
@ -805,6 +805,43 @@ macro_rules! uint_impl {
}
}
/// Strict subtraction with a signed integer. Computes `self - rhs`,
/// panicking if overflow occurred.
///
/// # Panics
///
/// ## Overflow behavior
///
/// This function will always panic on overflow, regardless of whether overflow checks are enabled.
///
/// # Examples
///
/// ```
/// #![feature(strict_overflow_ops)]
#[doc = concat!("assert_eq!(3", stringify!($SelfT), ".strict_sub_signed(2), 1);")]
/// ```
///
/// The following panic because of overflow:
///
/// ```should_panic
/// #![feature(strict_overflow_ops)]
#[doc = concat!("let _ = 1", stringify!($SelfT), ".strict_sub_signed(2);")]
/// ```
///
/// ```should_panic
/// #![feature(strict_overflow_ops)]
#[doc = concat!("let _ = (", stringify!($SelfT), "::MAX).strict_sub_signed(-1);")]
/// ```
#[unstable(feature = "strict_overflow_ops", issue = "118260")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn strict_sub_signed(self, rhs: $SignedT) -> Self {
let (a, b) = self.overflowing_sub_signed(rhs);
if b { overflow_panic::sub() } else { a }
}
#[doc = concat!(
"Checked integer subtraction. Computes `self - rhs` and checks if the result fits into an [`",
stringify!($SignedT), "`], returning `None` if overflow occurred."
@ -913,7 +950,7 @@ macro_rules! uint_impl {
pub const fn strict_mul(self, rhs: Self) -> Self {
let (a, b) = self.overflowing_mul(rhs);
if b { overflow_panic::mul() } else { a }
}
}
/// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
/// cannot occur.