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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
pub(crate) mod units {
use crate::{ConversionError, Period, TimeInt};
use core::{convert, ops};
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub struct Hertz<T: TimeInt = u32>(pub T);
impl<T: TimeInt, Rhs: TimeInt> ops::Mul<Rhs> for Hertz<T>
where
T: From<Rhs>,
{
type Output = Self;
fn mul(self, rhs: Rhs) -> Self::Output {
Self(self.0 * <T as convert::From<Rhs>>::from(rhs))
}
}
impl<T: TimeInt, Rhs: TimeInt> ops::Div<Rhs> for Hertz<T>
where
T: From<Rhs>,
{
type Output = Self;
fn div(self, rhs: Rhs) -> Self::Output {
Self(self.0 / <T as convert::From<Rhs>>::from(rhs))
}
}
impl<T: TimeInt> convert::TryFrom<Period<T>> for Hertz<T> {
type Error = ConversionError;
fn try_from(period: Period<T>) -> Result<Self, Self::Error> {
if !period.numerator().is_zero() {
Ok(Hertz(period.recip().to_integer()))
} else {
Err(ConversionError::DivByZero)
}
}
}
impl convert::From<u32> for Hertz {
fn from(value: u32) -> Self {
Self(value)
}
}
impl convert::From<Hertz> for u32 {
fn from(hertz: Hertz) -> Self {
hertz.0
}
}
}