diff options
| author | sam-anthony <samanthony6@protonmail.com> | 2022-03-08 19:55:08 -0330 |
|---|---|---|
| committer | sam-anthony <samanthony6@protonmail.com> | 2022-03-08 19:55:08 -0330 |
| commit | 90f42ad66feb6b77474d0da267d572eca16e2826 (patch) | |
| tree | a35319f2457e2e65bf8ff4536e142b7211fb09b9 /src/unit_of_measurement.rs | |
| parent | d286ed3eb877365c01f7a7ec56de9a0dc84bab2e (diff) | |
| download | volute-90f42ad66feb6b77474d0da267d572eca16e2826.zip | |
switchable pressure unit
Diffstat (limited to 'src/unit_of_measurement.rs')
| -rw-r--r-- | src/unit_of_measurement.rs | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/src/unit_of_measurement.rs b/src/unit_of_measurement.rs index a3b4b77..ce459bf 100644 --- a/src/unit_of_measurement.rs +++ b/src/unit_of_measurement.rs @@ -8,27 +8,47 @@ pub trait UnitOfMeasurement { pub mod pressure { use super::UnitOfMeasurement; - #[derive(Default)] - pub struct Pressure { - val: i32, - } + #[derive(Default, Clone)] + pub struct Pressure(i32); + #[derive(Copy, Clone)] pub enum Unit { Pascal = 1, KiloPascal = 1000, } + impl Unit { + // Pseudo iter::Cycle behavior. + pub fn next(&mut self) { + match self { + Unit::Pascal => { + *self = Unit::KiloPascal; + } + Unit::KiloPascal => { + *self = Unit::Pascal; + } + } + } + } + + impl ToString for Unit { + fn to_string(&self) -> String { + match self { + Self::Pascal => String::from("Pa"), + Self::KiloPascal => String::from("kPa"), + } + } + } + impl UnitOfMeasurement for Pressure { type Unit = Unit; fn from_unit(unit: Self::Unit, n: i32) -> Self { - Self { - val: n * unit as i32, - } + Self(n * unit as i32) } fn as_unit(&self, unit: Self::Unit) -> i32 { - self.val / unit as i32 + self.0 / unit as i32 } } } |