Skip to content

Commit 84734b1

Browse files
added unchecked_rem for signed and unsigned types
1 parent 94e28c4 commit 84734b1

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

library/core/src/num/int_macros.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,41 @@ macro_rules! int_impl {
10841084
}
10851085
}
10861086

1087+
/// Unchecked integer remainder. Computes `self % rhs`, assuming `rhs != 0`
1088+
/// or overflow cannot occur.
1089+
///
1090+
/// # Safety
1091+
///
1092+
/// This results in undefined behavior when
1093+
#[doc = concat!("`rhs == 0` or (`self ==", stringify!($SelfT), "::MIN` and `rhs == -1`)")]
1094+
/// i.e. when [`checked_rem`] would return `None`.
1095+
///
1096+
#[doc = concat!("[`checked_rem`]: ", stringify!($SelfT), "::checked_rem")]
1097+
#[unstable(
1098+
feature = "unchecked_div_rem",
1099+
reason = "consistency with other unchecked_* functions",
1100+
issue = "136716",
1101+
)]
1102+
#[must_use = "this returns the result of the operation, \
1103+
without modifying the original"]
1104+
#[inline(always)]
1105+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1106+
pub const unsafe fn unchecked_rem(self, rhs: Self) -> Self {
1107+
assert_unsafe_precondition!(
1108+
check_language_ub,
1109+
concat!(stringify!($SelfT), "::unchecked_rem cannot overflow or accept rhs as 0"),
1110+
(
1111+
lhs: $SelfT = self,
1112+
rhs: $SelfT = rhs
1113+
) => !lhs.overflowing_rem(rhs).1 || !(rhs == 0),
1114+
);
1115+
1116+
// SAFETY: this is guaranteed to be safe by the caller.
1117+
unsafe {
1118+
intrinsics::unchecked_rem(self, rhs)
1119+
}
1120+
}
1121+
10871122
/// Strict integer remainder. Computes `self % rhs`, panicking if
10881123
/// the division results in overflow.
10891124
///

library/core/src/num/uint_macros.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,40 @@ macro_rules! uint_impl {
11761176
}
11771177
}
11781178

1179+
/// Unchecked integer remainder. Computes `self % rhs`, assuming `rhs != 0`
1180+
///
1181+
/// # Safety
1182+
///
1183+
/// This results in undefined behavior when
1184+
#[doc = concat!("`rhs == 0`")]
1185+
/// i.e. when [`checked_rem`] would return `None`.
1186+
///
1187+
#[doc = concat!("[`checked_rem`]: ", stringify!($SelfT), "::checked_rem")]
1188+
#[unstable(
1189+
feature = "unchecked_div_rem",
1190+
reason = "consistency with other unchecked_* functions",
1191+
issue = "136716",
1192+
)]
1193+
#[must_use = "this returns the result of the operation, \
1194+
without modifying the original"]
1195+
#[inline(always)]
1196+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1197+
pub const unsafe fn unchecked_rem(self, rhs: Self) -> Self {
1198+
assert_unsafe_precondition!(
1199+
check_language_ub,
1200+
concat!(stringify!($SelfT), "::unchecked_rem cannot accept rhs as 0"),
1201+
(
1202+
lhs: $SelfT = self,
1203+
rhs: $SelfT = rhs
1204+
) => !(rhs == 0),
1205+
);
1206+
1207+
// SAFETY: this is guaranteed to be safe by the caller.
1208+
unsafe {
1209+
intrinsics::unchecked_rem(self, rhs)
1210+
}
1211+
}
1212+
11791213
/// Strict integer remainder. Computes `self % rhs`.
11801214
///
11811215
/// Strict remainder calculation on unsigned types is just the regular

0 commit comments

Comments
 (0)