@@ -433,6 +433,28 @@ macro_rules! int_impl {
433
433
unsafe { intrinsics:: unchecked_add( self , rhs) }
434
434
}
435
435
436
+ /// Checked addition with an unsigned integer. Computes `self + rhs`,
437
+ /// returning `None` if overflow occurred.
438
+ ///
439
+ /// # Examples
440
+ ///
441
+ /// Basic usage:
442
+ ///
443
+ /// ```
444
+ /// # #![feature(mixed_integer_ops)]
445
+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".checked_add_unsigned(2), Some(3));" ) ]
446
+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MAX - 2).checked_add_unsigned(3), None);" ) ]
447
+ /// ```
448
+ #[ unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
449
+ #[ rustc_const_unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
450
+ #[ must_use = "this returns the result of the operation, \
451
+ without modifying the original"]
452
+ #[ inline]
453
+ pub const fn checked_add_unsigned( self , rhs: $UnsignedT) -> Option <Self > {
454
+ let ( a, b) = self . overflowing_add_unsigned( rhs) ;
455
+ if unlikely!( b) { None } else { Some ( a) }
456
+ }
457
+
436
458
/// Checked integer subtraction. Computes `self - rhs`, returning `None` if
437
459
/// overflow occurred.
438
460
///
@@ -479,6 +501,28 @@ macro_rules! int_impl {
479
501
unsafe { intrinsics:: unchecked_sub( self , rhs) }
480
502
}
481
503
504
+ /// Checked subtraction with an unsigned integer. Computes `self - rhs`,
505
+ /// returning `None` if overflow occurred.
506
+ ///
507
+ /// # Examples
508
+ ///
509
+ /// Basic usage:
510
+ ///
511
+ /// ```
512
+ /// # #![feature(mixed_integer_ops)]
513
+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".checked_sub_unsigned(2), Some(-1));" ) ]
514
+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MIN + 2).checked_sub_unsigned(3), None);" ) ]
515
+ /// ```
516
+ #[ unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
517
+ #[ rustc_const_unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
518
+ #[ must_use = "this returns the result of the operation, \
519
+ without modifying the original"]
520
+ #[ inline]
521
+ pub const fn checked_sub_unsigned( self , rhs: $UnsignedT) -> Option <Self > {
522
+ let ( a, b) = self . overflowing_sub_unsigned( rhs) ;
523
+ if unlikely!( b) { None } else { Some ( a) }
524
+ }
525
+
482
526
/// Checked integer multiplication. Computes `self * rhs`, returning `None` if
483
527
/// overflow occurred.
484
528
///
@@ -826,6 +870,32 @@ macro_rules! int_impl {
826
870
intrinsics:: saturating_add( self , rhs)
827
871
}
828
872
873
+ /// Saturating addition with an unsigned integer. Computes `self + rhs`,
874
+ /// saturating at the numeric bounds instead of overflowing.
875
+ ///
876
+ /// # Examples
877
+ ///
878
+ /// Basic usage:
879
+ ///
880
+ /// ```
881
+ /// # #![feature(mixed_integer_ops)]
882
+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".saturating_add_unsigned(2), 3);" ) ]
883
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.saturating_add_unsigned(100), " , stringify!( $SelfT) , "::MAX);" ) ]
884
+ /// ```
885
+ #[ unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
886
+ #[ rustc_const_unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
887
+ #[ must_use = "this returns the result of the operation, \
888
+ without modifying the original"]
889
+ #[ inline]
890
+ pub const fn saturating_add_unsigned( self , rhs: $UnsignedT) -> Self {
891
+ // Overflow can only happen at the upper bound
892
+ // We cannot use `unwrap_or` here because it is not `const`
893
+ match self . checked_add_unsigned( rhs) {
894
+ Some ( x) => x,
895
+ None => Self :: MAX ,
896
+ }
897
+ }
898
+
829
899
/// Saturating integer subtraction. Computes `self - rhs`, saturating at the
830
900
/// numeric bounds instead of overflowing.
831
901
///
@@ -847,6 +917,32 @@ macro_rules! int_impl {
847
917
intrinsics:: saturating_sub( self , rhs)
848
918
}
849
919
920
+ /// Saturating subtraction with an unsigned integer. Computes `self - rhs`,
921
+ /// saturating at the numeric bounds instead of overflowing.
922
+ ///
923
+ /// # Examples
924
+ ///
925
+ /// Basic usage:
926
+ ///
927
+ /// ```
928
+ /// # #![feature(mixed_integer_ops)]
929
+ #[ doc = concat!( "assert_eq!(100" , stringify!( $SelfT) , ".saturating_sub_unsigned(127), -27);" ) ]
930
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MIN.saturating_sub_unsigned(100), " , stringify!( $SelfT) , "::MIN);" ) ]
931
+ /// ```
932
+ #[ unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
933
+ #[ rustc_const_unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
934
+ #[ must_use = "this returns the result of the operation, \
935
+ without modifying the original"]
936
+ #[ inline]
937
+ pub const fn saturating_sub_unsigned( self , rhs: $UnsignedT) -> Self {
938
+ // Overflow can only happen at the lower bound
939
+ // We cannot use `unwrap_or` here because it is not `const`
940
+ match self . checked_sub_unsigned( rhs) {
941
+ Some ( x) => x,
942
+ None => Self :: MIN ,
943
+ }
944
+ }
945
+
850
946
/// Saturating integer negation. Computes `-self`, returning `MAX` if `self == MIN`
851
947
/// instead of overflowing.
852
948
///
@@ -1002,6 +1098,27 @@ macro_rules! int_impl {
1002
1098
intrinsics:: wrapping_add( self , rhs)
1003
1099
}
1004
1100
1101
+ /// Wrapping (modular) addition with an unsigned integer. Computes
1102
+ /// `self + rhs`, wrapping around at the boundary of the type.
1103
+ ///
1104
+ /// # Examples
1105
+ ///
1106
+ /// Basic usage:
1107
+ ///
1108
+ /// ```
1109
+ /// # #![feature(mixed_integer_ops)]
1110
+ #[ doc = concat!( "assert_eq!(100" , stringify!( $SelfT) , ".wrapping_add_unsigned(27), 127);" ) ]
1111
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.wrapping_add_unsigned(2), " , stringify!( $SelfT) , "::MIN + 1);" ) ]
1112
+ /// ```
1113
+ #[ unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
1114
+ #[ rustc_const_unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
1115
+ #[ must_use = "this returns the result of the operation, \
1116
+ without modifying the original"]
1117
+ #[ inline( always) ]
1118
+ pub const fn wrapping_add_unsigned( self , rhs: $UnsignedT) -> Self {
1119
+ self . wrapping_add( rhs as Self )
1120
+ }
1121
+
1005
1122
/// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the
1006
1123
/// boundary of the type.
1007
1124
///
@@ -1022,6 +1139,27 @@ macro_rules! int_impl {
1022
1139
intrinsics:: wrapping_sub( self , rhs)
1023
1140
}
1024
1141
1142
+ /// Wrapping (modular) subtraction with an unsigned integer. Computes
1143
+ /// `self - rhs`, wrapping around at the boundary of the type.
1144
+ ///
1145
+ /// # Examples
1146
+ ///
1147
+ /// Basic usage:
1148
+ ///
1149
+ /// ```
1150
+ /// # #![feature(mixed_integer_ops)]
1151
+ #[ doc = concat!( "assert_eq!(0" , stringify!( $SelfT) , ".wrapping_sub_unsigned(127), -127);" ) ]
1152
+ #[ doc = concat!( "assert_eq!((-2" , stringify!( $SelfT) , ").wrapping_sub_unsigned(" , stringify!( $UnsignedT) , "::MAX), -1);" ) ]
1153
+ /// ```
1154
+ #[ unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
1155
+ #[ rustc_const_unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
1156
+ #[ must_use = "this returns the result of the operation, \
1157
+ without modifying the original"]
1158
+ #[ inline( always) ]
1159
+ pub const fn wrapping_sub_unsigned( self , rhs: $UnsignedT) -> Self {
1160
+ self . wrapping_sub( rhs as Self )
1161
+ }
1162
+
1025
1163
/// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at
1026
1164
/// the boundary of the type.
1027
1165
///
@@ -1372,6 +1510,33 @@ macro_rules! int_impl {
1372
1510
( sum as $SelfT, carry)
1373
1511
}
1374
1512
1513
+ /// Calculates `self` + `rhs` with an unsigned `rhs`
1514
+ ///
1515
+ /// Returns a tuple of the addition along with a boolean indicating
1516
+ /// whether an arithmetic overflow would occur. If an overflow would
1517
+ /// have occurred then the wrapped value is returned.
1518
+ ///
1519
+ /// # Examples
1520
+ ///
1521
+ /// Basic usage:
1522
+ ///
1523
+ /// ```
1524
+ /// # #![feature(mixed_integer_ops)]
1525
+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".overflowing_add_unsigned(2), (3, false));" ) ]
1526
+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MIN).overflowing_add_unsigned(" , stringify!( $UnsignedT) , "::MAX), (" , stringify!( $SelfT) , "::MAX, false));" ) ]
1527
+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MAX - 2).overflowing_add_unsigned(3), (" , stringify!( $SelfT) , "::MIN, true));" ) ]
1528
+ /// ```
1529
+ #[ unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
1530
+ #[ rustc_const_unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
1531
+ #[ must_use = "this returns the result of the operation, \
1532
+ without modifying the original"]
1533
+ #[ inline]
1534
+ pub const fn overflowing_add_unsigned( self , rhs: $UnsignedT) -> ( Self , bool ) {
1535
+ let rhs = rhs as Self ;
1536
+ let ( res, overflowed) = self . overflowing_add( rhs) ;
1537
+ ( res, overflowed ^ ( rhs < 0 ) )
1538
+ }
1539
+
1375
1540
/// Calculates `self` - `rhs`
1376
1541
///
1377
1542
/// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow
@@ -1423,6 +1588,33 @@ macro_rules! int_impl {
1423
1588
( sum as $SelfT, borrow)
1424
1589
}
1425
1590
1591
+ /// Calculates `self` - `rhs` with an unsigned `rhs`
1592
+ ///
1593
+ /// Returns a tuple of the subtraction along with a boolean indicating
1594
+ /// whether an arithmetic overflow would occur. If an overflow would
1595
+ /// have occurred then the wrapped value is returned.
1596
+ ///
1597
+ /// # Examples
1598
+ ///
1599
+ /// Basic usage:
1600
+ ///
1601
+ /// ```
1602
+ /// # #![feature(mixed_integer_ops)]
1603
+ #[ doc = concat!( "assert_eq!(1" , stringify!( $SelfT) , ".overflowing_sub_unsigned(2), (-1, false));" ) ]
1604
+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MAX).overflowing_sub_unsigned(" , stringify!( $UnsignedT) , "::MAX), (" , stringify!( $SelfT) , "::MIN, false));" ) ]
1605
+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MIN + 2).overflowing_sub_unsigned(3), (" , stringify!( $SelfT) , "::MAX, true));" ) ]
1606
+ /// ```
1607
+ #[ unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
1608
+ #[ rustc_const_unstable( feature = "mixed_integer_ops" , issue = "87840" ) ]
1609
+ #[ must_use = "this returns the result of the operation, \
1610
+ without modifying the original"]
1611
+ #[ inline]
1612
+ pub const fn overflowing_sub_unsigned( self , rhs: $UnsignedT) -> ( Self , bool ) {
1613
+ let rhs = rhs as Self ;
1614
+ let ( res, overflowed) = self . overflowing_sub( rhs) ;
1615
+ ( res, overflowed ^ ( rhs < 0 ) )
1616
+ }
1617
+
1426
1618
/// Calculates the multiplication of `self` and `rhs`.
1427
1619
///
1428
1620
/// Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic overflow
0 commit comments