1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/// Generate define of Quantity /// /// # Examples /// Define Length /// /// ``` /// use metron_core::{def_unit, def_quantity}; /// def_unit!{pub Metre{ from (Feet =*0.3048), }} /// def_unit!{pub Feet;} /// /// def_quantity!{ /// /// Length is a measure of distance. /// pub Length<Metre> { /// from( Feet ), /// } /// } /// /// ``` /// if you write abobe /// macro will generates like below /// ``` /// use metron_core::{def_unit, Quantity, FromUnit}; /// def_unit!{pub Metre{ from (Feet =* 0.3048), }} /// def_unit!{pub Feet;} /// /// /// Length is a measure of distance. /// pub struct Length; /// /// impl metron_core::Quantity for Length { /// type BaseUnit = Metre; /// } /// /// impl <N> FromUnit<N, Metre> for Length{ /// type Output = <Metre as FromUnit<N, Metre>>::Output; /// fn from_unit(num: N) -> Self::Output { /// /* conversion */ /// num /// } /// } /// /// impl <N> FromUnit<N, Feet> for Length { /// type Output = <<Self as Quantity>::BaseUnit as FromUnit<N, Metre>>::Output; /// fn from_unit(num: N) -> Self::Output{ /// /* conversion */ /// num /// } /// } /// /// ``` #[macro_export] macro_rules! def_quantity{ ( $(#[$meta_for_quantity:meta])* $vis:vis $quantity:ident $(#[$meta_for_base_unit:meta])* < $unit:ty >; ) => { $(#[$meta_for_quantity])* $vis struct $quantity; impl $crate::quantity::Quantity for $quantity { $(#[$meta_for_base_unit])* type BaseUnit = $unit; } $crate::impl_quantity_from! ($quantity, ( $unit ) ); }; ( $(#[$meta_for_quantity:meta])* $vis:vis $quantity:ident $(#[$meta_for_base_unit:meta])* < $unit:ty > { $( $(#[$meta_for_from:meta])* from $from_body:tt , )* // $(into $into_body:tt ,)* $(sym $sym_body:tt ,)? // $(dim $dim_body:tt ,)? }) => { $crate::def_quantity!( $(#[$meta_for_quantity])* $vis $quantity $(#[$meta_for_base_unit])* < $unit >; ); $( $crate::impl_quantity_from! ($quantity, $from_body); )* // $( $crate::impl_quantity_into! ($quantity, $into_body); )* $( $crate::impl_fmt_symbol! ($quantity, $sym_body); )? }; }