Expand description

Automatically derived typed Unit system with conversions.


use unit_system::prelude::*;

pub struct Meter(pub f32);
pub struct Inch(pub f64);

// we can convert between any `Unit`s whose bases define BaseIntoBase
impl BaseIntoBase<Inch> for Meter {
  fn convert_base(self) -> Inch {
     Inch(self.0 as f64 * 40.)

// CMeter, DMeter, HMeter, NMeter... have been automatically built
let centimeters: CMeter = CMeter(3.);
// the same for inches (non-SI units would require manual impls of Unit)
let kilo_inches: KInch = centimeters.convert_outer();

// KInch{ 0.012. }
println!("{:?}", kilo_inches);
assert!(f64::abs(kilo_inches.0 - 0.0012) < 1e-7);