Initial proof of concept
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
use generate_quantity::quantity_type;
|
||||
use crate::quantity::{Quantity, Value};
|
||||
|
||||
const VOLT_MV_RATIO: u16 = 1_000;
|
||||
|
||||
//----- Volts ----------------------------------
|
||||
quantity_type! {Volts, "V"}
|
||||
|
||||
impl<V: Value> Volts<V> {
|
||||
#[inline]
|
||||
pub fn to_milli_volts(self) -> MilliVolts<V> {
|
||||
let multiplier = V::from_u16(VOLT_MV_RATIO).unwrap();
|
||||
MilliVolts(self.0 * multiplier)
|
||||
}
|
||||
}
|
||||
|
||||
//----- Millivolts ----------------------------------
|
||||
quantity_type! {MilliVolts, "mV"}
|
||||
|
||||
impl<V: Value> MilliVolts<V> {
|
||||
pub fn to_volts(self) -> Volts<V> {
|
||||
let divisor = V::from_u16(VOLT_MV_RATIO).unwrap();
|
||||
Volts(self.0 / divisor)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Tests ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use float_cmp::assert_approx_eq;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn convert_u32() {
|
||||
let volts: Volts<u32> = 3.volts();
|
||||
let millivolts: MilliVolts<u32> = 3_000.milli_volts();
|
||||
|
||||
assert_eq!(volts.to_milli_volts().0, millivolts.0);
|
||||
assert_eq!(millivolts.to_volts().0, volts.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_f64() {
|
||||
let volts: Volts<f64> = 3.0.volts();
|
||||
let millivolts: MilliVolts<f64> = 3_000.0.milli_volts();
|
||||
|
||||
assert_approx_eq!(f64, volts.to_milli_volts().0, millivolts.0);
|
||||
assert_approx_eq!(f64, millivolts.to_volts().0, volts.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user