1use 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#[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
216macro_rules! sh_impl_all {
218 ($($t:ident)*) => ($(
219 sh_impl_unsigned! { $t, usize }
225
226 )*)
233}
234
235sh_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
236
237macro_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 #[doc = concat!("assert_eq!(<Wrapping<", stringify!($t), ">>::MIN, Wrapping(", stringify!($t), "::MIN));")]
616 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
618 pub const MIN: Self = Self(<$t>::MIN);
619
620 #[doc = concat!("assert_eq!(<Wrapping<", stringify!($t), ">>::MAX, Wrapping(", stringify!($t), "::MAX));")]
631 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
633 pub const MAX: Self = Self(<$t>::MAX);
634
635 #[doc = concat!("assert_eq!(<Wrapping<", stringify!($t), ">>::BITS, ", stringify!($t), "::BITS);")]
646 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
648 pub const BITS: u32 = <$t>::BITS;
649
650 #[doc = concat!("let n = Wrapping(0b01001100", stringify!($t), ");")]
661 #[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 #[doc = concat!("assert_eq!(Wrapping(!0", stringify!($t), ").count_zeros(), 0);")]
685 #[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 #[doc = concat!("let n = Wrapping(0b0101000", stringify!($t), ");")]
705 #[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 #[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 #[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 #[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 #[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 #[doc = concat!("let n = Wrapping(0x1A", stringify!($t), ");")]
841 #[doc = concat!(" assert_eq!(<Wrapping<", stringify!($t), ">>::from_be(n), n)")]
844 #[doc = concat!(" assert_eq!(<Wrapping<", stringify!($t), ">>::from_be(n), n.swap_bytes())")]
846 #[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 #[doc = concat!("let n = Wrapping(0x1A", stringify!($t), ");")]
869 #[doc = concat!(" assert_eq!(<Wrapping<", stringify!($t), ">>::from_le(n), n)")]
872 #[doc = concat!(" assert_eq!(<Wrapping<", stringify!($t), ">>::from_le(n), n.swap_bytes())")]
874 #[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 #[doc = concat!("let n = Wrapping(0x1A", stringify!($t), ");")]
897 #[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 #[doc = concat!("let n = Wrapping(0x1A", stringify!($t), ");")]
926 #[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 #[doc = concat!("assert_eq!(Wrapping(3", stringify!($t), ").pow(4), Wrapping(81));")]
952 #[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 #[doc = concat!("let n = Wrapping(", stringify!($t), "::MAX) >> 2;")]
990 #[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 #[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 #[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 #[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 #[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 #[doc = concat!("assert!(Wrapping(10", stringify!($t), ").is_positive());")]
1067 #[doc = concat!("assert!(!Wrapping(-10", stringify!($t), ").is_positive());")]
1068 #[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 #[doc = concat!("assert!(Wrapping(-10", stringify!($t), ").is_negative());")]
1088 #[doc = concat!("assert!(!Wrapping(10", stringify!($t), ").is_negative());")]
1089 #[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 #[doc = concat!("let n = Wrapping(", stringify!($t), "::MAX) >> 2;")]
1116 #[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 #[doc = concat!("assert!(Wrapping(16", stringify!($t), ").is_power_of_two());")]
1138 #[doc = concat!("assert!(!Wrapping(10", stringify!($t), ").is_power_of_two());")]
1139 #[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 #[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 #[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 }