瀏覽代碼

Remove usage of unwrap_or_else

Alex Crichton 7 年之前
父節點
當前提交
2147753559
共有 5 個文件被更改,包括 28 次插入25 次删除
  1. 3 1
      src/float/pow.rs
  2. 17 10
      src/int/mod.rs
  3. 2 2
      src/int/mul.rs
  4. 2 4
      src/int/sdiv.rs
  5. 4 8
      src/int/udiv.rs

+ 3 - 1
src/float/pow.rs

@@ -1,3 +1,5 @@
+use int::Int;
+
 /// Returns `a` raised to the power `b`
 macro_rules! pow {
     ($a: expr, $b: expr) => ({
@@ -8,7 +10,7 @@ macro_rules! pow {
             if (b & 1) != 0 {
                 r *= a;
             }
-            b = b.checked_div(2).unwrap_or_else(|| ::abort());
+            b = b.aborting_div(2);
             if b == 0 {
                 break;
             }

+ 17 - 10
src/int/mod.rs

@@ -66,8 +66,15 @@ pub trait Int:
     fn wrapping_add(self, other: Self) -> Self;
     fn wrapping_mul(self, other: Self) -> Self;
     fn wrapping_sub(self, other: Self) -> Self;
-    fn checked_div(self, other: Self) -> Option<Self>;
-    fn checked_rem(self, other: Self) -> Option<Self>;
+    fn aborting_div(self, other: Self) -> Self;
+    fn aborting_rem(self, other: Self) -> Self;
+}
+
+fn unwrap<T>(t: Option<T>) -> T {
+    match t {
+        Some(t) => t,
+        None => ::abort(),
+    }
 }
 
 macro_rules! int_impl {
@@ -120,12 +127,12 @@ macro_rules! int_impl {
                 <Self>::wrapping_sub(self, other)
             }
 
-            fn checked_div(self, other: Self) -> Option<Self> {
-                <Self>::checked_div(self, other)
+            fn aborting_div(self, other: Self) -> Self {
+                unwrap(<Self>::checked_div(self, other))
             }
 
-            fn checked_rem(self, other: Self) -> Option<Self> {
-                <Self>::checked_rem(self, other)
+            fn aborting_rem(self, other: Self) -> Self {
+                unwrap(<Self>::checked_rem(self, other))
             }
         }
 
@@ -181,12 +188,12 @@ macro_rules! int_impl {
                 <Self>::wrapping_sub(self, other)
             }
 
-            fn checked_div(self, other: Self) -> Option<Self> {
-                <Self>::checked_div(self, other)
+            fn aborting_div(self, other: Self) -> Self {
+                unwrap(<Self>::checked_div(self, other))
             }
 
-            fn checked_rem(self, other: Self) -> Option<Self> {
-                <Self>::checked_rem(self, other)
+            fn aborting_rem(self, other: Self) -> Self {
+                unwrap(<Self>::checked_rem(self, other))
             }
         }
     }

+ 2 - 2
src/int/mul.rs

@@ -54,11 +54,11 @@ trait Mulo: Int + ops::Neg<Output = Self> {
             return result;
         }
         if sa == sb {
-            if abs_a > Self::max_value().checked_div(abs_b).unwrap_or_else(|| ::abort()) {
+            if abs_a > Self::max_value().aborting_div(abs_b) {
                 *overflow = 1;
             }
         } else {
-            if abs_a > Self::min_value().checked_div(-abs_b).unwrap_or_else(|| ::abort()) {
+            if abs_a > Self::min_value().aborting_div(-abs_b) {
                 *overflow = 1;
             }
         }

+ 2 - 4
src/int/sdiv.rs

@@ -13,8 +13,7 @@ trait Div: Int {
         let b = (other ^ s_b).wrapping_sub(s_b);
         let s = s_a ^ s_b;
 
-        let r = a.unsigned().checked_div(b.unsigned())
-            .unwrap_or_else(|| ::abort());
+        let r = a.unsigned().aborting_div(b.unsigned());
         (Self::from_unsigned(r) ^ s) - s
     }
 }
@@ -32,8 +31,7 @@ trait Mod: Int {
         let s = self >> (Self::bits() - 1);
         let a = (self ^ s).wrapping_sub(s);
 
-        let r = a.unsigned().checked_rem(b.unsigned())
-            .unwrap_or_else(|| ::abort());
+        let r = a.unsigned().aborting_rem(b.unsigned());
         (Self::from_unsigned(r) ^ s) - s
     }
 }

+ 4 - 8
src/int/udiv.rs

@@ -11,11 +11,9 @@ macro_rules! udivmod_inner {
                 // 0 X
 
                 if let Some(rem) = rem {
-                    *rem = <$ty>::from(n.low().checked_rem(d.low())
-                                        .unwrap_or_else(|| ::abort()));
+                    *rem = <$ty>::from(n.low().aborting_rem(d.low()));
                 }
-                return <$ty>::from(n.low().checked_div(d.low())
-                                    .unwrap_or_else(|| ::abort()));
+                return <$ty>::from(n.low().aborting_div(d.low()))
             } else {
                 // 0 X
                 // ---
@@ -46,11 +44,9 @@ macro_rules! udivmod_inner {
                 // ---
                 // K 0
                 if let Some(rem) = rem {
-                    *rem = <$ty>::from_parts(0, n.high().checked_rem(d.high())
-                                                 .unwrap_or_else(|| ::abort()));
+                    *rem = <$ty>::from_parts(0, n.high().aborting_rem(d.high()));
                 }
-                return <$ty>::from(n.high().checked_div(d.high())
-                                    .unwrap_or_else(|| ::abort()));
+                return <$ty>::from(n.high().aborting_div(d.high()))
             }
 
             // K K