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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
pub trait Measurement {
fn get_base_units(&self) -> f64;
fn from_base_units(units: f64) -> Self;
}
#[macro_export]
macro_rules! implement_measurement {
($($t:ty)*) => ($(
impl ::std::ops::Add for $t {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self::from_base_units(self.get_base_units() + rhs.get_base_units())
}
}
impl ::std::ops::Sub for $t {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self::from_base_units(self.get_base_units() - rhs.get_base_units())
}
}
impl ::std::ops::Div<$t> for $t {
type Output = f64;
fn div(self, rhs: Self) -> f64 {
self.get_base_units() / rhs.get_base_units()
}
}
impl ::std::ops::Div<f64> for $t {
type Output = Self;
fn div(self, rhs: f64) -> Self {
Self::from_base_units(self.get_base_units() / rhs)
}
}
impl ::std::ops::Mul<$t> for $t {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
Self::from_base_units(self.get_base_units() * rhs.get_base_units())
}
}
impl ::std::ops::Mul<f64> for $t {
type Output = Self;
fn mul(self, rhs: f64) -> Self {
Self::from_base_units(self.get_base_units() * rhs)
}
}
impl ::std::cmp::Eq for $t { }
impl ::std::cmp::PartialEq for $t {
fn eq(&self, other: &Self) -> bool {
self.get_base_units() == other.get_base_units()
}
}
impl ::std::cmp::PartialOrd for $t {
fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> {
self.get_base_units().partial_cmp(&other.get_base_units())
}
}
)*)
}