is_odd/
lib.rs

1pub trait IsOdd {
2    fn is_odd(&self) -> bool;
3}
4
5macro_rules! prim_impl {
6    ($($t:tt)*) => {
7        $(
8            impl IsOdd for $t {
9                fn is_odd(&self) -> bool {
10                    self&1 != 0
11                }
12            }
13        )*
14    };
15}
16
17prim_impl!(i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 isize usize);
18
19impl IsOdd for f32 {
20    fn is_odd(&self) -> bool {
21        if self.is_finite() && self.fract() == 0.0 {
22            (*self as i32).is_odd()
23        } else {
24            false
25        }
26    }
27}
28
29impl IsOdd for f64 {
30    fn is_odd(&self) -> bool {
31        if self.is_finite() && self.fract() == 0.0 {
32            (*self as i64).is_odd()
33        } else {
34            false
35        }
36    }
37}