rust_fp_categories/
empty.rs

1pub trait Empty {
2    fn empty() -> Self;
3    fn is_empty(&self) -> bool;
4}
5
6macro_rules! numeric_empty_impl {
7    ($($t:ty)*) => ($(
8        impl Empty for $t {
9            fn empty() -> Self {
10                0
11            }
12            fn is_empty(&self) -> bool {
13                *self == 0
14            }
15        }
16    )*)
17}
18
19numeric_empty_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
20
21macro_rules! floating_numeric_empty_impl {
22    ($($t:ty)*) => ($(
23        impl Empty for $t {
24            fn empty() -> Self {
25                0.0
26            }
27            fn is_empty(&self) -> bool {
28                *self == 0.0
29            }
30        }
31    )*)
32}
33
34floating_numeric_empty_impl! { f32 f64 }
35
36impl<T> Empty for Vec<T> {
37    fn empty() -> Vec<T> {
38        vec![]
39    }
40    fn is_empty(&self) -> bool {
41        self.len() == 0
42    }
43}
44
45impl Empty for String {
46    fn empty() -> String {
47        "".to_string()
48    }
49    fn is_empty(&self) -> bool {
50        self.len() == 0
51    }
52}