#[cfg(test)]
mod test;
mod constants;
mod scale;
pub use constants::{
AVOGADRO_CONSTANT, BOLTZMANN_CONSTANT, ELEMENTARY_CHARGE, GAS_CONSTANT, LIGHT_SPEED,
PLANCK_CONSTANT, ROOM_TEMPERATURE,
};
pub use scale::{Scale, length_scale};
pub trait Unit {}
pub trait UnitMul<Rhs> {
type Output;
}
pub trait UnitDiv<Rhs> {
type Output;
}
pub trait UnitHalves {
type First;
type Second;
}
impl<A, B> UnitHalves for (A, B) {
type First = A;
type Second = B;
}
pub trait UnitSum {
type Output;
}
impl<A> UnitSum for (A, A) {
type Output = A;
}
pub trait UnitInv {
type Output;
}
macro_rules! units {
($($(#[$meta:meta])* $name:ident),+ $(,)?) => {
$(
$(#[$meta])*
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct $name;
impl Unit for $name {}
impl UnitMul<Dimensionless> for $name {
type Output = $name;
}
impl UnitDiv<Dimensionless> for $name {
type Output = $name;
}
impl UnitHalves for $name {
type First = $name;
type Second = $name;
}
impl UnitSum for $name {
type Output = $name;
}
)+
};
}
units!(
Dimensionless,
Length,
ReciprocalLength,
Area,
ReciprocalArea,
Volume,
SecondMomentOfArea,
Velocity,
Force,
ForcePerLength,
ForcePerVelocity,
Energy,
Power,
StressPerLength,
StressPerArea,
ViscosityPerLength,
ViscosityPerArea,
Stress,
ReciprocalStress,
Time,
Rate,
Viscosity,
ReciprocalViscosity,
Temperature,
ReciprocalTemperature,
StressPerTemperature,
PowerDensity,
TemperaturePerLength,
PowerPerArea,
PowerPerLengthTemperature,
PowerPerAreaTemperature,
PowerPerVolumeTemperature,
PowerPerTemperature,
PowerTemperature,
PowerTemperatureDensity,
Entropy,
Action,
Amount,
ReciprocalAmount,
MolarEntropy,
MolarEnergy,
Charge,
ReciprocalForcePerLength,
);
macro_rules! unit_products {
($($lhs:ident * $rhs:ident = $out:ident),+ $(,)?) => {
$(
impl UnitMul<$rhs> for $lhs {
type Output = $out;
}
impl UnitDiv<$rhs> for $out {
type Output = $lhs;
}
)+
};
}
unit_products!(
Dimensionless * Length = Length,
Dimensionless * ReciprocalLength = ReciprocalLength,
Dimensionless * Stress = Stress,
Dimensionless * Rate = Rate,
Dimensionless * Viscosity = Viscosity,
Dimensionless * Temperature = Temperature,
Viscosity * Rate = Stress,
Dimensionless * ReciprocalViscosity = ReciprocalViscosity,
Stress * ReciprocalViscosity = Rate,
ReciprocalViscosity * Stress = Rate,
Rate * Viscosity = Stress,
Length * ReciprocalLength = Dimensionless,
Dimensionless * ReciprocalStress = ReciprocalStress,
Stress * ReciprocalStress = Dimensionless,
ReciprocalStress * Stress = Dimensionless,
Dimensionless * ReciprocalTemperature = ReciprocalTemperature,
Temperature * ReciprocalTemperature = Dimensionless,
Dimensionless * StressPerTemperature = StressPerTemperature,
Stress * ReciprocalTemperature = StressPerTemperature,
StressPerTemperature * Temperature = Stress,
ReciprocalTemperature * Temperature = Dimensionless,
ReciprocalLength * Length = Dimensionless,
Dimensionless * Time = Time,
Rate * Time = Dimensionless,
Time * Rate = Dimensionless,
Stress * Time = Viscosity,
Dimensionless * PowerDensity = PowerDensity,
Stress * Rate = PowerDensity,
Rate * Stress = PowerDensity,
Dimensionless * Area = Area,
Dimensionless * ReciprocalArea = ReciprocalArea,
Dimensionless * Volume = Volume,
Dimensionless * SecondMomentOfArea = SecondMomentOfArea,
Dimensionless * Velocity = Velocity,
Dimensionless * Force = Force,
Dimensionless * ForcePerLength = ForcePerLength,
Dimensionless * ForcePerVelocity = ForcePerVelocity,
Dimensionless * Energy = Energy,
Dimensionless * Power = Power,
Dimensionless * StressPerLength = StressPerLength,
Dimensionless * StressPerArea = StressPerArea,
Dimensionless * ViscosityPerLength = ViscosityPerLength,
Dimensionless * ViscosityPerArea = ViscosityPerArea,
Length * Length = Area,
Length * Area = Volume,
Area * Length = Volume,
Area * Area = SecondMomentOfArea,
Length * Volume = SecondMomentOfArea,
Volume * Length = SecondMomentOfArea,
Area * ReciprocalArea = Dimensionless,
ReciprocalArea * Area = Dimensionless,
Length * ReciprocalArea = ReciprocalLength,
ReciprocalArea * Length = ReciprocalLength,
ReciprocalLength * ReciprocalLength = ReciprocalArea,
ReciprocalLength * Volume = Area,
ReciprocalLength * Area = Length,
Area * ReciprocalLength = Length,
Length * Rate = Velocity,
Rate * Length = Velocity,
Velocity * Time = Length,
Velocity * ReciprocalLength = Rate,
ReciprocalLength * Velocity = Rate,
Stress * Area = Force,
Area * Stress = Force,
Stress * Volume = Energy,
Force * Length = Energy,
Force * ReciprocalLength = ForcePerLength,
ForcePerLength * Length = Force,
Length * ForcePerLength = Force,
Velocity * ForcePerVelocity = Force,
Stress * ReciprocalLength = StressPerLength,
ReciprocalLength * Stress = StressPerLength,
StressPerLength * ReciprocalLength = StressPerArea,
StressPerLength * Length = Stress,
Length * StressPerLength = Stress,
StressPerLength * Area = ForcePerLength,
StressPerLength * Volume = Force,
StressPerArea * Volume = ForcePerLength,
Viscosity * ReciprocalLength = ViscosityPerLength,
ReciprocalLength * Viscosity = ViscosityPerLength,
ViscosityPerLength * ReciprocalLength = ViscosityPerArea,
ViscosityPerArea * Volume = ForcePerVelocity,
ForcePerVelocity * Velocity = Force,
Force * Velocity = Power,
PowerDensity * Volume = Power,
Energy * Rate = Power,
Power * Time = Energy,
Dimensionless * TemperaturePerLength = TemperaturePerLength,
Dimensionless * PowerPerArea = PowerPerArea,
Dimensionless * PowerPerLengthTemperature = PowerPerLengthTemperature,
Dimensionless * PowerPerAreaTemperature = PowerPerAreaTemperature,
Dimensionless * PowerPerVolumeTemperature = PowerPerVolumeTemperature,
Dimensionless * PowerPerTemperature = PowerPerTemperature,
Dimensionless * PowerTemperature = PowerTemperature,
Dimensionless * PowerTemperatureDensity = PowerTemperatureDensity,
Temperature * ReciprocalLength = TemperaturePerLength,
ReciprocalLength * Temperature = TemperaturePerLength,
TemperaturePerLength * PowerPerLengthTemperature = PowerPerArea,
PowerPerLengthTemperature * TemperaturePerLength = PowerPerArea,
PowerPerArea * ReciprocalLength = PowerDensity,
ReciprocalLength * PowerPerArea = PowerDensity,
PowerPerArea * TemperaturePerLength = PowerTemperatureDensity,
PowerTemperatureDensity * Volume = PowerTemperature,
PowerPerLengthTemperature * ReciprocalLength = PowerPerAreaTemperature,
ReciprocalLength * PowerPerLengthTemperature = PowerPerAreaTemperature,
PowerPerAreaTemperature * ReciprocalLength = PowerPerVolumeTemperature,
ReciprocalLength * PowerPerAreaTemperature = PowerPerVolumeTemperature,
PowerDensity * ReciprocalTemperature = EntropyDensityRate,
EntropyDensity * Rate = EntropyDensityRate,
PowerPerVolumeTemperature * Volume = PowerPerTemperature,
Power * Temperature = PowerTemperature,
PowerPerTemperature * Temperature = Power,
Temperature * PowerPerTemperature = Power,
Dimensionless * Entropy = Entropy,
Entropy * Temperature = Energy,
Temperature * Entropy = Energy,
Dimensionless * Action = Action,
Dimensionless * Amount = Amount,
Dimensionless * ReciprocalAmount = ReciprocalAmount,
Dimensionless * MolarEntropy = MolarEntropy,
Dimensionless * MolarEnergy = MolarEnergy,
Dimensionless * Charge = Charge,
Energy * Time = Action,
Time * Energy = Action,
Action * Rate = Energy,
Rate * Action = Energy,
Amount * ReciprocalAmount = Dimensionless,
ReciprocalAmount * Amount = Dimensionless,
Entropy * ReciprocalAmount = MolarEntropy,
ReciprocalAmount * Entropy = MolarEntropy,
MolarEntropy * Amount = Entropy,
MolarEntropy * Temperature = MolarEnergy,
Temperature * MolarEntropy = MolarEnergy,
Energy * ReciprocalAmount = MolarEnergy,
ReciprocalAmount * Energy = MolarEnergy,
MolarEnergy * Amount = Energy,
Dimensionless * ReciprocalForcePerLength = ReciprocalForcePerLength,
ReciprocalLength * Energy = Force,
Energy * ReciprocalLength = Force,
ReciprocalArea * Energy = ForcePerLength,
Energy * ReciprocalArea = ForcePerLength,
ForcePerLength * ReciprocalLength = Stress,
ReciprocalLength * ForcePerLength = Stress,
ForcePerLength * Area = Energy,
Area * ForcePerLength = Energy,
ReciprocalForcePerLength * Energy = Area,
Energy * ReciprocalForcePerLength = Area,
Length * Force = Energy,
ReciprocalForcePerLength * Force = Length,
Force * ReciprocalForcePerLength = Length,
Stress * Length = ForcePerLength,
Length * Stress = ForcePerLength,
);
impl<A, B, C, D> UnitMul<(C, D)> for (A, B)
where
A: UnitMul<C>,
B: UnitMul<D>,
{
type Output = (<A as UnitMul<C>>::Output, <B as UnitMul<D>>::Output);
}
impl<A, B, C, D> UnitDiv<(C, D)> for (A, B)
where
A: UnitDiv<C>,
B: UnitDiv<D>,
{
type Output = (<A as UnitDiv<C>>::Output, <B as UnitDiv<D>>::Output);
}
macro_rules! unit_inverses {
($($unit:ident => $inverse:ident),+ $(,)?) => {
$(
impl UnitInv for $unit {
type Output = $inverse;
}
)+
};
}
unit_inverses!(
Dimensionless => Dimensionless,
Length => ReciprocalLength,
ReciprocalLength => Length,
Area => ReciprocalArea,
ReciprocalArea => Area,
Stress => ReciprocalStress,
ReciprocalStress => Stress,
Temperature => ReciprocalTemperature,
ReciprocalTemperature => Temperature,
Viscosity => ReciprocalViscosity,
ReciprocalViscosity => Viscosity,
Time => Rate,
Rate => Time,
Amount => ReciprocalAmount,
ReciprocalAmount => Amount,
ForcePerLength => ReciprocalForcePerLength,
ReciprocalForcePerLength => ForcePerLength,
);
pub type Pressure = Stress;
pub type EnergyDensity = Stress;
pub type Modulus = Stress;
pub type Compliance = ReciprocalStress;
pub type Fluidity = ReciprocalViscosity;
pub type ThermalExpansion = ReciprocalTemperature;
pub type Frequency = Rate;
pub type Dissipation = PowerDensity;
pub type EntropyDensity = StressPerTemperature;
pub type EntropyDensityRate = PowerPerVolumeTemperature;