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
use core::num::Wrapping;

pub trait Zero {
    fn zero() -> Self;
    fn is_zero(&self) -> bool;
}

macro_rules! impl_zero {
    ($($T:ident),+) => (
        $(
            impl Zero for $T {
                #[inline(always)]
                fn zero() -> Self { 0 }
                #[inline]
                fn is_zero(&self) -> bool {
                    self == &0
                }
            }
        )+
    );
}

macro_rules! impl_zero_float {
    ($($T:ident),+) => (
        $(
            impl Zero for $T {
                #[inline(always)]
                fn zero() -> Self { 0.0 }
                #[inline]
                fn is_zero(&self) -> bool {
                    self == &0.0
                }
            }
        )+
    );
}

impl_zero!(
    u8,
    u16,
    u32,
    u64,
    u128,
    usize,
    i8,
    i16,
    i32,
    i64,
    i128,
    isize
);
impl_zero_float!(f32, f64);

impl<T> Zero for Wrapping<T>
where
    T: Zero,
{
    #[inline]
    fn zero() -> Self {
        Wrapping(T::zero())
    }
    #[inline]
    fn is_zero(&self) -> bool {
        self.0.is_zero()
    }
}