]> git.lizzy.rs Git - rust.git/commitdiff
Use bespoke macro instead of `?` inside const fns
authorDylan MacKenzie <ecstaticmorse@gmail.com>
Sun, 9 Feb 2020 00:31:59 +0000 (16:31 -0800)
committerDylan MacKenzie <ecstaticmorse@gmail.com>
Sun, 9 Feb 2020 01:18:13 +0000 (17:18 -0800)
src/libcore/num/mod.rs

index f86376cac888a0d867a67a2492fd2e9ef2285f20..c38f51a0f55bc29e948de2748fba368388e668e3 100644 (file)
 use crate::mem;
 use crate::str::FromStr;
 
+// Used because the `?` operator is not allowed in a const context.
+macro_rules! try_opt {
+    ($e:expr) => {
+        match $e {
+            Some(x) => x,
+            None => return None,
+        }
+    };
+}
+
 macro_rules! impl_nonzero_fmt {
     ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
         $(
@@ -1000,17 +1010,17 @@ pub fn checked_pow(self, mut exp: u32) -> Option<Self> {
 
                 while exp > 1 {
                     if (exp & 1) == 1 {
-                        acc = acc.checked_mul(base)?;
+                        acc = try_opt!(acc.checked_mul(base));
                     }
                     exp /= 2;
-                    base = base.checked_mul(base)?;
+                    base = try_opt!(base.checked_mul(base));
                 }
 
                 // Deal with the final bit of the exponent separately, since
                 // squaring the base afterwards is not necessary and may cause a
                 // needless overflow.
                 if exp == 1 {
-                    acc = acc.checked_mul(base)?;
+                    acc = try_opt!(acc.checked_mul(base));
                 }
 
                 Some(acc)
@@ -3126,17 +3136,17 @@ pub fn checked_pow(self, mut exp: u32) -> Option<Self> {
 
                 while exp > 1 {
                     if (exp & 1) == 1 {
-                        acc = acc.checked_mul(base)?;
+                        acc = try_opt!(acc.checked_mul(base));
                     }
                     exp /= 2;
-                    base = base.checked_mul(base)?;
+                    base = try_opt!(base.checked_mul(base));
                 }
 
                 // Deal with the final bit of the exponent separately, since
                 // squaring the base afterwards is not necessary and may cause a
                 // needless overflow.
                 if exp == 1 {
-                    acc = acc.checked_mul(base)?;
+                    acc = try_opt!(acc.checked_mul(base));
                 }
 
                 Some(acc)