@@ -898,6 +898,46 @@ macro_rules! int_impl {
898898 }
899899 }
900900
901+ /// Unchecked integer division. Computes `self / rhs`.
902+ ///
903+ /// Calling `x.unchecked_div(y)` is semantically equivalent to calling
904+ /// `x.`[`checked_div`]`(y).`[`unwrap_unchecked`]`()`.
905+ ///
906+ /// If you're just trying to avoid the panic in debug mode, then **do not**
907+ /// use this. Instead, you're looking for [`wrapping_div`].
908+ ///
909+ /// # Safety
910+ ///
911+ /// This results in undefined behavior when
912+ #[ doc = concat!( "`rhs == 0` or (`self == " , stringify!( $SelfT) , "::MIN` and `rhs == -1`)" ) ]
913+ /// i.e. when [`checked_div`] would return `None`.
914+ ///
915+ /// [`unwrap_unchecked`]: option/enum.Option.html#method.unwrap_unchecked
916+ #[ doc = concat!( "[`checked_div`]: " , stringify!( $SelfT) , "::checked_div" ) ]
917+ #[ doc = concat!( "[`wrapping_div`]: " , stringify!( $SelfT) , "::wrapping_div" ) ]
918+ #[ unstable(
919+ feature = "unchecked_div_rem" ,
920+ reason = "consistency with other unchecked_* functions" ,
921+ issue = "136716" ,
922+ ) ]
923+ #[ must_use = "this returns the result of the operation, \
924+ without modifying the original"]
925+ #[ inline( always) ]
926+ #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
927+ pub const unsafe fn unchecked_div( self , rhs: Self ) -> Self {
928+ assert_unsafe_precondition!(
929+ check_language_ub,
930+ concat!( stringify!( $SelfT) , "::unchecked_div cannot overflow or accept rhs as 0" ) ,
931+ (
932+ lhs: $SelfT = self ,
933+ rhs: $SelfT = rhs
934+ ) => !lhs. overflowing_div( rhs) . 1 || !( rhs == 0 ) ,
935+ ) ;
936+
937+ // SAFETY: this is guaranteed to be safe by the caller.
938+ unsafe { intrinsics:: unchecked_div( self , rhs) }
939+ }
940+
901941 /// Strict integer division. Computes `self / rhs`, panicking
902942 /// if overflow occurred.
903943 ///
0 commit comments