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
use std::time::Duration;

/// Extension methods for initializing [`Duration`] values.
pub trait TimeUnits {
    /// Milliseconds.
    fn ms(self) -> Duration;
    /// Seconds.
    fn secs(self) -> Duration;
    /// Minutes.
    fn minutes(self) -> Duration;
    /// Hours.
    fn hours(self) -> Duration;
}
impl TimeUnits for u64 {
    fn ms(self) -> Duration {
        Duration::from_millis(self)
    }

    fn secs(self) -> Duration {
        Duration::from_secs(self)
    }

    fn minutes(self) -> Duration {
        Duration::from_secs(self * 60)
    }

    fn hours(self) -> Duration {
        Duration::from_secs(self * 60 * 60)
    }
}
impl TimeUnits for f32 {
    fn ms(self) -> Duration {
        Duration::from_secs_f32(self / 60.0)
    }

    fn secs(self) -> Duration {
        Duration::from_secs_f32(self)
    }

    fn minutes(self) -> Duration {
        Duration::from_secs_f32(self * 60.0)
    }

    fn hours(self) -> Duration {
        Duration::from_secs_f32(self * 60.0 * 60.0)
    }
}