Skip to content

Commit a0e61e2

Browse files
committed
Add saturating_div to unsigned integer types
1 parent 6bb3aca commit a0e61e2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

library/core/src/num/uint_macros.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,36 @@ macro_rules! uint_impl {
10411041
}
10421042
}
10431043

1044+
/// Saturating integer division. Computes `self / rhs`, saturating at the
1045+
/// numeric bounds instead of overflowing.
1046+
///
1047+
/// # Examples
1048+
///
1049+
/// Basic usage:
1050+
///
1051+
/// ```
1052+
/// #![feature(saturating_div)]
1053+
///
1054+
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
1055+
///
1056+
/// ```
1057+
///
1058+
/// ```should_panic
1059+
/// #![feature(saturating_div)]
1060+
///
1061+
#[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
1062+
///
1063+
/// ```
1064+
#[unstable(feature = "saturating_div", issue = "87920")]
1065+
#[rustc_const_unstable(feature = "saturating_div", issue = "87920")]
1066+
#[must_use = "this returns the result of the operation, \
1067+
without modifying the original"]
1068+
#[inline]
1069+
pub const fn saturating_div(self, rhs: Self) -> Self {
1070+
// on unsigned types, there is no overflow in integer division
1071+
self.wrapping_div(rhs)
1072+
}
1073+
10441074
/// Saturating integer exponentiation. Computes `self.pow(exp)`,
10451075
/// saturating at the numeric bounds instead of overflowing.
10461076
///

0 commit comments

Comments
 (0)