1 | use core::num::Wrapping; |
2 | use core::ops::{Add, Mul, Neg, Shl, Shr, Sub}; |
3 | |
4 | macro_rules! wrapping_impl { |
5 | ($trait_name:ident, $method:ident, $t:ty) => { |
6 | impl $trait_name for $t { |
7 | #[inline] |
8 | fn $method(&self, v: &Self) -> Self { |
9 | <$t>::$method(*self, *v) |
10 | } |
11 | } |
12 | }; |
13 | ($trait_name:ident, $method:ident, $t:ty, $rhs:ty) => { |
14 | impl $trait_name<$rhs> for $t { |
15 | #[inline] |
16 | fn $method(&self, v: &$rhs) -> Self { |
17 | <$t>::$method(*self, *v) |
18 | } |
19 | } |
20 | }; |
21 | } |
22 | |
23 | /// Performs addition that wraps around on overflow. |
24 | pub trait WrappingAdd: Sized + Add<Self, Output = Self> { |
25 | /// Wrapping (modular) addition. Computes `self + other`, wrapping around at the boundary of |
26 | /// the type. |
27 | fn wrapping_add(&self, v: &Self) -> Self; |
28 | } |
29 | |
30 | wrapping_impl!(WrappingAdd, wrapping_add, u8); |
31 | wrapping_impl!(WrappingAdd, wrapping_add, u16); |
32 | wrapping_impl!(WrappingAdd, wrapping_add, u32); |
33 | wrapping_impl!(WrappingAdd, wrapping_add, u64); |
34 | wrapping_impl!(WrappingAdd, wrapping_add, usize); |
35 | wrapping_impl!(WrappingAdd, wrapping_add, u128); |
36 | |
37 | wrapping_impl!(WrappingAdd, wrapping_add, i8); |
38 | wrapping_impl!(WrappingAdd, wrapping_add, i16); |
39 | wrapping_impl!(WrappingAdd, wrapping_add, i32); |
40 | wrapping_impl!(WrappingAdd, wrapping_add, i64); |
41 | wrapping_impl!(WrappingAdd, wrapping_add, isize); |
42 | wrapping_impl!(WrappingAdd, wrapping_add, i128); |
43 | |
44 | /// Performs subtraction that wraps around on overflow. |
45 | pub trait WrappingSub: Sized + Sub<Self, Output = Self> { |
46 | /// Wrapping (modular) subtraction. Computes `self - other`, wrapping around at the boundary |
47 | /// of the type. |
48 | fn wrapping_sub(&self, v: &Self) -> Self; |
49 | } |
50 | |
51 | wrapping_impl!(WrappingSub, wrapping_sub, u8); |
52 | wrapping_impl!(WrappingSub, wrapping_sub, u16); |
53 | wrapping_impl!(WrappingSub, wrapping_sub, u32); |
54 | wrapping_impl!(WrappingSub, wrapping_sub, u64); |
55 | wrapping_impl!(WrappingSub, wrapping_sub, usize); |
56 | wrapping_impl!(WrappingSub, wrapping_sub, u128); |
57 | |
58 | wrapping_impl!(WrappingSub, wrapping_sub, i8); |
59 | wrapping_impl!(WrappingSub, wrapping_sub, i16); |
60 | wrapping_impl!(WrappingSub, wrapping_sub, i32); |
61 | wrapping_impl!(WrappingSub, wrapping_sub, i64); |
62 | wrapping_impl!(WrappingSub, wrapping_sub, isize); |
63 | wrapping_impl!(WrappingSub, wrapping_sub, i128); |
64 | |
65 | /// Performs multiplication that wraps around on overflow. |
66 | pub trait WrappingMul: Sized + Mul<Self, Output = Self> { |
67 | /// Wrapping (modular) multiplication. Computes `self * other`, wrapping around at the boundary |
68 | /// of the type. |
69 | fn wrapping_mul(&self, v: &Self) -> Self; |
70 | } |
71 | |
72 | wrapping_impl!(WrappingMul, wrapping_mul, u8); |
73 | wrapping_impl!(WrappingMul, wrapping_mul, u16); |
74 | wrapping_impl!(WrappingMul, wrapping_mul, u32); |
75 | wrapping_impl!(WrappingMul, wrapping_mul, u64); |
76 | wrapping_impl!(WrappingMul, wrapping_mul, usize); |
77 | wrapping_impl!(WrappingMul, wrapping_mul, u128); |
78 | |
79 | wrapping_impl!(WrappingMul, wrapping_mul, i8); |
80 | wrapping_impl!(WrappingMul, wrapping_mul, i16); |
81 | wrapping_impl!(WrappingMul, wrapping_mul, i32); |
82 | wrapping_impl!(WrappingMul, wrapping_mul, i64); |
83 | wrapping_impl!(WrappingMul, wrapping_mul, isize); |
84 | wrapping_impl!(WrappingMul, wrapping_mul, i128); |
85 | |
86 | macro_rules! wrapping_unary_impl { |
87 | ($trait_name:ident, $method:ident, $t:ty) => { |
88 | impl $trait_name for $t { |
89 | #[inline] |
90 | fn $method(&self) -> $t { |
91 | <$t>::$method(*self) |
92 | } |
93 | } |
94 | }; |
95 | } |
96 | |
97 | /// Performs a negation that does not panic. |
98 | pub trait WrappingNeg: Sized { |
99 | /// Wrapping (modular) negation. Computes `-self`, |
100 | /// wrapping around at the boundary of the type. |
101 | /// |
102 | /// Since unsigned types do not have negative equivalents |
103 | /// all applications of this function will wrap (except for `-0`). |
104 | /// For values smaller than the corresponding signed type's maximum |
105 | /// the result is the same as casting the corresponding signed value. |
106 | /// Any larger values are equivalent to `MAX + 1 - (val - MAX - 1)` where |
107 | /// `MAX` is the corresponding signed type's maximum. |
108 | /// |
109 | /// ``` |
110 | /// use num_traits::WrappingNeg; |
111 | /// |
112 | /// assert_eq!(100i8.wrapping_neg(), -100); |
113 | /// assert_eq!((-100i8).wrapping_neg(), 100); |
114 | /// assert_eq!((-128i8).wrapping_neg(), -128); // wrapped! |
115 | /// ``` |
116 | fn wrapping_neg(&self) -> Self; |
117 | } |
118 | |
119 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, u8); |
120 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, u16); |
121 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, u32); |
122 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, u64); |
123 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, usize); |
124 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, u128); |
125 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, i8); |
126 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, i16); |
127 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, i32); |
128 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, i64); |
129 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, isize); |
130 | wrapping_unary_impl!(WrappingNeg, wrapping_neg, i128); |
131 | |
132 | macro_rules! wrapping_shift_impl { |
133 | ($trait_name:ident, $method:ident, $t:ty) => { |
134 | impl $trait_name for $t { |
135 | #[inline] |
136 | fn $method(&self, rhs: u32) -> $t { |
137 | <$t>::$method(*self, rhs) |
138 | } |
139 | } |
140 | }; |
141 | } |
142 | |
143 | /// Performs a left shift that does not panic. |
144 | pub trait WrappingShl: Sized + Shl<usize, Output = Self> { |
145 | /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, |
146 | /// where `mask` removes any high order bits of `rhs` that would |
147 | /// cause the shift to exceed the bitwidth of the type. |
148 | /// |
149 | /// ``` |
150 | /// use num_traits::WrappingShl; |
151 | /// |
152 | /// let x: u16 = 0x0001; |
153 | /// |
154 | /// assert_eq!(WrappingShl::wrapping_shl(&x, 0), 0x0001); |
155 | /// assert_eq!(WrappingShl::wrapping_shl(&x, 1), 0x0002); |
156 | /// assert_eq!(WrappingShl::wrapping_shl(&x, 15), 0x8000); |
157 | /// assert_eq!(WrappingShl::wrapping_shl(&x, 16), 0x0001); |
158 | /// ``` |
159 | fn wrapping_shl(&self, rhs: u32) -> Self; |
160 | } |
161 | |
162 | wrapping_shift_impl!(WrappingShl, wrapping_shl, u8); |
163 | wrapping_shift_impl!(WrappingShl, wrapping_shl, u16); |
164 | wrapping_shift_impl!(WrappingShl, wrapping_shl, u32); |
165 | wrapping_shift_impl!(WrappingShl, wrapping_shl, u64); |
166 | wrapping_shift_impl!(WrappingShl, wrapping_shl, usize); |
167 | wrapping_shift_impl!(WrappingShl, wrapping_shl, u128); |
168 | |
169 | wrapping_shift_impl!(WrappingShl, wrapping_shl, i8); |
170 | wrapping_shift_impl!(WrappingShl, wrapping_shl, i16); |
171 | wrapping_shift_impl!(WrappingShl, wrapping_shl, i32); |
172 | wrapping_shift_impl!(WrappingShl, wrapping_shl, i64); |
173 | wrapping_shift_impl!(WrappingShl, wrapping_shl, isize); |
174 | wrapping_shift_impl!(WrappingShl, wrapping_shl, i128); |
175 | |
176 | /// Performs a right shift that does not panic. |
177 | pub trait WrappingShr: Sized + Shr<usize, Output = Self> { |
178 | /// Panic-free bitwise shift-right; yields `self >> mask(rhs)`, |
179 | /// where `mask` removes any high order bits of `rhs` that would |
180 | /// cause the shift to exceed the bitwidth of the type. |
181 | /// |
182 | /// ``` |
183 | /// use num_traits::WrappingShr; |
184 | /// |
185 | /// let x: u16 = 0x8000; |
186 | /// |
187 | /// assert_eq!(WrappingShr::wrapping_shr(&x, 0), 0x8000); |
188 | /// assert_eq!(WrappingShr::wrapping_shr(&x, 1), 0x4000); |
189 | /// assert_eq!(WrappingShr::wrapping_shr(&x, 15), 0x0001); |
190 | /// assert_eq!(WrappingShr::wrapping_shr(&x, 16), 0x8000); |
191 | /// ``` |
192 | fn wrapping_shr(&self, rhs: u32) -> Self; |
193 | } |
194 | |
195 | wrapping_shift_impl!(WrappingShr, wrapping_shr, u8); |
196 | wrapping_shift_impl!(WrappingShr, wrapping_shr, u16); |
197 | wrapping_shift_impl!(WrappingShr, wrapping_shr, u32); |
198 | wrapping_shift_impl!(WrappingShr, wrapping_shr, u64); |
199 | wrapping_shift_impl!(WrappingShr, wrapping_shr, usize); |
200 | wrapping_shift_impl!(WrappingShr, wrapping_shr, u128); |
201 | |
202 | wrapping_shift_impl!(WrappingShr, wrapping_shr, i8); |
203 | wrapping_shift_impl!(WrappingShr, wrapping_shr, i16); |
204 | wrapping_shift_impl!(WrappingShr, wrapping_shr, i32); |
205 | wrapping_shift_impl!(WrappingShr, wrapping_shr, i64); |
206 | wrapping_shift_impl!(WrappingShr, wrapping_shr, isize); |
207 | wrapping_shift_impl!(WrappingShr, wrapping_shr, i128); |
208 | |
209 | // Well this is a bit funny, but all the more appropriate. |
210 | impl<T: WrappingAdd> WrappingAdd for Wrapping<T> |
211 | where |
212 | Wrapping<T>: Add<Output = Wrapping<T>>, |
213 | { |
214 | fn wrapping_add(&self, v: &Self) -> Self { |
215 | Wrapping(self.0.wrapping_add(&v.0)) |
216 | } |
217 | } |
218 | impl<T: WrappingSub> WrappingSub for Wrapping<T> |
219 | where |
220 | Wrapping<T>: Sub<Output = Wrapping<T>>, |
221 | { |
222 | fn wrapping_sub(&self, v: &Self) -> Self { |
223 | Wrapping(self.0.wrapping_sub(&v.0)) |
224 | } |
225 | } |
226 | impl<T: WrappingMul> WrappingMul for Wrapping<T> |
227 | where |
228 | Wrapping<T>: Mul<Output = Wrapping<T>>, |
229 | { |
230 | fn wrapping_mul(&self, v: &Self) -> Self { |
231 | Wrapping(self.0.wrapping_mul(&v.0)) |
232 | } |
233 | } |
234 | impl<T: WrappingNeg> WrappingNeg for Wrapping<T> |
235 | where |
236 | Wrapping<T>: Neg<Output = Wrapping<T>>, |
237 | { |
238 | fn wrapping_neg(&self) -> Self { |
239 | Wrapping(self.0.wrapping_neg()) |
240 | } |
241 | } |
242 | impl<T: WrappingShl> WrappingShl for Wrapping<T> |
243 | where |
244 | Wrapping<T>: Shl<usize, Output = Wrapping<T>>, |
245 | { |
246 | fn wrapping_shl(&self, rhs: u32) -> Self { |
247 | Wrapping(self.0.wrapping_shl(rhs)) |
248 | } |
249 | } |
250 | impl<T: WrappingShr> WrappingShr for Wrapping<T> |
251 | where |
252 | Wrapping<T>: Shr<usize, Output = Wrapping<T>>, |
253 | { |
254 | fn wrapping_shr(&self, rhs: u32) -> Self { |
255 | Wrapping(self.0.wrapping_shr(rhs)) |
256 | } |
257 | } |
258 | |
259 | #[test] |
260 | fn test_wrapping_traits() { |
261 | fn wrapping_add<T: WrappingAdd>(a: T, b: T) -> T { |
262 | a.wrapping_add(&b) |
263 | } |
264 | fn wrapping_sub<T: WrappingSub>(a: T, b: T) -> T { |
265 | a.wrapping_sub(&b) |
266 | } |
267 | fn wrapping_mul<T: WrappingMul>(a: T, b: T) -> T { |
268 | a.wrapping_mul(&b) |
269 | } |
270 | fn wrapping_neg<T: WrappingNeg>(a: T) -> T { |
271 | a.wrapping_neg() |
272 | } |
273 | fn wrapping_shl<T: WrappingShl>(a: T, b: u32) -> T { |
274 | a.wrapping_shl(b) |
275 | } |
276 | fn wrapping_shr<T: WrappingShr>(a: T, b: u32) -> T { |
277 | a.wrapping_shr(b) |
278 | } |
279 | assert_eq!(wrapping_add(255, 1), 0u8); |
280 | assert_eq!(wrapping_sub(0, 1), 255u8); |
281 | assert_eq!(wrapping_mul(255, 2), 254u8); |
282 | assert_eq!(wrapping_neg(255), 1u8); |
283 | assert_eq!(wrapping_shl(255, 8), 255u8); |
284 | assert_eq!(wrapping_shr(255, 8), 255u8); |
285 | assert_eq!(wrapping_add(255, 1), (Wrapping(255u8) + Wrapping(1u8)).0); |
286 | assert_eq!(wrapping_sub(0, 1), (Wrapping(0u8) - Wrapping(1u8)).0); |
287 | assert_eq!(wrapping_mul(255, 2), (Wrapping(255u8) * Wrapping(2u8)).0); |
288 | assert_eq!(wrapping_neg(255), (-Wrapping(255u8)).0); |
289 | assert_eq!(wrapping_shl(255, 8), (Wrapping(255u8) << 8).0); |
290 | assert_eq!(wrapping_shr(255, 8), (Wrapping(255u8) >> 8).0); |
291 | } |
292 | |
293 | #[test] |
294 | fn wrapping_is_wrappingadd() { |
295 | fn require_wrappingadd<T: WrappingAdd>(_: &T) {} |
296 | require_wrappingadd(&Wrapping(42)); |
297 | } |
298 | |
299 | #[test] |
300 | fn wrapping_is_wrappingsub() { |
301 | fn require_wrappingsub<T: WrappingSub>(_: &T) {} |
302 | require_wrappingsub(&Wrapping(42)); |
303 | } |
304 | |
305 | #[test] |
306 | fn wrapping_is_wrappingmul() { |
307 | fn require_wrappingmul<T: WrappingMul>(_: &T) {} |
308 | require_wrappingmul(&Wrapping(42)); |
309 | } |
310 | |
311 | #[test] |
312 | fn wrapping_is_wrappingneg() { |
313 | fn require_wrappingneg<T: WrappingNeg>(_: &T) {} |
314 | require_wrappingneg(&Wrapping(42)); |
315 | } |
316 | |
317 | #[test] |
318 | fn wrapping_is_wrappingshl() { |
319 | fn require_wrappingshl<T: WrappingShl>(_: &T) {} |
320 | require_wrappingshl(&Wrapping(42)); |
321 | } |
322 | |
323 | #[test] |
324 | fn wrapping_is_wrappingshr() { |
325 | fn require_wrappingshr<T: WrappingShr>(_: &T) {} |
326 | require_wrappingshr(&Wrapping(42)); |
327 | } |
328 | |