Remove duplication by adding a macro
This commit is contained in:
parent
3efae7f7d9
commit
0cd476220b
@ -84,31 +84,15 @@ fn unwrap<T>(t: Option<T>) -> T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! int_impl {
|
macro_rules! int_impl_common {
|
||||||
($ity:ty, $uty:ty, $bits:expr) => {
|
($ty:ty, $bits:expr) => {
|
||||||
impl Int for $uty {
|
|
||||||
type OtherSign = $ity;
|
|
||||||
type UnsignedInt = $uty;
|
|
||||||
|
|
||||||
const BITS: u32 = $bits;
|
const BITS: u32 = $bits;
|
||||||
|
|
||||||
const ZERO: Self = 0;
|
const ZERO: Self = 0;
|
||||||
const ONE: Self = 1;
|
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 {
|
fn from_bool(b: bool) -> Self {
|
||||||
b as $uty
|
b as $ty
|
||||||
}
|
}
|
||||||
|
|
||||||
fn max_value() -> Self {
|
fn max_value() -> Self {
|
||||||
@ -146,17 +130,34 @@ macro_rules! int_impl {
|
|||||||
fn leading_zeros(self) -> u32 {
|
fn leading_zeros(self) -> u32 {
|
||||||
<Self>::leading_zeros(self)
|
<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 {
|
impl Int for $ity {
|
||||||
type OtherSign = $uty;
|
type OtherSign = $uty;
|
||||||
type UnsignedInt = $uty;
|
type UnsignedInt = $uty;
|
||||||
|
|
||||||
const BITS: u32 = $bits;
|
|
||||||
|
|
||||||
const ZERO: Self = 0;
|
|
||||||
const ONE: Self = 1;
|
|
||||||
|
|
||||||
fn extract_sign(self) -> (bool, $uty) {
|
fn extract_sign(self) -> (bool, $uty) {
|
||||||
if self < 0 {
|
if self < 0 {
|
||||||
(true, (!(self as $uty)).wrapping_add(1))
|
(true, (!(self as $uty)).wrapping_add(1))
|
||||||
@ -173,45 +174,7 @@ macro_rules! int_impl {
|
|||||||
me as $ity
|
me as $ity
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bool(b: bool) -> Self {
|
int_impl_common!($ity, $bits);
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user