edge_util/
easy_duration.rs

1use std::time::Duration;
2
3/// A trait to simplify construction a [`std::time::Duration`] from numeric types.
4///
5/// The trait is implemented for the common numeric types, like [`i32`], [`u32`], ...
6///
7/// Note that for signed integers, the absolute value will be used.
8pub trait EasyDuration: Sized {
9    fn seconds(self) -> Duration;
10
11    fn minutes(self) -> Duration {
12        self.seconds() * 60
13    }
14
15    fn hours(self) -> Duration {
16        self.seconds() * 60 * 60
17    }
18
19    fn days(self) -> Duration {
20        self.seconds() * 60 * 60 * 24
21    }
22
23    fn years(self) -> Duration {
24        self.days() * 365
25    }
26}
27
28impl EasyDuration for u8 {
29    fn seconds(self) -> Duration {
30        Duration::from_secs(self.into())
31    }
32}
33
34impl EasyDuration for u16 {
35    fn seconds(self) -> Duration {
36        Duration::from_secs(self.into())
37    }
38}
39
40impl EasyDuration for u32 {
41    fn seconds(self) -> Duration {
42        Duration::from_secs(self.into())
43    }
44}
45
46impl EasyDuration for u64 {
47    fn seconds(self) -> Duration {
48        Duration::from_secs(self)
49    }
50}
51
52impl EasyDuration for i8 {
53    fn seconds(self) -> Duration {
54        Duration::from_secs(self.unsigned_abs() as u64)
55    }
56}
57
58impl EasyDuration for i16 {
59    fn seconds(self) -> Duration {
60        Duration::from_secs(self.unsigned_abs() as u64)
61    }
62}
63
64impl EasyDuration for i32 {
65    fn seconds(self) -> Duration {
66        let secs = self.abs().try_into().unwrap();
67        Duration::from_secs(secs)
68    }
69}
70
71impl EasyDuration for i64 {
72    fn seconds(self) -> Duration {
73        let secs = self.abs().try_into().unwrap();
74        Duration::from_secs(secs)
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_easy_duration() {
84        // Seconds.
85        assert_eq!(1i8.seconds(), Duration::from_secs(1));
86        assert_eq!(1i16.seconds(), Duration::from_secs(1));
87        assert_eq!(1i32.seconds(), Duration::from_secs(1));
88        assert_eq!(1i64.seconds(), Duration::from_secs(1));
89        assert_eq!(1u8.seconds(), Duration::from_secs(1));
90        assert_eq!(1u16.seconds(), Duration::from_secs(1));
91        assert_eq!(1u32.seconds(), Duration::from_secs(1));
92        assert_eq!(1u64.seconds(), Duration::from_secs(1));
93
94        // Minutes
95        assert_eq!(1i8.minutes(), Duration::from_secs(60));
96        assert_eq!(1i16.minutes(), Duration::from_secs(60));
97        assert_eq!(1i32.minutes(), Duration::from_secs(60));
98        assert_eq!(1i64.minutes(), Duration::from_secs(60));
99        assert_eq!(1u8.minutes(), Duration::from_secs(60));
100        assert_eq!(1u16.minutes(), Duration::from_secs(60));
101        assert_eq!(1u32.minutes(), Duration::from_secs(60));
102        assert_eq!(1u64.minutes(), Duration::from_secs(60));
103
104        // Hours.
105        assert_eq!(1i8.hours(), Duration::from_secs(60 * 60));
106        assert_eq!(1i16.hours(), Duration::from_secs(60 * 60));
107        assert_eq!(1i32.hours(), Duration::from_secs(60 * 60));
108        assert_eq!(1i64.hours(), Duration::from_secs(60 * 60));
109        assert_eq!(1u8.hours(), Duration::from_secs(60 * 60));
110        assert_eq!(1u16.hours(), Duration::from_secs(60 * 60));
111        assert_eq!(1u32.hours(), Duration::from_secs(60 * 60));
112        assert_eq!(1u64.hours(), Duration::from_secs(60 * 60));
113
114        // Days.
115        assert_eq!(1i8.days(), Duration::from_secs(60 * 60 * 24));
116        assert_eq!(1i16.days(), Duration::from_secs(60 * 60 * 24));
117        assert_eq!(1i32.days(), Duration::from_secs(60 * 60 * 24));
118        assert_eq!(1i64.days(), Duration::from_secs(60 * 60 * 24));
119        assert_eq!(1u8.days(), Duration::from_secs(60 * 60 * 24));
120        assert_eq!(1u16.days(), Duration::from_secs(60 * 60 * 24));
121        assert_eq!(1u32.days(), Duration::from_secs(60 * 60 * 24));
122        assert_eq!(1u64.days(), Duration::from_secs(60 * 60 * 24));
123    }
124}