]> git.lizzy.rs Git - rust.git/blobdiff - library/core/src/num/int_macros.rs
Simplify saturating_div
[rust.git] / library / core / src / num / int_macros.rs
index 7c36f4cdd203fb49b801fb8829d24ee76005ce46..2c866812937e975ba5b4406a46154da5d5c3ca40 100644 (file)
@@ -946,14 +946,9 @@ pub const fn saturating_mul(self, rhs: Self) -> Self {
                       without modifying the original"]
         #[inline]
         pub const fn saturating_div(self, rhs: Self) -> Self {
-            let (result, overflowed) = self.overflowing_div(rhs);
-
-            if !overflowed {
-                result
-            } else if (self < 0) == (rhs < 0) {
-                Self::MAX
-            } else {
-                Self::MIN
+            match self.overflowing_div(rhs) {
+                (result, false) => result,
+                (_result, true) => Self::MAX, // MIN / -1 is the only possible saturating overflow
             }
         }