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