diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-01-15 22:40:41 -0330 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-01-15 22:40:41 -0330 |
| commit | b6247a8372eb18a19c35a924316941d6c1e585d2 (patch) | |
| tree | eb7c1db8ff7f4524f3ac093d6f445ee52c747302 /src | |
| parent | 585fbf852c1e76470df42ebe99ede62440ce19d9 (diff) | |
| download | volute-b6247a8372eb18a19c35a924316941d6c1e585d2.zip | |
volume
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 3 | ||||
| -rw-r--r-- | src/main.rs | 3 | ||||
| -rw-r--r-- | src/volume.rs | 53 |
3 files changed, 59 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..0e68f71 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ +mod volume; + +type Percent = u8; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/volume.rs b/src/volume.rs new file mode 100644 index 0000000..ea78938 --- /dev/null +++ b/src/volume.rs @@ -0,0 +1,53 @@ +use std::ops::Mul; + +trait Volume { + /// Returns the volume in SI units (cubic metres). + fn si(self) -> CubicMetre; +} + +#[derive(Debug, PartialEq)] +struct CubicMetre(f64); + +#[derive(Debug, PartialEq)] +struct Litre(f64); + +impl Volume for Litre { + fn si(self) -> CubicMetre { + CubicMetre(self.0 * 10_f64.powf(-3.)) + } +} + +impl From<i32> for Litre { + fn from(value: i32) -> Self { + Self(value as f64) + } +} + +impl From<CubicMetre> for Litre { + fn from(value: CubicMetre) -> Self { + Self(value.0 * 10_f64.powf(3.)) + } +} + +impl Mul<f64> for Litre { + type Output = Self; + + fn mul(self, rhs: f64) -> Self::Output { + Self(self.0 * rhs) + } +} + +#[cfg(test)] +mod tests { + use crate::volume::{CubicMetre, Litre, Volume}; + + #[test] + fn litre_to_cubic_metre() { + assert_eq!(Litre(1000.).si(), CubicMetre(1.)) + } + + #[test] + fn cubic_metre_to_litre() { + assert_eq!(Litre::from(CubicMetre(1.)), Litre(1000.)) + } +} |