wasmer_deploy_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
24impl EasyDuration for u8 {
25    fn seconds(self) -> Duration {
26        Duration::from_secs(self.into())
27    }
28}
29
30impl EasyDuration for u16 {
31    fn seconds(self) -> Duration {
32        Duration::from_secs(self.into())
33    }
34}
35
36impl EasyDuration for u32 {
37    fn seconds(self) -> Duration {
38        Duration::from_secs(self.into())
39    }
40}
41
42impl EasyDuration for u64 {
43    fn seconds(self) -> Duration {
44        Duration::from_secs(self)
45    }
46}
47
48impl EasyDuration for i8 {
49    fn seconds(self) -> Duration {
50        Duration::from_secs(self.unsigned_abs() as u64)
51    }
52}
53
54impl EasyDuration for i16 {
55    fn seconds(self) -> Duration {
56        Duration::from_secs(self.unsigned_abs() as u64)
57    }
58}
59
60impl EasyDuration for i32 {
61    fn seconds(self) -> Duration {
62        let secs = self.abs().try_into().unwrap();
63        Duration::from_secs(secs)
64    }
65}
66
67impl EasyDuration for i64 {
68    fn seconds(self) -> Duration {
69        let secs = self.abs().try_into().unwrap();
70        Duration::from_secs(secs)
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_easy_duration() {
80        // Seconds.
81        assert_eq!(1i8.seconds(), Duration::from_secs(1));
82        assert_eq!(1i16.seconds(), Duration::from_secs(1));
83        assert_eq!(1i32.seconds(), Duration::from_secs(1));
84        assert_eq!(1i64.seconds(), Duration::from_secs(1));
85        assert_eq!(1u8.seconds(), Duration::from_secs(1));
86        assert_eq!(1u16.seconds(), Duration::from_secs(1));
87        assert_eq!(1u32.seconds(), Duration::from_secs(1));
88        assert_eq!(1u64.seconds(), Duration::from_secs(1));
89
90        // Minutes
91        assert_eq!(1i8.minutes(), Duration::from_secs(60));
92        assert_eq!(1i16.minutes(), Duration::from_secs(60));
93        assert_eq!(1i32.minutes(), Duration::from_secs(60));
94        assert_eq!(1i64.minutes(), Duration::from_secs(60));
95        assert_eq!(1u8.minutes(), Duration::from_secs(60));
96        assert_eq!(1u16.minutes(), Duration::from_secs(60));
97        assert_eq!(1u32.minutes(), Duration::from_secs(60));
98        assert_eq!(1u64.minutes(), Duration::from_secs(60));
99
100        // Hours.
101        assert_eq!(1i8.hours(), Duration::from_secs(60 * 60));
102        assert_eq!(1i16.hours(), Duration::from_secs(60 * 60));
103        assert_eq!(1i32.hours(), Duration::from_secs(60 * 60));
104        assert_eq!(1i64.hours(), Duration::from_secs(60 * 60));
105        assert_eq!(1u8.hours(), Duration::from_secs(60 * 60));
106        assert_eq!(1u16.hours(), Duration::from_secs(60 * 60));
107        assert_eq!(1u32.hours(), Duration::from_secs(60 * 60));
108        assert_eq!(1u64.hours(), Duration::from_secs(60 * 60));
109
110        // Days.
111        assert_eq!(1i8.days(), Duration::from_secs(60 * 60 * 24));
112        assert_eq!(1i16.days(), Duration::from_secs(60 * 60 * 24));
113        assert_eq!(1i32.days(), Duration::from_secs(60 * 60 * 24));
114        assert_eq!(1i64.days(), Duration::from_secs(60 * 60 * 24));
115        assert_eq!(1u8.days(), Duration::from_secs(60 * 60 * 24));
116        assert_eq!(1u16.days(), Duration::from_secs(60 * 60 * 24));
117        assert_eq!(1u32.days(), Duration::from_secs(60 * 60 * 24));
118        assert_eq!(1u64.days(), Duration::from_secs(60 * 60 * 24));
119    }
120}