Clean up macros a little bit
timw4mail/rusty-numbers/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2020-02-19 21:52:00 -05:00
parent 4f5dcddcc5
commit 35c99a3ba1
1 changed files with 9 additions and 14 deletions

View File

@ -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 {