From 35c99a3ba1a5677b85a239bbcf592240bbe146fc Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 19 Feb 2020 21:52:00 -0500 Subject: [PATCH] Clean up macros a little bit --- src/num.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/num.rs b/src/num.rs index 8cbec0b..d3e72e4 100644 --- a/src/num.rs +++ b/src/num.rs @@ -1,4 +1,6 @@ //! Numeric Helper Traits +//! +//! Home to the numeric trait chain of doom, aka `Unsigned` #![allow(unused_comparisons)] use core::convert::TryFrom; use core::fmt::Debug; @@ -52,6 +54,7 @@ pub trait Num: + Sub + SubAssign { + /// Implement absolute value function for unsigned numbers fn abs(self) -> Self { self } @@ -112,10 +115,10 @@ pub trait Unsigned: /// A Trait representing signed integer primitives pub trait Signed: Int {} -macro_rules! impl_num { - ($( $Type: ty ),* ) => { +macro_rules! impl_empty { + ($Ident: ty, ($( $Type: ty ),*) ) => { $( - impl Num for $Type {} + impl $Ident for $Type {} )* } } @@ -167,7 +170,7 @@ macro_rules! impl_unsigned { $( impl Unsigned for $Type { /// 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 { if a == b { return a; @@ -214,15 +217,7 @@ macro_rules! impl_unsigned { }; } -macro_rules! impl_signed { - ($($type: ty),* ) => { - $( - impl Signed for $type {} - )* - } -} - -impl_num!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize); +impl_empty!(Num, (i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize)); impl_int!( (i8, u8), (u8, u8), @@ -238,7 +233,7 @@ impl_int!( (usize, 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)] mod tests {