use std::ops::{Add, Sub};
#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Default, Clone, Copy)]
pub struct UnitDimensions {
pub kg: i8,
pub m: i8,
pub sec: i8,
pub k: i8,
pub a: i8,
pub mol: i8,
pub cd: i8,
}
impl Add for UnitDimensions {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Self {
kg: self.kg + other.kg,
m: self.m + other.m,
sec: self.sec + other.sec,
k: self.k + other.k,
a: self.a + other.a,
mol: self.mol + other.mol,
cd: self.cd + other.cd,
}
}
}
impl Sub for UnitDimensions {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Self {
kg: self.kg - other.kg,
m: self.m - other.m,
sec: self.sec - other.sec,
k: self.k - other.k,
a: self.a - other.a,
mol: self.mol - other.mol,
cd: self.cd - other.cd,
}
}
}
#[cfg(test)]
mod test {
use super::UnitDimensions;
#[test]
fn test_add() {
let dim1 = UnitDimensions {
kg: 1,
m: 1,
sec: 1,
k: 1,
a: 1,
mol: 1,
cd: 1,
};
let dim2 = UnitDimensions {
kg: 2,
m: 2,
sec: 2,
k: 2,
a: 2,
mol: 2,
cd: 2,
};
assert_eq!(
dim1 + dim2,
UnitDimensions {
kg: 3,
m: 3,
sec: 3,
k: 3,
a: 3,
mol: 3,
cd: 3
}
)
}
#[test]
fn test_sub() {
let dim1 = UnitDimensions {
kg: 3,
m: 3,
sec: 3,
k: 3,
a: 3,
mol: 3,
cd: 3,
};
let dim2 = UnitDimensions {
kg: 2,
m: 2,
sec: 2,
k: 2,
a: 2,
mol: 2,
cd: 2,
};
assert_eq!(
dim1 - dim2,
UnitDimensions {
kg: 1,
m: 1,
sec: 1,
k: 1,
a: 1,
mol: 1,
cd: 1,
}
)
}
}