Skip to content

Commit

Permalink
Add saturating_div to unsigned integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerkindt committed Aug 19, 2021
1 parent 6bb3aca commit a0e61e2
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions library/core/src/num/uint_macros.rs
Expand Up @@ -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.
///
Expand Down

0 comments on commit a0e61e2

Please sign in to comment.