From d286ed3eb877365c01f7a7ec56de9a0dc84bab2e Mon Sep 17 00:00:00 2001 From: sam-anthony Date: Sun, 6 Mar 2022 21:56:57 -0330 Subject: simplify/remove a lot of stuff --- src/unit_of_measurement.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'src/unit_of_measurement.rs') diff --git a/src/unit_of_measurement.rs b/src/unit_of_measurement.rs index 30a6ff3..a3b4b77 100644 --- a/src/unit_of_measurement.rs +++ b/src/unit_of_measurement.rs @@ -1,22 +1,33 @@ +pub trait UnitOfMeasurement { + type Unit; + + fn from_unit(unit: Self::Unit, n: i32) -> Self; + fn as_unit(&self, unit: Self::Unit) -> i32; +} + pub mod pressure { - pub enum PressureUnit { - Pascal = 1, // base unit. Every other variant will be a multiple of this. - KiloPascal = 1000, - } + use super::UnitOfMeasurement; #[derive(Default)] pub struct Pressure { - val: i32, // Base unit is pascals. + val: i32, } - impl Pressure { - pub fn from_unit(unit: PressureUnit, n: i32) -> Self { + pub enum Unit { + Pascal = 1, + KiloPascal = 1000, + } + + impl UnitOfMeasurement for Pressure { + type Unit = Unit; + + fn from_unit(unit: Self::Unit, n: i32) -> Self { Self { val: n * unit as i32, } } - pub fn as_unit(&self, unit: PressureUnit) -> i32 { + fn as_unit(&self, unit: Self::Unit) -> i32 { self.val / unit as i32 } } -- cgit v1.2.3