@@ -668,6 +668,40 @@ macro_rules! int_impl {
668
668
}
669
669
}
670
670
671
+ /// Checked integer division. Computes `self / rhs`, returning `None` if one `rhs == 0`,
672
+ /// the division results in overflow, or remainder is not zero.
673
+ ///
674
+ /// # Examples
675
+ ///
676
+ /// Basic usage:
677
+ ///
678
+ /// ```
679
+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MIN + 1).checked_div(-1), Some(" , stringify!( $Max) , "));" ) ]
680
+ #[ doc = concat!( "assert_eq!(-5" , stringify!( $SelfT) , ".checked_div(2), None);" ) ]
681
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MIN.checked_div(-1), None);" ) ]
682
+ #[ doc = concat!( "assert_eq!((1" , stringify!( $SelfT) , ").checked_div(0), None);" ) ]
683
+ /// ```
684
+ #[ unstable(
685
+ feature = "checked_norem_div" ,
686
+ issue = "1" ,
687
+ ) ]
688
+ #[ must_use = "this returns the result of the operation, \
689
+ without modifying the original"]
690
+ #[ inline]
691
+ pub const fn checked_norem_div( self , rhs: Self ) -> Option <Self > {
692
+ if unlikely!( rhs == 0 || ( ( self == Self :: MIN ) && ( rhs == -1 ) ) ) {
693
+ None
694
+ } else {
695
+ // SAFETY: div by zero and by INT_MIN have been checked above
696
+ unsafe {
697
+ if unlikely!( intrinsics:: unchecked_rem( self , rhs) == 0 ) {
698
+ None
699
+ } else {
700
+ Some ( intrinsics:: unchecked_div( self , rhs) )
701
+ }
702
+ }
703
+ }
704
+ }
671
705
/// Checked integer remainder. Computes `self % rhs`, returning `None` if
672
706
/// `rhs == 0` or the division results in overflow.
673
707
///
0 commit comments