From 2147753559ab02142fca34bad3a8de3fee9b0cf0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 21:31:54 -0700 Subject: [PATCH] Remove usage of unwrap_or_else --- src/float/pow.rs | 4 +++- src/int/mod.rs | 27 +++++++++++++++++---------- src/int/mul.rs | 4 ++-- src/int/sdiv.rs | 6 ++---- src/int/udiv.rs | 12 ++++-------- 5 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/float/pow.rs b/src/float/pow.rs index f5c5757..bc15dc0 100644 --- a/src/float/pow.rs +++ b/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; } diff --git a/src/int/mod.rs b/src/int/mod.rs index 9ad3d88..0334a4a 100644 --- a/src/int/mod.rs +++ b/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; - fn checked_rem(self, other: Self) -> Option; + fn aborting_div(self, other: Self) -> Self; + fn aborting_rem(self, other: Self) -> Self; +} + +fn unwrap(t: Option) -> T { + match t { + Some(t) => t, + None => ::abort(), + } } macro_rules! int_impl { @@ -120,12 +127,12 @@ macro_rules! int_impl { ::wrapping_sub(self, other) } - fn checked_div(self, other: Self) -> Option { - ::checked_div(self, other) + fn aborting_div(self, other: Self) -> Self { + unwrap(::checked_div(self, other)) } - fn checked_rem(self, other: Self) -> Option { - ::checked_rem(self, other) + fn aborting_rem(self, other: Self) -> Self { + unwrap(::checked_rem(self, other)) } } @@ -181,12 +188,12 @@ macro_rules! int_impl { ::wrapping_sub(self, other) } - fn checked_div(self, other: Self) -> Option { - ::checked_div(self, other) + fn aborting_div(self, other: Self) -> Self { + unwrap(::checked_div(self, other)) } - fn checked_rem(self, other: Self) -> Option { - ::checked_rem(self, other) + fn aborting_rem(self, other: Self) -> Self { + unwrap(::checked_rem(self, other)) } } } diff --git a/src/int/mul.rs b/src/int/mul.rs index d61a6b4..98a8987 100644 --- a/src/int/mul.rs +++ b/src/int/mul.rs @@ -54,11 +54,11 @@ trait Mulo: Int + ops::Neg { 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; } } diff --git a/src/int/sdiv.rs b/src/int/sdiv.rs index 6ce7ffd..6dd090f 100644 --- a/src/int/sdiv.rs +++ b/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 } } diff --git a/src/int/udiv.rs b/src/int/udiv.rs index 27baff9..471f01d 100644 --- a/src/int/udiv.rs +++ b/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