@@ -898,6 +898,39 @@ macro_rules! int_impl {
898898 }
899899 }
900900
901+ /// Unchecked integer division. Computes `self / rhs`, assuming `rhs != 0` and
902+ /// overflow cannot occur.
903+ ///
904+ /// Calling `x.unchecked_div(y)` is semantically equivalent to calling
905+ /// `x.`[`checked_div`]`(y).`[`unwrap_unchecked`]`()`.
906+ ///
907+ /// # Safety
908+ ///
909+ /// This results in undefined behavior when `rhs == 0` or
910+ #[ doc = concat!( "(`self == " , stringify!( $SelfT) , "::MIN` and `rhs == -1`)," ) ]
911+ /// i.e. when [`checked_div`] would return `None`.
912+ ///
913+ /// [`unwrap_unchecked`]: option/enum.Option.html#method.unwrap_unchecked
914+ #[ doc = concat!( "[`checked_div`]: " , stringify!( $SelfT) , "::checked_div" ) ]
915+ #[ doc = concat!( "[`wrapping_div`]: " , stringify!( $SelfT) , "::wrapping_div" ) ]
916+ #[ unstable( feature = "unchecked_div_rem" , issue = "136716" ) ]
917+ #[ must_use = "this returns the result of the operation, \
918+ without modifying the original"]
919+ #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
920+ pub const unsafe fn unchecked_div( self , rhs: Self ) -> Self {
921+ assert_unsafe_precondition!(
922+ check_language_ub,
923+ concat!( stringify!( $SelfT) , "::unchecked_div cannot overflow or divide by zero" ) ,
924+ (
925+ lhs: $SelfT = self ,
926+ rhs: $SelfT = rhs
927+ ) => rhs != 0 && !lhs. overflowing_div( rhs) . 1 ,
928+ ) ;
929+
930+ // SAFETY: this is guaranteed to be safe by the caller.
931+ unsafe { intrinsics:: unchecked_div( self , rhs) }
932+ }
933+
901934 /// Strict integer division. Computes `self / rhs`, panicking
902935 /// if overflow occurred.
903936 ///
0 commit comments