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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! Simple macros to declare 'type checked' aliases for integers and floats.
//!
//! For usage, see float_alias and int_alias page.

/// A simple macro to Declare alias for Integer types and implement arithmetics.
///
/// Concretely, it implements for the alias type, Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, MulAssign, DivAssign, RemAssign.
/// And Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord are Derived.
///
/// You can access its value by deref.
/// # Examples
///
/// ```
/// #[macro_use]
/// extern crate num_alias;
/// fn main() {
///     int_alias!(Val, i32);
///     let a = Val(5);
///     let b = Val(4);
///     let c = a * b;
/// }
/// ```
#[macro_export]
macro_rules! int_alias {
    ($alias:ident, $type:ty) => {
        #[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
        pub struct $alias($type);
        impl ::std::ops::Deref for $alias {
            type Target = $type;
            fn deref(&self) -> &$type {
                &self.0
            }
        }
        impl ::std::ops::Add for $alias {
            type Output = $alias;
            fn add(self, other: $alias) -> $alias {
                $alias(self.0 + other.0)
            }
        }
        impl ::std::ops::Sub for $alias {
            type Output = $alias;
            fn sub(self, other: $alias) -> $alias {
                $alias(self.0 - other.0)
            }
        }
        impl ::std::ops::Mul for $alias {
            type Output = $alias;
            fn mul(self, other: $alias) -> $alias {
                $alias(self.0 * other.0)
            }
        }
        impl ::std::ops::Div for $alias {
            type Output = $alias;
            fn div(self, other: $alias) -> $alias {
                $alias(self.0 / other.0)
            }
        }
        impl ::std::ops::Rem for $alias {
            type Output = $alias;
            fn rem(self, other: $alias) -> $alias {
                $alias(self.0 % other.0)
            }
        }
        impl ::std::ops::AddAssign for $alias {
            fn add_assign(&mut self, other: $alias) {
                *self = *self + other
            }
        }
        impl ::std::ops::SubAssign for $alias {
            fn sub_assign(&mut self, other: $alias) {
                *self = *self - other
            }
        }
        impl ::std::ops::MulAssign for $alias {
            fn mul_assign(&mut self, other: $alias) {
                *self = *self * other
            }
        }
        impl ::std::ops::DivAssign for $alias {
            fn div_assign(&mut self, other: $alias) {
                *self = *self / other
            }
        }
        impl ::std::ops::RemAssign for $alias {
            fn rem_assign(&mut self, other: $alias) {
                *self = *self % other
            }
        }
    }
}

/// A simple macro to Declare alias for Integer types and implement arithmetics.
///
/// Concretely, it implements for the alias type, Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, MulAssign, DivAssign, RemAssign.
/// And Debug, Copy, Clone, PartialEq, PartialOrd are Derived.
///
/// You can access its value by deref.
/// # Examples
///
/// ```
/// #[macro_use]
/// extern crate num_alias;
/// fn main() {
///     float_alias!(Fval, f64);
///     let a = Fval(5.0);
///     let b = Fval(4.0);
///     let c = (a * b).sqrt();
/// }
/// ```
#[macro_export]
macro_rules! float_alias {
    ($alias:ident, $type:ty) => {
        #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
        pub struct $alias($type);
        impl ::std::ops::Deref for $alias {
            type Target = $type;
            fn deref(&self) -> &$type {
                &self.0
            }
        }
        impl ::std::ops::Add for $alias {
            type Output = $alias;
            fn add(self, other: $alias) -> $alias {
                $alias(self.0 + other.0)
            }
        }
        impl ::std::ops::Sub for $alias {
            type Output = $alias;
            fn sub(self, other: $alias) -> $alias {
                $alias(self.0 - other.0)
            }
        }
        impl ::std::ops::Mul for $alias {
            type Output = $alias;
            fn mul(self, other: $alias) -> $alias {
                $alias(self.0 * other.0)
            }
        }
        impl ::std::ops::Div for $alias {
            type Output = $alias;
            fn div(self, other: $alias) -> $alias {
                $alias(self.0 / other.0)
            }
        }
        impl ::std::ops::Rem for $alias {
            type Output = $alias;
            fn rem(self, other: $alias) -> $alias {
                $alias(self.0 % other.0)
            }
        }
        impl ::std::ops::AddAssign for $alias {
            fn add_assign(&mut self, other: $alias) {
                *self = *self + other
            }
        }
        impl ::std::ops::SubAssign for $alias {
            fn sub_assign(&mut self, other: $alias) {
                *self = *self - other
            }
        }
        impl ::std::ops::MulAssign for $alias {
            fn mul_assign(&mut self, other: $alias) {
                *self = *self * other
            }
        }
        impl ::std::ops::DivAssign for $alias {
            fn div_assign(&mut self, other: $alias) {
                *self = *self / other
            }
        }
        impl ::std::ops::RemAssign for $alias {
            fn rem_assign(&mut self, other: $alias) {
                *self = *self % other
            }
        }
    }
}
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        int_alias!(Val, i32);
        float_alias!(Fval, f64);
        let a = Val(20);
        assert_eq!(*a, 20);
        let b = Val(6);
        assert_eq!(a + b, Val(26));
        assert_eq!(a - b, Val(14));
        assert_eq!(a * b, Val(120));
        assert_eq!(a / b, Val(3));
        assert_eq!(a % b, Val(2));
        let a = Fval(20.0);
        let b = Fval(6.0);
        assert_eq!(a + b, Fval(26.0));
        assert_eq!(a - b, Fval(14.0));
        assert_eq!(a * b, Fval(120.0));
        assert_eq!(a / b, Fval(20.0 / 6.0));
        assert_eq!(a % b, Fval(2.0));
        assert_eq!(a.sqrt(), 20.0f64.sqrt());
    }
}