libhaystack/haystack/units/
unit_dimension.rs1use std::ops::{Add, Sub};
6
7#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Default, Clone, Copy)]
11pub struct UnitDimensions {
12 pub kg: i8,
14 pub m: i8,
16 pub sec: i8,
18 pub k: i8,
20 pub a: i8,
22 pub mol: i8,
24 pub cd: i8,
26}
27
28impl Add for UnitDimensions {
30 type Output = Self;
31
32 fn add(self, other: Self) -> Self::Output {
33 Self {
34 kg: self.kg + other.kg,
35 m: self.m + other.m,
36 sec: self.sec + other.sec,
37 k: self.k + other.k,
38 a: self.a + other.a,
39 mol: self.mol + other.mol,
40 cd: self.cd + other.cd,
41 }
42 }
43}
44
45impl Sub for UnitDimensions {
47 type Output = Self;
48
49 fn sub(self, other: Self) -> Self::Output {
50 Self {
51 kg: self.kg - other.kg,
52 m: self.m - other.m,
53 sec: self.sec - other.sec,
54 k: self.k - other.k,
55 a: self.a - other.a,
56 mol: self.mol - other.mol,
57 cd: self.cd - other.cd,
58 }
59 }
60}
61
62#[cfg(test)]
63mod test {
64 use super::UnitDimensions;
65 #[test]
66 fn test_add() {
67 let dim1 = UnitDimensions {
68 kg: 1,
69 m: 1,
70 sec: 1,
71 k: 1,
72 a: 1,
73 mol: 1,
74 cd: 1,
75 };
76 let dim2 = UnitDimensions {
77 kg: 2,
78 m: 2,
79 sec: 2,
80 k: 2,
81 a: 2,
82 mol: 2,
83 cd: 2,
84 };
85
86 assert_eq!(
87 dim1 + dim2,
88 UnitDimensions {
89 kg: 3,
90 m: 3,
91 sec: 3,
92 k: 3,
93 a: 3,
94 mol: 3,
95 cd: 3
96 }
97 )
98 }
99
100 #[test]
101 fn test_sub() {
102 let dim1 = UnitDimensions {
103 kg: 3,
104 m: 3,
105 sec: 3,
106 k: 3,
107 a: 3,
108 mol: 3,
109 cd: 3,
110 };
111 let dim2 = UnitDimensions {
112 kg: 2,
113 m: 2,
114 sec: 2,
115 k: 2,
116 a: 2,
117 mol: 2,
118 cd: 2,
119 };
120
121 assert_eq!(
122 dim1 - dim2,
123 UnitDimensions {
124 kg: 1,
125 m: 1,
126 sec: 1,
127 k: 1,
128 a: 1,
129 mol: 1,
130 cd: 1,
131 }
132 )
133 }
134}