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
pub use simple_si_units_macros::Unit;
pub use simple_si_units_core::UnitData;
trait Length {
type NumType;
fn meters(self) -> Self::NumType;
}
trait Time {
type NumType;
fn seconds(self) -> Self::NumType;
}
trait Velocity {
type NumType;
fn meters_per_second(self) -> Self::NumType;
}
#[derive(Debug, Copy, Clone, PartialEq)]
struct GLength<MT> {
m: MT
}
impl<MT> Length for GLength<MT> {
type NumType = MT;
fn meters(self) -> Self::NumType{
self.m
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
struct GTime<MT> {
s: MT
}
impl<MT> Time for GTime<MT> {
type NumType = MT;
fn seconds(self) -> Self::NumType{
self.s
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
struct GVelocity<MT> {
mps: MT
}
impl<MT> Velocity for GVelocity<MT> {
type NumType = MT;
fn meters_per_second(self) -> Self::NumType{
self.mps
}
}
impl<DT, TT> std::ops::Div<TT> for GLength<DT>
where TT: Time<NumType=DT>, DT: std::ops::Div<DT> + std::ops::Div<Output = DT>
{
type Output = GVelocity<DT>;
fn div(self, rhs: TT) -> Self::Output {
let y: GVelocity<DT> = GVelocity{mps: self.meters() / rhs.seconds()};
return y;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn velocity_api() {
let d = GLength{m: 5.0f64};
let t = GTime{s: 2.0f64};
let v = d / t;
assert_eq!(v, GVelocity{mps: 2.5f64});
let a = 1;
let b = 2;
let c = &a + &a;
}
}