]> git.lizzy.rs Git - rust.git/commitdiff
Make `saturating_mul` a `const fn`
authorDylan MacKenzie <ecstaticmorse@gmail.com>
Mon, 3 Feb 2020 21:16:38 +0000 (13:16 -0800)
committerDylan MacKenzie <ecstaticmorse@gmail.com>
Tue, 4 Feb 2020 19:04:04 +0000 (11:04 -0800)
Co-Authored-By: 9999years <rbt@sent.as>
src/libcore/lib.rs
src/libcore/num/mod.rs

index f8857b9d733778755d52d45da93b711d1f06f94c..76e3d0d32997fb382a360b9e59998c7562a69bf6 100644 (file)
@@ -75,6 +75,7 @@
 #![feature(const_int_checked)]
 #![feature(const_int_euclidean)]
 #![feature(const_int_overflowing)]
+#![feature(const_int_saturating)]
 #![feature(const_panic)]
 #![feature(const_fn_union)]
 #![feature(const_generics)]
index 690a07ce68090c3e0e58757bb8a7686f97a5734e..1dccc87dd18b274b615c3dd05d29dc4a3b09b94e 100644 (file)
@@ -1142,17 +1142,19 @@ pub fn saturating_abs(self) -> Self {
 $EndFeature, "
 ```"),
             #[stable(feature = "wrapping", since = "1.7.0")]
+            #[rustc_const_unstable(feature = "const_int_saturating", issue = "53718")]
             #[must_use = "this returns the result of the operation, \
                           without modifying the original"]
             #[inline]
-            pub fn saturating_mul(self, rhs: Self) -> Self {
-                self.checked_mul(rhs).unwrap_or_else(|| {
-                    if (self < 0) == (rhs < 0) {
+            pub const fn saturating_mul(self, rhs: Self) -> Self {
+                match self.checked_mul(rhs) {
+                    Some(x) => x,
+                    None => if (self < 0) == (rhs < 0) {
                         Self::max_value()
                     } else {
                         Self::min_value()
                     }
-                })
+                }
             }
         }
 
@@ -3195,11 +3197,15 @@ pub const fn saturating_sub(self, rhs: Self) -> Self {
 "::MAX);", $EndFeature, "
 ```"),
             #[stable(feature = "wrapping", since = "1.7.0")]
+            #[rustc_const_unstable(feature = "const_int_saturating", issue = "53718")]
             #[must_use = "this returns the result of the operation, \
                           without modifying the original"]
             #[inline]
-            pub fn saturating_mul(self, rhs: Self) -> Self {
-                self.checked_mul(rhs).unwrap_or(Self::max_value())
+            pub const fn saturating_mul(self, rhs: Self) -> Self {
+                match self.checked_mul(rhs) {
+                    Some(x) => x,
+                    None => Self::max_value(),
+                }
             }
         }