@@ -898,6 +898,45 @@ 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+ #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
926+ pub const unsafe fn unchecked_div( self , rhs: Self ) -> Self {
927+ assert_unsafe_precondition!(
928+ check_language_ub,
929+ concat!( stringify!( $SelfT) , "::unchecked_div cannot overflow or accept rhs as 0" ) ,
930+ (
931+ lhs: $SelfT = self ,
932+ rhs: $SelfT = rhs
933+ ) => !lhs. overflowing_div( rhs) . 1 || !( rhs == 0 ) ,
934+ ) ;
935+
936+ // SAFETY: this is guaranteed to be safe by the caller.
937+ unsafe { intrinsics:: unchecked_div( self , rhs) }
938+ }
939+
901940 /// Strict integer division. Computes `self / rhs`, panicking
902941 /// if overflow occurred.
903942 ///
0 commit comments