Clean up macros a little bit
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
This commit is contained in:
parent
4f5dcddcc5
commit
35c99a3ba1
23
src/num.rs
23
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user