Skip to content

Commit 6113f4b

Browse files
committed
Add conversions between power / voltage / current
1 parent 2928c7a commit 6113f4b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ impl_maths!(Power, Force, Speed);
160160
impl_maths!(Speed, time::Duration, Acceleration);
161161
impl_maths!(Volume, Length, Area);
162162
impl_maths!(Power, AngularVelocity, Torque);
163+
impl_maths!(Power, Voltage, Current);
163164
impl_maths!(Voltage, Resistance, Current);
164165

165166
// Force * Distance is ambiguous. Create an ambiguous struct the user can then

src/power.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ implement_measurement! { Power }
128128
#[cfg(test)]
129129
mod test {
130130
use power::*;
131+
use current::*;
132+
use voltage::*;
131133
use test_utils::assert_almost_eq;
132134

133135
#[test]
@@ -228,4 +230,28 @@ mod test {
228230
assert_eq!(a >= b, false);
229231
}
230232

233+
#[test]
234+
fn mul_voltage_current() {
235+
let u = Voltage::from_volts(230.0);
236+
let i = Current::from_amperes(10.0);
237+
let p = u * i;
238+
assert_almost_eq(p.as_kilowatts(), 2.3);
239+
}
240+
241+
#[test]
242+
fn div_voltage() {
243+
let u = Voltage::from_volts(230.0);
244+
let p = Power::from_kilowatts(2.3);
245+
let i = p / u;
246+
assert_eq!(i.as_amperes(), 10.0);
247+
}
248+
249+
#[test]
250+
fn div_current() {
251+
let i = Current::from_amperes(10.0);
252+
let p = Power::from_kilowatts(2.3);
253+
let u = p / i;
254+
assert_eq!(u.as_volts(), 230.0);
255+
}
256+
231257
}

0 commit comments

Comments
 (0)