@@ -763,6 +763,33 @@ macro_rules! uint_impl {
763763 }
764764 }
765765
766+ /// Checked subtraction with a signed integer. Computes `self - rhs`,
767+ /// returning `None` if overflow occurred.
768+ ///
769+ /// # Examples
770+ ///
771+ /// Basic usage:
772+ ///
773+ /// ```
774+ /// #![feature(mixed_integer_ops_unsigned_sub)]
775+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".checked_sub_signed(2), None);" ) ]
776+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".checked_sub_signed(-2), Some(3));" ) ]
777+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MAX - 2).checked_sub_signed(-4), None);" ) ]
778+ /// ```
779+ #[ unstable( feature = "mixed_integer_ops_unsigned_sub" , issue = "126043" ) ]
780+ #[ must_use = "this returns the result of the operation, \
781+ without modifying the original"]
782+ #[ inline]
783+ pub const fn checked_sub_signed( self , rhs: $SignedT) -> Option <Self > {
784+ let ( res, overflow) = self . overflowing_sub_signed( rhs) ;
785+
786+ if !overflow {
787+ Some ( res)
788+ } else {
789+ None
790+ }
791+ }
792+
766793 #[ doc = concat!(
767794 "Checked integer subtraction. Computes `self - rhs` and checks if the result fits into an [`" ,
768795 stringify!( $SignedT) , "`], returning `None` if overflow occurred."
@@ -1793,6 +1820,35 @@ macro_rules! uint_impl {
17931820 intrinsics:: saturating_sub( self , rhs)
17941821 }
17951822
1823+ /// Saturating integer subtraction. Computes `self` - `rhs`, saturating at
1824+ /// the numeric bounds instead of overflowing.
1825+ ///
1826+ /// # Examples
1827+ ///
1828+ /// Basic usage:
1829+ ///
1830+ /// ```
1831+ /// #![feature(mixed_integer_ops_unsigned_sub)]
1832+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".saturating_sub_signed(2), 0);" ) ]
1833+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".saturating_sub_signed(-2), 3);" ) ]
1834+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MAX - 2).saturating_sub_signed(-4), " , stringify!( $SelfT) , "::MAX);" ) ]
1835+ /// ```
1836+ #[ unstable( feature = "mixed_integer_ops_unsigned_sub" , issue = "126043" ) ]
1837+ #[ must_use = "this returns the result of the operation, \
1838+ without modifying the original"]
1839+ #[ inline]
1840+ pub const fn saturating_sub_signed( self , rhs: $SignedT) -> Self {
1841+ let ( res, overflow) = self . overflowing_sub_signed( rhs) ;
1842+
1843+ if !overflow {
1844+ res
1845+ } else if rhs < 0 {
1846+ Self :: MAX
1847+ } else {
1848+ 0
1849+ }
1850+ }
1851+
17961852 /// Saturating integer multiplication. Computes `self * rhs`,
17971853 /// saturating at the numeric bounds instead of overflowing.
17981854 ///
@@ -1926,6 +1982,27 @@ macro_rules! uint_impl {
19261982 intrinsics:: wrapping_sub( self , rhs)
19271983 }
19281984
1985+ /// Wrapping (modular) subtraction with a signed integer. Computes
1986+ /// `self - rhs`, wrapping around at the boundary of the type.
1987+ ///
1988+ /// # Examples
1989+ ///
1990+ /// Basic usage:
1991+ ///
1992+ /// ```
1993+ /// #![feature(mixed_integer_ops_unsigned_sub)]
1994+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".wrapping_sub_signed(2), " , stringify!( $SelfT) , "::MAX);" ) ]
1995+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".wrapping_sub_signed(-2), 3);" ) ]
1996+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MAX - 2).wrapping_sub_signed(-4), 1);" ) ]
1997+ /// ```
1998+ #[ unstable( feature = "mixed_integer_ops_unsigned_sub" , issue = "126043" ) ]
1999+ #[ must_use = "this returns the result of the operation, \
2000+ without modifying the original"]
2001+ #[ inline]
2002+ pub const fn wrapping_sub_signed( self , rhs: $SignedT) -> Self {
2003+ self . wrapping_sub( rhs as Self )
2004+ }
2005+
19292006 /// Wrapping (modular) multiplication. Computes `self *
19302007 /// rhs`, wrapping around at the boundary of the type.
19312008 ///
@@ -2378,6 +2455,32 @@ macro_rules! uint_impl {
23782455 ( c, b || d)
23792456 }
23802457
2458+ /// Calculates `self` - `rhs` with a signed `rhs`
2459+ ///
2460+ /// Returns a tuple of the subtraction along with a boolean indicating
2461+ /// whether an arithmetic overflow would occur. If an overflow would
2462+ /// have occurred then the wrapped value is returned.
2463+ ///
2464+ /// # Examples
2465+ ///
2466+ /// Basic usage:
2467+ ///
2468+ /// ```
2469+ /// #![feature(mixed_integer_ops_unsigned_sub)]
2470+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".overflowing_sub_signed(2), (" , stringify!( $SelfT) , "::MAX, true));" ) ]
2471+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".overflowing_sub_signed(-2), (3, false));" ) ]
2472+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MAX - 2).overflowing_sub_signed(-4), (1, true));" ) ]
2473+ /// ```
2474+ #[ unstable( feature = "mixed_integer_ops_unsigned_sub" , issue = "126043" ) ]
2475+ #[ must_use = "this returns the result of the operation, \
2476+ without modifying the original"]
2477+ #[ inline]
2478+ pub const fn overflowing_sub_signed( self , rhs: $SignedT) -> ( Self , bool ) {
2479+ let ( res, overflow) = self . overflowing_sub( rhs as Self ) ;
2480+
2481+ ( res, overflow ^ ( rhs < 0 ) )
2482+ }
2483+
23812484 /// Computes the absolute difference between `self` and `other`.
23822485 ///
23832486 /// # Examples
0 commit comments