@@ -765,6 +765,33 @@ macro_rules! uint_impl {
765
765
}
766
766
}
767
767
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
+
768
795
#[ doc = concat!(
769
796
"Checked integer subtraction. Computes `self - rhs` and checks if the result fits into an [`" ,
770
797
stringify!( $SignedT) , "`], returning `None` if overflow occurred."
@@ -1757,6 +1784,35 @@ macro_rules! uint_impl {
1757
1784
intrinsics:: saturating_sub( self , rhs)
1758
1785
}
1759
1786
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
+
1760
1816
/// Saturating integer multiplication. Computes `self * rhs`,
1761
1817
/// saturating at the numeric bounds instead of overflowing.
1762
1818
///
@@ -1890,6 +1946,27 @@ macro_rules! uint_impl {
1890
1946
intrinsics:: wrapping_sub( self , rhs)
1891
1947
}
1892
1948
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
+
1893
1970
/// Wrapping (modular) multiplication. Computes `self *
1894
1971
/// rhs`, wrapping around at the boundary of the type.
1895
1972
///
@@ -2328,6 +2405,32 @@ macro_rules! uint_impl {
2328
2405
( c, b || d)
2329
2406
}
2330
2407
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
+
2331
2434
/// Computes the absolute difference between `self` and `other`.
2332
2435
///
2333
2436
/// # Examples
0 commit comments