@@ -898,6 +898,42 @@ 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+ /// If you're just trying to avoid the panic in debug mode, then **do not**
908+ /// use this. Instead, you're looking for [`wrapping_div`].
909+ ///
910+ /// # Safety
911+ ///
912+ /// This results in undefined behavior when `rhs == 0` or
913+ #[ doc = concat!( "(`self == " , stringify!( $SelfT) , "::MIN` and `rhs == -1`)," ) ]
914+ /// i.e. when [`checked_div`] would return `None`.
915+ ///
916+ /// [`unwrap_unchecked`]: Option::unwrap_unchecked
917+ #[ doc = concat!( "[`checked_div`]: " , stringify!( $SelfT) , "::checked_div" ) ]
918+ #[ doc = concat!( "[`wrapping_div`]: " , stringify!( $SelfT) , "::wrapping_div" ) ]
919+ #[ unstable( feature = "unchecked_div_rem" , issue = "136716" ) ]
920+ #[ must_use = "this returns the result of the operation, \
921+ without modifying the original"]
922+ #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
923+ pub const unsafe fn unchecked_div( self , rhs: Self ) -> Self {
924+ assert_unsafe_precondition!(
925+ check_language_ub,
926+ concat!( stringify!( $SelfT) , "::unchecked_div cannot overflow or divide by zero" ) ,
927+ (
928+ lhs: $SelfT = self ,
929+ rhs: $SelfT = rhs
930+ ) => rhs != 0 && !lhs. overflowing_div( rhs) . 1 ,
931+ ) ;
932+
933+ // SAFETY: this is guaranteed to be safe by the caller.
934+ unsafe { intrinsics:: unchecked_div( self , rhs) }
935+ }
936+
901937 /// Strict integer division. Computes `self / rhs`, panicking
902938 /// if overflow occurred.
903939 ///
0 commit comments