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
pub trait Empty {
    fn empty() -> Self;
    fn is_empty(&self) -> bool;
}

macro_rules! numeric_empty_impl {
    ($($t:ty)*) => ($(
        impl Empty for $t {
            fn empty() -> Self {
                0
            }
            fn is_empty(&self) -> bool {
                *self == 0
            }
        }
    )*)
}

numeric_empty_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }

macro_rules! floating_numeric_empty_impl {
    ($($t:ty)*) => ($(
        impl Empty for $t {
            fn empty() -> Self {
                0.0
            }
            fn is_empty(&self) -> bool {
                *self == 0.0
            }
        }
    )*)
}

floating_numeric_empty_impl! { f32 f64 }

impl<T> Empty for Vec<T> {
    fn empty() -> Vec<T> {
        vec![]
    }
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl Empty for String {
    fn empty() -> String {
        "".to_string()
    }
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}