Restructuring

This commit is contained in:
Max Känner 2023-02-21 20:07:54 +01:00
parent 3029084604
commit 0dcb7b2722
3 changed files with 451 additions and 440 deletions

308
src/display.rs Normal file
View File

@ -0,0 +1,308 @@
use std::fmt::{Display, Formatter};
use typenum::Integer;
use crate::{types::*, SiUnit};
macro_rules! display_unit_defmt {
($formatter: ident, $param: ident, $symbol: literal, $e1: ident, $e2: ident, $e3: ident, $e4: ident, $e5: ident, $e6: ident) => {
if $param::I64 != 0 {
if $e1::I64 == 0
&& $e2::I64 == 0
&& $e3::I64 == 0
&& $e4::I64 == 0
&& $e5::I64 == 0
&& $e6::I64 == 0
{
defmt::write!($formatter, $symbol);
} else {
defmt::write!($formatter, "*{}", $symbol);
}
if $param::I64 != 1 {
defmt::write!($formatter, "^{}", $param::I64);
}
}
};
}
macro_rules! display_special_unit_defmt {
($formatter: ident, $self: ident, $(($symbol: literal, $other: ty)),* $(,)?) => {
$(
if ::core::any::TypeId::of::<$self>() == ::core::any::TypeId::of::<$other>() {
defmt::write!($formatter, $symbol);
return;
}
)*
};
}
#[cfg(feature = "defmt")]
impl<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela> defmt::Format
for SiUnit<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela>
where
Second: Integer,
Meter: Integer,
Kilogram: Integer,
Ampere: Integer,
Kelvin: Integer,
Mole: Integer,
Candela: Integer,
T: defmt::Format + 'static,
{
fn format(&self, f: defmt::Formatter<'_>) {
defmt::write!(f, "{}", self.value);
// derived units with special symbols
display_special_unit_defmt!(f, Self,
("Hz", Herz<T>),
("N", Newton<T>),
("Pa", Pascal<T>),
("J", Joule<T>),
("W", Watt<T>),
("C", Coulomb<T>),
("V", Volt<T>),
("F", Farad<T>),
("Ohm", Ohm<T>),
("S", Siemens<T>),
("Wb", Weber<T>),
("T", Tesla<T>),
("H", Henry<T>),
("lx", Lux<T>),
("Gy|Sv", Gray<T>),
("kat", Katal<T>),
);
// derived units
display_special_unit_defmt!(f, Self,
("m/s", MeterPerSecond<T>),
("m/s²", MeterPerSquareSecond<T>),
("kg/m³", KilogramPerCubicMeter<T>),
("kg/m²", KilogramPerSquareMeter<T>),
("m³/kg", CubicMeterPerKilogram<T>),
("A/m²", AmperePerSquareMeter<T>),
("A/m", AmperePerMeter<T>),
("mol/m³", MolePerCubicMeter<T>),
);
// derived units including special names
display_special_unit_defmt!(f, Self,
("Pa*s", PascalSecond<T>),
("N/m", NewtonPerMeter<T>),
("W/m²", WattPerSquareMeter<T>),
("J/K", JoulePerKelvin<T>),
("J/(kg*K)", JoulePerKilogramKelvin<T>),
("W/(m*K)", WattPerMeterKelvin<T>),
("V/m", VoltPerMeter<T>),
("C/m³", CoulombPerCubicMeter<T>),
("C/m²", CoulombPerSquareMeter<T>),
("F/m", FaradPerMeter<T>),
("H/m", HenryPerMeter<T>),
("J/mol", JoulePerMole<T>),
("J/(mol*K)", JoulePerMoleKelvin<T>),
("C/kg", CoulombPerKilogram<T>),
("Gy/s", GrayPerSecond<T>),
("kat/m³", KatalPerCubicMeter<T>),
);
// base units
display_unit_defmt!(f, Second, "s", Meter, Kilogram, Ampere, Kelvin, Mole, Candela);
display_unit_defmt!(f, Meter, "m", Second, Kilogram, Ampere, Kelvin, Mole, Candela);
display_unit_defmt!(f, Kilogram, "kg", Second, Meter, Ampere, Kelvin, Mole, Candela);
display_unit_defmt!(f, Ampere, "A", Second, Meter, Kilogram, Kelvin, Mole, Candela);
display_unit_defmt!(f, Kelvin, "K", Second, Meter, Kilogram, Ampere, Mole, Candela);
display_unit_defmt!(f, Mole, "mol", Second, Meter, Kilogram, Ampere, Kelvin, Candela);
display_unit_defmt!(f, Candela, "cd", Second, Meter, Kilogram, Ampere, Kelvin, Mole);
}
}
macro_rules! display_unit {
($formatter: ident, $param: ident, $symbol: literal, $e1: ident, $e2: ident, $e3: ident, $e4: ident, $e5: ident, $e6: ident) => {
if $param::I64 != 0 {
if $e1::I64 == 0
&& $e2::I64 == 0
&& $e3::I64 == 0
&& $e4::I64 == 0
&& $e5::I64 == 0
&& $e6::I64 == 0
{
write!($formatter, $symbol)?;
} else {
write!($formatter, "*{}", $symbol)?;
}
if $param::I64 != 1 {
write!($formatter, "^{}", $param::I64)?;
}
}
};
}
macro_rules! display_special_unit {
($formatter: ident, $self: ident, $(($symbol: literal, $other: ty)),* $(,)?) => {
$(
if ::core::any::TypeId::of::<$self>() == ::core::any::TypeId::of::<$other>() {
return write!($formatter, $symbol);
}
)*
};
}
impl<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela> Display
for SiUnit<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela>
where
Second: Integer,
Meter: Integer,
Kilogram: Integer,
Ampere: Integer,
Kelvin: Integer,
Mole: Integer,
Candela: Integer,
T: Display + 'static,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)?;
// derived units with special symbols
display_special_unit!(f, Self,
("Hz", Herz<T>),
("N", Newton<T>),
("Pa", Pascal<T>),
("J", Joule<T>),
("W", Watt<T>),
("C", Coulomb<T>),
("V", Volt<T>),
("F", Farad<T>),
("Ohm", Ohm<T>),
("S", Siemens<T>),
("Wb", Weber<T>),
("T", Tesla<T>),
("H", Henry<T>),
("lx", Lux<T>),
("Gy|Sv", Gray<T>),
("kat", Katal<T>),
);
// derived units
display_special_unit!(f, Self,
("m/s", MeterPerSecond<T>),
("m/s²", MeterPerSquareSecond<T>),
("kg/m³", KilogramPerCubicMeter<T>),
("kg/m²", KilogramPerSquareMeter<T>),
("m³/kg", CubicMeterPerKilogram<T>),
("A/m²", AmperePerSquareMeter<T>),
("A/m", AmperePerMeter<T>),
("mol/m³", MolePerCubicMeter<T>),
);
// derived units including special names
display_special_unit!(f, Self,
("Pa*s", PascalSecond<T>),
("N/m", NewtonPerMeter<T>),
("W/m²", WattPerSquareMeter<T>),
("J/K", JoulePerKelvin<T>),
("J/(kg*K)", JoulePerKilogramKelvin<T>),
("W/(m*K)", WattPerMeterKelvin<T>),
("V/m", VoltPerMeter<T>),
("C/m³", CoulombPerCubicMeter<T>),
("C/m²", CoulombPerSquareMeter<T>),
("F/m", FaradPerMeter<T>),
("H/m", HenryPerMeter<T>),
("J/mol", JoulePerMole<T>),
("J/(mol*K)", JoulePerMoleKelvin<T>),
("C/kg", CoulombPerKilogram<T>),
("Gy/s", GrayPerSecond<T>),
("kat/m³", KatalPerCubicMeter<T>),
);
// base units
display_unit!(f, Second, "s", Meter, Kilogram, Ampere, Kelvin, Mole, Candela);
display_unit!(f, Meter, "m", Second, Kilogram, Ampere, Kelvin, Mole, Candela);
display_unit!(f, Kilogram, "kg", Second, Meter, Ampere, Kelvin, Mole, Candela);
display_unit!(f, Ampere, "A", Second, Meter, Kilogram, Kelvin, Mole, Candela);
display_unit!(f, Kelvin, "K", Second, Meter, Kilogram, Ampere, Mole, Candela);
display_unit!(f, Mole, "mol", Second, Meter, Kilogram, Ampere, Kelvin, Candela);
display_unit!(f, Candela, "cd", Second, Meter, Kilogram, Ampere, Kelvin, Mole);
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn debug() {
let m = Meter::new(2);
assert_eq!(format!("{m:?}"), "SiUnit { value: 2, _s: PhantomData<typenum::int::Z0>, _m: PhantomData<typenum::int::PInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>>>, _kg: PhantomData<typenum::int::Z0>, _a: PhantomData<typenum::int::Z0>, _k: PhantomData<typenum::int::Z0>, _mol: PhantomData<typenum::int::Z0>, _cd: PhantomData<typenum::int::Z0> }".to_owned());
}
#[test]
fn display() {
let unit = Unit::new(2);
let second = Second::new(2);
let meter = Meter::new(2);
let kilogram = Kilogram::new(2);
let ampere = Ampere::new(2);
let kelvin = Kelvin::new(2);
let mole = Mole::new(2);
let candela = Candela::new(2);
assert_eq!(unit.to_string(), "2");
assert_eq!(second.to_string(), "2s");
assert_eq!(meter.to_string(), "2m");
assert_eq!(kilogram.to_string(), "2kg");
assert_eq!(ampere.to_string(), "2A");
assert_eq!(kelvin.to_string(), "2K");
assert_eq!(mole.to_string(), "2mol");
assert_eq!(candela.to_string(), "2cd");
let square_second: SiUnit<i32, _, _, _, _, _, _, _> = second * second;
let square_meter: SiUnit<i32, _, _, _, _, _, _, _> = meter * meter;
let square_kilogram: SiUnit<i32, _, _, _, _, _, _, _> = kilogram * kilogram;
let square_ampere: SiUnit<i32, _, _, _, _, _, _, _> = ampere * ampere;
let square_kelvin: SiUnit<i32, _, _, _, _, _, _, _> = kelvin * kelvin;
let square_mole: SiUnit<i32, _, _, _, _, _, _, _> = mole * mole;
let square_candela: SiUnit<i32, _, _, _, _, _, _, _> = candela * candela;
assert_eq!(square_second.to_string(), "4s^2");
assert_eq!(square_meter.to_string(), "4m^2");
assert_eq!(square_kilogram.to_string(), "4kg^2");
assert_eq!(square_ampere.to_string(), "4A^2");
assert_eq!(square_kelvin.to_string(), "4K^2");
assert_eq!(square_mole.to_string(), "4mol^2");
assert_eq!(square_candela.to_string(), "4cd^2");
assert_eq!(Herz::new(2).to_string(), "2Hz");
assert_eq!(Newton::new(2).to_string(), "2N");
assert_eq!(Pascal::new(2).to_string(), "2Pa");
assert_eq!(Joule::new(2).to_string(), "2J");
assert_eq!(Watt::new(2).to_string(), "2W");
assert_eq!(Coulomb::new(2).to_string(), "2C");
assert_eq!(Volt::new(2).to_string(), "2V");
assert_eq!(Farad::new(2).to_string(), "2F");
assert_eq!(Ohm::new(2).to_string(), "2Ohm");
assert_eq!(Siemens::new(2).to_string(), "2S");
assert_eq!(Weber::new(2).to_string(), "2Wb");
assert_eq!(Tesla::new(2).to_string(), "2T");
assert_eq!(Henry::new(2).to_string(), "2H");
assert_eq!(Lux::new(2).to_string(), "2lx");
assert_eq!(Gray::new(2).to_string(), "2Gy|Sv");
assert_eq!(Katal::new(2).to_string(), "2kat");
assert_eq!(MeterPerSecond::new(2).to_string(), "2m/s");
assert_eq!(MeterPerSquareSecond::new(2).to_string(), "2m/s²");
assert_eq!(KilogramPerCubicMeter::new(2).to_string(), "2kg/m³");
assert_eq!(KilogramPerSquareMeter::new(2).to_string(), "2kg/m²");
assert_eq!(CubicMeterPerKilogram::new(2).to_string(), "2m³/kg");
assert_eq!(AmperePerSquareMeter::new(2).to_string(), "2A/m²");
assert_eq!(AmperePerMeter::new(2).to_string(), "2A/m");
assert_eq!(MolePerCubicMeter::new(2).to_string(), "2mol/m³");
assert_eq!(PascalSecond::new(2).to_string(), "2Pa*s");
assert_eq!(NewtonPerMeter::new(2).to_string(), "2N/m");
assert_eq!(WattPerSquareMeter::new(2).to_string(), "2W/m²");
assert_eq!(JoulePerKelvin::new(2).to_string(), "2J/K");
assert_eq!(JoulePerKilogramKelvin::new(2).to_string(), "2J/(kg*K)");
assert_eq!(WattPerMeterKelvin::new(2).to_string(), "2W/(m*K)");
assert_eq!(VoltPerMeter::new(2).to_string(), "2V/m");
assert_eq!(CoulombPerCubicMeter::new(2).to_string(), "2C/m³");
assert_eq!(CoulombPerSquareMeter::new(2).to_string(), "2C/m²");
assert_eq!(FaradPerMeter::new(2).to_string(), "2F/m");
assert_eq!(HenryPerMeter::new(2).to_string(), "2H/m");
assert_eq!(JoulePerMole::new(2).to_string(), "2J/mol");
assert_eq!(JoulePerMoleKelvin::new(2).to_string(), "2J/(mol*K)");
assert_eq!(CoulombPerKilogram::new(2).to_string(), "2C/kg");
assert_eq!(GrayPerSecond::new(2).to_string(), "2Gy/s");
assert_eq!(KatalPerCubicMeter::new(2).to_string(), "2kat/m³");
}
}

