dimensioned::make_units_adv!
[−]
[src]
macro_rules! make_units_adv { ($System:ident, $Unitless:ident, $one:ident, $OneType:ident, $val:expr; base { $($Root:ident, $Type:ident, $constant:ident, $print_as:ident;)+ } derived { // $($derived_constant:ident: $Derived:ident = $FirstType:ident $($op:tt $Type:ident)*);*; $($derived_constant:ident: $Derived:ident = $e:expr;)* } ) => { ... }; }
Create a unit system with more flexibility than make_units!()
.
As this macro performs various imports, it is strongly recommended that you call it inside of its own module.
Example
Here we define the CGS unit system.
#[macro_use] extern crate dimensioned; mod cgs { make_units_adv! { CGS, Unitless, one, f64, 1.0; base { P2, Centimeter, cm, cm; P2, Gram, g, g; P1, Second, s, s; } derived { } } }
The line CGS, Unitless, one, f64, 1.0;
names the unit system CGS
, names its type for
unitless data Unitless
and creates the corresponding constant one
. It also states
that all constants will be of type Dim<D, f64>
and will be initialized to a value of
1.0
.
Once associated constants hit, std::num::One
will be used to determine the initalize value.
The base
block is used to define the base units of this system. The line P2, Centimeter, cm, cm;
creates the unit Centimeter
, the corresponding constant cm
, and
will use the token "cm" to print Centimeter
s. It also states that square roots will be
allowed for Centimeter
s; the P2
is the type number for 2 and dictates the highest
root allowed. You will almost always want this to be P1
. For CGS
, though, some
derived units are defined in terms of square roots of base units, so they are necessary
to allow.
The derived
block is not yet implemented, but will be used to define derived units and
constants.