diff options
| author | Michael Watzko <michael@watzko.de> | 2021-08-19 11:07:34 +0200 |
|---|---|---|
| committer | Michael Watzko <michael@watzko.de> | 2021-08-19 11:07:34 +0200 |
| commit | a0e61e278057ffa3a6ca1d39fe04bf1569d3cf40 (patch) | |
| tree | e81126b1d52dfa4b7445d516f9bff923dd0c3743 | |
| parent | 6bb3acab74f9cf4bf3d3b81f0805416cd7b3ee20 (diff) | |
| download | rust-a0e61e278057ffa3a6ca1d39fe04bf1569d3cf40.tar.gz rust-a0e61e278057ffa3a6ca1d39fe04bf1569d3cf40.zip | |
Add saturating_div to unsigned integer types
| -rw-r--r-- | library/core/src/num/uint_macros.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index ae113a47e95..d6bd3115a02 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -1041,6 +1041,36 @@ macro_rules! uint_impl { } } + /// Saturating integer division. Computes `self / rhs`, saturating at the + /// numeric bounds instead of overflowing. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_div)] + /// + #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")] + /// + /// ``` + /// + /// ```should_panic + /// #![feature(saturating_div)] + /// + #[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")] + /// + /// ``` + #[unstable(feature = "saturating_div", issue = "87920")] + #[rustc_const_unstable(feature = "saturating_div", issue = "87920")] + #[must_use = "this returns the result of the operation, \ + without modifying the original"] + #[inline] + pub const fn saturating_div(self, rhs: Self) -> Self { + // on unsigned types, there is no overflow in integer division + self.wrapping_div(rhs) + } + /// Saturating integer exponentiation. Computes `self.pow(exp)`, /// saturating at the numeric bounds instead of overflowing. /// |
