Remove usage of unwrap_or_else

master
Alex Crichton 2017-06-23 21:31:54 -07:00
parent a2bdb4b379
commit 2147753559
5 changed files with 28 additions and 25 deletions

View File

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

View File

@ -66,8 +66,15 @@ pub trait Int:
fn wrapping_add(self, other: Self) -> Self; fn wrapping_add(self, other: Self) -> Self;
fn wrapping_mul(self, other: Self) -> Self; fn wrapping_mul(self, other: Self) -> Self;
fn wrapping_sub(self, other: Self) -> Self; fn wrapping_sub(self, other: Self) -> Self;
fn checked_div(self, other: Self) -> Option<Self>; fn aborting_div(self, other: Self) -> Self;
fn checked_rem(self, other: Self) -> Option<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 { macro_rules! int_impl {
@ -120,12 +127,12 @@ macro_rules! int_impl {
<Self>::wrapping_sub(self, other) <Self>::wrapping_sub(self, other)
} }
fn checked_div(self, other: Self) -> Option<Self> { fn aborting_div(self, other: Self) -> Self {
<Self>::checked_div(self, other) unwrap(<Self>::checked_div(self, other))
} }
fn checked_rem(self, other: Self) -> Option<Self> { fn aborting_rem(self, other: Self) -> Self {
<Self>::checked_rem(self, other) unwrap(<Self>::checked_rem(self, other))
} }
} }
@ -181,12 +188,12 @@ macro_rules! int_impl {
<Self>::wrapping_sub(self, other) <Self>::wrapping_sub(self, other)
} }
fn checked_div(self, other: Self) -> Option<Self> { fn aborting_div(self, other: Self) -> Self {
<Self>::checked_div(self, other) unwrap(<Self>::checked_div(self, other))
} }
fn checked_rem(self, other: Self) -> Option<Self> { fn aborting_rem(self, other: Self) -> Self {
<Self>::checked_rem(self, other) unwrap(<Self>::checked_rem(self, other))
} }
} }
} }

View File

@ -54,11 +54,11 @@ trait Mulo: Int + ops::Neg<Output = Self> {
return result; return result;
} }
if sa == sb { 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; *overflow = 1;
} }
} else { } 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; *overflow = 1;
} }
} }

View File

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

View File

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