Skip to content

Commit 83e444a

Browse files
committed
Add checked_norem_div method for integer types
1 parent 156da98 commit 83e444a

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

library/core/src/num/int_macros.rs

+36
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,42 @@ 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+
/// #![feature(checked_norem_div)]
680+
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_norem_div(-1), Some(", stringify!($Max), "));")]
681+
#[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_norem_div(2), None);")]
682+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_norem_div(-1), None);")]
683+
#[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_norem_div(0), None);")]
684+
/// ```
685+
#[unstable(
686+
feature = "checked_norem_div",
687+
issue = "1",
688+
)]
689+
#[must_use = "this returns the result of the operation, \
690+
without modifying the original"]
691+
#[inline]
692+
pub const fn checked_norem_div(self, rhs: Self) -> Option<Self> {
693+
if unlikely!(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
694+
None
695+
} else {
696+
// SAFETY: div by zero and by INT_MIN have been checked above
697+
unsafe {
698+
if unlikely!(intrinsics::unchecked_rem(self, rhs) != 0) {
699+
None
700+
} else {
701+
Some(intrinsics::unchecked_div(self, rhs))
702+
}
703+
}
704+
}
705+
}
706+
671707
/// Checked integer remainder. Computes `self % rhs`, returning `None` if
672708
/// `rhs == 0` or the division results in overflow.
673709
///

library/core/src/num/uint_macros.rs

+35
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,41 @@ 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+
/// #![feature(checked_norem_div)]
666+
#[doc = concat!("assert_eq!(128", stringify!($SelfT), ".checked_norem_div(2), Some(64));")]
667+
#[doc = concat!("assert_eq!(129", stringify!($SelfT), ".checked_norem_div(2), None);")]
668+
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_norem_div(0), None);")]
669+
/// ```
670+
#[unstable(
671+
feature = "checked_norem_div",
672+
issue = "1",
673+
)]
674+
#[must_use = "this returns the result of the operation, \
675+
without modifying the original"]
676+
#[inline]
677+
pub const fn checked_norem_div(self, rhs: Self) -> Option<Self> {
678+
if unlikely!(rhs == 0) {
679+
None
680+
} else {
681+
// SAFETY: div by zero has been checked above and unsigned types have no other
682+
// failure modes for division
683+
unsafe {
684+
if unlikely!(intrinsics::unchecked_rem(self, rhs) != 0) {
685+
None
686+
} else {
687+
Some(intrinsics::unchecked_div(self, rhs))
688+
}
689+
}
690+
}
691+
}
657692

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

0 commit comments

Comments
 (0)