Skip to main content

core/num/
wrapping.rs

1//! Definitions of `Wrapping<T>`.
2
3use crate::fmt;
4use crate::ops::{
5    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
6    Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
7};
8
9/// Provides intentionally-wrapped arithmetic on `T`.
10///
11/// Operations like `+` on `u32` values are intended to never overflow,
12/// and in some debug configurations overflow is detected and results
13/// in a panic. While most arithmetic falls into this category, some
14/// code explicitly expects and relies upon modular arithmetic (e.g.,
15/// hashing).
16///
17/// Wrapping arithmetic can be achieved either through methods like
18/// `wrapping_add`, or through the `Wrapping<T>` type, which says that
19/// all standard arithmetic operations on the underlying value are
20/// intended to have wrapping semantics.
21///
22/// The underlying value can be retrieved through the `.0` index of the
23/// `Wrapping` tuple.
24///
25/// # Examples
26///
27/// ```
28/// use std::num::Wrapping;
29///
30/// let zero = Wrapping(0u32);
31/// let one = Wrapping(1u32);
32///
33/// assert_eq!(u32::MAX, (zero - one).0);
34/// ```
35///
36/// # Layout
37///
38/// `Wrapping<T>` is guaranteed to have the same layout and ABI as `T`.
39#[stable(feature = "rust1", since = "1.0.0")]
40#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Hash)]
41#[repr(transparent)]
42#[rustc_diagnostic_item = "Wrapping"]
43pub struct Wrapping<T>(#[stable(feature = "rust1", since = "1.0.0")] pub T);
44
45#[stable(feature = "rust1", since = "1.0.0")]
46impl<T: fmt::Debug> fmt::Debug for Wrapping<T> {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        self.0.fmt(f)
49    }
50}
51
52#[stable(feature = "wrapping_display", since = "1.10.0")]
53impl<T: fmt::Display> fmt::Display for Wrapping<T> {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        self.0.fmt(f)
56    }
57}
58
59#[stable(feature = "wrapping_fmt", since = "1.11.0")]
60impl<T: fmt::Binary> fmt::Binary for Wrapping<T> {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        self.0.fmt(f)
63    }
64}
65
66#[stable(feature = "wrapping_fmt", since = "1.11.0")]
67impl<T: fmt::Octal> fmt::Octal for Wrapping<T> {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        self.0.fmt(f)
70    }
71}
72
73#[stable(feature = "wrapping_fmt", since = "1.11.0")]
74impl<T: fmt::LowerHex> fmt::LowerHex for Wrapping<T> {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        self.0.fmt(f)
77    }
78}
79
80#[stable(feature = "wrapping_fmt", since = "1.11.0")]
81impl<T: fmt::UpperHex> fmt::UpperHex for Wrapping<T> {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        self.0.fmt(f)
84    }
85}
86
87#[allow(unused_macros)]
88macro_rules! sh_impl_signed {
89    ($t:ident, $f:ident) => {
90        #[stable(feature = "rust1", since = "1.0.0")]
91        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
92        impl const Shl<$f> for Wrapping<$t> {
93            type Output = Wrapping<$t>;
94
95            #[inline]
96            #[ferrocene::prevalidated]
97            fn shl(self, other: $f) -> Wrapping<$t> {
98                if other < 0 {
99                    Wrapping(self.0.wrapping_shr(-other as u32))
100                } else {
101                    Wrapping(self.0.wrapping_shl(other as u32))
102                }
103            }
104        }
105        forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
106        #[stable(feature = "wrapping_ref_ops", since = "1.39.0")]
107        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
108
109        #[stable(feature = "op_assign_traits", since = "1.8.0")]
110        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
111        impl const ShlAssign<$f> for Wrapping<$t> {
112            #[inline]
113            #[ferrocene::prevalidated]
114            fn shl_assign(&mut self, other: $f) {
115                *self = *self << other;
116            }
117        }
118        forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f,
119        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
120        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
121
122        #[stable(feature = "rust1", since = "1.0.0")]
123        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
124        impl const Shr<$f> for Wrapping<$t> {
125            type Output = Wrapping<$t>;
126
127            #[inline]
128            #[ferrocene::prevalidated]
129            fn shr(self, other: $f) -> Wrapping<$t> {
130                if other < 0 {
131                    Wrapping(self.0.wrapping_shl(-other as u32))
132                } else {
133                    Wrapping(self.0.wrapping_shr(other as u32))
134                }
135            }
136        }
137        forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
138        #[stable(feature = "wrapping_ref_ops", since = "1.39.0")]
139        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
140
141        #[stable(feature = "op_assign_traits", since = "1.8.0")]
142        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
143        impl const ShrAssign<$f> for Wrapping<$t> {
144            #[inline]
145            #[ferrocene::prevalidated]
146            fn shr_assign(&mut self, other: $f) {
147                *self = *self >> other;
148            }
149        }
150        forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f,
151        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
152        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
153    };
154}
155
156macro_rules! sh_impl_unsigned {
157    ($t:ident, $f:ident) => {
158        #[stable(feature = "rust1", since = "1.0.0")]
159        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
160        impl const Shl<$f> for Wrapping<$t> {
161            type Output = Wrapping<$t>;
162
163            #[inline]
164            #[ferrocene::prevalidated]
165            fn shl(self, other: $f) -> Wrapping<$t> {
166                Wrapping(self.0.wrapping_shl(other as u32))
167            }
168        }
169        forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
170        #[stable(feature = "wrapping_ref_ops", since = "1.39.0")]
171        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
172
173        #[stable(feature = "op_assign_traits", since = "1.8.0")]
174        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
175        impl const ShlAssign<$f> for Wrapping<$t> {
176            #[inline]
177            #[ferrocene::prevalidated]
178            fn shl_assign(&mut self, other: $f) {
179                *self = *self << other;
180            }
181        }
182        forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f,
183        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
184        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
185
186        #[stable(feature = "rust1", since = "1.0.0")]
187        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
188        impl const Shr<$f> for Wrapping<$t> {
189            type Output = Wrapping<$t>;
190
191            #[inline]
192            #[ferrocene::prevalidated]
193            fn shr(self, other: $f) -> Wrapping<$t> {
194                Wrapping(self.0.wrapping_shr(other as u32))
195            }
196        }
197        forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
198        #[stable(feature = "wrapping_ref_ops", since = "1.39.0")]
199        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
200
201        #[stable(feature = "op_assign_traits", since = "1.8.0")]
202        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
203        impl const ShrAssign<$f> for Wrapping<$t> {
204            #[inline]
205            #[ferrocene::prevalidated]
206            fn shr_assign(&mut self, other: $f) {
207                *self = *self >> other;
208            }
209        }
210        forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f,
211        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
212        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
213    };
214}
215
216// FIXME (#23545): uncomment the remaining impls
217macro_rules! sh_impl_all {
218    ($($t:ident)*) => ($(
219        //sh_impl_unsigned! { $t, u8 }
220        //sh_impl_unsigned! { $t, u16 }
221        //sh_impl_unsigned! { $t, u32 }
222        //sh_impl_unsigned! { $t, u64 }
223        //sh_impl_unsigned! { $t, u128 }
224        sh_impl_unsigned! { $t, usize }
225
226        //sh_impl_signed! { $t, i8 }
227        //sh_impl_signed! { $t, i16 }
228        //sh_impl_signed! { $t, i32 }
229        //sh_impl_signed! { $t, i64 }
230        //sh_impl_signed! { $t, i128 }
231        //sh_impl_signed! { $t, isize }
232    )*)
233}
234
235sh_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
236
237// FIXME(30524): impl Op<T> for Wrapping<T>, impl OpAssign<T> for Wrapping<T>
238macro_rules! wrapping_impl {
239    ($($t:ty)*) => ($(
240        #[stable(feature = "rust1", since = "1.0.0")]
241        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
242        impl const Add for Wrapping<$t> {
243            type Output = Wrapping<$t>;
244
245            #[inline]
246            #[ferrocene::prevalidated]
247            fn add(self, other: Wrapping<$t>) -> Wrapping<$t> {
248                Wrapping(self.0.wrapping_add(other.0))
249            }
250        }
251        forward_ref_binop! { impl Add, add for Wrapping<$t>, Wrapping<$t>,
252        #[stable(feature = "wrapping_ref", since = "1.14.0")]
253        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
254
255        #[stable(feature = "op_assign_traits", since = "1.8.0")]
256        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
257        impl const AddAssign for Wrapping<$t> {
258            #[inline]
259            #[ferrocene::prevalidated]
260            fn add_assign(&mut self, other: Wrapping<$t>) {
261                *self = *self + other;
262            }
263        }
264        forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, Wrapping<$t>,
265        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
266        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
267
268        #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")]
269        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
270        impl const AddAssign<$t> for Wrapping<$t> {
271            #[inline]
272            #[ferrocene::prevalidated]
273            fn add_assign(&mut self, other: $t) {
274                *self = *self + Wrapping(other);
275            }
276        }
277        forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, $t,
278        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
279        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
280
281        #[stable(feature = "rust1", since = "1.0.0")]
282        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
283        impl const Sub for Wrapping<$t> {
284            type Output = Wrapping<$t>;
285
286            #[inline]
287            #[ferrocene::prevalidated]
288            fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> {
289                Wrapping(self.0.wrapping_sub(other.0))
290            }
291        }
292        forward_ref_binop! { impl Sub, sub for Wrapping<$t>, Wrapping<$t>,
293        #[stable(feature = "wrapping_ref", since = "1.14.0")]
294        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
295
296        #[stable(feature = "op_assign_traits", since = "1.8.0")]
297        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
298        impl const SubAssign for Wrapping<$t> {
299            #[inline]
300            #[ferrocene::prevalidated]
301            fn sub_assign(&mut self, other: Wrapping<$t>) {
302                *self = *self - other;
303            }
304        }
305        forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, Wrapping<$t>,
306        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
307        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
308
309        #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")]
310        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
311        impl const SubAssign<$t> for Wrapping<$t> {
312            #[inline]
313            #[ferrocene::prevalidated]
314            fn sub_assign(&mut self, other: $t) {
315                *self = *self - Wrapping(other);
316            }
317        }
318        forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, $t,
319        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
320        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
321
322        #[stable(feature = "rust1", since = "1.0.0")]
323        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
324        impl const Mul for Wrapping<$t> {
325            type Output = Wrapping<$t>;
326
327            #[inline]
328            #[ferrocene::prevalidated]
329            fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> {
330                Wrapping(self.0.wrapping_mul(other.0))
331            }
332        }
333        forward_ref_binop! { impl Mul, mul for Wrapping<$t>, Wrapping<$t>,
334        #[stable(feature = "wrapping_ref", since = "1.14.0")]
335        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
336
337        #[stable(feature = "op_assign_traits", since = "1.8.0")]
338        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
339        impl const MulAssign for Wrapping<$t> {
340            #[inline]
341            #[ferrocene::prevalidated]
342            fn mul_assign(&mut self, other: Wrapping<$t>) {
343                *self = *self * other;
344            }
345        }
346        forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, Wrapping<$t>,
347        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
348        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
349
350        #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")]
351        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
352        impl const MulAssign<$t> for Wrapping<$t> {
353            #[inline]
354            #[ferrocene::prevalidated]
355            fn mul_assign(&mut self, other: $t) {
356                *self = *self * Wrapping(other);
357            }
358        }
359        forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, $t,
360        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
361        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
362
363        #[stable(feature = "wrapping_div", since = "1.3.0")]
364        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
365        impl const Div for Wrapping<$t> {
366            type Output = Wrapping<$t>;
367
368            #[inline]
369            #[ferrocene::prevalidated]
370            fn div(self, other: Wrapping<$t>) -> Wrapping<$t> {
371                Wrapping(self.0.wrapping_div(other.0))
372            }
373        }
374        forward_ref_binop! { impl Div, div for Wrapping<$t>, Wrapping<$t>,
375        #[stable(feature = "wrapping_ref", since = "1.14.0")]
376        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
377
378        #[stable(feature = "op_assign_traits", since = "1.8.0")]
379        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
380        impl const DivAssign for Wrapping<$t> {
381            #[inline]
382            #[ferrocene::prevalidated]
383            fn div_assign(&mut self, other: Wrapping<$t>) {
384                *self = *self / other;
385            }
386        }
387        forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, Wrapping<$t>,
388        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
389        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
390
391        #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")]
392        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
393        impl const DivAssign<$t> for Wrapping<$t> {
394            #[inline]
395            #[ferrocene::prevalidated]
396            fn div_assign(&mut self, other: $t) {
397                *self = *self / Wrapping(other);
398            }
399        }
400        forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, $t,
401        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
402        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
403
404        #[stable(feature = "wrapping_impls", since = "1.7.0")]
405        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
406        impl const Rem for Wrapping<$t> {
407            type Output = Wrapping<$t>;
408
409            #[inline]
410            #[ferrocene::prevalidated]
411            fn rem(self, other: Wrapping<$t>) -> Wrapping<$t> {
412                Wrapping(self.0.wrapping_rem(other.0))
413            }
414        }
415        forward_ref_binop! { impl Rem, rem for Wrapping<$t>, Wrapping<$t>,
416        #[stable(feature = "wrapping_ref", since = "1.14.0")]
417        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
418
419        #[stable(feature = "op_assign_traits", since = "1.8.0")]
420        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
421        impl const RemAssign for Wrapping<$t> {
422            #[inline]
423            #[ferrocene::prevalidated]
424            fn rem_assign(&mut self, other: Wrapping<$t>) {
425                *self = *self % other;
426            }
427        }
428        forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, Wrapping<$t>,
429        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
430        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
431
432        #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")]
433        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
434        impl const RemAssign<$t> for Wrapping<$t> {
435            #[inline]
436            #[ferrocene::prevalidated]
437            fn rem_assign(&mut self, other: $t) {
438                *self = *self % Wrapping(other);
439            }
440        }
441        forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, $t,
442        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
443        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
444
445        #[stable(feature = "rust1", since = "1.0.0")]
446        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
447        impl const Not for Wrapping<$t> {
448            type Output = Wrapping<$t>;
449
450            #[inline]
451            #[ferrocene::prevalidated]
452            fn not(self) -> Wrapping<$t> {
453                Wrapping(!self.0)
454            }
455        }
456        forward_ref_unop! { impl Not, not for Wrapping<$t>,
457        #[stable(feature = "wrapping_ref", since = "1.14.0")]
458        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
459
460        #[stable(feature = "rust1", since = "1.0.0")]
461        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
462        impl const BitXor for Wrapping<$t> {
463            type Output = Wrapping<$t>;
464
465            #[inline]
466            #[ferrocene::prevalidated]
467            fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> {
468                Wrapping(self.0 ^ other.0)
469            }
470        }
471        forward_ref_binop! { impl BitXor, bitxor for Wrapping<$t>, Wrapping<$t>,
472        #[stable(feature = "wrapping_ref", since = "1.14.0")]
473        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
474
475        #[stable(feature = "op_assign_traits", since = "1.8.0")]
476        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
477        impl const BitXorAssign for Wrapping<$t> {
478            #[inline]
479            #[ferrocene::prevalidated]
480            fn bitxor_assign(&mut self, other: Wrapping<$t>) {
481                *self = *self ^ other;
482            }
483        }
484        forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, Wrapping<$t>,
485        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
486        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
487
488        #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")]
489        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
490        impl const BitXorAssign<$t> for Wrapping<$t> {
491            #[inline]
492            #[ferrocene::prevalidated]
493            fn bitxor_assign(&mut self, other: $t) {
494                *self = *self ^ Wrapping(other);
495            }
496        }
497        forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, $t,
498        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
499        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
500
501        #[stable(feature = "rust1", since = "1.0.0")]
502        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
503        impl const BitOr for Wrapping<$t> {
504            type Output = Wrapping<$t>;
505
506            #[inline]
507            #[ferrocene::prevalidated]
508            fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> {
509                Wrapping(self.0 | other.0)
510            }
511        }
512        forward_ref_binop! { impl BitOr, bitor for Wrapping<$t>, Wrapping<$t>,
513        #[stable(feature = "wrapping_ref", since = "1.14.0")]
514        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
515
516        #[stable(feature = "op_assign_traits", since = "1.8.0")]
517        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
518        impl const BitOrAssign for Wrapping<$t> {
519            #[inline]
520            #[ferrocene::prevalidated]
521            fn bitor_assign(&mut self, other: Wrapping<$t>) {
522                *self = *self | other;
523            }
524        }
525        forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, Wrapping<$t>,
526        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
527        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
528
529        #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")]
530        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
531        impl const BitOrAssign<$t> for Wrapping<$t> {
532            #[inline]
533            #[ferrocene::prevalidated]
534            fn bitor_assign(&mut self, other: $t) {
535                *self = *self | Wrapping(other);
536            }
537        }
538        forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, $t,
539        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
540        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
541
542        #[stable(feature = "rust1", since = "1.0.0")]
543        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
544        impl const BitAnd for Wrapping<$t> {
545            type Output = Wrapping<$t>;
546
547            #[inline]
548            #[ferrocene::prevalidated]
549            fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> {
550                Wrapping(self.0 & other.0)
551            }
552        }
553        forward_ref_binop! { impl BitAnd, bitand for Wrapping<$t>, Wrapping<$t>,
554        #[stable(feature = "wrapping_ref", since = "1.14.0")]
555        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
556
557        #[stable(feature = "op_assign_traits", since = "1.8.0")]
558        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
559        impl const BitAndAssign for Wrapping<$t> {
560            #[inline]
561            #[ferrocene::prevalidated]
562            fn bitand_assign(&mut self, other: Wrapping<$t>) {
563                *self = *self & other;
564            }
565        }
566        forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, Wrapping<$t>,
567        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
568        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
569
570        #[stable(feature = "wrapping_int_assign_impl", since = "1.60.0")]
571        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
572        impl const BitAndAssign<$t> for Wrapping<$t> {
573            #[inline]
574            #[ferrocene::prevalidated]
575            fn bitand_assign(&mut self, other: $t) {
576                *self = *self & Wrapping(other);
577            }
578        }
579        forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, $t,
580        #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
581        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
582
583        #[stable(feature = "wrapping_neg", since = "1.10.0")]
584        #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
585        impl const Neg for Wrapping<$t> {
586            type Output = Self;
587            #[inline]
588            #[ferrocene::prevalidated]
589            fn neg(self) -> Self {
590                Wrapping(0) - self
591            }
592        }
593        forward_ref_unop! { impl Neg, neg for Wrapping<$t>,
594        #[stable(feature = "wrapping_ref", since = "1.14.0")]
595        #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
596
597    )*)
598}
599
600wrapping_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
601
602macro_rules! wrapping_int_impl {
603    ($($t:ty)*) => ($(
604        impl Wrapping<$t> {
605            /// Returns the smallest value that can be represented by this integer type.
606            ///
607            /// # Examples
608            ///
609            /// Basic usage:
610            ///
611            /// ```
612            /// #![feature(wrapping_int_impl)]
613            /// use std::num::Wrapping;
614            ///
615            #[doc = concat!("assert_eq!(<Wrapping<", stringify!($t), ">>::MIN, Wrapping(", stringify!($t), "::MIN));")]
616            /// ```
617            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
618            pub const MIN: Self = Self(<$t>::MIN);
619
620            /// Returns the largest value that can be represented by this integer type.
621            ///
622            /// # Examples
623            ///
624            /// Basic usage:
625            ///
626            /// ```
627            /// #![feature(wrapping_int_impl)]
628            /// use std::num::Wrapping;
629            ///
630            #[doc = concat!("assert_eq!(<Wrapping<", stringify!($t), ">>::MAX, Wrapping(", stringify!($t), "::MAX));")]
631            /// ```
632            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
633            pub const MAX: Self = Self(<$t>::MAX);
634
635            /// Returns the size of this integer type in bits.
636            ///
637            /// # Examples
638            ///
639            /// Basic usage:
640            ///
641            /// ```
642            /// #![feature(wrapping_int_impl)]
643            /// use std::num::Wrapping;
644            ///
645            #[doc = concat!("assert_eq!(<Wrapping<", stringify!($t), ">>::BITS, ", stringify!($t), "::BITS);")]
646            /// ```
647            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
648            pub const BITS: u32 = <$t>::BITS;
649
650            /// Returns the number of ones in the binary representation of `self`.
651            ///
652            /// # Examples
653            ///
654            /// Basic usage:
655            ///
656            /// ```
657            /// #![feature(wrapping_int_impl)]
658            /// use std::num::Wrapping;
659            ///
660            #[doc = concat!("let n = Wrapping(0b01001100", stringify!($t), ");")]
661            ///
662            /// assert_eq!(n.count_ones(), 3);
663            /// ```
664            #[inline]
665            #[doc(alias = "popcount")]
666            #[doc(alias = "popcnt")]
667            #[must_use = "this returns the result of the operation, \
668                          without modifying the original"]
669            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
670            pub const fn count_ones(self) -> u32 {
671                self.0.count_ones()
672            }
673
674            /// Returns the number of zeros in the binary representation of `self`.
675            ///
676            /// # Examples
677            ///
678            /// Basic usage:
679            ///
680            /// ```
681            /// #![feature(wrapping_int_impl)]
682            /// use std::num::Wrapping;
683            ///
684            #[doc = concat!("assert_eq!(Wrapping(!0", stringify!($t), ").count_zeros(), 0);")]
685            /// ```
686            #[inline]
687            #[must_use = "this returns the result of the operation, \
688                          without modifying the original"]
689            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
690            pub const fn count_zeros(self) -> u32 {
691                self.0.count_zeros()
692            }
693
694            /// Returns the number of trailing zeros in the binary representation of `self`.
695            ///
696            /// # Examples
697            ///
698            /// Basic usage:
699            ///
700            /// ```
701            /// #![feature(wrapping_int_impl)]
702            /// use std::num::Wrapping;
703            ///
704            #[doc = concat!("let n = Wrapping(0b0101000", stringify!($t), ");")]
705            ///
706            /// assert_eq!(n.trailing_zeros(), 3);
707            /// ```
708            #[inline]
709            #[must_use = "this returns the result of the operation, \
710                          without modifying the original"]
711            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
712            pub const fn trailing_zeros(self) -> u32 {
713                self.0.trailing_zeros()
714            }
715
716            /// Shifts the bits to the left by a specified amount, `n`,
717            /// wrapping the truncated bits to the end of the resulting
718            /// integer.
719            ///
720            /// Please note this isn't the same operation as the `<<` shifting
721            /// operator!
722            ///
723            /// # Examples
724            ///
725            /// Basic usage:
726            ///
727            /// ```
728            /// #![feature(wrapping_int_impl)]
729            /// use std::num::Wrapping;
730            ///
731            /// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
732            /// let m: Wrapping<i64> = Wrapping(-0x76543210FEDCBA99);
733            ///
734            /// assert_eq!(n.rotate_left(32), m);
735            /// ```
736            #[inline]
737            #[must_use = "this returns the result of the operation, \
738                          without modifying the original"]
739            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
740            pub const fn rotate_left(self, n: u32) -> Self {
741                Wrapping(self.0.rotate_left(n))
742            }
743
744            /// Shifts the bits to the right by a specified amount, `n`,
745            /// wrapping the truncated bits to the beginning of the resulting
746            /// integer.
747            ///
748            /// Please note this isn't the same operation as the `>>` shifting
749            /// operator!
750            ///
751            /// # Examples
752            ///
753            /// Basic usage:
754            ///
755            /// ```
756            /// #![feature(wrapping_int_impl)]
757            /// use std::num::Wrapping;
758            ///
759            /// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
760            /// let m: Wrapping<i64> = Wrapping(-0xFEDCBA987654322);
761            ///
762            /// assert_eq!(n.rotate_right(4), m);
763            /// ```
764            #[inline]
765            #[must_use = "this returns the result of the operation, \
766                          without modifying the original"]
767            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
768            pub const fn rotate_right(self, n: u32) -> Self {
769                Wrapping(self.0.rotate_right(n))
770            }
771
772            /// Reverses the byte order of the integer.
773            ///
774            /// # Examples
775            ///
776            /// Basic usage:
777            ///
778            /// ```
779            /// #![feature(wrapping_int_impl)]
780            /// use std::num::Wrapping;
781            ///
782            /// let n: Wrapping<i16> = Wrapping(0b0000000_01010101);
783            /// assert_eq!(n, Wrapping(85));
784            ///
785            /// let m = n.swap_bytes();
786            ///
787            /// assert_eq!(m, Wrapping(0b01010101_00000000));
788            /// assert_eq!(m, Wrapping(21760));
789            /// ```
790            #[inline]
791            #[must_use = "this returns the result of the operation, \
792                          without modifying the original"]
793            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
794            pub const fn swap_bytes(self) -> Self {
795                Wrapping(self.0.swap_bytes())
796            }
797
798            /// Reverses the bit pattern of the integer.
799            ///
800            /// # Examples
801            ///
802            /// Please note that this example is shared among integer types, which is why `i16`
803            /// is used.
804            ///
805            /// Basic usage:
806            ///
807            /// ```
808            /// use std::num::Wrapping;
809            ///
810            /// let n = Wrapping(0b0000000_01010101i16);
811            /// assert_eq!(n, Wrapping(85));
812            ///
813            /// let m = n.reverse_bits();
814            ///
815            /// assert_eq!(m.0 as u16, 0b10101010_00000000);
816            /// assert_eq!(m, Wrapping(-22016));
817            /// ```
818            #[stable(feature = "reverse_bits", since = "1.37.0")]
819            #[rustc_const_stable(feature = "const_reverse_bits", since = "1.37.0")]
820            #[must_use = "this returns the result of the operation, \
821                          without modifying the original"]
822            #[inline]
823            pub const fn reverse_bits(self) -> Self {
824                Wrapping(self.0.reverse_bits())
825            }
826
827            /// Converts an integer from big endian to the target's endianness.
828            ///
829            /// On big endian this is a no-op. On little endian the bytes are
830            /// swapped.
831            ///
832            /// # Examples
833            ///
834            /// Basic usage:
835            ///
836            /// ```
837            /// #![feature(wrapping_int_impl)]
838            /// use std::num::Wrapping;
839            ///
840            #[doc = concat!("let n = Wrapping(0x1A", stringify!($t), ");")]
841            ///
842            /// if cfg!(target_endian = "big") {
843            #[doc = concat!("    assert_eq!(<Wrapping<", stringify!($t), ">>::from_be(n), n)")]
844            /// } else {
845            #[doc = concat!("    assert_eq!(<Wrapping<", stringify!($t), ">>::from_be(n), n.swap_bytes())")]
846            /// }
847            /// ```
848            #[inline]
849            #[must_use]
850            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
851            pub const fn from_be(x: Self) -> Self {
852                Wrapping(<$t>::from_be(x.0))
853            }
854
855            /// Converts an integer from little endian to the target's endianness.
856            ///
857            /// On little endian this is a no-op. On big endian the bytes are
858            /// swapped.
859            ///
860            /// # Examples
861            ///
862            /// Basic usage:
863            ///
864            /// ```
865            /// #![feature(wrapping_int_impl)]
866            /// use std::num::Wrapping;
867            ///
868            #[doc = concat!("let n = Wrapping(0x1A", stringify!($t), ");")]
869            ///
870            /// if cfg!(target_endian = "little") {
871            #[doc = concat!("    assert_eq!(<Wrapping<", stringify!($t), ">>::from_le(n), n)")]
872            /// } else {
873            #[doc = concat!("    assert_eq!(<Wrapping<", stringify!($t), ">>::from_le(n), n.swap_bytes())")]
874            /// }
875            /// ```
876            #[inline]
877            #[must_use]
878            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
879            pub const fn from_le(x: Self) -> Self {
880                Wrapping(<$t>::from_le(x.0))
881            }
882
883            /// Converts `self` to big endian from the target's endianness.
884            ///
885            /// On big endian this is a no-op. On little endian the bytes are
886            /// swapped.
887            ///
888            /// # Examples
889            ///
890            /// Basic usage:
891            ///
892            /// ```
893            /// #![feature(wrapping_int_impl)]
894            /// use std::num::Wrapping;
895            ///
896            #[doc = concat!("let n = Wrapping(0x1A", stringify!($t), ");")]
897            ///
898            /// if cfg!(target_endian = "big") {
899            ///     assert_eq!(n.to_be(), n)
900            /// } else {
901            ///     assert_eq!(n.to_be(), n.swap_bytes())
902            /// }
903            /// ```
904            #[inline]
905            #[must_use = "this returns the result of the operation, \
906                          without modifying the original"]
907            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
908            pub const fn to_be(self) -> Self {
909                Wrapping(self.0.to_be())
910            }
911
912            /// Converts `self` to little endian from the target's endianness.
913            ///
914            /// On little endian this is a no-op. On big endian the bytes are
915            /// swapped.
916            ///
917            /// # Examples
918            ///
919            /// Basic usage:
920            ///
921            /// ```
922            /// #![feature(wrapping_int_impl)]
923            /// use std::num::Wrapping;
924            ///
925            #[doc = concat!("let n = Wrapping(0x1A", stringify!($t), ");")]
926            ///
927            /// if cfg!(target_endian = "little") {
928            ///     assert_eq!(n.to_le(), n)
929            /// } else {
930            ///     assert_eq!(n.to_le(), n.swap_bytes())
931            /// }
932            /// ```
933            #[inline]
934            #[must_use = "this returns the result of the operation, \
935                          without modifying the original"]
936            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
937            pub const fn to_le(self) -> Self {
938                Wrapping(self.0.to_le())
939            }
940
941            /// Raises self to the power of `exp`, using exponentiation by squaring.
942            ///
943            /// # Examples
944            ///
945            /// Basic usage:
946            ///
947            /// ```
948            /// #![feature(wrapping_int_impl)]
949            /// use std::num::Wrapping;
950            ///
951            #[doc = concat!("assert_eq!(Wrapping(3", stringify!($t), ").pow(4), Wrapping(81));")]
952            /// ```
953            ///
954            /// Results that are too large are wrapped:
955            ///
956            /// ```
957            /// #![feature(wrapping_int_impl)]
958            /// use std::num::Wrapping;
959            ///
960            /// assert_eq!(Wrapping(3i8).pow(5), Wrapping(-13));
961            /// assert_eq!(Wrapping(3i8).pow(6), Wrapping(-39));
962            /// ```
963            #[inline]
964            #[must_use = "this returns the result of the operation, \
965                          without modifying the original"]
966            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
967            pub fn pow(self, exp: u32) -> Self {
968                Wrapping(self.0.wrapping_pow(exp))
969            }
970        }
971    )*)
972}
973
974wrapping_int_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
975
976macro_rules! wrapping_int_impl_signed {
977    ($($t:ty)*) => ($(
978        impl Wrapping<$t> {
979            /// Returns the number of leading zeros in the binary representation of `self`.
980            ///
981            /// # Examples
982            ///
983            /// Basic usage:
984            ///
985            /// ```
986            /// #![feature(wrapping_int_impl)]
987            /// use std::num::Wrapping;
988            ///
989            #[doc = concat!("let n = Wrapping(", stringify!($t), "::MAX) >> 2;")]
990            ///
991            /// assert_eq!(n.leading_zeros(), 3);
992            /// ```
993            #[inline]
994            #[must_use = "this returns the result of the operation, \
995                          without modifying the original"]
996            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
997            pub const fn leading_zeros(self) -> u32 {
998                self.0.leading_zeros()
999            }
1000
1001            /// Computes the absolute value of `self`, wrapping around at
1002            /// the boundary of the type.
1003            ///
1004            /// The only case where such wrapping can occur is when one takes the absolute value of the negative
1005            /// minimal value for the type this is a positive value that is too large to represent in the type. In
1006            /// such a case, this function returns `MIN` itself.
1007            ///
1008            /// # Examples
1009            ///
1010            /// Basic usage:
1011            ///
1012            /// ```
1013            /// #![feature(wrapping_int_impl)]
1014            /// use std::num::Wrapping;
1015            ///
1016            #[doc = concat!("assert_eq!(Wrapping(100", stringify!($t), ").abs(), Wrapping(100));")]
1017            #[doc = concat!("assert_eq!(Wrapping(-100", stringify!($t), ").abs(), Wrapping(100));")]
1018            #[doc = concat!("assert_eq!(Wrapping(", stringify!($t), "::MIN).abs(), Wrapping(", stringify!($t), "::MIN));")]
1019            /// assert_eq!(Wrapping(-128i8).abs().0 as u8, 128u8);
1020            /// ```
1021            #[inline]
1022            #[must_use = "this returns the result of the operation, \
1023                          without modifying the original"]
1024            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
1025            pub fn abs(self) -> Wrapping<$t> {
1026                Wrapping(self.0.wrapping_abs())
1027            }
1028
1029            /// Returns a number representing sign of `self`.
1030            ///
1031            ///  - `0` if the number is zero
1032            ///  - `1` if the number is positive
1033            ///  - `-1` if the number is negative
1034            ///
1035            /// # Examples
1036            ///
1037            /// Basic usage:
1038            ///
1039            /// ```
1040            /// #![feature(wrapping_int_impl)]
1041            /// use std::num::Wrapping;
1042            ///
1043            #[doc = concat!("assert_eq!(Wrapping(10", stringify!($t), ").signum(), Wrapping(1));")]
1044            #[doc = concat!("assert_eq!(Wrapping(0", stringify!($t), ").signum(), Wrapping(0));")]
1045            #[doc = concat!("assert_eq!(Wrapping(-10", stringify!($t), ").signum(), Wrapping(-1));")]
1046            /// ```
1047            #[inline]
1048            #[must_use = "this returns the result of the operation, \
1049                          without modifying the original"]
1050            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
1051            pub fn signum(self) -> Wrapping<$t> {
1052                Wrapping(self.0.signum())
1053            }
1054
1055            /// Returns `true` if `self` is positive and `false` if the number is zero or
1056            /// negative.
1057            ///
1058            /// # Examples
1059            ///
1060            /// Basic usage:
1061            ///
1062            /// ```
1063            /// #![feature(wrapping_int_impl)]
1064            /// use std::num::Wrapping;
1065            ///
1066            #[doc = concat!("assert!(Wrapping(10", stringify!($t), ").is_positive());")]
1067            #[doc = concat!("assert!(!Wrapping(-10", stringify!($t), ").is_positive());")]
1068            /// ```
1069            #[must_use]
1070            #[inline]
1071            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
1072            pub const fn is_positive(self) -> bool {
1073                self.0.is_positive()
1074            }
1075
1076            /// Returns `true` if `self` is negative and `false` if the number is zero or
1077            /// positive.
1078            ///
1079            /// # Examples
1080            ///
1081            /// Basic usage:
1082            ///
1083            /// ```
1084            /// #![feature(wrapping_int_impl)]
1085            /// use std::num::Wrapping;
1086            ///
1087            #[doc = concat!("assert!(Wrapping(-10", stringify!($t), ").is_negative());")]
1088            #[doc = concat!("assert!(!Wrapping(10", stringify!($t), ").is_negative());")]
1089            /// ```
1090            #[must_use]
1091            #[inline]
1092            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
1093            pub const fn is_negative(self) -> bool {
1094                self.0.is_negative()
1095            }
1096        }
1097    )*)
1098}
1099
1100wrapping_int_impl_signed! { isize i8 i16 i32 i64 i128 }
1101
1102macro_rules! wrapping_int_impl_unsigned {
1103    ($($t:ty)*) => ($(
1104        impl Wrapping<$t> {
1105            /// Returns the number of leading zeros in the binary representation of `self`.
1106            ///
1107            /// # Examples
1108            ///
1109            /// Basic usage:
1110            ///
1111            /// ```
1112            /// #![feature(wrapping_int_impl)]
1113            /// use std::num::Wrapping;
1114            ///
1115            #[doc = concat!("let n = Wrapping(", stringify!($t), "::MAX) >> 2;")]
1116            ///
1117            /// assert_eq!(n.leading_zeros(), 2);
1118            /// ```
1119            #[inline]
1120            #[must_use = "this returns the result of the operation, \
1121                          without modifying the original"]
1122            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
1123            pub const fn leading_zeros(self) -> u32 {
1124                self.0.leading_zeros()
1125            }
1126
1127            /// Returns `true` if and only if `self == 2^k` for some `k`.
1128            ///
1129            /// # Examples
1130            ///
1131            /// Basic usage:
1132            ///
1133            /// ```
1134            /// #![feature(wrapping_int_impl)]
1135            /// use std::num::Wrapping;
1136            ///
1137            #[doc = concat!("assert!(Wrapping(16", stringify!($t), ").is_power_of_two());")]
1138            #[doc = concat!("assert!(!Wrapping(10", stringify!($t), ").is_power_of_two());")]
1139            /// ```
1140            #[must_use]
1141            #[inline]
1142            #[unstable(feature = "wrapping_int_impl", issue = "32463")]
1143            pub fn is_power_of_two(self) -> bool {
1144                self.0.is_power_of_two()
1145            }
1146
1147            /// Returns the smallest power of two greater than or equal to `self`.
1148            ///
1149            /// When return value overflows (i.e., `self > (1 << (N-1))` for type
1150            /// `uN`), overflows to `2^N = 0`.
1151            ///
1152            /// # Examples
1153            ///
1154            /// Basic usage:
1155            ///
1156            /// ```
1157            /// #![feature(wrapping_next_power_of_two)]
1158            /// use std::num::Wrapping;
1159            ///
1160            #[doc = concat!("assert_eq!(Wrapping(2", stringify!($t), ").next_power_of_two(), Wrapping(2));")]
1161            #[doc = concat!("assert_eq!(Wrapping(3", stringify!($t), ").next_power_of_two(), Wrapping(4));")]
1162            #[doc = concat!("assert_eq!(Wrapping(200_u8).next_power_of_two(), Wrapping(0));")]
1163            /// ```
1164            #[inline]
1165            #[must_use = "this returns the result of the operation, \
1166                          without modifying the original"]
1167            #[unstable(feature = "wrapping_next_power_of_two", issue = "32463",
1168                       reason = "needs decision on wrapping behavior")]
1169            pub fn next_power_of_two(self) -> Self {
1170                Wrapping(self.0.wrapping_next_power_of_two())
1171            }
1172        }
1173    )*)
1174}
1175
1176wrapping_int_impl_unsigned! { usize u8 u16 u32 u64 u128 }