Skip to content

Commit

Permalink
Remove duplication by adding a macro
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed Sep 14, 2017
1 parent 3efae7f commit 0cd4762
Showing 1 changed file with 26 additions and 63 deletions.
89 changes: 26 additions & 63 deletions src/int/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,15 @@ fn unwrap<T>(t: Option<T>) -> T {
}
}

macro_rules! int_impl {
($ity:ty, $uty:ty, $bits:expr) => {
impl Int for $uty {
type OtherSign = $ity;
type UnsignedInt = $uty;

macro_rules! int_impl_common {
($ty:ty, $bits:expr) => {
const BITS: u32 = $bits;

const ZERO: Self = 0;
const ONE: Self = 1;

fn extract_sign(self) -> (bool, $uty) {
(false, self)
}

fn unsigned(self) -> $uty {
self
}

fn from_unsigned(me: $uty) -> Self {
me
}

fn from_bool(b: bool) -> Self {
b as $uty
b as $ty
}

fn max_value() -> Self {
Expand Down Expand Up @@ -146,17 +130,34 @@ macro_rules! int_impl {
fn leading_zeros(self) -> u32 {
<Self>::leading_zeros(self)
}
}
}

macro_rules! int_impl {
($ity:ty, $uty:ty, $bits:expr) => {
impl Int for $uty {
type OtherSign = $ity;
type UnsignedInt = $uty;

fn extract_sign(self) -> (bool, $uty) {
(false, self)
}

fn unsigned(self) -> $uty {
self
}

fn from_unsigned(me: $uty) -> Self {
me
}

int_impl_common!($uty, $bits);
}

impl Int for $ity {
type OtherSign = $uty;
type UnsignedInt = $uty;

const BITS: u32 = $bits;

const ZERO: Self = 0;
const ONE: Self = 1;

fn extract_sign(self) -> (bool, $uty) {
if self < 0 {
(true, (!(self as $uty)).wrapping_add(1))
Expand All @@ -173,45 +174,7 @@ macro_rules! int_impl {
me as $ity
}

fn from_bool(b: bool) -> Self {
b as $ity
}

fn max_value() -> Self {
<Self>::max_value()
}

fn min_value() -> Self {
<Self>::min_value()
}

fn wrapping_add(self, other: Self) -> Self {
<Self>::wrapping_add(self, other)
}

fn wrapping_mul(self, other: Self) -> Self {
<Self>::wrapping_mul(self, other)
}

fn wrapping_sub(self, other: Self) -> Self {
<Self>::wrapping_sub(self, other)
}

fn wrapping_shl(self, other: u32) -> Self {
<Self>::wrapping_shl(self, other)
}

fn aborting_div(self, other: Self) -> Self {
unwrap(<Self>::checked_div(self, other))
}

fn aborting_rem(self, other: Self) -> Self {
unwrap(<Self>::checked_rem(self, other))
}

fn leading_zeros(self) -> u32 {
<Self>::leading_zeros(self)
}
int_impl_common!($ity, $bits);
}
}
}
Expand Down

0 comments on commit 0cd4762

Please sign in to comment.