unit_system 0.1.0

Automatically derived typed unit systems.
Documentation
  • Coverage
  • 19.44%
    7 out of 36 items documented2 out of 4 items with examples
  • Size
  • Source code size: 8.5 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 6.76 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • carrascomj/unit_system
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • carrascomj

Automatically derived typed Unit system with conversions.

Example

use unit_system::prelude::*;

#[derive(Unit)]
pub struct Meter(pub f32);
#[derive(Unit)]
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);