unit_system_derive

Crates.iounit_system_derive
lib.rsunit_system_derive
version0.1.0
sourcesrc
created_at2022-01-28 09:52:29.674654
updated_at2022-01-28 09:52:29.674654
descriptionProc_macro derives for the unit_system crate.
homepage
repositoryhttps://github.com/carrascomj/unit_system
max_upload_size
id523009
size7,529
Jorge Carrasco (carrascomj)

documentation

README

unit_system_derive

Proc_macro derivation of Unit for the unit_system crate.

It derives Unit for a struct B and declares structs which also derive Unit and whose BaseUnit is the initial struct B. They form a typical SI unit system. Roughly,

#[derive(Unit)]
struct B(f64);

results into

// The base unit is its own `BaseUnit`
impl Unit for B {
  type BaseUnit = Self;
  fn to_base(self) -> Self {
    self
  }
  fn from_base(base: B) -> Self {
    base
  }
}
struct NB(f64);
struct MuB(f64);
struct MB(f64);
struct CB(f64);
struct DeciB(f64);
struct HB(f64);
struct KB(f64);

/// kiloB is 1000 B
impl Unit for KB {
  type BaseUnit = B;
  fn to_base(self) -> B {
    B(self * 1000.)
  }
  fn from_base(base: Self::BaseUnit) -> Self {
    Self(base / 1000.)
  }
}

// ...the same for the rest of units
Commit count: 8

cargo fmt