Skip to content

Commit 1a6551b

Browse files
added unchecked_rem for signed and unsigned types
1 parent 2500a42 commit 1a6551b

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

library/core/src/num/int_macros.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,40 @@ macro_rules! int_impl {
10831083
}
10841084
}
10851085

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

library/core/src/num/uint_macros.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,38 @@ macro_rules! uint_impl {
11741174
}
11751175
}
11761176

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

0 commit comments

Comments
 (0)