nrf52_hal_common/
time.rs

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