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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#![allow(clippy::missing_docs_in_private_items)] // TODO temporary

macro_rules! declare_structs {
    ($($t:ident)*) => {$(
        #[derive(Debug, Copy, Clone)]
        pub struct $t;

        impl $t {
            pub const fn per<T>(self, _: T) -> <(Self, T) as Per>::Output
            where
                (Self, T): Per,
                T: Copy,
            {
                <(Self, T)>::VALUE
            }
        }
    )*};
}

declare_structs! {
    Nanosecond
    Microsecond
    Millisecond
    Second
    Minute
    Hour
    Day
    Week
}

mod sealed {
    pub trait Sealed {}
}

pub trait Per: sealed::Sealed {
    type Output;

    const VALUE: Self::Output;
}

macro_rules! impl_per {
    ($($t:ty : $x:ident in $y:ident = $val:expr)*) => {$(
        impl sealed::Sealed for ($x, $y) {}

        impl Per for ($x, $y) {
            type Output = $t;

            const VALUE: $t = $val;
        }
    )*};
}

impl_per! {
    u16: Nanosecond in Microsecond = 1_000
    u32: Nanosecond in Millisecond = 1_000_000
    u32: Nanosecond in Second = 1_000_000_000
    u64: Nanosecond in Minute = 60_000_000_000
    u64: Nanosecond in Hour = 3_600_000_000_000
    u64: Nanosecond in Day = 86_400_000_000_000
    u64: Nanosecond in Week = 604_800_000_000_000

    u16: Microsecond in Millisecond = 1_000
    u32: Microsecond in Second = 1_000_000
    u32: Microsecond in Minute = 60_000_000
    u32: Microsecond in Hour = 3_600_000_000
    u64: Microsecond in Day = 86_400_000_000
    u64: Microsecond in Week = 604_800_000_000

    u16: Millisecond in Second = 1_000
    u16: Millisecond in Minute = 60_000
    u32: Millisecond in Hour = 3_600_000
    u32: Millisecond in Day = 86_400_000
    u32: Millisecond in Week = 604_800_000

    u8: Second in Minute = 60
    u16: Second in Hour = 3_600
    u32: Second in Day = 86_400
    u32: Second in Week = 604_800

    u8: Minute in Hour = 60
    u16: Minute in Day = 1_440
    u16: Minute in Week = 10_080

    u8: Hour in Day = 24
    u8: Hour in Week = 168

    u8: Day in Week = 7
}