1 //! Definitions of `Wrapping<T>`.
4 use crate::ops
::{Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign}
;
5 use crate::ops
::{BitXor, BitXorAssign, Div, DivAssign}
;
6 use crate::ops
::{Mul, MulAssign, Neg, Not, Rem, RemAssign}
;
7 use crate::ops
::{Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign}
;
9 /// Provides intentionally-wrapped arithmetic on `T`.
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.,
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.
22 /// The underlying value can be retrieved through the `.0` index of the
28 /// use std::num::Wrapping;
30 /// let zero = Wrapping(0u32);
31 /// let one = Wrapping(1u32);
33 /// assert_eq!(u32::MAX, (zero - one).0);
35 #[stable(feature = "rust1", since = "1.0.0")]
36 #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Hash)]
38 pub struct Wrapping
<T
>(#[stable(feature = "rust1", since = "1.0.0")] pub T);
40 #[stable(feature = "rust1", since = "1.0.0")]
41 impl<T
: fmt
::Debug
> fmt
::Debug
for Wrapping
<T
> {
42 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
47 #[stable(feature = "wrapping_display", since = "1.10.0")]
48 impl<T
: fmt
::Display
> fmt
::Display
for Wrapping
<T
> {
49 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
54 #[stable(feature = "wrapping_fmt", since = "1.11.0")]
55 impl<T
: fmt
::Binary
> fmt
::Binary
for Wrapping
<T
> {
56 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
61 #[stable(feature = "wrapping_fmt", since = "1.11.0")]
62 impl<T
: fmt
::Octal
> fmt
::Octal
for Wrapping
<T
> {
63 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
68 #[stable(feature = "wrapping_fmt", since = "1.11.0")]
69 impl<T
: fmt
::LowerHex
> fmt
::LowerHex
for Wrapping
<T
> {
70 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
75 #[stable(feature = "wrapping_fmt", since = "1.11.0")]
76 impl<T
: fmt
::UpperHex
> fmt
::UpperHex
for Wrapping
<T
> {
77 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
82 #[allow(unused_macros)]
83 macro_rules
! sh_impl_signed
{
84 ($t
:ident
, $f
:ident
) => {
85 #[stable(feature = "rust1", since = "1.0.0")]
86 impl Shl
<$f
> for Wrapping
<$t
> {
87 type Output
= Wrapping
<$t
>;
90 fn shl(self, other
: $f
) -> Wrapping
<$t
> {
92 Wrapping(self.0.wrapping_shr((-other
& self::shift_max
::$t
as $f
) as u32))
94 Wrapping(self.0.wrapping_shl((other
& self::shift_max
::$t
as $f
) as u32))
98 forward_ref_binop
! { impl Shl
, shl
for Wrapping
<$t
>, $f
,
99 #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
101 #[stable(feature = "op_assign_traits", since = "1.8.0")]
102 impl ShlAssign
<$f
> for Wrapping
<$t
> {
104 fn shl_assign(&mut self, other
: $f
) {
105 *self = *self << other
;
108 forward_ref_op_assign
! { impl ShlAssign, shl_assign for Wrapping<$t>, $f }
110 #[stable(feature = "rust1", since = "1.0.0")]
111 impl Shr
<$f
> for Wrapping
<$t
> {
112 type Output
= Wrapping
<$t
>;
115 fn shr(self, other
: $f
) -> Wrapping
<$t
> {
117 Wrapping(self.0.wrapping_shl((-other
& self::shift_max
::$t
as $f
) as u32))
119 Wrapping(self.0.wrapping_shr((other
& self::shift_max
::$t
as $f
) as u32))
123 forward_ref_binop
! { impl Shr
, shr
for Wrapping
<$t
>, $f
,
124 #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
126 #[stable(feature = "op_assign_traits", since = "1.8.0")]
127 impl ShrAssign
<$f
> for Wrapping
<$t
> {
129 fn shr_assign(&mut self, other
: $f
) {
130 *self = *self >> other
;
133 forward_ref_op_assign
! { impl ShrAssign, shr_assign for Wrapping<$t>, $f }
137 macro_rules
! sh_impl_unsigned
{
138 ($t
:ident
, $f
:ident
) => {
139 #[stable(feature = "rust1", since = "1.0.0")]
140 impl Shl
<$f
> for Wrapping
<$t
> {
141 type Output
= Wrapping
<$t
>;
144 fn shl(self, other
: $f
) -> Wrapping
<$t
> {
145 Wrapping(self.0.wrapping_shl((other
& self::shift_max
::$t
as $f
) as u32))
148 forward_ref_binop
! { impl Shl
, shl
for Wrapping
<$t
>, $f
,
149 #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
151 #[stable(feature = "op_assign_traits", since = "1.8.0")]
152 impl ShlAssign
<$f
> for Wrapping
<$t
> {
154 fn shl_assign(&mut self, other
: $f
) {
155 *self = *self << other
;
158 forward_ref_op_assign
! { impl ShlAssign, shl_assign for Wrapping<$t>, $f }
160 #[stable(feature = "rust1", since = "1.0.0")]
161 impl Shr
<$f
> for Wrapping
<$t
> {
162 type Output
= Wrapping
<$t
>;
165 fn shr(self, other
: $f
) -> Wrapping
<$t
> {
166 Wrapping(self.0.wrapping_shr((other
& self::shift_max
::$t
as $f
) as u32))
169 forward_ref_binop
! { impl Shr
, shr
for Wrapping
<$t
>, $f
,
170 #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
172 #[stable(feature = "op_assign_traits", since = "1.8.0")]
173 impl ShrAssign
<$f
> for Wrapping
<$t
> {
175 fn shr_assign(&mut self, other
: $f
) {
176 *self = *self >> other
;
179 forward_ref_op_assign
! { impl ShrAssign, shr_assign for Wrapping<$t>, $f }
183 // FIXME (#23545): uncomment the remaining impls
184 macro_rules
! sh_impl_all
{
185 ($
($t
:ident
)*) => ($
(
186 //sh_impl_unsigned! { $t, u8 }
187 //sh_impl_unsigned! { $t, u16 }
188 //sh_impl_unsigned! { $t, u32 }
189 //sh_impl_unsigned! { $t, u64 }
190 //sh_impl_unsigned! { $t, u128 }
191 sh_impl_unsigned
! { $t, usize }
193 //sh_impl_signed! { $t, i8 }
194 //sh_impl_signed! { $t, i16 }
195 //sh_impl_signed! { $t, i32 }
196 //sh_impl_signed! { $t, i64 }
197 //sh_impl_signed! { $t, i128 }
198 //sh_impl_signed! { $t, isize }
202 sh_impl_all
! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
204 // FIXME(30524): impl Op<T> for Wrapping<T>, impl OpAssign<T> for Wrapping<T>
205 macro_rules
! wrapping_impl
{
207 #[stable(feature = "rust1", since = "1.0.0")]
208 impl Add
for Wrapping
<$t
> {
209 type Output
= Wrapping
<$t
>;
212 fn add(self, other
: Wrapping
<$t
>) -> Wrapping
<$t
> {
213 Wrapping(self.0.wrapping_add(other
.0))
216 forward_ref_binop
! { impl Add
, add
for Wrapping
<$t
>, Wrapping
<$t
>,
217 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
219 #[stable(feature = "op_assign_traits", since = "1.8.0")]
220 impl AddAssign
for Wrapping
<$t
> {
222 fn add_assign(&mut self, other
: Wrapping
<$t
>) {
223 *self = *self + other
;
226 forward_ref_op_assign
! { impl AddAssign, add_assign for Wrapping<$t>, Wrapping<$t> }
228 #[stable(feature = "rust1", since = "1.0.0")]
229 impl Sub
for Wrapping
<$t
> {
230 type Output
= Wrapping
<$t
>;
233 fn sub(self, other
: Wrapping
<$t
>) -> Wrapping
<$t
> {
234 Wrapping(self.0.wrapping_sub(other
.0))
237 forward_ref_binop
! { impl Sub
, sub
for Wrapping
<$t
>, Wrapping
<$t
>,
238 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
240 #[stable(feature = "op_assign_traits", since = "1.8.0")]
241 impl SubAssign
for Wrapping
<$t
> {
243 fn sub_assign(&mut self, other
: Wrapping
<$t
>) {
244 *self = *self - other
;
247 forward_ref_op_assign
! { impl SubAssign, sub_assign for Wrapping<$t>, Wrapping<$t> }
249 #[stable(feature = "rust1", since = "1.0.0")]
250 impl Mul
for Wrapping
<$t
> {
251 type Output
= Wrapping
<$t
>;
254 fn mul(self, other
: Wrapping
<$t
>) -> Wrapping
<$t
> {
255 Wrapping(self.0.wrapping_mul(other
.0))
258 forward_ref_binop
! { impl Mul
, mul
for Wrapping
<$t
>, Wrapping
<$t
>,
259 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
261 #[stable(feature = "op_assign_traits", since = "1.8.0")]
262 impl MulAssign
for Wrapping
<$t
> {
264 fn mul_assign(&mut self, other
: Wrapping
<$t
>) {
265 *self = *self * other
;
268 forward_ref_op_assign
! { impl MulAssign, mul_assign for Wrapping<$t>, Wrapping<$t> }
270 #[stable(feature = "wrapping_div", since = "1.3.0")]
271 impl Div
for Wrapping
<$t
> {
272 type Output
= Wrapping
<$t
>;
275 fn div(self, other
: Wrapping
<$t
>) -> Wrapping
<$t
> {
276 Wrapping(self.0.wrapping_div(other
.0))
279 forward_ref_binop
! { impl Div
, div
for Wrapping
<$t
>, Wrapping
<$t
>,
280 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
282 #[stable(feature = "op_assign_traits", since = "1.8.0")]
283 impl DivAssign
for Wrapping
<$t
> {
285 fn div_assign(&mut self, other
: Wrapping
<$t
>) {
286 *self = *self / other
;
289 forward_ref_op_assign
! { impl DivAssign, div_assign for Wrapping<$t>, Wrapping<$t> }
291 #[stable(feature = "wrapping_impls", since = "1.7.0")]
292 impl Rem
for Wrapping
<$t
> {
293 type Output
= Wrapping
<$t
>;
296 fn rem(self, other
: Wrapping
<$t
>) -> Wrapping
<$t
> {
297 Wrapping(self.0.wrapping_rem(other
.0))
300 forward_ref_binop
! { impl Rem
, rem
for Wrapping
<$t
>, Wrapping
<$t
>,
301 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
303 #[stable(feature = "op_assign_traits", since = "1.8.0")]
304 impl RemAssign
for Wrapping
<$t
> {
306 fn rem_assign(&mut self, other
: Wrapping
<$t
>) {
307 *self = *self % other
;
310 forward_ref_op_assign
! { impl RemAssign, rem_assign for Wrapping<$t>, Wrapping<$t> }
312 #[stable(feature = "rust1", since = "1.0.0")]
313 impl Not
for Wrapping
<$t
> {
314 type Output
= Wrapping
<$t
>;
317 fn not(self) -> Wrapping
<$t
> {
321 forward_ref_unop
! { impl Not
, not
for Wrapping
<$t
>,
322 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
324 #[stable(feature = "rust1", since = "1.0.0")]
325 impl BitXor
for Wrapping
<$t
> {
326 type Output
= Wrapping
<$t
>;
329 fn bitxor(self, other
: Wrapping
<$t
>) -> Wrapping
<$t
> {
330 Wrapping(self.0 ^ other
.0)
333 forward_ref_binop
! { impl BitXor
, bitxor
for Wrapping
<$t
>, Wrapping
<$t
>,
334 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
336 #[stable(feature = "op_assign_traits", since = "1.8.0")]
337 impl BitXorAssign
for Wrapping
<$t
> {
339 fn bitxor_assign(&mut self, other
: Wrapping
<$t
>) {
340 *self = *self ^ other
;
343 forward_ref_op_assign
! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, Wrapping<$t> }
345 #[stable(feature = "rust1", since = "1.0.0")]
346 impl BitOr
for Wrapping
<$t
> {
347 type Output
= Wrapping
<$t
>;
350 fn bitor(self, other
: Wrapping
<$t
>) -> Wrapping
<$t
> {
351 Wrapping(self.0 | other
.0)
354 forward_ref_binop
! { impl BitOr
, bitor
for Wrapping
<$t
>, Wrapping
<$t
>,
355 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
357 #[stable(feature = "op_assign_traits", since = "1.8.0")]
358 impl BitOrAssign
for Wrapping
<$t
> {
360 fn bitor_assign(&mut self, other
: Wrapping
<$t
>) {
361 *self = *self | other
;
364 forward_ref_op_assign
! { impl BitOrAssign, bitor_assign for Wrapping<$t>, Wrapping<$t> }
366 #[stable(feature = "rust1", since = "1.0.0")]
367 impl BitAnd
for Wrapping
<$t
> {
368 type Output
= Wrapping
<$t
>;
371 fn bitand(self, other
: Wrapping
<$t
>) -> Wrapping
<$t
> {
372 Wrapping(self.0 & other
.0)
375 forward_ref_binop
! { impl BitAnd
, bitand
for Wrapping
<$t
>, Wrapping
<$t
>,
376 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
378 #[stable(feature = "op_assign_traits", since = "1.8.0")]
379 impl BitAndAssign
for Wrapping
<$t
> {
381 fn bitand_assign(&mut self, other
: Wrapping
<$t
>) {
382 *self = *self & other
;
385 forward_ref_op_assign
! { impl BitAndAssign, bitand_assign for Wrapping<$t>, Wrapping<$t> }
387 #[stable(feature = "wrapping_neg", since = "1.10.0")]
388 impl Neg
for Wrapping
<$t
> {
391 fn neg(self) -> Self {
395 forward_ref_unop
! { impl Neg
, neg
for Wrapping
<$t
>,
396 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
401 wrapping_impl
! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
403 macro_rules
! wrapping_int_impl
{
407 concat
!("Returns the smallest value that can be represented by this integer type.
414 #![feature(wrapping_int_impl)]
415 use std::num::Wrapping;
417 assert_eq!(<Wrapping<", stringify
!($t
), ">>::MIN, Wrapping(", stringify
!($t
), "::MIN));
419 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
420 pub const MIN
: Self = Self(<$t
>::MIN
);
424 concat
!("Returns the largest value that can be represented by this integer type.
431 #![feature(wrapping_int_impl)]
432 use std::num::Wrapping;
434 assert_eq!(<Wrapping<", stringify
!($t
), ">>::MAX, Wrapping(", stringify
!($t
), "::MAX));
436 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
437 pub const MAX
: Self = Self(<$t
>::MAX
);
441 concat
!("Returns the number of ones in the binary representation of `self`.
448 #![feature(wrapping_int_impl)]
449 use std::num::Wrapping;
451 let n = Wrapping(0b01001100", stringify
!($t
), ");
453 assert_eq!(n.count_ones(), 3);
456 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
457 pub const fn count_ones(self) -> u32 {
463 concat
!("Returns the number of zeros in the binary representation of `self`.
470 #![feature(wrapping_int_impl)]
471 use std::num::Wrapping;
473 assert_eq!(Wrapping(!0", stringify
!($t
), ").count_zeros(), 0);
476 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
477 pub const fn count_zeros(self) -> u32 {
483 concat
!("Returns the number of trailing zeros in the binary representation
491 #![feature(wrapping_int_impl)]
492 use std::num::Wrapping;
494 let n = Wrapping(0b0101000", stringify
!($t
), ");
496 assert_eq!(n.trailing_zeros(), 3);
499 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
500 pub const fn trailing_zeros(self) -> u32 {
501 self.0.trailing_zeros()
505 /// Shifts the bits to the left by a specified amount, `n`,
506 /// wrapping the truncated bits to the end of the resulting
509 /// Please note this isn't the same operation as the `<<` shifting
517 /// #![feature(wrapping_int_impl)]
518 /// use std::num::Wrapping;
520 /// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
521 /// let m: Wrapping<i64> = Wrapping(-0x76543210FEDCBA99);
523 /// assert_eq!(n.rotate_left(32), m);
526 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
527 pub const fn rotate_left(self, n
: u32) -> Self {
528 Wrapping(self.0.rotate_left(n
))
531 /// Shifts the bits to the right by a specified amount, `n`,
532 /// wrapping the truncated bits to the beginning of the resulting
535 /// Please note this isn't the same operation as the `>>` shifting
543 /// #![feature(wrapping_int_impl)]
544 /// use std::num::Wrapping;
546 /// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
547 /// let m: Wrapping<i64> = Wrapping(-0xFEDCBA987654322);
549 /// assert_eq!(n.rotate_right(4), m);
552 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
553 pub const fn rotate_right(self, n
: u32) -> Self {
554 Wrapping(self.0.rotate_right(n
))
557 /// Reverses the byte order of the integer.
564 /// #![feature(wrapping_int_impl)]
565 /// use std::num::Wrapping;
567 /// let n: Wrapping<i16> = Wrapping(0b0000000_01010101);
568 /// assert_eq!(n, Wrapping(85));
570 /// let m = n.swap_bytes();
572 /// assert_eq!(m, Wrapping(0b01010101_00000000));
573 /// assert_eq!(m, Wrapping(21760));
576 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
577 pub const fn swap_bytes(self) -> Self {
578 Wrapping(self.0.swap_bytes())
581 /// Reverses the bit pattern of the integer.
585 /// Please note that this example is shared between integer types.
586 /// Which explains why `i16` is used here.
591 /// use std::num::Wrapping;
593 /// let n = Wrapping(0b0000000_01010101i16);
594 /// assert_eq!(n, Wrapping(85));
596 /// let m = n.reverse_bits();
598 /// assert_eq!(m.0 as u16, 0b10101010_00000000);
599 /// assert_eq!(m, Wrapping(-22016));
601 #[stable(feature = "reverse_bits", since = "1.37.0")]
602 #[rustc_const_stable(feature = "const_reverse_bits", since = "1.37.0")]
605 pub const fn reverse_bits(self) -> Self {
606 Wrapping(self.0.reverse_bits())
610 concat
!("Converts an integer from big endian to the target's endianness.
612 On big endian this is a no-op. On little endian the bytes are
620 #![feature(wrapping_int_impl)]
621 use std::num::Wrapping;
623 let n = Wrapping(0x1A", stringify
!($t
), ");
625 if cfg!(target_endian = \"big\") {
626 assert_eq!(<Wrapping<", stringify
!($t
), ">>::from_be(n), n)
628 assert_eq!(<Wrapping<", stringify
!($t
), ">>::from_be(n), n.swap_bytes())
632 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
633 pub const fn from_be(x
: Self) -> Self {
634 Wrapping(<$t
>::from_be(x
.0))
639 concat
!("Converts an integer from little endian to the target's endianness.
641 On little endian this is a no-op. On big endian the bytes are
649 #![feature(wrapping_int_impl)]
650 use std::num::Wrapping;
652 let n = Wrapping(0x1A", stringify
!($t
), ");
654 if cfg!(target_endian = \"little\") {
655 assert_eq!(<Wrapping<", stringify
!($t
), ">>::from_le(n), n)
657 assert_eq!(<Wrapping<", stringify
!($t
), ">>::from_le(n), n.swap_bytes())
661 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
662 pub const fn from_le(x
: Self) -> Self {
663 Wrapping(<$t
>::from_le(x
.0))
668 concat
!("Converts `self` to big endian from the target's endianness.
670 On big endian this is a no-op. On little endian the bytes are
678 #![feature(wrapping_int_impl)]
679 use std::num::Wrapping;
681 let n = Wrapping(0x1A", stringify
!($t
), ");
683 if cfg!(target_endian = \"big\") {
684 assert_eq!(n.to_be(), n)
686 assert_eq!(n.to_be(), n.swap_bytes())
690 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
691 pub const fn to_be(self) -> Self {
692 Wrapping(self.0.to_be())
697 concat
!("Converts `self` to little endian from the target's endianness.
699 On little endian this is a no-op. On big endian the bytes are
707 #![feature(wrapping_int_impl)]
708 use std::num::Wrapping;
710 let n = Wrapping(0x1A", stringify
!($t
), ");
712 if cfg!(target_endian = \"little\") {
713 assert_eq!(n.to_le(), n)
715 assert_eq!(n.to_le(), n.swap_bytes())
719 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
720 pub const fn to_le(self) -> Self {
721 Wrapping(self.0.to_le())
726 concat
!("Raises self to the power of `exp`, using exponentiation by squaring.
733 #![feature(wrapping_int_impl)]
734 use std::num::Wrapping;
736 assert_eq!(Wrapping(3", stringify
!($t
), ").pow(4), Wrapping(81));
739 Results that are too large are wrapped:
742 #![feature(wrapping_int_impl)]
743 use std::num::Wrapping;
745 assert_eq!(Wrapping(3i8).pow(5), Wrapping(-13));
746 assert_eq!(Wrapping(3i8).pow(6), Wrapping(-39));
749 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
750 pub fn pow(self, exp
: u32) -> Self {
751 Wrapping(self.0.wrapping_pow(exp
))
758 wrapping_int_impl
! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
760 macro_rules
! wrapping_int_impl_signed
{
764 concat
!("Returns the number of leading zeros in the binary representation of `self`.
771 #![feature(wrapping_int_impl)]
772 use std::num::Wrapping;
774 let n = Wrapping(", stringify
!($t
), "::MAX) >> 2;
776 assert_eq!(n.leading_zeros(), 3);
779 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
780 pub const fn leading_zeros(self) -> u32 {
781 self.0.leading_zeros()
786 concat
!("Computes the absolute value of `self`, wrapping around at
787 the boundary of the type.
789 The only case where such wrapping can occur is when one takes the absolute value of the negative
790 minimal value for the type this is a positive value that is too large to represent in the type. In
791 such a case, this function returns `MIN` itself.
798 #![feature(wrapping_int_impl)]
799 use std::num::Wrapping;
801 assert_eq!(Wrapping(100", stringify
!($t
), ").abs(), Wrapping(100));
802 assert_eq!(Wrapping(-100", stringify
!($t
), ").abs(), Wrapping(100));
803 assert_eq!(Wrapping(", stringify
!($t
), "::MIN).abs(), Wrapping(", stringify
!($t
), "::MIN));
804 assert_eq!(Wrapping(-128i8).abs().0 as u8, 128u8);
807 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
808 pub fn abs(self) -> Wrapping
<$t
> {
809 Wrapping(self.0.wrapping_abs())
814 concat
!("Returns a number representing sign of `self`.
816 - `0` if the number is zero
817 - `1` if the number is positive
818 - `-1` if the number is negative
825 #![feature(wrapping_int_impl)]
826 use std::num::Wrapping;
828 assert_eq!(Wrapping(10", stringify
!($t
), ").signum(), Wrapping(1));
829 assert_eq!(Wrapping(0", stringify
!($t
), ").signum(), Wrapping(0));
830 assert_eq!(Wrapping(-10", stringify
!($t
), ").signum(), Wrapping(-1));
833 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
834 pub fn signum(self) -> Wrapping
<$t
> {
835 Wrapping(self.0.signum())
840 concat
!("Returns `true` if `self` is positive and `false` if the number is zero or
848 #![feature(wrapping_int_impl)]
849 use std::num::Wrapping;
851 assert!(Wrapping(10", stringify
!($t
), ").is_positive());
852 assert!(!Wrapping(-10", stringify
!($t
), ").is_positive());
855 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
856 pub const fn is_positive(self) -> bool
{
862 concat
!("Returns `true` if `self` is negative and `false` if the number is zero or
870 #![feature(wrapping_int_impl)]
871 use std::num::Wrapping;
873 assert!(Wrapping(-10", stringify
!($t
), ").is_negative());
874 assert!(!Wrapping(10", stringify
!($t
), ").is_negative());
877 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
878 pub const fn is_negative(self) -> bool
{
886 wrapping_int_impl_signed
! { isize i8 i16 i32 i64 i128 }
888 macro_rules
! wrapping_int_impl_unsigned
{
892 concat
!("Returns the number of leading zeros in the binary representation of `self`.
899 #![feature(wrapping_int_impl)]
900 use std::num::Wrapping;
902 let n = Wrapping(", stringify
!($t
), "::MAX) >> 2;
904 assert_eq!(n.leading_zeros(), 2);
907 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
908 pub const fn leading_zeros(self) -> u32 {
909 self.0.leading_zeros()
914 concat
!("Returns `true` if and only if `self == 2^k` for some `k`.
921 #![feature(wrapping_int_impl)]
922 use std::num::Wrapping;
924 assert!(Wrapping(16", stringify
!($t
), ").is_power_of_two());
925 assert!(!Wrapping(10", stringify
!($t
), ").is_power_of_two());
928 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
929 pub fn is_power_of_two(self) -> bool
{
930 self.0.is_power_of_two
()
935 concat
!("Returns the smallest power of two greater than or equal to `self`.
937 When return value overflows (i.e., `self > (1 << (N-1))` for type
938 `uN`), overflows to `2^N = 0`.
945 #![feature(wrapping_next_power_of_two)]
946 use std::num::Wrapping;
948 assert_eq!(Wrapping(2", stringify
!($t
), ").next_power_of_two(), Wrapping(2));
949 assert_eq!(Wrapping(3", stringify
!($t
), ").next_power_of_two(), Wrapping(4));
950 assert_eq!(Wrapping(200_u8).next_power_of_two(), Wrapping(0));
953 #[unstable(feature = "wrapping_next_power_of_two", issue = "32463",
954 reason
= "needs decision on wrapping behaviour")]
955 pub fn next_power_of_two(self) -> Self {
956 Wrapping(self.0.wrapping_next_power_of_two())
963 wrapping_int_impl_unsigned
! { usize u8 u16 u32 u64 u128 }
966 #![allow(non_upper_case_globals)]
968 #[cfg(target_pointer_width = "16")]
970 pub const usize: u32 = super::u16;
971 pub const isize: u32 = super::i16;
974 #[cfg(target_pointer_width = "32")]
976 pub const usize: u32 = super::u32;
977 pub const isize: u32 = super::i32;
980 #[cfg(target_pointer_width = "64")]
982 pub const usize: u32 = super::u64;
983 pub const isize: u32 = super::i64;
986 pub const i8: u32 = (1 << 3) - 1;
987 pub const i16: u32 = (1 << 4) - 1;
988 pub const i32: u32 = (1 << 5) - 1;
989 pub const i64: u32 = (1 << 6) - 1;
990 pub const i128
: u32 = (1 << 7) - 1;
991 pub use self::platform
::isize;
993 pub const u8: u32 = i8;
994 pub const u16: u32 = i16;
995 pub const u32: u32 = i32;
996 pub const u64: u32 = i64;
997 pub const u128
: u32 = i128
;
998 pub use self::platform
::usize;