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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#[macro_export]
macro_rules! units {
    (
        $(#[$blanket_meta:meta])*
        $blanket:ident($($super:tt)*);
        #[$derive:meta] $base:ty:
        $(
            $(#[$meta:meta])*
            $tys:ident;
        )*
        $(
            <$a:path> * <$b:path> = $c:path;
        )*
    ) => {
        $(#[$blanket_meta])*
        pub trait $blanket : $($super)* {
            /// Returns the raw value of this struct.
            fn value(self) -> $base;
        }

        $(
            $(#[$meta])*
            #[$derive]
            pub struct $tys(pub $base);

            impl From<$base> for $tys {
                #[inline(always)]
                fn from(base: $base) -> Self {
                    Self(base)
                }
            }

            impl $tys {
                /// Returns the raw value of this struct.
                #[inline(always)]
                pub fn value(self) -> $base {
                    self.0
                }
            }

            impl $blanket for $tys {
                #[inline(always)]
                fn value(self) -> $base {
                    self.0
                }
            }

            impl ::std::ops::Add for $tys {
                type Output = Self;

                #[inline(always)]
                fn add(self, other: Self) -> Self {
                    $tys(self.value() + other.value())
                }
            }

            impl ::std::ops::AddAssign for $tys {
                #[inline(always)]
                fn add_assign(&mut self, other: Self) {
                    self.0 += other.value();
                }
            }

            impl ::std::ops::Sub for $tys {
                type Output = Self;

                #[inline(always)]
                fn sub(self, other: Self) -> Self {
                    $tys(self.value() - other.value())
                }
            }

            impl ::std::ops::SubAssign for $tys {
                #[inline(always)]
                fn sub_assign(&mut self, other: Self) {
                    self.0 -= other.value();
                }
            }

            impl ::std::ops::Mul<$base> for $tys {
                type Output = Self;

                #[inline(always)]
                fn mul(self, other: $base) -> Self {
                    Self(self.0 * other)
                }
            }

            impl ::std::ops::MulAssign<$base> for $tys {
                #[inline(always)]
                fn mul_assign(&mut self, other: $base) {
                    self.0 *= other;
                }
            }

            impl ::std::ops::DivAssign<$base> for $tys {
                #[inline(always)]
                fn div_assign(&mut self, other: $base) {
                    self.0 /= other;
                }
            }
        )*

        $(
            impl ::std::ops::Mul<$b> for $a {
                type Output = $c;

                #[inline(always)]
                fn mul(self, other: $b) -> $c {
                    $c(self.value() * other.value())
                }
            }

            impl ::std::ops::Mul<$a> for $b {
                type Output = $c;

                #[inline(always)]
                fn mul(self, other: $a) -> $c {
                    $c(self.value() * other.value())
                }
            }

            impl ::std::ops::Div<$b> for $c {
                type Output = $a;

                #[inline(always)]
                fn div(self, other: $b) -> $a {
                    $a(self.value() / other.value())
                }
            }

            impl ::std::ops::Div<$a> for $c {
                type Output = $b;

                #[inline(always)]
                fn div(self, other: $a) -> $b {
                    $b(self.value() / other.value())
                }
            }
        )*
    };
}

#[cfg(test)]
mod tests {
    units! {
        Blanket(std::fmt::Debug + Clone + Copy + Default + PartialEq + PartialOrd);

        #[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd)] f64:
        Accel; Veloc; Length; Time; Mass; Force; Energy;

        <Accel> * <Time> = Veloc;
        <Veloc> * <Time> = Length;
        <Mass> * <Accel> = Force;
        <Force> * <Length> = Energy;
    }
}