1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use core::ops::{Add, Div, Mul, Neg, Sub};
use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign};

pub trait Operations<Rhs = Self, Output = Self>:
      Add<Rhs, Output = Output>
    + Sub<Rhs, Output = Output>
    + Mul<Rhs, Output = Output>
    + Div<Rhs, Output = Output>
{
}

impl<T, Rhs, Output> Operations<Rhs, Output> for T where
    T:    Add<Rhs, Output = Output>
        + Sub<Rhs, Output = Output>
        + Mul<Rhs, Output = Output>
        + Div<Rhs, Output = Output>
{
}

pub trait AssignOperations<Rhs = Self>:
      AddAssign<Rhs> 
    + SubAssign<Rhs> 
    + MulAssign<Rhs> 
    + DivAssign<Rhs> 
{
}

impl<T, Rhs> AssignOperations<Rhs> for T where 
    T:    AddAssign<Rhs> 
        + SubAssign<Rhs> 
        + MulAssign<Rhs> 
        + DivAssign<Rhs>
{
}

pub trait Zero: Sized + Add<Self, Output = Self> {
    /// Returns the additive identity element 0
    fn zero() -> Self;
}

pub trait One: Sized + Mul<Self, Output = Self> {
    /// Returns the multiplicative identity element 1
    fn one() -> Self;
}
   

pub trait Number: PartialEq + Sized + Operations + AssignOperations + Zero + One
{

}

pub trait Signed: Number + Neg<Output = Self> {
    fn abs(&self) -> Self;
}

macro_rules! impl_identity {
    ($name: ident for $($t: ty)*, $method: ident, $v: expr) => ($(
        impl $name for $t {
            #[inline]
            fn $method() -> $t {
                $v
            }
        }
    )*)
}

impl_identity!( Zero for f32 f64, zero, 0.0 );
impl_identity!( One for f32 f64, one, 1.0 );
impl_identity!( Zero for usize u8 u16 u32 u64 isize i8 i16 i32 i64, zero, 0 );
impl_identity!( One for usize u8 u16 u32 u64 isize i8 i16 i32 i64, one, 1 );

macro_rules! impl_signed {
    ($($t:ty)*, $v: expr) => ($(
        impl Signed for $t {
            #[inline]
            fn abs(&self) -> $t {
                if *self < $v { -*self } else { *self }
            }
        }
    )*)
}

impl_signed!( isize i8 i16 i32 i64, 0 );
impl_signed!( f32 f64, 0.0 );

macro_rules! impl_trait {
    ($name: ident for $($t: ty)*) => ($(
        impl $name for $t {

        }
    )*)
}

impl_trait!( Number for usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64);