View File

@ -1,3 +1,6 @@
mod display;
pub mod types;
use std::{
fmt::{Display, Formatter},
marker::PhantomData,
@ -9,6 +12,7 @@ use std::{
use num_traits::{Num, One, Zero};
use typenum::{int::Z0, op, Integer};
use types::Unit;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct SiUnit<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela>
@ -31,221 +35,6 @@ where
_cd: PhantomData<Candela>,
}
macro_rules! display_unit_defmt {
($formatter: ident, $param: ident, $symbol: literal, $e1: ident, $e2: ident, $e3: ident, $e4: ident, $e5: ident, $e6: ident) => {
if $param::I64 != 0 {
if $e1::I64 == 0
&& $e2::I64 == 0
&& $e3::I64 == 0
&& $e4::I64 == 0
&& $e5::I64 == 0
&& $e6::I64 == 0
{
defmt::write!($formatter, $symbol);
} else {
defmt::write!($formatter, "*{}", $symbol);
}
if $param::I64 != 1 {
defmt::write!($formatter, "^{}", $param::I64);
}
}
};
}
macro_rules! display_special_unit_defmt {
($formatter: ident, $self: ident, $(($symbol: literal, $other: ty)),* $(,)?) => {
$(
if ::core::any::TypeId::of::<$self>() == ::core::any::TypeId::of::<$other>() {
defmt::write!($formatter, $symbol);
return;
}
)*
};
}
#[cfg(feature = "defmt")]
impl<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela> defmt::Format
for SiUnit<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela>
where
Second: Integer,
Meter: Integer,
Kilogram: Integer,
Ampere: Integer,
Kelvin: Integer,
Mole: Integer,
Candela: Integer,
T: defmt::Format + 'static,
{
fn format(&self, f: defmt::Formatter<'_>) {
defmt::write!(f, "{}", self.value);
// derived units with special symbols
display_special_unit_defmt!(f, Self,
("Hz", Herz<T>),
("N", Newton<T>),
("Pa", Pascal<T>),
("J", Joule<T>),
("W", Watt<T>),
("C", Coulomb<T>),
("V", Volt<T>),
("F", Farad<T>),
("Ohm", Ohm<T>),
("S", Siemens<T>),
("Wb", Weber<T>),
("T", Tesla<T>),
("H", Henry<T>),
("lx", Lux<T>),
("Gy|Sv", Gray<T>),
("kat", Katal<T>),
);
// derived units
display_special_unit_defmt!(f, Self,
("m/s", MeterPerSecond<T>),
("m/s²", MeterPerSquareSecond<T>),
("kg/m³", KilogramPerCubicMeter<T>),
("kg/m²", KilogramPerSquareMeter<T>),
("m³/kg", CubicMeterPerKilogram<T>),
("A/m²", AmperePerSquareMeter<T>),
("A/m", AmperePerMeter<T>),
("mol/m³", MolePerCubicMeter<T>),
);
// derived units including special names
display_special_unit_defmt!(f, Self,
("Pa*s", PascalSecond<T>),
("N/m", NewtonPerMeter<T>),
("W/m²", WattPerSquareMeter<T>),
("J/K", JoulePerKelvin<T>),
("J/(kg*K)", JoulePerKilogramKelvin<T>),
("W/(m*K)", WattPerMeterKelvin<T>),
("V/m", VoltPerMeter<T>),
("C/m³", CoulombPerCubicMeter<T>),
("C/m²", CoulombPerSquareMeter<T>),
("F/m", FaradPerMeter<T>),
("H/m", HenryPerMeter<T>),
("J/mol", JoulePerMole<T>),
("J/(mol*K)", JoulePerMoleKelvin<T>),
("C/kg", CoulombPerKilogram<T>),
("Gy/s", GrayPerSecond<T>),
("kat/m³", KatalPerCubicMeter<T>),
);
// base units
display_unit_defmt!(f, Second, "s", Meter, Kilogram, Ampere, Kelvin, Mole, Candela);
display_unit_defmt!(f, Meter, "m", Second, Kilogram, Ampere, Kelvin, Mole, Candela);
display_unit_defmt!(f, Kilogram, "kg", Second, Meter, Ampere, Kelvin, Mole, Candela);
display_unit_defmt!(f, Ampere, "A", Second, Meter, Kilogram, Kelvin, Mole, Candela);
display_unit_defmt!(f, Kelvin, "K", Second, Meter, Kilogram, Ampere, Mole, Candela);
display_unit_defmt!(f, Mole, "mol", Second, Meter, Kilogram, Ampere, Kelvin, Candela);
display_unit_defmt!(f, Candela, "cd", Second, Meter, Kilogram, Ampere, Kelvin, Mole);
}
}
macro_rules! display_unit {
($formatter: ident, $param: ident, $symbol: literal, $e1: ident, $e2: ident, $e3: ident, $e4: ident, $e5: ident, $e6: ident) => {
if $param::I64 != 0 {
if $e1::I64 == 0
&& $e2::I64 == 0
&& $e3::I64 == 0
&& $e4::I64 == 0
&& $e5::I64 == 0
&& $e6::I64 == 0
{
write!($formatter, $symbol)?;
} else {
write!($formatter, "*{}", $symbol)?;
}
if $param::I64 != 1 {
write!($formatter, "^{}", $param::I64)?;
}
}
};
}
macro_rules! display_special_unit {
($formatter: ident, $self: ident, $(($symbol: literal, $other: ty)),* $(,)?) => {
$(
if ::core::any::TypeId::of::<$self>() == ::core::any::TypeId::of::<$other>() {
return write!($formatter, $symbol);
}
)*
};
}
impl<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela> Display
for SiUnit<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela>
where
Second: Integer,
Meter: Integer,
Kilogram: Integer,
Ampere: Integer,
Kelvin: Integer,
Mole: Integer,
Candela: Integer,
T: Display + 'static,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)?;
// derived units with special symbols
display_special_unit!(f, Self,
("Hz", Herz<T>),
("N", Newton<T>),
("Pa", Pascal<T>),
("J", Joule<T>),
("W", Watt<T>),
("C", Coulomb<T>),
("V", Volt<T>),
("F", Farad<T>),
("Ohm", Ohm<T>),
("S", Siemens<T>),
("Wb", Weber<T>),
("T", Tesla<T>),
("H", Henry<T>),
("lx", Lux<T>),
("Gy|Sv", Gray<T>),
("kat", Katal<T>),
);
// derived units
display_special_unit!(f, Self,
("m/s", MeterPerSecond<T>),
("m/s²", MeterPerSquareSecond<T>),
("kg/m³", KilogramPerCubicMeter<T>),
("kg/m²", KilogramPerSquareMeter<T>),
("m³/kg", CubicMeterPerKilogram<T>),
("A/m²", AmperePerSquareMeter<T>),
("A/m", AmperePerMeter<T>),
("mol/m³", MolePerCubicMeter<T>),
);
// derived units including special names
display_special_unit!(f, Self,
("Pa*s", PascalSecond<T>),
("N/m", NewtonPerMeter<T>),
("W/m²", WattPerSquareMeter<T>),
("J/K", JoulePerKelvin<T>),
("J/(kg*K)", JoulePerKilogramKelvin<T>),
("W/(m*K)", WattPerMeterKelvin<T>),
("V/m", VoltPerMeter<T>),
("C/m³", CoulombPerCubicMeter<T>),
("C/m²", CoulombPerSquareMeter<T>),
("F/m", FaradPerMeter<T>),
("H/m", HenryPerMeter<T>),
("J/mol", JoulePerMole<T>),
("J/(mol*K)", JoulePerMoleKelvin<T>),
("C/kg", CoulombPerKilogram<T>),
("Gy/s", GrayPerSecond<T>),
("kat/m³", KatalPerCubicMeter<T>),
);
// base units
display_unit!(f, Second, "s", Meter, Kilogram, Ampere, Kelvin, Mole, Candela);
display_unit!(f, Meter, "m", Second, Kilogram, Ampere, Kelvin, Mole, Candela);
display_unit!(f, Kilogram, "kg", Second, Meter, Ampere, Kelvin, Mole, Candela);
display_unit!(f, Ampere, "A", Second, Meter, Kilogram, Kelvin, Mole, Candela);
display_unit!(f, Kelvin, "K", Second, Meter, Kilogram, Ampere, Mole, Candela);
display_unit!(f, Mole, "mol", Second, Meter, Kilogram, Ampere, Kelvin, Candela);
display_unit!(f, Candela, "cd", Second, Meter, Kilogram, Ampere, Kelvin, Mole);
Ok(())
}
}
impl<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela> Deref
for SiUnit<T, Second, Meter, Kilogram, Ampere, Kelvin, Mole, Candela>
where
@ -765,233 +554,10 @@ where
}
}
#[rustfmt::skip]
mod aliases {
use super::SiUnit;
use typenum::consts::{N1, N2, N3, P1, P2, P3, P4, Z0};
// Base units
pub type Unit<T> = SiUnit<T, Z0, Z0, Z0, Z0, Z0, Z0, Z0>;
/// time
pub type Second<T> = SiUnit<T, P1, Z0, Z0, Z0, Z0, Z0, Z0>;
/// length
pub type Meter<T> = SiUnit<T, Z0, P1, Z0, Z0, Z0, Z0, Z0>;
/// mass
pub type Kilogram<T> = SiUnit<T, Z0, Z0, P1, Z0, Z0, Z0, Z0>;
/// electric current
pub type Ampere<T> = SiUnit<T, Z0, Z0, Z0, P1, Z0, Z0, Z0>;
/// thermodynamic temperature
pub type Kelvin<T> = SiUnit<T, Z0, Z0, Z0, Z0, P1, Z0, Z0>;
/// amount of substance
pub type Mole<T> = SiUnit<T, Z0, Z0, Z0, Z0, Z0, P1, Z0>;
/// luminous intensity
pub type Candela<T> = SiUnit<T, Z0, Z0, Z0, Z0, Z0, Z0, P1>;
// Derived units with special names
/// plane angle
pub type Radian<T> = Unit<T>;
/// solid angle
pub type Steradian<T> = Unit<T>;
/// frequency
pub type Herz<T> = SiUnit<T, N1, Z0, Z0, Z0, Z0, Z0, Z0>;
/// force, weight
pub type Newton<T> = SiUnit<T, N2, P1, P1, Z0, Z0, Z0, Z0>;
/// pressure, stress
pub type Pascal<T> = SiUnit<T, N2, N1, P1, Z0, Z0, Z0, Z0>;
/// energy, work, heat
pub type Joule<T> = SiUnit<T, N2, P2, P1, Z0, Z0, Z0, Z0>;
/// power, radiant flux
pub type Watt<T> = SiUnit<T, N3, P2, P1, Z0, Z0, Z0, Z0>;
/// electric charge
pub type Coulomb<T> = SiUnit<T, P1, Z0, Z0, P1, Z0, Z0, Z0>;
/// electric potential, voltage, emf
pub type Volt<T> = SiUnit<T, N3, P2, P1, N1, Z0, Z0, Z0>;
/// capacitance
pub type Farad<T> = SiUnit<T, P4, N2, N1, P2, Z0, Z0, Z0>;
/// resistance impedance, reactance
pub type Ohm<T> = SiUnit<T, N3, P2, P1, N2, Z0, Z0, Z0>;
/// electrical conductance
pub type Siemens<T> = SiUnit<T, P3, N2, N1, P2, Z0, Z0, Z0>;
/// magnetic flux
pub type Weber<T> = SiUnit<T, N2, P2, P1, N1, Z0, Z0, Z0>;
/// magnetic flux density
pub type Tesla<T> = SiUnit<T, N2, Z0, P1, N1, Z0, Z0, Z0>;
/// inductance
pub type Henry<T> = SiUnit<T, N2, P2, P1, N2, Z0, Z0, Z0>;
/// luminous flux
pub type Lumen<T> = Candela<T>;
/// iluminance
pub type Lux<T> = SiUnit<T, Z0, N2, Z0, Z0, Z0, Z0, P1>;
/// activity referred to a radionuclide
pub type Becquerel<T> = Herz<T>;
/// absorbed dose
pub type Gray<T> = SiUnit<T, N2, P2, Z0, Z0, Z0, Z0, Z0>;
/// equivalent dose
pub type Sievert<T> = Gray<T>;
/// catalytic activity
pub type Katal<T> = SiUnit<T, N1, Z0, Z0, Z0, Z0, P1, Z0>;
// Derived units without special names
/// area
pub type SquareMeter<T> = SiUnit<T, Z0, P2, Z0, Z0, Z0, Z0, Z0>;
/// volume
pub type CubicMeter<T> = SiUnit<T, Z0, P3, Z0, Z0, Z0, Z0, Z0>;
/// speed, velocity
pub type MeterPerSecond<T> = SiUnit<T, N1, P1, Z0, Z0, Z0, Z0, Z0>;
/// acceleration
pub type MeterPerSquareSecond<T> = SiUnit<T, N2, P1, Z0, Z0, Z0, Z0, Z0>;
/// wavenumber, vergence
pub type ReciprocalMeter<T> = SiUnit<T, Z0, N1, Z0, Z0, Z0, Z0, Z0>;
/// density, mass concentration
pub type KilogramPerCubicMeter<T> = SiUnit<T, Z0, N3, P1, Z0, Z0, Z0, Z0>;
/// surface density
pub type KilogramPerSquareMeter<T> = SiUnit<T, Z0, N2, P1, Z0, Z0, Z0, Z0>;
/// specific density
pub type CubicMeterPerKilogram<T> = SiUnit<T, Z0, P3, N1, Z0, Z0, Z0, Z0>;
/// current density
pub type AmperePerSquareMeter<T> = SiUnit<T, Z0, N2, Z0, P1, Z0, Z0, Z0>;
/// magnetic field strength
pub type AmperePerMeter<T> = SiUnit<T, Z0, N1, Z0, P1, Z0, Z0, Z0>;
/// concentration
pub type MolePerCubicMeter<T> = SiUnit<T, Z0, N3, Z0, Z0, Z0, P1, Z0>;
/// luminance
pub type CandelaPerSquareMeter<T> = SiUnit<T, Z0, N2, Z0, Z0, Z0, Z0, P1>;
// Derived units including special names
/// dynamic viscosity
pub type PascalSecond<T> = SiUnit<T, N1, N1, P1, Z0, Z0, Z0, Z0>;
/// moment of force
pub type NewtonMeter<T> = SiUnit<T, N2, P2, P1, Z0, Z0, Z0, Z0>;
/// surface tension
pub type NewtonPerMeter<T> = SiUnit<T, N2, Z0, P1, Z0, Z0, Z0, Z0>;
/// angular velocity, angular frequency
pub type RadianPerSecond<T> = SiUnit<T, N1, Z0, Z0, Z0, Z0, Z0, Z0>;
/// angular acceleration
pub type RadianPerSquareSecond<T> = SiUnit<T, N2, Z0, Z0, Z0, Z0, Z0, Z0>;
/// heat flux density, irradiance
pub type WattPerSquareMeter<T> = SiUnit<T, N3, Z0, P1, Z0, Z0, Z0, Z0>;
/// entropy, heat capacity
pub type JoulePerKelvin<T> = SiUnit<T, N2, P2, P1, Z0, N1, Z0, Z0>;
/// specific heat capacity, specific entropy
pub type JoulePerKilogramKelvin<T> = SiUnit<T, N2, P2, Z0, Z0, N1, Z0, Z0>;
/// specific energy
pub type JoulePerKilogram<T> = SiUnit<T, N2, P2, Z0, Z0, Z0, Z0, Z0>;
/// themal conductivity
pub type WattPerMeterKelvin<T> = SiUnit<T, N3, P1, P1, Z0, N1, Z0, Z0>;
/// energy density
pub type JoulePerCubicMeter<T> = SiUnit<T, N2, N1, P1, Z0, Z0, Z0, Z0>;
/// electric field strength
pub type VoltPerMeter<T> = SiUnit<T, N3, P1, P1, N1, Z0, Z0, Z0>;
/// electric charge density
pub type CoulombPerCubicMeter<T> = SiUnit<T, P1, N3, Z0, P1, Z0, Z0, Z0>;
/// surface charge density, electirc flusx density, electric displacement
pub type CoulombPerSquareMeter<T> = SiUnit<T, P1, N2, Z0, P1, Z0, Z0, Z0>;
/// permittivity
pub type FaradPerMeter<T> = SiUnit<T, P4, N3, N1, P2, Z0, Z0, Z0>;
/// permeability
pub type HenryPerMeter<T> = SiUnit<T, N2, P1, P1, N2, Z0, Z0, Z0>;
/// molar energy
pub type JoulePerMole<T> = SiUnit<T, N2, P2, P1, Z0, Z0, N1, Z0>;
/// molar entropy, molar heat capacity
pub type JoulePerMoleKelvin<T> = SiUnit<T, N2, P2, P1, Z0, N1, N1, Z0>;
/// exposure (x- and gamma-rays)
pub type CoulombPerKilogram<T> = SiUnit<T, P1, Z0, N1, P1, Z0, Z0, Z0>;
/// absorbed dose rate
pub type GrayPerSecond<T> = SiUnit<T, N3, P2, Z0, Z0, Z0, Z0, Z0>;
/// radiant intensity
pub type WattPerSteradian<T> = SiUnit<T, N3, P2, P1, Z0, Z0, Z0, Z0>;
/// radiance
pub type WattPerSquareMeterSteradian<T> = SiUnit<T, N3, Z0, P1, Z0, Z0, Z0, Z0>;
/// catalytic activity concentration
pub type KatalPerCubicMeter<T> = SiUnit<T, N1, N3, Z0, Z0, Z0, P1, Z0>;
}
pub use aliases::*;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn debug() {
let m = Meter::new(2);
assert_eq!(format!("{m:?}"), "SiUnit { value: 2, _s: PhantomData<typenum::int::Z0>, _m: PhantomData<typenum::int::PInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>>>, _kg: PhantomData<typenum::int::Z0>, _a: PhantomData<typenum::int::Z0>, _k: PhantomData<typenum::int::Z0>, _mol: PhantomData<typenum::int::Z0>, _cd: PhantomData<typenum::int::Z0> }".to_owned());
}
#[test]
fn display() {
let unit = Unit::new(2);
let second = Second::new(2);
let meter = Meter::new(2);
let kilogram = Kilogram::new(2);
let ampere = Ampere::new(2);
let kelvin = Kelvin::new(2);
let mole = Mole::new(2);
let candela = Candela::new(2);
assert_eq!(unit.to_string(), "2");
assert_eq!(second.to_string(), "2s");
assert_eq!(meter.to_string(), "2m");
assert_eq!(kilogram.to_string(), "2kg");
assert_eq!(ampere.to_string(), "2A");
assert_eq!(kelvin.to_string(), "2K");
assert_eq!(mole.to_string(), "2mol");
assert_eq!(candela.to_string(), "2cd");
let square_second: SiUnit<i32, _, _, _, _, _, _, _> = second * second;
let square_meter: SiUnit<i32, _, _, _, _, _, _, _> = meter * meter;
let square_kilogram: SiUnit<i32, _, _, _, _, _, _, _> = kilogram * kilogram;
let square_ampere: SiUnit<i32, _, _, _, _, _, _, _> = ampere * ampere;
let square_kelvin: SiUnit<i32, _, _, _, _, _, _, _> = kelvin * kelvin;
let square_mole: SiUnit<i32, _, _, _, _, _, _, _> = mole * mole;
let square_candela: SiUnit<i32, _, _, _, _, _, _, _> = candela * candela;
assert_eq!(square_second.to_string(), "4s^2");
assert_eq!(square_meter.to_string(), "4m^2");
assert_eq!(square_kilogram.to_string(), "4kg^2");
assert_eq!(square_ampere.to_string(), "4A^2");
assert_eq!(square_kelvin.to_string(), "4K^2");
assert_eq!(square_mole.to_string(), "4mol^2");
assert_eq!(square_candela.to_string(), "4cd^2");
assert_eq!(Herz::new(2).to_string(), "2Hz");
assert_eq!(Newton::new(2).to_string(), "2N");
assert_eq!(Pascal::new(2).to_string(), "2Pa");
assert_eq!(Joule::new(2).to_string(), "2J");
assert_eq!(Watt::new(2).to_string(), "2W");
assert_eq!(Coulomb::new(2).to_string(), "2C");
assert_eq!(Volt::new(2).to_string(), "2V");
assert_eq!(Farad::new(2).to_string(), "2F");
assert_eq!(Ohm::new(2).to_string(), "2Ohm");
assert_eq!(Siemens::new(2).to_string(), "2S");
assert_eq!(Weber::new(2).to_string(), "2Wb");
assert_eq!(Tesla::new(2).to_string(), "2T");
assert_eq!(Henry::new(2).to_string(), "2H");
assert_eq!(Lux::new(2).to_string(), "2lx");
assert_eq!(Gray::new(2).to_string(), "2Gy|Sv");
assert_eq!(Katal::new(2).to_string(), "2kat");
assert_eq!(MeterPerSecond::new(2).to_string(), "2m/s");
assert_eq!(MeterPerSquareSecond::new(2).to_string(), "2m/s²");
assert_eq!(KilogramPerCubicMeter::new(2).to_string(), "2kg/m³");
assert_eq!(KilogramPerSquareMeter::new(2).to_string(), "2kg/m²");
assert_eq!(CubicMeterPerKilogram::new(2).to_string(), "2m³/kg");
assert_eq!(AmperePerSquareMeter::new(2).to_string(), "2A/m²");
assert_eq!(AmperePerMeter::new(2).to_string(), "2A/m");
assert_eq!(MolePerCubicMeter::new(2).to_string(), "2mol/m³");
assert_eq!(PascalSecond::new(2).to_string(), "2Pa*s");
assert_eq!(NewtonPerMeter::new(2).to_string(), "2N/m");
assert_eq!(WattPerSquareMeter::new(2).to_string(), "2W/m²");
assert_eq!(JoulePerKelvin::new(2).to_string(), "2J/K");
assert_eq!(JoulePerKilogramKelvin::new(2).to_string(), "2J/(kg*K)");
assert_eq!(WattPerMeterKelvin::new(2).to_string(), "2W/(m*K)");
assert_eq!(VoltPerMeter::new(2).to_string(), "2V/m");
assert_eq!(CoulombPerCubicMeter::new(2).to_string(), "2C/m³");
assert_eq!(CoulombPerSquareMeter::new(2).to_string(), "2C/m²");
assert_eq!(FaradPerMeter::new(2).to_string(), "2F/m");
assert_eq!(HenryPerMeter::new(2).to_string(), "2H/m");
assert_eq!(JoulePerMole::new(2).to_string(), "2J/mol");
assert_eq!(JoulePerMoleKelvin::new(2).to_string(), "2J/(mol*K)");
assert_eq!(CoulombPerKilogram::new(2).to_string(), "2C/kg");
assert_eq!(GrayPerSecond::new(2).to_string(), "2Gy/s");
assert_eq!(KatalPerCubicMeter::new(2).to_string(), "2kat/m³");
}
use super::types::*;
use num_traits::{Num, One, Zero};
#[test]
fn clone() {

137
src/types.rs Normal file
View File

@ -0,0 +1,137 @@
use crate::SiUnit;
use typenum::consts::{N1, N2, N3, P1, P2, P3, P4, Z0};
// Base units
pub type Unit<T> = SiUnit<T, Z0, Z0, Z0, Z0, Z0, Z0, Z0>;
/// time
pub type Second<T> = SiUnit<T, P1, Z0, Z0, Z0, Z0, Z0, Z0>;
/// length
pub type Meter<T> = SiUnit<T, Z0, P1, Z0, Z0, Z0, Z0, Z0>;
/// mass
pub type Kilogram<T> = SiUnit<T, Z0, Z0, P1, Z0, Z0, Z0, Z0>;
/// electric current
pub type Ampere<T> = SiUnit<T, Z0, Z0, Z0, P1, Z0, Z0, Z0>;
/// thermodynamic temperature
pub type Kelvin<T> = SiUnit<T, Z0, Z0, Z0, Z0, P1, Z0, Z0>;
/// amount of substance
pub type Mole<T> = SiUnit<T, Z0, Z0, Z0, Z0, Z0, P1, Z0>;
/// luminous intensity
pub type Candela<T> = SiUnit<T, Z0, Z0, Z0, Z0, Z0, Z0, P1>;
// Derived units with special names
/// plane angle
pub type Radian<T> = Unit<T>;
/// solid angle
pub type Steradian<T> = Unit<T>;
/// frequency
pub type Herz<T> = SiUnit<T, N1, Z0, Z0, Z0, Z0, Z0, Z0>;
/// force, weight
pub type Newton<T> = SiUnit<T, N2, P1, P1, Z0, Z0, Z0, Z0>;
/// pressure, stress
pub type Pascal<T> = SiUnit<T, N2, N1, P1, Z0, Z0, Z0, Z0>;
/// energy, work, heat
pub type Joule<T> = SiUnit<T, N2, P2, P1, Z0, Z0, Z0, Z0>;
/// power, radiant flux
pub type Watt<T> = SiUnit<T, N3, P2, P1, Z0, Z0, Z0, Z0>;
/// electric charge
pub type Coulomb<T> = SiUnit<T, P1, Z0, Z0, P1, Z0, Z0, Z0>;
/// electric potential, voltage, emf
pub type Volt<T> = SiUnit<T, N3, P2, P1, N1, Z0, Z0, Z0>;
/// capacitance
pub type Farad<T> = SiUnit<T, P4, N2, N1, P2, Z0, Z0, Z0>;
/// resistance impedance, reactance
pub type Ohm<T> = SiUnit<T, N3, P2, P1, N2, Z0, Z0, Z0>;
/// electrical conductance
pub type Siemens<T> = SiUnit<T, P3, N2, N1, P2, Z0, Z0, Z0>;
/// magnetic flux
pub type Weber<T> = SiUnit<T, N2, P2, P1, N1, Z0, Z0, Z0>;
/// magnetic flux density
pub type Tesla<T> = SiUnit<T, N2, Z0, P1, N1, Z0, Z0, Z0>;
/// inductance
pub type Henry<T> = SiUnit<T, N2, P2, P1, N2, Z0, Z0, Z0>;
/// luminous flux
pub type Lumen<T> = Candela<T>;
/// iluminance
pub type Lux<T> = SiUnit<T, Z0, N2, Z0, Z0, Z0, Z0, P1>;
/// activity referred to a radionuclide
pub type Becquerel<T> = Herz<T>;
/// absorbed dose
pub type Gray<T> = SiUnit<T, N2, P2, Z0, Z0, Z0, Z0, Z0>;
/// equivalent dose
pub type Sievert<T> = Gray<T>;
/// catalytic activity
pub type Katal<T> = SiUnit<T, N1, Z0, Z0, Z0, Z0, P1, Z0>;
// Derived units without special names
/// area
pub type SquareMeter<T> = SiUnit<T, Z0, P2, Z0, Z0, Z0, Z0, Z0>;
/// volume
pub type CubicMeter<T> = SiUnit<T, Z0, P3, Z0, Z0, Z0, Z0, Z0>;
/// speed, velocity
pub type MeterPerSecond<T> = SiUnit<T, N1, P1, Z0, Z0, Z0, Z0, Z0>;
/// acceleration
pub type MeterPerSquareSecond<T> = SiUnit<T, N2, P1, Z0, Z0, Z0, Z0, Z0>;
/// wavenumber, vergence
pub type ReciprocalMeter<T> = SiUnit<T, Z0, N1, Z0, Z0, Z0, Z0, Z0>;
/// density, mass concentration
pub type KilogramPerCubicMeter<T> = SiUnit<T, Z0, N3, P1, Z0, Z0, Z0, Z0>;
/// surface density
pub type KilogramPerSquareMeter<T> = SiUnit<T, Z0, N2, P1, Z0, Z0, Z0, Z0>;
/// specific density
pub type CubicMeterPerKilogram<T> = SiUnit<T, Z0, P3, N1, Z0, Z0, Z0, Z0>;
/// current density
pub type AmperePerSquareMeter<T> = SiUnit<T, Z0, N2, Z0, P1, Z0, Z0, Z0>;
/// magnetic field strength
pub type AmperePerMeter<T> = SiUnit<T, Z0, N1, Z0, P1, Z0, Z0, Z0>;
/// concentration
pub type MolePerCubicMeter<T> = SiUnit<T, Z0, N3, Z0, Z0, Z0, P1, Z0>;
/// luminance
pub type CandelaPerSquareMeter<T> = SiUnit<T, Z0, N2, Z0, Z0, Z0, Z0, P1>;
// Derived units including special names
/// dynamic viscosity
pub type PascalSecond<T> = SiUnit<T, N1, N1, P1, Z0, Z0, Z0, Z0>;
/// moment of force
pub type NewtonMeter<T> = SiUnit<T, N2, P2, P1, Z0, Z0, Z0, Z0>;
/// surface tension
pub type NewtonPerMeter<T> = SiUnit<T, N2, Z0, P1, Z0, Z0, Z0, Z0>;
/// angular velocity, angular frequency
pub type RadianPerSecond<T> = SiUnit<T, N1, Z0, Z0, Z0, Z0, Z0, Z0>;
/// angular acceleration
pub type RadianPerSquareSecond<T> = SiUnit<T, N2, Z0, Z0, Z0, Z0, Z0, Z0>;
/// heat flux density, irradiance
pub type WattPerSquareMeter<T> = SiUnit<T, N3, Z0, P1, Z0, Z0, Z0, Z0>;
/// entropy, heat capacity
pub type JoulePerKelvin<T> = SiUnit<T, N2, P2, P1, Z0, N1, Z0, Z0>;
/// specific heat capacity, specific entropy
pub type JoulePerKilogramKelvin<T> = SiUnit<T, N2, P2, Z0, Z0, N1, Z0, Z0>;
/// specific energy
pub type JoulePerKilogram<T> = SiUnit<T, N2, P2, Z0, Z0, Z0, Z0, Z0>;
/// themal conductivity
pub type WattPerMeterKelvin<T> = SiUnit<T, N3, P1, P1, Z0, N1, Z0, Z0>;
/// energy density
pub type JoulePerCubicMeter<T> = SiUnit<T, N2, N1, P1, Z0, Z0, Z0, Z0>;
/// electric field strength
pub type VoltPerMeter<T> = SiUnit<T, N3, P1, P1, N1, Z0, Z0, Z0>;
/// electric charge density
pub type CoulombPerCubicMeter<T> = SiUnit<T, P1, N3, Z0, P1, Z0, Z0, Z0>;
/// surface charge density, electirc flusx density, electric displacement
pub type CoulombPerSquareMeter<T> = SiUnit<T, P1, N2, Z0, P1, Z0, Z0, Z0>;
/// permittivity
pub type FaradPerMeter<T> = SiUnit<T, P4, N3, N1, P2, Z0, Z0, Z0>;
/// permeability
pub type HenryPerMeter<T> = SiUnit<T, N2, P1, P1, N2, Z0, Z0, Z0>;
/// molar energy
pub type JoulePerMole<T> = SiUnit<T, N2, P2, P1, Z0, Z0, N1, Z0>;
/// molar entropy, molar heat capacity
pub type JoulePerMoleKelvin<T> = SiUnit<T, N2, P2, P1, Z0, N1, N1, Z0>;
/// exposure (x- and gamma-rays)
pub type CoulombPerKilogram<T> = SiUnit<T, P1, Z0, N1, P1, Z0, Z0, Z0>;
/// absorbed dose rate
pub type GrayPerSecond<T> = SiUnit<T, N3, P2, Z0, Z0, Z0, Z0, Z0>;
/// radiant intensity
pub type WattPerSteradian<T> = SiUnit<T, N3, P2, P1, Z0, Z0, Z0, Z0>;
/// radiance
pub type WattPerSquareMeterSteradian<T> = SiUnit<T, N3, Z0, P1, Z0, Z0, Z0, Z0>;
/// catalytic activity concentration
pub type KatalPerCubicMeter<T> = SiUnit<T, N1, N3, Z0, Z0, Z0, P1, Z0>;