Skip to content

Commit 0162cc5

Browse files
Rollup merge of #143282 - nxsaken:strict_sub_signed, r=jhpratt
Add `uX::strict_sub_signed` #116090 missed `strict_sub_signed`, adding it here. Part of #118260. r? ``@m-ou-se``
2 parents ca9eecd + 79ed7c1 commit 0162cc5

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

library/core/src/num/uint_macros.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ macro_rules! uint_impl {
556556
pub const fn strict_add(self, rhs: Self) -> Self {
557557
let (a, b) = self.overflowing_add(rhs);
558558
if b { overflow_panic::add() } else { a }
559-
}
559+
}
560560

561561
/// Unchecked integer addition. Computes `self + rhs`, assuming overflow
562562
/// cannot occur.
@@ -653,7 +653,7 @@ macro_rules! uint_impl {
653653
pub const fn strict_add_signed(self, rhs: $SignedT) -> Self {
654654
let (a, b) = self.overflowing_add_signed(rhs);
655655
if b { overflow_panic::add() } else { a }
656-
}
656+
}
657657

658658
/// Checked integer subtraction. Computes `self - rhs`, returning
659659
/// `None` if overflow occurred.
@@ -713,7 +713,7 @@ macro_rules! uint_impl {
713713
pub const fn strict_sub(self, rhs: Self) -> Self {
714714
let (a, b) = self.overflowing_sub(rhs);
715715
if b { overflow_panic::sub() } else { a }
716-
}
716+
}
717717

718718
/// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
719719
/// cannot occur.
@@ -805,6 +805,43 @@ macro_rules! uint_impl {
805805
}
806806
}
807807

808+
/// Strict subtraction with a signed integer. Computes `self - rhs`,
809+
/// panicking if overflow occurred.
810+
///
811+
/// # Panics
812+
///
813+
/// ## Overflow behavior
814+
///
815+
/// This function will always panic on overflow, regardless of whether overflow checks are enabled.
816+
///
817+
/// # Examples
818+
///
819+
/// ```
820+
/// #![feature(strict_overflow_ops)]
821+
#[doc = concat!("assert_eq!(3", stringify!($SelfT), ".strict_sub_signed(2), 1);")]
822+
/// ```
823+
///
824+
/// The following panic because of overflow:
825+
///
826+
/// ```should_panic
827+
/// #![feature(strict_overflow_ops)]
828+
#[doc = concat!("let _ = 1", stringify!($SelfT), ".strict_sub_signed(2);")]
829+
/// ```
830+
///
831+
/// ```should_panic
832+
/// #![feature(strict_overflow_ops)]
833+
#[doc = concat!("let _ = (", stringify!($SelfT), "::MAX).strict_sub_signed(-1);")]
834+
/// ```
835+
#[unstable(feature = "strict_overflow_ops", issue = "118260")]
836+
#[must_use = "this returns the result of the operation, \
837+
without modifying the original"]
838+
#[inline]
839+
#[track_caller]
840+
pub const fn strict_sub_signed(self, rhs: $SignedT) -> Self {
841+
let (a, b) = self.overflowing_sub_signed(rhs);
842+
if b { overflow_panic::sub() } else { a }
843+
}
844+
808845
#[doc = concat!(
809846
"Checked integer subtraction. Computes `self - rhs` and checks if the result fits into an [`",
810847
stringify!($SignedT), "`], returning `None` if overflow occurred."
@@ -913,7 +950,7 @@ macro_rules! uint_impl {
913950
pub const fn strict_mul(self, rhs: Self) -> Self {
914951
let (a, b) = self.overflowing_mul(rhs);
915952
if b { overflow_panic::mul() } else { a }
916-
}
953+
}
917954

918955
/// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
919956
/// cannot occur.

0 commit comments

Comments
 (0)