1#[derive(Clone, Copy, PartialEq, Eq, Debug)]
7pub struct Bps(pub u32);
8
9#[derive(Clone, Copy, PartialEq, Eq, Debug)]
11pub struct Hertz(pub u32);
12
13#[derive(Clone, Copy, PartialEq, Eq, Debug)]
15pub struct KiloHertz(pub u32);
16
17#[derive(Clone, Copy, PartialEq, Eq, Debug)]
19pub struct MegaHertz(pub u32);
20
21#[derive(Clone, Copy, PartialEq, Eq, Debug)]
25pub struct Seconds(pub u32);
26
27#[derive(Clone, Copy, PartialEq, Eq, Debug)]
29pub struct Milliseconds(pub u32);
30
31#[derive(Clone, Copy, PartialEq, Eq, Debug)]
33pub struct Microseconds(pub u32);
34
35#[derive(Clone, Copy, PartialEq, Eq, Debug)]
37pub struct Nanoseconds(pub u32);
38
39pub trait U32Ext {
41 fn bps(self) -> Bps;
43
44 fn hz(self) -> Hertz;
46
47 fn khz(self) -> KiloHertz;
49
50 fn mhz(self) -> MegaHertz;
52
53 fn s(self) -> Seconds;
55
56 fn ms(self) -> Milliseconds;
58
59 fn us(self) -> Microseconds;
61
62 fn ns(self) -> Nanoseconds;
64}
65
66impl U32Ext for u32 {
67 fn bps(self) -> Bps {
70 Bps(self)
71 }
72
73 fn hz(self) -> Hertz {
74 Hertz(self)
75 }
76
77 fn khz(self) -> KiloHertz {
78 KiloHertz(self)
79 }
80
81 fn mhz(self) -> MegaHertz {
82 MegaHertz(self)
83 }
84
85 fn s(self) -> Seconds {
88 Seconds(self)
89 }
90
91 fn ms(self) -> Milliseconds {
92 Milliseconds(self)
93 }
94
95 fn us(self) -> Microseconds {
96 Microseconds(self)
97 }
98
99 fn ns(self) -> Nanoseconds {
100 Nanoseconds(self)
101 }
102}
103
104impl From<KiloHertz> for Hertz {
107 fn from(item: KiloHertz) -> Self {
108 Hertz(item.0 * 1_000)
109 }
110}
111
112impl From<MegaHertz> for Hertz {
113 fn from(item: MegaHertz) -> Self {
114 Hertz(item.0 * 1_000_000)
115 }
116}
117
118impl From<MegaHertz> for KiloHertz {
119 fn from(item: MegaHertz) -> Self {
120 KiloHertz(item.0 * 1_000)
121 }
122}
123
124impl From<Hertz> for KiloHertz {
125 fn from(item: Hertz) -> Self {
126 KiloHertz(item.0 / 1_000)
127 }
128}
129
130impl From<Hertz> for MegaHertz {
131 fn from(item: Hertz) -> Self {
132 MegaHertz(item.0 / 1_000_000)
133 }
134}
135
136impl From<KiloHertz> for MegaHertz {
137 fn from(item: KiloHertz) -> Self {
138 MegaHertz(item.0 / 1_000)
139 }
140}
141
142impl From<Seconds> for Milliseconds {
145 fn from(item: Seconds) -> Self {
146 Milliseconds(item.0 * 1_000)
147 }
148}
149impl From<Seconds> for Microseconds {
150 fn from(item: Seconds) -> Self {
151 Microseconds(item.0 * 1_000_000)
152 }
153}
154
155impl From<Seconds> for Nanoseconds {
156 fn from(item: Seconds) -> Self {
157 Nanoseconds(item.0 * 1_000_000_000)
158 }
159}
160
161impl From<Milliseconds> for Microseconds {
162 fn from(item: Milliseconds) -> Self {
163 Microseconds(item.0 * 1_000)
164 }
165}
166
167impl From<Microseconds> for Nanoseconds {
168 fn from(item: Microseconds) -> Self {
169 Nanoseconds(item.0 * 1_000)
170 }
171}
172
173impl From<Milliseconds> for Seconds {
174 fn from(item: Milliseconds) -> Self {
175 Seconds(item.0 / 1_000)
176 }
177}
178
179impl From<Microseconds> for Seconds {
180 fn from(item: Microseconds) -> Self {
181 Seconds(item.0 / 1_000_000)
182 }
183}
184
185impl From<Microseconds> for Milliseconds {
186 fn from(item: Microseconds) -> Self {
187 Milliseconds(item.0 / 1_000)
188 }
189}
190
191impl From<Milliseconds> for Nanoseconds {
192 fn from(item: Milliseconds) -> Self {
193 Nanoseconds(item.0 * 1_000_000)
194 }
195}
196
197impl From<Nanoseconds> for Hertz {
200 fn from(item: Nanoseconds) -> Self {
201 Hertz(1_000_000_000_u32 / item.0)
202 }
203}
204
205impl From<Microseconds> for Hertz {
206 fn from(item: Microseconds) -> Self {
207 Hertz(1_000_000_u32 / item.0)
208 }
209}
210
211impl From<Nanoseconds> for KiloHertz {
212 fn from(item: Nanoseconds) -> Self {
213 KiloHertz(1_000_000_u32 / item.0)
214 }
215}
216
217impl From<Nanoseconds> for MegaHertz {
218 fn from(item: Nanoseconds) -> Self {
219 MegaHertz(1_000_u32 / item.0)
220 }
221}
222
223impl From<Hertz> for Microseconds {
224 fn from(item: Hertz) -> Self {
225 Microseconds(1_000_000_u32 / item.0)
226 }
227}
228
229impl From<Hertz> for Nanoseconds {
230 fn from(item: Hertz) -> Self {
231 Nanoseconds(1_000_000_000u32 / item.0)
232 }
233}
234
235impl From<KiloHertz> for Nanoseconds {
236 fn from(item: KiloHertz) -> Self {
237 Nanoseconds(1_000_000u32 / item.0)
238 }
239}
240
241impl From<MegaHertz> for Nanoseconds {
242 fn from(item: MegaHertz) -> Self {
243 Nanoseconds(1_000u32 / item.0)
244 }
245}
246
247#[cfg(test)]
248mod tests {
249 use crate::time::*;
250
251 #[test]
252 fn convert_us_to_hz() {
253 let as_us: Microseconds = 3.hz().into();
254 assert_eq!(as_us.0, 333_333_u32);
255 }
256
257 #[test]
258 fn convert_ms_to_us() {
259 let as_us: Microseconds = 3.ms().into();
260 assert_eq!(as_us.0, 3_000_u32);
261 }
262
263 #[test]
264 fn convert_mhz_to_hz() {
265 let as_hz: Hertz = 48.mhz().into();
266 assert_eq!(as_hz.0, 48_000_000_u32);
267 }
268
269 #[test]
270 fn convert_hz_to_ns() {
271 let as_ns: Nanoseconds = 3.mhz().into();
272 assert_eq!(as_ns.0, 333_u32);
273 }
274
275 #[test]
276 fn convert_hz_to_ns_even() {
277 let as_ns: Nanoseconds = 2.mhz().into();
278 assert_eq!(as_ns.0, 500_u32);
279 }
280}
281