Switched custom quantities to using a macro for generation.
This commit is contained in:
@ -1,22 +1,6 @@
|
||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||
use crate::quantity::{Quantity, Value};
|
||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||
use generate_quantity::quantity_type;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Watts per Square Meter ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
|
||||
#[display(fmt = "{} {}", _0, "Self::symbol()")]
|
||||
#[repr(transparent)]
|
||||
pub struct WattsPerSquareMeter<V: Value>(pub V);
|
||||
|
||||
impl <V: Value> Quantity<V> for WattsPerSquareMeter<V> {
|
||||
#[inline(always)]
|
||||
fn value(self) -> V {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn symbol() -> &'static str {
|
||||
"W/m²"
|
||||
}
|
||||
}
|
||||
//----- Watter per Square Meter ----------------------------------
|
||||
quantity_type! {WattsPerSquareMeter, "W/m²"}
|
||||
|
@ -43,58 +43,7 @@ pub trait Quantity<V: Value>: Copy + PartialEq + PartialOrd + Add + Sub {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Value: Num + Copy + PartialOrd + FromPrimitive + NumCast + Display {
|
||||
//----- Temperature ----------------------------------
|
||||
#[inline(always)]
|
||||
fn kelvins(self) -> Kelvins<Self> {
|
||||
Kelvins(self)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn deci_kelvins(self) -> DeciKelvins<Self> {
|
||||
DeciKelvins(self)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn degrees_celsius(self) -> DegreesCelsius<Self> {
|
||||
DegreesCelsius(self)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn deci_degrees_celsius(self) -> DeciDegreesCelsius<Self> {
|
||||
DeciDegreesCelsius(self)
|
||||
}
|
||||
|
||||
//----- Voltage ----------------------------------
|
||||
#[inline(always)]
|
||||
fn volts(self) -> Volts<Self> {
|
||||
Volts(self)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn millivolts(self) -> MilliVolts<Self> {
|
||||
MilliVolts(self)
|
||||
}
|
||||
|
||||
//----- Resistance ----------------------------------
|
||||
#[inline(always)]
|
||||
fn ohms(self) -> Ohms<Self> {
|
||||
Ohms(self)
|
||||
}
|
||||
|
||||
//----- Volume rate ----------------------------------
|
||||
#[inline(always)]
|
||||
fn liters_per_minute(self) -> LitersPerMinute<Self> {
|
||||
LitersPerMinute(self)
|
||||
}
|
||||
|
||||
//----- Irradiance ----------------------------------
|
||||
#[inline(always)]
|
||||
fn watts_per_square_meter(self) -> WattsPerSquareMeter<Self> {
|
||||
WattsPerSquareMeter(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Value: Num + Copy + PartialOrd + FromPrimitive + NumCast + Display {}
|
||||
impl<V> Value for V where V: Num + Copy + PartialOrd + FromPrimitive + NumCast + Display {}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
@ -161,6 +110,12 @@ mod defmt_impl {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q: Quantity<usize>> Format for Fmt<usize, Q> {
|
||||
fn format(&self, fmt: Formatter) {
|
||||
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q: Quantity<i8>> Format for Fmt<i8, Q> {
|
||||
fn format(&self, fmt: Formatter) {
|
||||
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());
|
||||
@ -191,6 +146,12 @@ mod defmt_impl {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q: Quantity<isize>> Format for Fmt<isize, Q> {
|
||||
fn format(&self, fmt: Formatter) {
|
||||
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q: Quantity<f32>> Format for Fmt<f32, Q> {
|
||||
fn format(&self, fmt: Formatter) {
|
||||
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());
|
||||
|
@ -1,22 +1,6 @@
|
||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||
use generate_quantity::quantity_type;
|
||||
use crate::quantity::{Quantity, Value};
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Ohms ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
|
||||
#[display(fmt = "{} {}", _0, "Self::symbol()")]
|
||||
#[repr(transparent)]
|
||||
pub struct Ohms<V: Value>(pub V);
|
||||
|
||||
impl <V: Value> Quantity<V> for Ohms<V> {
|
||||
#[inline(always)]
|
||||
fn value(self) -> V {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn symbol() -> &'static str {
|
||||
"Ω"
|
||||
}
|
||||
}
|
||||
//----- Ohms ----------------------------------
|
||||
quantity_type! {Ohms, "Ω"}
|
||||
|
@ -1,28 +1,11 @@
|
||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||
use num_traits::float::FloatCore;
|
||||
use generate_quantity::quantity_type;
|
||||
use crate::quantity::{DECI, Quantity, Value};
|
||||
|
||||
const KELVIN_CELSIUS_OFFSET: f64 = 273.15;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Kelvins ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
|
||||
#[display(fmt = "{} {}", _0, "Self::symbol()")]
|
||||
#[repr(transparent)]
|
||||
pub struct Kelvins<V: Value>(pub V);
|
||||
|
||||
impl <V: Value> Quantity<V> for Kelvins<V> {
|
||||
#[inline(always)]
|
||||
fn value(self) -> V {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn symbol() -> &'static str {
|
||||
"K"
|
||||
}
|
||||
}
|
||||
//----- Kelvins ----------------------------------
|
||||
quantity_type! {Kelvins, "K"}
|
||||
|
||||
impl <V: Value> Kelvins<V> {
|
||||
#[inline]
|
||||
@ -39,25 +22,8 @@ impl <V: Value> Kelvins<V> {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Deci Kelvins ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
|
||||
#[display(fmt = "{} {}", _0, "Self::symbol()")]
|
||||
#[repr(transparent)]
|
||||
pub struct DeciKelvins<V: Value>(pub V);
|
||||
|
||||
impl <V: Value> Quantity<V> for DeciKelvins<V> {
|
||||
#[inline(always)]
|
||||
fn value(self) -> V {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn symbol() -> &'static str {
|
||||
"dK"
|
||||
}
|
||||
}
|
||||
//----- Deci Kelvins ----------------------------------
|
||||
quantity_type! {DeciKelvins, "dK"}
|
||||
|
||||
impl<V: Value> DeciKelvins<V> {
|
||||
#[inline]
|
||||
@ -67,25 +33,8 @@ impl<V: Value> DeciKelvins<V> {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Degrees Celsius ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
|
||||
#[display(fmt = "{} {}", _0, "Self::symbol()")]
|
||||
#[repr(transparent)]
|
||||
pub struct DegreesCelsius<V: Value>(pub V);
|
||||
|
||||
impl <V: Value> Quantity<V> for DegreesCelsius<V> {
|
||||
#[inline(always)]
|
||||
fn value(self) -> V {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn symbol() -> &'static str {
|
||||
"℃"
|
||||
}
|
||||
}
|
||||
//----- Degrees Celsius ----------------------------------
|
||||
quantity_type! {DegreesCelsius, "℃"}
|
||||
|
||||
impl <V: Value> DegreesCelsius<V> {
|
||||
#[inline]
|
||||
@ -102,25 +51,8 @@ impl <V: Value> DegreesCelsius<V> {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Deci Degrees Celsius ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
|
||||
#[display(fmt = "{} {}", _0, "Self::symbol()")]
|
||||
#[repr(transparent)]
|
||||
pub struct DeciDegreesCelsius<V: Value>(pub V);
|
||||
|
||||
impl <V: Value> Quantity<V> for DeciDegreesCelsius<V> {
|
||||
#[inline(always)]
|
||||
fn value(self) -> V {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn symbol() -> &'static str {
|
||||
"d℃"
|
||||
}
|
||||
}
|
||||
//----- Deci Degrees Celsius ----------------------------------
|
||||
quantity_type! {DeciDegreesCelsius, "d℃"}
|
||||
|
||||
impl<V: Value> DeciDegreesCelsius<V> {
|
||||
#[inline]
|
||||
|
@ -1,27 +1,11 @@
|
||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||
use generate_quantity::quantity_type;
|
||||
use crate::quantity::{Quantity, Value};
|
||||
|
||||
const VOLT_MV_RATIO: u16 = 1_000;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Volts ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
|
||||
#[display(fmt = "{} {}", _0, "Self::symbol()")]
|
||||
#[repr(transparent)]
|
||||
pub struct Volts<V: Value>(pub V);
|
||||
|
||||
impl <V: Value> Quantity<V> for Volts<V> {
|
||||
#[inline(always)]
|
||||
fn value(self) -> V {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn symbol() -> &'static str {
|
||||
"V"
|
||||
}
|
||||
}
|
||||
//----- Volts ----------------------------------
|
||||
quantity_type! {Volts, "V"}
|
||||
|
||||
impl<V: Value> Volts<V> {
|
||||
#[inline]
|
||||
@ -31,25 +15,8 @@ impl<V: Value> Volts<V> {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- MilliVolts ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
|
||||
#[display(fmt = "{} {}", _0, "Self::symbol()")]
|
||||
#[repr(transparent)]
|
||||
pub struct MilliVolts<V: Value>(pub V);
|
||||
|
||||
impl <V: Value> Quantity<V> for MilliVolts<V> {
|
||||
#[inline(always)]
|
||||
fn value(self) -> V {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn symbol() -> &'static str {
|
||||
"mV"
|
||||
}
|
||||
}
|
||||
//----- Milli Volts ----------------------------------
|
||||
quantity_type! {MilliVolts, "mV"}
|
||||
|
||||
impl<V: Value> MilliVolts<V> {
|
||||
pub fn to_volts(self) -> Volts<V> {
|
||||
@ -69,7 +36,7 @@ mod tests {
|
||||
#[test]
|
||||
fn convert_u32() {
|
||||
let volts: Volts<u32> = 3.volts();
|
||||
let millivolts: MilliVolts<u32> = 3_000.millivolts();
|
||||
let millivolts: MilliVolts<u32> = 3_000.milli_volts();
|
||||
|
||||
assert_eq!(volts.to_millivolts().0, millivolts.0);
|
||||
assert_eq!(millivolts.to_volts().0, volts.0);
|
||||
@ -78,7 +45,7 @@ mod tests {
|
||||
#[test]
|
||||
fn convert_f64() {
|
||||
let volts: Volts<f64> = 3.0.volts();
|
||||
let millivolts: MilliVolts<f64> = 3_000.0.millivolts();
|
||||
let millivolts: MilliVolts<f64> = 3_000.0.milli_volts();
|
||||
|
||||
assert_approx_eq!(f64, volts.to_millivolts().0, millivolts.0);
|
||||
assert_approx_eq!(f64, millivolts.to_volts().0, volts.0);
|
||||
|
@ -1,22 +1,6 @@
|
||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||
use crate::quantity::{Quantity, Value};
|
||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||
use generate_quantity::quantity_type;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Liters per Minute------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
|
||||
#[display(fmt = "{} {}", _0, "Self::symbol()")]
|
||||
#[repr(transparent)]
|
||||
pub struct LitersPerMinute<V: Value>(pub V);
|
||||
|
||||
impl <V: Value> Quantity<V> for LitersPerMinute<V> {
|
||||
#[inline(always)]
|
||||
fn value(self) -> V {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn symbol() -> &'static str {
|
||||
"L/min"
|
||||
}
|
||||
}
|
||||
//----- Liters per Minute ----------------------------------
|
||||
quantity_type! {LitersPerMinute, "L/min"}
|
||||
|
Reference in New Issue
Block a user