dimensioned::make_units!
[−]
[src]
macro_rules! make_units { ($System:ident, $Unitless:ident, $one:ident; base { $($Type:ident, $constant:ident, $print_as:ident;)+ } derived { $($derived_constant:ident: $Derived:ident = $e:expr;)* // $($derived_constant:ident: $Derived:ident = $FirstType:ident $($op:tt $Type:ident)*);*; } ) => { ... }; }
Create a unit system.
As this macro performs various imports, it is strongly recommended that you call it inside of its own module.
Note that it has some imports from the peano crate, so it must be included.
Example
#[macro_use] extern crate dimensioned; mod fruit { make_units! { Fruit, Unitless, one; base { Apple, apple, a; Banana, banana, b; Cucumber, cuke, c; Mango, mango, m; Watermelon, watermelon, w; } derived { } } } use fruit::{apple, banana, cuke, mango, watermelon}; fn main() { let fruit_salad = apple * banana * mango * mango * watermelon; println!("Mmmm, delicious: {}", fruit_salad); assert_eq!(format!("{}", fruit_salad), "1 a*b*m^2*w"); }
The line Fruit, Unitless, one;
names the unit system Fruit
, names its type for
unitless data Unitless
and creates the corresponding constant one
.
The base
block is used to define the base units of this system. The line Apple, apple, a;
creates the unit Apple
, the corresponding constant apple
, and will use
the token "a" to print Apple
s.
The derived
block is not yet implemented, but will be used to define derived units and
constants.