Clean up macros a little bit
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-02-19 21:52:00 -05:00
parent 4f5dcddcc5
commit 35c99a3ba1

View File

@ -1,4 +1,6 @@
//! Numeric Helper Traits //! Numeric Helper Traits
//!
//! Home to the numeric trait chain of doom, aka `Unsigned`
#![allow(unused_comparisons)] #![allow(unused_comparisons)]
use core::convert::TryFrom; use core::convert::TryFrom;
use core::fmt::Debug; use core::fmt::Debug;
@ -52,6 +54,7 @@ pub trait Num:
+ Sub + Sub
+ SubAssign + SubAssign
{ {
/// Implement absolute value function for unsigned numbers
fn abs(self) -> Self { fn abs(self) -> Self {
self self
} }
@ -112,10 +115,10 @@ pub trait Unsigned:
/// A Trait representing signed integer primitives /// A Trait representing signed integer primitives
pub trait Signed: Int {} pub trait Signed: Int {}
macro_rules! impl_num { macro_rules! impl_empty {
($( $Type: ty ),* ) => { ($Ident: ty, ($( $Type: ty ),*) ) => {
$( $(
impl Num for $Type {} impl $Ident for $Type {}
)* )*
} }
} }
@ -167,7 +170,7 @@ macro_rules! impl_unsigned {
$( $(
impl Unsigned for $Type { impl Unsigned for $Type {
/// Implementation based on /// Implementation based on
/// [https://en.wikipedia.org/wiki/Binary_GCD_algorithm](https://en.wikipedia.org/wiki/Binary_GCD_algorithm) /// [Binary GCD algorithm](https://en.wikipedia.org/wiki/Binary_GCD_algorithm)
fn gcd(a: $Type, b: $Type) -> $Type { fn gcd(a: $Type, b: $Type) -> $Type {
if a == b { if a == b {
return a; return a;
@ -214,15 +217,7 @@ macro_rules! impl_unsigned {
}; };
} }
macro_rules! impl_signed { impl_empty!(Num, (i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize));
($($type: ty),* ) => {
$(
impl Signed for $type {}
)*
}
}
impl_num!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
impl_int!( impl_int!(
(i8, u8), (i8, u8),
(u8, u8), (u8, u8),
@ -238,7 +233,7 @@ impl_int!(
(usize, usize) (usize, usize)
); );
impl_unsigned!(u8, u16, u32, u64, u128, usize); impl_unsigned!(u8, u16, u32, u64, u128, usize);
impl_signed!(i8, i16, i32, i64, i128, isize); impl_empty!(Signed, (i8, i16, i32, i64, i128, isize));
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {