}
}
+ /// Saturating integer division. Computes `self / rhs`, saturating at the
+ /// numeric bounds instead of overflowing.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ ///
+ /// ```
+ #[unstable(feature = "saturating_int_impl", issue = "87920")]
+ #[rustc_const_unstable(feature = "saturating_int_impl", 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 {
+ match self.checked_div(rhs) {
+ Some(x) => x,
+ None => if (self < 0) == (rhs < 0) {
+ Self::MAX
+ } else {
+ Self::MIN
+ }
+ }
+ }
+
/// Saturating integer exponentiation. Computes `self.pow(exp)`,
/// saturating at the numeric bounds instead of overflowing.
///
#[inline]
fn div(self, other: Saturating<$t>) -> Saturating<$t> {
- let expected_signum = self.0.signum() * other.0.signum();
- let (result, overflowed) = self.0.overflowing_div(other.0);
-
- if !overflowed {
- Saturating(result)
- } else if expected_signum < 0 {
- Saturating(<$t>::MIN)
- } else {
- Saturating(<$t>::MAX)
- }
+ Saturating(self.0.saturating_div(other.0))
}
}
forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>,