Skip to content

Commit 59c55ac

Browse files
author
James O'Cull
committed
Updates for multiplication and division. Doc and test updates.
1 parent 91f50b9 commit 59c55ac

File tree

4 files changed

+75
-11
lines changed

4 files changed

+75
-11
lines changed

src/length/traits.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,50 @@ impl Sub for Length {
1919
}
2020
}
2121

22-
impl Div for Length {
22+
///
23+
/// Dividing a `Length` by another `Length` returns a ratio.
24+
///
25+
impl Div<Length> for Length {
26+
type Output = f64;
27+
28+
fn div(self, rhs: Length) -> f64 {
29+
self.meters / rhs.meters
30+
}
31+
}
32+
33+
///
34+
/// Dividing a `Length` by a factor returns a new portion of that length.
35+
///
36+
impl Div<f64> for Length {
2337
type Output = Length;
2438

25-
fn div(self, rhs: Length) -> Length {
26-
Length::from_meters(self.meters / rhs.meters)
39+
fn div(self, rhs: f64) -> Length {
40+
Length::from_meters(self.meters / rhs)
2741
}
2842
}
2943

30-
impl Mul for Length {
44+
///
45+
/// Multiplying a `Length` by another `Length` returns the product of those lengths.
46+
///
47+
impl Mul<Length> for Length {
3148
type Output = Length;
3249

3350
fn mul(self, rhs: Length) -> Length {
3451
Length::from_meters(self.meters * rhs.meters)
3552
}
3653
}
3754

55+
///
56+
/// Multiplying a `Length` by a factor increases (or decreases) that length a number of times.
57+
///
58+
impl Mul<f64> for Length {
59+
type Output = Length;
60+
61+
fn mul(self, rhs: f64) -> Length {
62+
Length::from_meters(self.meters * rhs)
63+
}
64+
}
65+
3866
impl Eq for Length { }
3967
impl PartialEq for Length {
4068
fn eq(&self, other: &Length) -> bool {

src/temperature/traits.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,50 @@ impl Sub for Temperature {
1919
}
2020
}
2121

22-
impl Div for Temperature {
22+
///
23+
/// Dividing a Temperature by another Temperature returns a ratio.
24+
///
25+
impl Div<Temperature> for Temperature {
26+
type Output = f64;
27+
28+
fn div(self, rhs: Temperature) -> f64 {
29+
self.kelvin / rhs.kelvin
30+
}
31+
}
32+
33+
///
34+
/// Dividing a `Temperature` by a factor returns a new portion of that temperature.
35+
///
36+
impl Div<f64> for Temperature {
2337
type Output = Temperature;
2438

25-
fn div(self, rhs: Temperature) -> Temperature {
26-
Temperature::from_kelvin(self.kelvin / rhs.kelvin)
39+
fn div(self, rhs: f64) -> Temperature {
40+
Temperature::from_kelvin(self.kelvin / rhs)
2741
}
2842
}
2943

30-
impl Mul for Temperature {
44+
///
45+
/// Multiplying a `Temperature` by another `Temperature` returns the product of those temperatures.
46+
///
47+
impl Mul<Temperature> for Temperature {
3148
type Output = Temperature;
3249

3350
fn mul(self, rhs: Temperature) -> Temperature {
3451
Temperature::from_kelvin(self.kelvin * rhs.kelvin)
3552
}
3653
}
3754

55+
///
56+
/// Multiplying a `Temperature` by a factor increases (or decreases) that temperature a number of times.
57+
///
58+
impl Mul<f64> for Temperature {
59+
type Output = Temperature;
60+
61+
fn mul(self, rhs: f64) -> Temperature {
62+
Temperature::from_kelvin(self.kelvin * rhs)
63+
}
64+
}
65+
3866
impl Eq for Temperature { }
3967
impl PartialEq for Temperature {
4068
fn eq(&self, other: &Temperature) -> bool {

src/tests/length_tests.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,19 @@ fn mul() {
169169
let a = Length::from_meters(2.0);
170170
let b = Length::from_meters(4.0);
171171
let c = a * b;
172+
let d = b * 2.0;
172173
assert_almost_eq(c.as_meters(), 8.0);
174+
assert_almost_eq(d.as_meters(), 8.0);
173175
}
174176

175177
#[test]
176178
fn div() {
177179
let a = Length::from_meters(2.0);
178180
let b = Length::from_meters(4.0);
179-
let c = a / b;
180-
assert_almost_eq(c.as_meters(), 0.5);
181+
let c = a * b;
182+
let d = a * 2.0;
183+
assert_almost_eq(c.as_meters(), 8.0);
184+
assert_almost_eq(d.as_meters(), 4.0);
181185
}
182186

183187
#[test]

src/tests/temperature_tests.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,19 @@ fn mul() {
5656
let a = Temperature::from_kelvin(2.0);
5757
let b = Temperature::from_kelvin(4.0);
5858
let c = a * b;
59+
let d = a * 2.0;
5960
assert_almost_eq(c.as_kelvin(), 8.0);
61+
assert_almost_eq(d.as_kelvin(), 4.0);
6062
}
6163

6264
#[test]
6365
fn div() {
6466
let a = Temperature::from_kelvin(2.0);
6567
let b = Temperature::from_kelvin(4.0);
6668
let c = a / b;
67-
assert_almost_eq(c.as_kelvin(), 0.5);
69+
let d = a / 2.0;
70+
assert_almost_eq(c, 0.5);
71+
assert_almost_eq(d.as_kelvin(), 1.0);
6872
}
6973

7074
#[test]

0 commit comments

Comments
 (0)