blob: ea78938822a9ea198db2316b97236724bab1127f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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.))
}
}
|