gd32vf103xx_hal/
time.rs

1//! Time units
2
3/// Bits per second
4#[derive(PartialEq, PartialOrd, Clone, Copy)]
5pub struct Bps(pub u32);
6
7/// Hertz
8#[derive(PartialEq, PartialOrd, Clone, Copy)]
9pub struct Hertz(pub u32);
10
11/// KiloHertz
12#[derive(PartialEq, PartialOrd, Clone, Copy)]
13pub struct KiloHertz(pub u32);
14
15/// MegaHertz
16#[derive(PartialEq, PartialOrd, Clone, Copy)]
17pub struct MegaHertz(pub u32);
18
19/// Time unit
20#[derive(PartialEq, PartialOrd, Clone, Copy)]
21pub struct MilliSeconds(pub u32);
22
23/// Extension trait that adds convenience methods to the `u32` type
24pub trait U32Ext {
25    /// Wrap in `Bps`
26    fn bps(self) -> Bps;
27
28    /// Wrap in `Hertz`
29    fn hz(self) -> Hertz;
30
31    /// Wrap in `KiloHertz`
32    fn khz(self) -> KiloHertz;
33
34    /// Wrap in `MegaHertz`
35    fn mhz(self) -> MegaHertz;
36
37    /// Wrap in `MilliSeconds`
38    fn ms(self) -> MilliSeconds;
39}
40
41impl U32Ext for u32 {
42    fn bps(self) -> Bps {
43        Bps(self)
44    }
45
46    fn hz(self) -> Hertz {
47        Hertz(self)
48    }
49
50    fn khz(self) -> KiloHertz {
51        KiloHertz(self)
52    }
53
54    fn mhz(self) -> MegaHertz {
55        MegaHertz(self)
56    }
57
58    fn ms(self) -> MilliSeconds {
59        MilliSeconds(self)
60    }
61}
62
63impl Into<Hertz> for KiloHertz {
64    fn into(self) -> Hertz {
65        Hertz(self.0 * 1_000)
66    }
67}
68
69impl Into<Hertz> for MegaHertz {
70    fn into(self) -> Hertz {
71        Hertz(self.0 * 1_000_000)
72    }
73}
74
75impl Into<KiloHertz> for MegaHertz {
76    fn into(self) -> KiloHertz {
77        KiloHertz(self.0 * 1_000)
78    }
79}