Skip to content

Commit 46eaf11

Browse files
committed
Add checked_norem_div method for integer types
1 parent 156da98 commit 46eaf11

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

library/core/src/num/int_macros.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,40 @@ macro_rules! int_impl {
668668
}
669669
}
670670

671+
/// Checked integer division. Computes `self / rhs`, returning `None` if one `rhs == 0`,
672+
/// the division results in overflow, or remainder is not zero.
673+
///
674+
/// # Examples
675+
///
676+
/// Basic usage:
677+
///
678+
/// ```
679+
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div(-1), Some(", stringify!($Max), "));")]
680+
#[doc = concat!("assert_eq!(-5", stringify!($SelfT), ".checked_div(2), None);")]
681+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div(-1), None);")]
682+
#[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);")]
683+
/// ```
684+
#[unstable(
685+
feature = "checked_norem_div",
686+
issue = "1",
687+
)]
688+
#[must_use = "this returns the result of the operation, \
689+
without modifying the original"]
690+
#[inline]
691+
pub const fn checked_norem_div(self, rhs: Self) -> Option<Self> {
692+
if unlikely!(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
693+
None
694+
} else {
695+
// SAFETY: div by zero and by INT_MIN have been checked above
696+
unsafe {
697+
if unlikely!(intrinsics::unchecked_rem(self, rhs) == 0) {
698+
None
699+
} else {
700+
Some(intrinsics::unchecked_div(self, rhs))
701+
}
702+
}
703+
}
704+
}
671705
/// Checked integer remainder. Computes `self % rhs`, returning `None` if
672706
/// `rhs == 0` or the division results in overflow.
673707
///

library/core/src/num/uint_macros.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,40 @@ macro_rules! uint_impl {
654654
}
655655
}
656656

657+
/// Checked integer division. Computes `self / rhs`, returning `None`
658+
/// if `rhs == 0` or if division remainder is not zero.
659+
///
660+
/// # Examples
661+
///
662+
/// Basic usage:
663+
///
664+
/// ```
665+
#[doc = concat!("assert_eq!(128", stringify!($SelfT), ".checked_div(2), Some(64));")]
666+
#[doc = concat!("assert_eq!(129", stringify!($SelfT), ".checked_div(2), None);")]
667+
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_div(0), None);")]
668+
/// ```
669+
#[unstable(
670+
feature = "checked_norem_div",
671+
issue = "1",
672+
)]
673+
#[must_use = "this returns the result of the operation, \
674+
without modifying the original"]
675+
#[inline]
676+
pub const fn checked_norem_div(self, rhs: Self) -> Option<Self> {
677+
if unlikely!(rhs == 0) {
678+
None
679+
} else {
680+
// SAFETY: div by zero has been checked above and unsigned types have no other
681+
// failure modes for division
682+
unsafe {
683+
if unlikely!(intrinsics::unchecked_rem(self, rhs) == 0) {
684+
None
685+
} else {
686+
Some(intrinsics::unchecked_div(self, rhs))
687+
}
688+
}
689+
}
690+
}
657691

658692
/// Checked integer remainder. Computes `self % rhs`, returning `None`
659693
/// if `rhs == 0`.

0 commit comments

Comments
 (0)