1#![allow(clippy::missing_docs_in_private_items)] // TODO temporary
2
3macro_rules! declare_structs {
4 ($($t:ident)*) => {$(
5 #[derive(Debug, Copy, Clone)]
6 pub struct $t;
7
8 impl $t {
9 pub const fn per<T>(self, _: T) -> <(Self, T) as Per>::Output
10 where
11 (Self, T): Per,
12 T: Copy,
13 {
14 <(Self, T)>::VALUE
15 }
16 }
17 )*};
18}
19
20declare_structs! {
21 Nanosecond
22 Microsecond
23 Millisecond
24 Second
25 Minute
26 Hour
27 Day
28 Week
29}
30
31mod sealed {
32 pub trait Sealed {}
33}
34
35pub trait Per: sealed::Sealed {
36 type Output;
37
38 const VALUE: Self::Output;
39}
40
41macro_rules! impl_per {
42 ($($t:ty : $x:ident in $y:ident = $val:expr)*) => {$(
43 impl sealed::Sealed for ($x, $y) {}
44
45 impl Per for ($x, $y) {
46 type Output = $t;
47
48 const VALUE: $t = $val;
49 }
50 )*};
51}
52
53impl_per! {
54 u16: Nanosecond in Microsecond = 1_000
55 u32: Nanosecond in Millisecond = 1_000_000
56 u32: Nanosecond in Second = 1_000_000_000
57 u64: Nanosecond in Minute = 60_000_000_000
58 u64: Nanosecond in Hour = 3_600_000_000_000
59 u64: Nanosecond in Day = 86_400_000_000_000
60 u64: Nanosecond in Week = 604_800_000_000_000
61
62 u16: Microsecond in Millisecond = 1_000
63 u32: Microsecond in Second = 1_000_000
64 u32: Microsecond in Minute = 60_000_000
65 u32: Microsecond in Hour = 3_600_000_000
66 u64: Microsecond in Day = 86_400_000_000
67 u64: Microsecond in Week = 604_800_000_000
68
69 u16: Millisecond in Second = 1_000
70 u16: Millisecond in Minute = 60_000
71 u32: Millisecond in Hour = 3_600_000
72 u32: Millisecond in Day = 86_400_000
73 u32: Millisecond in Week = 604_800_000
74
75 u8: Second in Minute = 60
76 u16: Second in Hour = 3_600
77 u32: Second in Day = 86_400
78 u32: Second in Week = 604_800
79
80 u8: Minute in Hour = 60
81 u16: Minute in Day = 1_440
82 u16: Minute in Week = 10_080
83
84 u8: Hour in Day = 24
85 u8: Hour in Week = 168
86
87 u8: Day in Week = 7
88}
89