Skip to content

Commit 2500a42

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

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

library/core/src/num/int_macros.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
///

library/core/src/num/uint_macros.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,44 @@ macro_rules! uint_impl {
10171017
}
10181018
}
10191019

1020+
/// Unchecked integer division. Computes `self / rhs`.
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
1031+
#[doc = concat!("`rhs == 0`")]
1032+
/// i.e. when [`checked_div`] would return `None`.
1033+
///
1034+
/// [`unwrap_unchecked`]: option/enum.Option.html#method.unwrap_unchecked
1035+
#[doc = concat!("[`checked_div`]: ", stringify!($SelfT), "::checked_div")]
1036+
#[doc = concat!("[`wrapping_div`]: ", stringify!($SelfT), "::wrapping_div")]
1037+
#[unstable(
1038+
feature = "unchecked_div_rem",
1039+
reason = "consistency with other unchecked_* functions",
1040+
issue = "136716",
1041+
)]
1042+
#[must_use = "this returns the result of the operation, \
1043+
without modifying the original"]
1044+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1045+
pub const unsafe fn unchecked_div(self, rhs: Self) -> Self {
1046+
assert_unsafe_precondition!(
1047+
check_language_ub,
1048+
concat!(stringify!($SelfT), "::unchecked_div cannot accept rhs as zero"),
1049+
(
1050+
rhs: $SelfT = rhs
1051+
) => !(rhs == 0),
1052+
);
1053+
1054+
// SAFETY: this is guaranteed to be safe by the caller.
1055+
unsafe { intrinsics::unchecked_div(self, rhs) }
1056+
}
1057+
10201058
/// Strict integer division. Computes `self / rhs`.
10211059
///
10221060
/// Strict division on unsigned types is just normal division. There's no

0 commit comments

Comments
 (0)