Skip to content

Commit 7732367

Browse files
added unchecked_div for signed and unsigned types
1 parent e0be1a0 commit 7732367

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

library/core/src/num/int_macros.rs

+36
Original file line numberDiff line numberDiff line change
@@ -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
///

library/core/src/num/uint_macros.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,39 @@ macro_rules! uint_impl {
10171017
}
10181018
}
10191019

1020+
/// Unchecked integer division. Computes `self / rhs`, assuming `rhs` is not zero.
1021+
///
1022+
/// Calling `x.unchecked_div(y)` is semantically equivalent to calling
1023+
/// `x.`[`checked_div`]`(y).`[`unwrap_unchecked`]`()`.
1024+
///
1025+
/// If you're just trying to avoid the panic in debug mode, then **do not**
1026+
/// use this. Instead, you're looking for [`wrapping_div`].
1027+
///
1028+
/// # Safety
1029+
///
1030+
/// This results in undefined behavior when `rhs == 0`
1031+
/// i.e. when [`checked_div`] would return `None`.
1032+
///
1033+
/// [`unwrap_unchecked`]: Option::unwrap_unchecked
1034+
#[doc = concat!("[`checked_div`]: ", stringify!($SelfT), "::checked_div")]
1035+
#[doc = concat!("[`wrapping_div`]: ", stringify!($SelfT), "::wrapping_div")]
1036+
#[unstable(feature = "unchecked_div_rem", issue = "136716")]
1037+
#[must_use = "this returns the result of the operation, \
1038+
without modifying the original"]
1039+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1040+
pub const unsafe fn unchecked_div(self, rhs: Self) -> Self {
1041+
assert_unsafe_precondition!(
1042+
check_language_ub,
1043+
concat!(stringify!($SelfT), "::unchecked_div cannot divide by zero"),
1044+
(
1045+
rhs: $SelfT = rhs
1046+
) => rhs != 0,
1047+
);
1048+
1049+
// SAFETY: this is guaranteed to be safe by the caller.
1050+
unsafe { intrinsics::unchecked_div(self, rhs) }
1051+
}
1052+
10201053
/// Strict integer division. Computes `self / rhs`.
10211054
///
10221055
/// Strict division on unsigned types is just normal division. There's no

0 commit comments

Comments
 (0)