vectrix/
traits.rs

1//! Abstractions over number types.
2
3/// Defines the absolute value for a type.
4pub trait Abs {
5    /// Returns the absolute value of this type.
6    fn abs(self) -> Self;
7}
8
9/// Defines a multiplicative identity element for a type.
10pub trait One {
11    /// Returns the multiplicative identity element of this type.
12    fn one() -> Self;
13}
14
15/// Defines a additive identity element for a type.
16pub trait Zero {
17    /// Returns the additive identity element of this type.
18    fn zero() -> Self;
19}
20
21macro_rules! impl_one {
22    ($one:literal $($ty:ty)+) => ($(
23        impl One for $ty {
24            #[inline]
25            fn one() -> $ty {
26                $one
27            }
28        }
29    )+)
30}
31
32macro_rules! impl_zero {
33    ($zero:literal $($ty:ty)+) => ($(
34        impl Zero for $ty {
35            #[inline]
36            fn zero() -> $ty {
37                $zero
38            }
39        }
40    )+)
41}
42
43macro_rules! impl_abs {
44    ($($ty:ident)+) => ($(
45        impl Abs for $ty {
46            #[inline]
47            fn abs(self) -> $ty {
48                $ty::abs(self)
49            }
50        }
51    )+)
52}
53
54macro_rules! impl_abs_self {
55    ($($ty:ident)+) => ($(
56        impl Abs for $ty {
57            #[inline]
58            fn abs(self) -> $ty {
59                self
60            }
61        }
62    )+)
63}
64
65impl_one! { true bool }
66impl_one! { 1 usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
67impl_one! { 1.0 f32 f64 }
68
69impl_zero! { false bool }
70impl_zero! { 0 usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
71impl_zero! { 0.0 f32 f64 }
72
73impl_abs_self! { usize u8 u16 u32 u64 u128 }
74impl_abs! { isize i8 i16 i32 i64 i128 }
75#[cfg(feature = "std")]
76impl_abs! { f32 f64 }