1use core::num::Wrapping;
2use core::ops::{Add, Mul, Neg, Shl, Shr, Sub};
3
4macro_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.
24pub 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
30wrapping_impl!(WrappingAdd, wrapping_add, u8);
31wrapping_impl!(WrappingAdd, wrapping_add, u16);
32wrapping_impl!(WrappingAdd, wrapping_add, u32);
33wrapping_impl!(WrappingAdd, wrapping_add, u64);
34wrapping_impl!(WrappingAdd, wrapping_add, usize);
35wrapping_impl!(WrappingAdd, wrapping_add, u128);
36
37wrapping_impl!(WrappingAdd, wrapping_add, i8);
38wrapping_impl!(WrappingAdd, wrapping_add, i16);
39wrapping_impl!(WrappingAdd, wrapping_add, i32);
40wrapping_impl!(WrappingAdd, wrapping_add, i64);
41wrapping_impl!(WrappingAdd, wrapping_add, isize);
42wrapping_impl!(WrappingAdd, wrapping_add, i128);
43
44/// Performs subtraction that wraps around on overflow.
45pub 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
51wrapping_impl!(WrappingSub, wrapping_sub, u8);
52wrapping_impl!(WrappingSub, wrapping_sub, u16);
53wrapping_impl!(WrappingSub, wrapping_sub, u32);
54wrapping_impl!(WrappingSub, wrapping_sub, u64);
55wrapping_impl!(WrappingSub, wrapping_sub, usize);
56wrapping_impl!(WrappingSub, wrapping_sub, u128);
57
58wrapping_impl!(WrappingSub, wrapping_sub, i8);
59wrapping_impl!(WrappingSub, wrapping_sub, i16);
60wrapping_impl!(WrappingSub, wrapping_sub, i32);
61wrapping_impl!(WrappingSub, wrapping_sub, i64);
62wrapping_impl!(WrappingSub, wrapping_sub, isize);
63wrapping_impl!(WrappingSub, wrapping_sub, i128);
64
65/// Performs multiplication that wraps around on overflow.
66pub 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
72wrapping_impl!(WrappingMul, wrapping_mul, u8);
73wrapping_impl!(WrappingMul, wrapping_mul, u16);
74wrapping_impl!(WrappingMul, wrapping_mul, u32);
75wrapping_impl!(WrappingMul, wrapping_mul, u64);
76wrapping_impl!(WrappingMul, wrapping_mul, usize);
77wrapping_impl!(WrappingMul, wrapping_mul, u128);
78
79wrapping_impl!(WrappingMul, wrapping_mul, i8);
80wrapping_impl!(WrappingMul, wrapping_mul, i16);
81wrapping_impl!(WrappingMul, wrapping_mul, i32);
82wrapping_impl!(WrappingMul, wrapping_mul, i64);
83wrapping_impl!(WrappingMul, wrapping_mul, isize);
84wrapping_impl!(WrappingMul, wrapping_mul, i128);
85
86macro_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.
98pub 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
119wrapping_unary_impl!(WrappingNeg, wrapping_neg, u8);
120wrapping_unary_impl!(WrappingNeg, wrapping_neg, u16);
121wrapping_unary_impl!(WrappingNeg, wrapping_neg, u32);
122wrapping_unary_impl!(WrappingNeg, wrapping_neg, u64);
123wrapping_unary_impl!(WrappingNeg, wrapping_neg, usize);
124wrapping_unary_impl!(WrappingNeg, wrapping_neg, u128);
125wrapping_unary_impl!(WrappingNeg, wrapping_neg, i8);
126wrapping_unary_impl!(WrappingNeg, wrapping_neg, i16);
127wrapping_unary_impl!(WrappingNeg, wrapping_neg, i32);
128wrapping_unary_impl!(WrappingNeg, wrapping_neg, i64);
129wrapping_unary_impl!(WrappingNeg, wrapping_neg, isize);
130wrapping_unary_impl!(WrappingNeg, wrapping_neg, i128);
131
132macro_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.
144pub 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
162wrapping_shift_impl!(WrappingShl, wrapping_shl, u8);
163wrapping_shift_impl!(WrappingShl, wrapping_shl, u16);
164wrapping_shift_impl!(WrappingShl, wrapping_shl, u32);
165wrapping_shift_impl!(WrappingShl, wrapping_shl, u64);
166wrapping_shift_impl!(WrappingShl, wrapping_shl, usize);
167wrapping_shift_impl!(WrappingShl, wrapping_shl, u128);
168
169wrapping_shift_impl!(WrappingShl, wrapping_shl, i8);
170wrapping_shift_impl!(WrappingShl, wrapping_shl, i16);
171wrapping_shift_impl!(WrappingShl, wrapping_shl, i32);
172wrapping_shift_impl!(WrappingShl, wrapping_shl, i64);
173wrapping_shift_impl!(WrappingShl, wrapping_shl, isize);
174wrapping_shift_impl!(WrappingShl, wrapping_shl, i128);
175
176/// Performs a right shift that does not panic.
177pub 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
195wrapping_shift_impl!(WrappingShr, wrapping_shr, u8);
196wrapping_shift_impl!(WrappingShr, wrapping_shr, u16);
197wrapping_shift_impl!(WrappingShr, wrapping_shr, u32);
198wrapping_shift_impl!(WrappingShr, wrapping_shr, u64);
199wrapping_shift_impl!(WrappingShr, wrapping_shr, usize);
200wrapping_shift_impl!(WrappingShr, wrapping_shr, u128);
201
202wrapping_shift_impl!(WrappingShr, wrapping_shr, i8);
203wrapping_shift_impl!(WrappingShr, wrapping_shr, i16);
204wrapping_shift_impl!(WrappingShr, wrapping_shr, i32);
205wrapping_shift_impl!(WrappingShr, wrapping_shr, i64);
206wrapping_shift_impl!(WrappingShr, wrapping_shr, isize);
207wrapping_shift_impl!(WrappingShr, wrapping_shr, i128);
208
209// Well this is a bit funny, but all the more appropriate.
210impl<T: WrappingAdd> WrappingAdd for Wrapping<T>
211where
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}
218impl<T: WrappingSub> WrappingSub for Wrapping<T>
219where
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}
226impl<T: WrappingMul> WrappingMul for Wrapping<T>
227where
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}
234impl<T: WrappingNeg> WrappingNeg for Wrapping<T>
235where
236 Wrapping<T>: Neg<Output = Wrapping<T>>,
237{
238 fn wrapping_neg(&self) -> Self {
239 Wrapping(self.0.wrapping_neg())
240 }
241}
242impl<T: WrappingShl> WrappingShl for Wrapping<T>
243where
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}
250impl<T: WrappingShr> WrappingShr for Wrapping<T>
251where
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]
260fn 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]
294fn wrapping_is_wrappingadd() {
295 fn require_wrappingadd<T: WrappingAdd>(_: &T) {}
296 require_wrappingadd(&Wrapping(42));
297}
298
299#[test]
300fn wrapping_is_wrappingsub() {
301 fn require_wrappingsub<T: WrappingSub>(_: &T) {}
302 require_wrappingsub(&Wrapping(42));
303}
304
305#[test]
306fn wrapping_is_wrappingmul() {
307 fn require_wrappingmul<T: WrappingMul>(_: &T) {}
308 require_wrappingmul(&Wrapping(42));
309}
310
311#[test]
312fn wrapping_is_wrappingneg() {
313 fn require_wrappingneg<T: WrappingNeg>(_: &T) {}
314 require_wrappingneg(&Wrapping(42));
315}
316
317#[test]
318fn wrapping_is_wrappingshl() {
319 fn require_wrappingshl<T: WrappingShl>(_: &T) {}
320 require_wrappingshl(&Wrapping(42));
321}
322
323#[test]
324fn wrapping_is_wrappingshr() {
325 fn require_wrappingshr<T: WrappingShr>(_: &T) {}
326 require_wrappingshr(&Wrapping(42));
327}
328