#unit #unit-system #derive #f64 #self

macro unit_system_derive

Proc_macro derives for the unit_system crate

1 unstable release

0.1.0 Jan 28, 2022

#9 in #unit-system


Used in unit_system

MIT/Apache

8KB
158 lines

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

Dependencies

~1.5MB
~36K SLoC