Skip to content

Commit 797c249

Browse files
committed
Implement mixed_integer_ops_unsigned_sub
1 parent 09bda4f commit 797c249

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

core/src/num/uint_macros.rs

+103
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,33 @@ macro_rules! uint_impl {
765765
}
766766
}
767767

768+
/// Checked subtraction with a signed integer. Computes `self - rhs`,
769+
/// returning `None` if overflow occurred.
770+
///
771+
/// # Examples
772+
///
773+
/// Basic usage:
774+
///
775+
/// ```
776+
/// #![feature(mixed_integer_ops_unsigned_sub)]
777+
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_sub_signed(2), None);")]
778+
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_sub_signed(-2), Some(3));")]
779+
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_sub_signed(-4), None);")]
780+
/// ```
781+
#[unstable(feature = "mixed_integer_ops_unsigned_sub", issue = "126043")]
782+
#[must_use = "this returns the result of the operation, \
783+
without modifying the original"]
784+
#[inline]
785+
pub const fn checked_sub_signed(self, rhs: $SignedT) -> Option<Self> {
786+
let (res, overflow) = self.overflowing_sub_signed(rhs);
787+
788+
if !overflow {
789+
Some(res)
790+
} else {
791+
None
792+
}
793+
}
794+
768795
#[doc = concat!(
769796
"Checked integer subtraction. Computes `self - rhs` and checks if the result fits into an [`",
770797
stringify!($SignedT), "`], returning `None` if overflow occurred."
@@ -1757,6 +1784,35 @@ macro_rules! uint_impl {
17571784
intrinsics::saturating_sub(self, rhs)
17581785
}
17591786

1787+
/// Saturating integer subtraction. Computes `self` - `rhs`, saturating at
1788+
/// the numeric bounds instead of overflowing.
1789+
///
1790+
/// # Examples
1791+
///
1792+
/// Basic usage:
1793+
///
1794+
/// ```
1795+
/// #![feature(mixed_integer_ops_unsigned_sub)]
1796+
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_sub_signed(2), 0);")]
1797+
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_sub_signed(-2), 3);")]
1798+
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).saturating_sub_signed(-4), ", stringify!($SelfT), "::MAX);")]
1799+
/// ```
1800+
#[unstable(feature = "mixed_integer_ops_unsigned_sub", issue = "126043")]
1801+
#[must_use = "this returns the result of the operation, \
1802+
without modifying the original"]
1803+
#[inline]
1804+
pub const fn saturating_sub_signed(self, rhs: $SignedT) -> Self {
1805+
let (res, overflow) = self.overflowing_sub_signed(rhs);
1806+
1807+
if !overflow {
1808+
res
1809+
} else if rhs < 0 {
1810+
Self::MAX
1811+
} else {
1812+
0
1813+
}
1814+
}
1815+
17601816
/// Saturating integer multiplication. Computes `self * rhs`,
17611817
/// saturating at the numeric bounds instead of overflowing.
17621818
///
@@ -1890,6 +1946,27 @@ macro_rules! uint_impl {
18901946
intrinsics::wrapping_sub(self, rhs)
18911947
}
18921948

1949+
/// Wrapping (modular) subtraction with a signed integer. Computes
1950+
/// `self - rhs`, wrapping around at the boundary of the type.
1951+
///
1952+
/// # Examples
1953+
///
1954+
/// Basic usage:
1955+
///
1956+
/// ```
1957+
/// #![feature(mixed_integer_ops_unsigned_sub)]
1958+
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".wrapping_sub_signed(2), ", stringify!($SelfT), "::MAX);")]
1959+
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".wrapping_sub_signed(-2), 3);")]
1960+
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).wrapping_sub_signed(-4), 1);")]
1961+
/// ```
1962+
#[unstable(feature = "mixed_integer_ops_unsigned_sub", issue = "126043")]
1963+
#[must_use = "this returns the result of the operation, \
1964+
without modifying the original"]
1965+
#[inline]
1966+
pub const fn wrapping_sub_signed(self, rhs: $SignedT) -> Self {
1967+
self.wrapping_sub(rhs as Self)
1968+
}
1969+
18931970
/// Wrapping (modular) multiplication. Computes `self *
18941971
/// rhs`, wrapping around at the boundary of the type.
18951972
///
@@ -2328,6 +2405,32 @@ macro_rules! uint_impl {
23282405
(c, b || d)
23292406
}
23302407

2408+
/// Calculates `self` - `rhs` with a signed `rhs`
2409+
///
2410+
/// Returns a tuple of the subtraction along with a boolean indicating
2411+
/// whether an arithmetic overflow would occur. If an overflow would
2412+
/// have occurred then the wrapped value is returned.
2413+
///
2414+
/// # Examples
2415+
///
2416+
/// Basic usage:
2417+
///
2418+
/// ```
2419+
/// #![feature(mixed_integer_ops_unsigned_sub)]
2420+
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_signed(2), (", stringify!($SelfT), "::MAX, true));")]
2421+
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_signed(-2), (3, false));")]
2422+
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_sub_signed(-4), (1, true));")]
2423+
/// ```
2424+
#[unstable(feature = "mixed_integer_ops_unsigned_sub", issue = "126043")]
2425+
#[must_use = "this returns the result of the operation, \
2426+
without modifying the original"]
2427+
#[inline]
2428+
pub const fn overflowing_sub_signed(self, rhs: $SignedT) -> (Self, bool) {
2429+
let (res, overflow) = self.overflowing_sub(rhs as Self);
2430+
2431+
(res, overflow ^ (rhs < 0))
2432+
}
2433+
23312434
/// Computes the absolute difference between `self` and `other`.
23322435
///
23332436
/// # Examples

0 commit comments

Comments
 (0)