stm32f0xx_hal/
time.rs

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