range_parser/unit.rs
1/// A trait for types that have a unit value.
2///
3/// E.g. 1 for integers, 1.0 for floats, etc.
4pub trait Unit {
5 fn unit() -> Self;
6}
7
8/// Implement One for common numeric types.
9macro_rules! impl_one_for_numeric {
10 ($($t:ty)*) => ($(
11 impl Unit for $t {
12 fn unit() -> Self {
13 1
14 }
15 }
16 )*)
17}
18
19impl_one_for_numeric!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
20
21/// Implement One for common float types.
22macro_rules! impl_one_for_floats {
23 ($($t:ty)*) => ($(
24 impl Unit for $t {
25 fn unit() -> Self {
26 1.0
27 }
28 }
29 )*)
30}
31
32impl_one_for_floats!(f32 f64);