1 macro_rules! uint_impl {
2 ($SelfT:ty, $ActualT:ty, $BITS:expr, $MaxV:expr, $Feature:expr, $EndFeature:expr,
3 $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
4 $reversed:expr, $le_bytes:expr, $be_bytes:expr,
5 $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => {
7 concat!("The smallest value that can be represented by this integer type.
14 ", $Feature, "assert_eq!(", stringify!($SelfT), "::MIN, 0);", $EndFeature, "
16 #[stable(feature = "assoc_int_consts", since = "1.43.0")]
17 pub const MIN: Self = 0;
21 concat!("The largest value that can be represented by this integer type.
28 ", $Feature, "assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($MaxV), ");",
31 #[stable(feature = "assoc_int_consts", since = "1.43.0")]
32 pub const MAX: Self = !0;
36 concat!("The size of this integer type in bits.
41 ", $Feature, "#![feature(int_bits_const)]
42 assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");",
45 #[unstable(feature = "int_bits_const", issue = "76904")]
46 pub const BITS: u32 = $BITS;
50 concat!("Converts a string slice in a given base to an integer.
52 The string is expected to be an optional `+` sign
54 Leading and trailing whitespace represent an error.
55 Digits are a subset of these characters, depending on `radix`:
63 This function panics if `radix` is not in the range from 2 to 36.
70 ", $Feature, "assert_eq!(", stringify!($SelfT), "::from_str_radix(\"A\", 16), Ok(10));",
73 #[stable(feature = "rust1", since = "1.0.0")]
74 pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
75 from_str_radix(src, radix)
80 concat!("Returns the number of ones in the binary representation of `self`.
87 ", $Feature, "let n = 0b01001100", stringify!($SelfT), ";
89 assert_eq!(n.count_ones(), 3);", $EndFeature, "
91 #[stable(feature = "rust1", since = "1.0.0")]
92 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
94 pub const fn count_ones(self) -> u32 {
95 intrinsics::ctpop(self as $ActualT) as u32
100 concat!("Returns the number of zeros in the binary representation of `self`.
107 ", $Feature, "assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 0);", $EndFeature, "
109 #[stable(feature = "rust1", since = "1.0.0")]
110 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
112 pub const fn count_zeros(self) -> u32 {
118 concat!("Returns the number of leading zeros in the binary representation of `self`.
125 ", $Feature, "let n = ", stringify!($SelfT), "::MAX >> 2;
127 assert_eq!(n.leading_zeros(), 2);", $EndFeature, "
129 #[stable(feature = "rust1", since = "1.0.0")]
130 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
132 pub const fn leading_zeros(self) -> u32 {
133 intrinsics::ctlz(self as $ActualT) as u32
138 concat!("Returns the number of trailing zeros in the binary representation
146 ", $Feature, "let n = 0b0101000", stringify!($SelfT), ";
148 assert_eq!(n.trailing_zeros(), 3);", $EndFeature, "
150 #[stable(feature = "rust1", since = "1.0.0")]
151 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
153 pub const fn trailing_zeros(self) -> u32 {
154 intrinsics::cttz(self) as u32
159 concat!("Returns the number of leading ones in the binary representation of `self`.
166 ", $Feature, "let n = !(", stringify!($SelfT), "::MAX >> 2);
168 assert_eq!(n.leading_ones(), 2);", $EndFeature, "
170 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
171 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
173 pub const fn leading_ones(self) -> u32 {
174 (!self).leading_zeros()
179 concat!("Returns the number of trailing ones in the binary representation
187 ", $Feature, "let n = 0b1010111", stringify!($SelfT), ";
189 assert_eq!(n.trailing_ones(), 3);", $EndFeature, "
191 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
192 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
194 pub const fn trailing_ones(self) -> u32 {
195 (!self).trailing_zeros()
200 concat!("Shifts the bits to the left by a specified amount, `n`,
201 wrapping the truncated bits to the end of the resulting integer.
203 Please note this isn't the same operation as the `<<` shifting operator!
210 let n = ", $rot_op, stringify!($SelfT), ";
211 let m = ", $rot_result, ";
213 assert_eq!(n.rotate_left(", $rot, "), m);
215 #[stable(feature = "rust1", since = "1.0.0")]
216 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
217 #[must_use = "this returns the result of the operation, \
218 without modifying the original"]
220 pub const fn rotate_left(self, n: u32) -> Self {
221 intrinsics::rotate_left(self, n as $SelfT)
226 concat!("Shifts the bits to the right by a specified amount, `n`,
227 wrapping the truncated bits to the beginning of the resulting
230 Please note this isn't the same operation as the `>>` shifting operator!
237 let n = ", $rot_result, stringify!($SelfT), ";
238 let m = ", $rot_op, ";
240 assert_eq!(n.rotate_right(", $rot, "), m);
242 #[stable(feature = "rust1", since = "1.0.0")]
243 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
244 #[must_use = "this returns the result of the operation, \
245 without modifying the original"]
247 pub const fn rotate_right(self, n: u32) -> Self {
248 intrinsics::rotate_right(self, n as $SelfT)
254 Reverses the byte order of the integer.
261 let n = ", $swap_op, stringify!($SelfT), ";
262 let m = n.swap_bytes();
264 assert_eq!(m, ", $swapped, ");
266 #[stable(feature = "rust1", since = "1.0.0")]
267 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
269 pub const fn swap_bytes(self) -> Self {
270 intrinsics::bswap(self as $ActualT) as Self
275 concat!("Reverses the bit pattern of the integer.
282 let n = ", $swap_op, stringify!($SelfT), ";
283 let m = n.reverse_bits();
285 assert_eq!(m, ", $reversed, ");
287 #[stable(feature = "reverse_bits", since = "1.37.0")]
288 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
291 pub const fn reverse_bits(self) -> Self {
292 intrinsics::bitreverse(self as $ActualT) as Self
297 concat!("Converts an integer from big endian to the target's endianness.
299 On big endian this is a no-op. On little endian the bytes are
307 ", $Feature, "let n = 0x1A", stringify!($SelfT), ";
309 if cfg!(target_endian = \"big\") {
310 assert_eq!(", stringify!($SelfT), "::from_be(n), n)
312 assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())
315 #[stable(feature = "rust1", since = "1.0.0")]
316 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
318 pub const fn from_be(x: Self) -> Self {
319 #[cfg(target_endian = "big")]
323 #[cfg(not(target_endian = "big"))]
331 concat!("Converts an integer from little endian to the target's endianness.
333 On little endian this is a no-op. On big endian the bytes are
341 ", $Feature, "let n = 0x1A", stringify!($SelfT), ";
343 if cfg!(target_endian = \"little\") {
344 assert_eq!(", stringify!($SelfT), "::from_le(n), n)
346 assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())
349 #[stable(feature = "rust1", since = "1.0.0")]
350 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
352 pub const fn from_le(x: Self) -> Self {
353 #[cfg(target_endian = "little")]
357 #[cfg(not(target_endian = "little"))]
365 concat!("Converts `self` to big endian from the target's endianness.
367 On big endian this is a no-op. On little endian the bytes are
375 ", $Feature, "let n = 0x1A", stringify!($SelfT), ";
377 if cfg!(target_endian = \"big\") {
378 assert_eq!(n.to_be(), n)
380 assert_eq!(n.to_be(), n.swap_bytes())
383 #[stable(feature = "rust1", since = "1.0.0")]
384 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
386 pub const fn to_be(self) -> Self { // or not to be?
387 #[cfg(target_endian = "big")]
391 #[cfg(not(target_endian = "big"))]
399 concat!("Converts `self` to little endian from the target's endianness.
401 On little endian this is a no-op. On big endian the bytes are
409 ", $Feature, "let n = 0x1A", stringify!($SelfT), ";
411 if cfg!(target_endian = \"little\") {
412 assert_eq!(n.to_le(), n)
414 assert_eq!(n.to_le(), n.swap_bytes())
417 #[stable(feature = "rust1", since = "1.0.0")]
418 #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
420 pub const fn to_le(self) -> Self {
421 #[cfg(target_endian = "little")]
425 #[cfg(not(target_endian = "little"))]
433 concat!("Checked integer addition. Computes `self + rhs`, returning `None`
434 if overflow occurred.
441 ", $Feature, "assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(1), ",
442 "Some(", stringify!($SelfT), "::MAX - 1));
443 assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);", $EndFeature, "
445 #[stable(feature = "rust1", since = "1.0.0")]
446 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
447 #[must_use = "this returns the result of the operation, \
448 without modifying the original"]
450 pub const fn checked_add(self, rhs: Self) -> Option<Self> {
451 let (a, b) = self.overflowing_add(rhs);
452 if unlikely!(b) {None} else {Some(a)}
457 concat!("Unchecked integer addition. Computes `self + rhs`, assuming overflow
458 cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT),
459 "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`."),
461 feature = "unchecked_math",
462 reason = "niche optimization path",
465 #[must_use = "this returns the result of the operation, \
466 without modifying the original"]
468 pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
469 // SAFETY: the caller must uphold the safety contract for
471 unsafe { intrinsics::unchecked_add(self, rhs) }
476 concat!("Checked integer subtraction. Computes `self - rhs`, returning
477 `None` if overflow occurred.
484 ", $Feature, "assert_eq!(1", stringify!($SelfT), ".checked_sub(1), Some(0));
485 assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
487 #[stable(feature = "rust1", since = "1.0.0")]
488 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
489 #[must_use = "this returns the result of the operation, \
490 without modifying the original"]
492 pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
493 let (a, b) = self.overflowing_sub(rhs);
494 if unlikely!(b) {None} else {Some(a)}
499 concat!("Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
500 cannot occur. This results in undefined behavior when `self - rhs > ", stringify!($SelfT),
501 "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`."),
503 feature = "unchecked_math",
504 reason = "niche optimization path",
507 #[must_use = "this returns the result of the operation, \
508 without modifying the original"]
510 pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
511 // SAFETY: the caller must uphold the safety contract for
513 unsafe { intrinsics::unchecked_sub(self, rhs) }
518 concat!("Checked integer multiplication. Computes `self * rhs`, returning
519 `None` if overflow occurred.
526 ", $Feature, "assert_eq!(5", stringify!($SelfT), ".checked_mul(1), Some(5));
527 assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);", $EndFeature, "
529 #[stable(feature = "rust1", since = "1.0.0")]
530 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
531 #[must_use = "this returns the result of the operation, \
532 without modifying the original"]
534 pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
535 let (a, b) = self.overflowing_mul(rhs);
536 if unlikely!(b) {None} else {Some(a)}
541 concat!("Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
542 cannot occur. This results in undefined behavior when `self * rhs > ", stringify!($SelfT),
543 "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`."),
545 feature = "unchecked_math",
546 reason = "niche optimization path",
549 #[must_use = "this returns the result of the operation, \
550 without modifying the original"]
552 pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
553 // SAFETY: the caller must uphold the safety contract for
555 unsafe { intrinsics::unchecked_mul(self, rhs) }
560 concat!("Checked integer division. Computes `self / rhs`, returning `None`
568 ", $Feature, "assert_eq!(128", stringify!($SelfT), ".checked_div(2), Some(64));
569 assert_eq!(1", stringify!($SelfT), ".checked_div(0), None);", $EndFeature, "
571 #[stable(feature = "rust1", since = "1.0.0")]
572 #[rustc_const_unstable(feature = "const_checked_int_methods", issue = "53718")]
573 #[must_use = "this returns the result of the operation, \
574 without modifying the original"]
576 pub const fn checked_div(self, rhs: Self) -> Option<Self> {
577 if unlikely!(rhs == 0) {
580 // SAFETY: div by zero has been checked above and unsigned types have no other
581 // failure modes for division
582 Some(unsafe { intrinsics::unchecked_div(self, rhs) })
588 concat!("Checked Euclidean division. Computes `self.div_euclid(rhs)`, returning `None`
596 assert_eq!(128", stringify!($SelfT), ".checked_div_euclid(2), Some(64));
597 assert_eq!(1", stringify!($SelfT), ".checked_div_euclid(0), None);
599 #[stable(feature = "euclidean_division", since = "1.38.0")]
600 #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
601 #[must_use = "this returns the result of the operation, \
602 without modifying the original"]
604 pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
605 if unlikely!(rhs == 0) {
608 Some(self.div_euclid(rhs))
615 concat!("Checked integer remainder. Computes `self % rhs`, returning `None`
623 ", $Feature, "assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));
624 assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);", $EndFeature, "
626 #[stable(feature = "wrapping", since = "1.7.0")]
627 #[rustc_const_unstable(feature = "const_checked_int_methods", issue = "53718")]
628 #[must_use = "this returns the result of the operation, \
629 without modifying the original"]
631 pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
632 if unlikely!(rhs == 0) {
635 // SAFETY: div by zero has been checked above and unsigned types have no other
636 // failure modes for division
637 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
643 concat!("Checked Euclidean modulo. Computes `self.rem_euclid(rhs)`, returning `None`
651 assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));
652 assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);
654 #[stable(feature = "euclidean_division", since = "1.38.0")]
655 #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
656 #[must_use = "this returns the result of the operation, \
657 without modifying the original"]
659 pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
660 if unlikely!(rhs == 0) {
663 Some(self.rem_euclid(rhs))
669 concat!("Checked negation. Computes `-self`, returning `None` unless `self ==
672 Note that negating any positive integer will overflow.
679 ", $Feature, "assert_eq!(0", stringify!($SelfT), ".checked_neg(), Some(0));
680 assert_eq!(1", stringify!($SelfT), ".checked_neg(), None);", $EndFeature, "
682 #[stable(feature = "wrapping", since = "1.7.0")]
683 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
685 pub const fn checked_neg(self) -> Option<Self> {
686 let (a, b) = self.overflowing_neg();
687 if unlikely!(b) {None} else {Some(a)}
692 concat!("Checked shift left. Computes `self << rhs`, returning `None`
693 if `rhs` is larger than or equal to the number of bits in `self`.
700 ", $Feature, "assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));
701 assert_eq!(0x10", stringify!($SelfT), ".checked_shl(129), None);", $EndFeature, "
703 #[stable(feature = "wrapping", since = "1.7.0")]
704 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
705 #[must_use = "this returns the result of the operation, \
706 without modifying the original"]
708 pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
709 let (a, b) = self.overflowing_shl(rhs);
710 if unlikely!(b) {None} else {Some(a)}
715 concat!("Checked shift right. Computes `self >> rhs`, returning `None`
716 if `rhs` is larger than or equal to the number of bits in `self`.
723 ", $Feature, "assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));
724 assert_eq!(0x10", stringify!($SelfT), ".checked_shr(129), None);", $EndFeature, "
726 #[stable(feature = "wrapping", since = "1.7.0")]
727 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
728 #[must_use = "this returns the result of the operation, \
729 without modifying the original"]
731 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
732 let (a, b) = self.overflowing_shr(rhs);
733 if unlikely!(b) {None} else {Some(a)}
738 concat!("Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
746 ", $Feature, "assert_eq!(2", stringify!($SelfT), ".checked_pow(5), Some(32));
747 assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);", $EndFeature, "
749 #[stable(feature = "no_panic_pow", since = "1.34.0")]
750 #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
751 #[must_use = "this returns the result of the operation, \
752 without modifying the original"]
754 pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
759 let mut acc: Self = 1;
763 acc = try_opt!(acc.checked_mul(base));
766 base = try_opt!(base.checked_mul(base));
769 // since exp!=0, finally the exp must be 1.
770 // Deal with the final bit of the exponent separately, since
771 // squaring the base afterwards is not necessary and may cause a
772 // needless overflow.
774 Some(try_opt!(acc.checked_mul(base)))
779 concat!("Saturating integer addition. Computes `self + rhs`, saturating at
780 the numeric bounds instead of overflowing.
787 ", $Feature, "assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);
788 assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(127), ", stringify!($SelfT), "::MAX);",
792 #[stable(feature = "rust1", since = "1.0.0")]
793 #[must_use = "this returns the result of the operation, \
794 without modifying the original"]
795 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
797 pub const fn saturating_add(self, rhs: Self) -> Self {
798 intrinsics::saturating_add(self, rhs)
803 concat!("Saturating integer subtraction. Computes `self - rhs`, saturating
804 at the numeric bounds instead of overflowing.
811 ", $Feature, "assert_eq!(100", stringify!($SelfT), ".saturating_sub(27), 73);
812 assert_eq!(13", stringify!($SelfT), ".saturating_sub(127), 0);", $EndFeature, "
814 #[stable(feature = "rust1", since = "1.0.0")]
815 #[must_use = "this returns the result of the operation, \
816 without modifying the original"]
817 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
819 pub const fn saturating_sub(self, rhs: Self) -> Self {
820 intrinsics::saturating_sub(self, rhs)
825 concat!("Saturating integer multiplication. Computes `self * rhs`,
826 saturating at the numeric bounds instead of overflowing.
834 assert_eq!(2", stringify!($SelfT), ".saturating_mul(10), 20);
835 assert_eq!((", stringify!($SelfT), "::MAX).saturating_mul(10), ", stringify!($SelfT),
836 "::MAX);", $EndFeature, "
838 #[stable(feature = "wrapping", since = "1.7.0")]
839 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
840 #[must_use = "this returns the result of the operation, \
841 without modifying the original"]
843 pub const fn saturating_mul(self, rhs: Self) -> Self {
844 match self.checked_mul(rhs) {
852 concat!("Saturating integer exponentiation. Computes `self.pow(exp)`,
853 saturating at the numeric bounds instead of overflowing.
861 assert_eq!(4", stringify!($SelfT), ".saturating_pow(3), 64);
862 assert_eq!(", stringify!($SelfT), "::MAX.saturating_pow(2), ", stringify!($SelfT), "::MAX);",
865 #[stable(feature = "no_panic_pow", since = "1.34.0")]
866 #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
867 #[must_use = "this returns the result of the operation, \
868 without modifying the original"]
870 pub const fn saturating_pow(self, exp: u32) -> Self {
871 match self.checked_pow(exp) {
879 concat!("Wrapping (modular) addition. Computes `self + rhs`,
880 wrapping around at the boundary of the type.
887 ", $Feature, "assert_eq!(200", stringify!($SelfT), ".wrapping_add(55), 255);
888 assert_eq!(200", stringify!($SelfT), ".wrapping_add(", stringify!($SelfT), "::MAX), 199);",
891 #[stable(feature = "rust1", since = "1.0.0")]
892 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
893 #[must_use = "this returns the result of the operation, \
894 without modifying the original"]
896 pub const fn wrapping_add(self, rhs: Self) -> Self {
897 intrinsics::wrapping_add(self, rhs)
902 concat!("Wrapping (modular) subtraction. Computes `self - rhs`,
903 wrapping around at the boundary of the type.
910 ", $Feature, "assert_eq!(100", stringify!($SelfT), ".wrapping_sub(100), 0);
911 assert_eq!(100", stringify!($SelfT), ".wrapping_sub(", stringify!($SelfT), "::MAX), 101);",
914 #[stable(feature = "rust1", since = "1.0.0")]
915 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
916 #[must_use = "this returns the result of the operation, \
917 without modifying the original"]
919 pub const fn wrapping_sub(self, rhs: Self) -> Self {
920 intrinsics::wrapping_sub(self, rhs)
924 /// Wrapping (modular) multiplication. Computes `self *
925 /// rhs`, wrapping around at the boundary of the type.
931 /// Please note that this example is shared between integer types.
932 /// Which explains why `u8` is used here.
935 /// assert_eq!(10u8.wrapping_mul(12), 120);
936 /// assert_eq!(25u8.wrapping_mul(12), 44);
938 #[stable(feature = "rust1", since = "1.0.0")]
939 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
940 #[must_use = "this returns the result of the operation, \
941 without modifying the original"]
943 pub const fn wrapping_mul(self, rhs: Self) -> Self {
944 intrinsics::wrapping_mul(self, rhs)
948 concat!("Wrapping (modular) division. Computes `self / rhs`.
949 Wrapped division on unsigned types is just normal division.
950 There's no way wrapping could ever happen.
951 This function exists, so that all operations
952 are accounted for in the wrapping operations.
959 ", $Feature, "assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);", $EndFeature, "
961 #[stable(feature = "num_wrapping", since = "1.2.0")]
962 #[rustc_const_unstable(feature = "const_wrapping_int_methods", issue = "53718")]
963 #[must_use = "this returns the result of the operation, \
964 without modifying the original"]
966 pub const fn wrapping_div(self, rhs: Self) -> Self {
972 concat!("Wrapping Euclidean division. Computes `self.div_euclid(rhs)`.
973 Wrapped division on unsigned types is just normal division.
974 There's no way wrapping could ever happen.
975 This function exists, so that all operations
976 are accounted for in the wrapping operations.
977 Since, for the positive integers, all common
978 definitions of division are equal, this
979 is exactly equal to `self.wrapping_div(rhs)`.
986 assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);
988 #[stable(feature = "euclidean_division", since = "1.38.0")]
989 #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
990 #[must_use = "this returns the result of the operation, \
991 without modifying the original"]
993 pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
999 concat!("Wrapping (modular) remainder. Computes `self % rhs`.
1000 Wrapped remainder calculation on unsigned types is
1001 just the regular remainder calculation.
1002 There's no way wrapping could ever happen.
1003 This function exists, so that all operations
1004 are accounted for in the wrapping operations.
1011 ", $Feature, "assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);", $EndFeature, "
1013 #[stable(feature = "num_wrapping", since = "1.2.0")]
1014 #[rustc_const_unstable(feature = "const_wrapping_int_methods", issue = "53718")]
1015 #[must_use = "this returns the result of the operation, \
1016 without modifying the original"]
1018 pub const fn wrapping_rem(self, rhs: Self) -> Self {
1024 concat!("Wrapping Euclidean modulo. Computes `self.rem_euclid(rhs)`.
1025 Wrapped modulo calculation on unsigned types is
1026 just the regular remainder calculation.
1027 There's no way wrapping could ever happen.
1028 This function exists, so that all operations
1029 are accounted for in the wrapping operations.
1030 Since, for the positive integers, all common
1031 definitions of division are equal, this
1032 is exactly equal to `self.wrapping_rem(rhs)`.
1039 assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);
1041 #[stable(feature = "euclidean_division", since = "1.38.0")]
1042 #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1043 #[must_use = "this returns the result of the operation, \
1044 without modifying the original"]
1046 pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
1051 /// Wrapping (modular) negation. Computes `-self`,
1052 /// wrapping around at the boundary of the type.
1054 /// Since unsigned types do not have negative equivalents
1055 /// all applications of this function will wrap (except for `-0`).
1056 /// For values smaller than the corresponding signed type's maximum
1057 /// the result is the same as casting the corresponding signed value.
1058 /// Any larger values are equivalent to `MAX + 1 - (val - MAX - 1)` where
1059 /// `MAX` is the corresponding signed type's maximum.
1065 /// Please note that this example is shared between integer types.
1066 /// Which explains why `i8` is used here.
1069 /// assert_eq!(100i8.wrapping_neg(), -100);
1070 /// assert_eq!((-128i8).wrapping_neg(), -128);
1072 #[stable(feature = "num_wrapping", since = "1.2.0")]
1073 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1075 pub const fn wrapping_neg(self) -> Self {
1076 self.overflowing_neg().0
1080 concat!("Panic-free bitwise shift-left; yields `self << mask(rhs)`,
1081 where `mask` removes any high-order bits of `rhs` that
1082 would cause the shift to exceed the bitwidth of the type.
1084 Note that this is *not* the same as a rotate-left; the
1085 RHS of a wrapping shift-left is restricted to the range
1086 of the type, rather than the bits shifted out of the LHS
1087 being returned to the other end. The primitive integer
1088 types all implement a [`rotate_left`](#method.rotate_left) function,
1089 which may be what you want instead.
1096 ", $Feature, "assert_eq!(1", stringify!($SelfT), ".wrapping_shl(7), 128);
1097 assert_eq!(1", stringify!($SelfT), ".wrapping_shl(128), 1);", $EndFeature, "
1099 #[stable(feature = "num_wrapping", since = "1.2.0")]
1100 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1101 #[must_use = "this returns the result of the operation, \
1102 without modifying the original"]
1104 pub const fn wrapping_shl(self, rhs: u32) -> Self {
1105 // SAFETY: the masking by the bitsize of the type ensures that we do not shift
1108 intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
1114 concat!("Panic-free bitwise shift-right; yields `self >> mask(rhs)`,
1115 where `mask` removes any high-order bits of `rhs` that
1116 would cause the shift to exceed the bitwidth of the type.
1118 Note that this is *not* the same as a rotate-right; the
1119 RHS of a wrapping shift-right is restricted to the range
1120 of the type, rather than the bits shifted out of the LHS
1121 being returned to the other end. The primitive integer
1122 types all implement a [`rotate_right`](#method.rotate_right) function,
1123 which may be what you want instead.
1130 ", $Feature, "assert_eq!(128", stringify!($SelfT), ".wrapping_shr(7), 1);
1131 assert_eq!(128", stringify!($SelfT), ".wrapping_shr(128), 128);", $EndFeature, "
1133 #[stable(feature = "num_wrapping", since = "1.2.0")]
1134 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1135 #[must_use = "this returns the result of the operation, \
1136 without modifying the original"]
1138 pub const fn wrapping_shr(self, rhs: u32) -> Self {
1139 // SAFETY: the masking by the bitsize of the type ensures that we do not shift
1142 intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
1148 concat!("Wrapping (modular) exponentiation. Computes `self.pow(exp)`,
1149 wrapping around at the boundary of the type.
1156 ", $Feature, "assert_eq!(3", stringify!($SelfT), ".wrapping_pow(5), 243);
1157 assert_eq!(3u8.wrapping_pow(6), 217);", $EndFeature, "
1159 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1160 #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
1161 #[must_use = "this returns the result of the operation, \
1162 without modifying the original"]
1164 pub const fn wrapping_pow(self, mut exp: u32) -> Self {
1168 let mut base = self;
1169 let mut acc: Self = 1;
1173 acc = acc.wrapping_mul(base);
1176 base = base.wrapping_mul(base);
1179 // since exp!=0, finally the exp must be 1.
1180 // Deal with the final bit of the exponent separately, since
1181 // squaring the base afterwards is not necessary and may cause a
1182 // needless overflow.
1183 acc.wrapping_mul(base)
1188 concat!("Calculates `self` + `rhs`
1190 Returns a tuple of the addition along with a boolean indicating
1191 whether an arithmetic overflow would occur. If an overflow would
1192 have occurred then the wrapped value is returned.
1200 assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));
1201 assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (0, true));", $EndFeature, "
1203 #[stable(feature = "wrapping", since = "1.7.0")]
1204 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1205 #[must_use = "this returns the result of the operation, \
1206 without modifying the original"]
1208 pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1209 let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
1215 concat!("Calculates `self` - `rhs`
1217 Returns a tuple of the subtraction along with a boolean indicating
1218 whether an arithmetic overflow would occur. If an overflow would
1219 have occurred then the wrapped value is returned.
1227 assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));
1228 assert_eq!(0", stringify!($SelfT), ".overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));",
1231 #[stable(feature = "wrapping", since = "1.7.0")]
1232 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1233 #[must_use = "this returns the result of the operation, \
1234 without modifying the original"]
1236 pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
1237 let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
1242 /// Calculates the multiplication of `self` and `rhs`.
1244 /// Returns a tuple of the multiplication along with a boolean
1245 /// indicating whether an arithmetic overflow would occur. If an
1246 /// overflow would have occurred then the wrapped value is returned.
1252 /// Please note that this example is shared between integer types.
1253 /// Which explains why `u32` is used here.
1256 /// assert_eq!(5u32.overflowing_mul(2), (10, false));
1257 /// assert_eq!(1_000_000_000u32.overflowing_mul(10), (1410065408, true));
1259 #[stable(feature = "wrapping", since = "1.7.0")]
1260 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1261 #[must_use = "this returns the result of the operation, \
1262 without modifying the original"]
1264 pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
1265 let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
1270 concat!("Calculates the divisor when `self` is divided by `rhs`.
1272 Returns a tuple of the divisor along with a boolean indicating
1273 whether an arithmetic overflow would occur. Note that for unsigned
1274 integers overflow never occurs, so the second value is always
1279 This function will panic if `rhs` is 0.
1286 ", $Feature, "assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));", $EndFeature, "
1289 #[stable(feature = "wrapping", since = "1.7.0")]
1290 #[rustc_const_unstable(feature = "const_overflowing_int_methods", issue = "53718")]
1291 #[must_use = "this returns the result of the operation, \
1292 without modifying the original"]
1293 pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1299 concat!("Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
1301 Returns a tuple of the divisor along with a boolean indicating
1302 whether an arithmetic overflow would occur. Note that for unsigned
1303 integers overflow never occurs, so the second value is always
1305 Since, for the positive integers, all common
1306 definitions of division are equal, this
1307 is exactly equal to `self.overflowing_div(rhs)`.
1311 This function will panic if `rhs` is 0.
1318 assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));
1321 #[stable(feature = "euclidean_division", since = "1.38.0")]
1322 #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1323 #[must_use = "this returns the result of the operation, \
1324 without modifying the original"]
1325 pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
1331 concat!("Calculates the remainder when `self` is divided by `rhs`.
1333 Returns a tuple of the remainder after dividing along with a boolean
1334 indicating whether an arithmetic overflow would occur. Note that for
1335 unsigned integers overflow never occurs, so the second value is
1340 This function will panic if `rhs` is 0.
1347 ", $Feature, "assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));", $EndFeature, "
1350 #[stable(feature = "wrapping", since = "1.7.0")]
1351 #[rustc_const_unstable(feature = "const_overflowing_int_methods", issue = "53718")]
1352 #[must_use = "this returns the result of the operation, \
1353 without modifying the original"]
1354 pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1360 concat!("Calculates the remainder `self.rem_euclid(rhs)` as if by Euclidean division.
1362 Returns a tuple of the modulo after dividing along with a boolean
1363 indicating whether an arithmetic overflow would occur. Note that for
1364 unsigned integers overflow never occurs, so the second value is
1366 Since, for the positive integers, all common
1367 definitions of division are equal, this operation
1368 is exactly equal to `self.overflowing_rem(rhs)`.
1372 This function will panic if `rhs` is 0.
1379 assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));
1382 #[stable(feature = "euclidean_division", since = "1.38.0")]
1383 #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1384 #[must_use = "this returns the result of the operation, \
1385 without modifying the original"]
1386 pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1392 concat!("Negates self in an overflowing fashion.
1394 Returns `!self + 1` using wrapping operations to return the value
1395 that represents the negation of this unsigned value. Note that for
1396 positive unsigned values overflow always occurs, but negating 0 does
1404 ", $Feature, "assert_eq!(0", stringify!($SelfT), ".overflowing_neg(), (0, false));
1405 assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2i32 as ", stringify!($SelfT),
1406 ", true));", $EndFeature, "
1409 #[stable(feature = "wrapping", since = "1.7.0")]
1410 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1411 pub const fn overflowing_neg(self) -> (Self, bool) {
1412 ((!self).wrapping_add(1), self != 0)
1417 concat!("Shifts self left by `rhs` bits.
1419 Returns a tuple of the shifted version of self along with a boolean
1420 indicating whether the shift value was larger than or equal to the
1421 number of bits. If the shift value is too large, then value is
1422 masked (N-1) where N is the number of bits, and this value is then
1423 used to perform the shift.
1430 ", $Feature, "assert_eq!(0x1", stringify!($SelfT), ".overflowing_shl(4), (0x10, false));
1431 assert_eq!(0x1", stringify!($SelfT), ".overflowing_shl(132), (0x10, true));", $EndFeature, "
1433 #[stable(feature = "wrapping", since = "1.7.0")]
1434 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1435 #[must_use = "this returns the result of the operation, \
1436 without modifying the original"]
1438 pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1439 (self.wrapping_shl(rhs), (rhs > ($BITS - 1)))
1444 concat!("Shifts self right by `rhs` bits.
1446 Returns a tuple of the shifted version of self along with a boolean
1447 indicating whether the shift value was larger than or equal to the
1448 number of bits. If the shift value is too large, then value is
1449 masked (N-1) where N is the number of bits, and this value is then
1450 used to perform the shift.
1457 ", $Feature, "assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));
1458 assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(132), (0x1, true));", $EndFeature, "
1460 #[stable(feature = "wrapping", since = "1.7.0")]
1461 #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1462 #[must_use = "this returns the result of the operation, \
1463 without modifying the original"]
1465 pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1466 (self.wrapping_shr(rhs), (rhs > ($BITS - 1)))
1471 concat!("Raises self to the power of `exp`, using exponentiation by squaring.
1473 Returns a tuple of the exponentiation along with a bool indicating
1474 whether an overflow happened.
1481 ", $Feature, "assert_eq!(3", stringify!($SelfT), ".overflowing_pow(5), (243, false));
1482 assert_eq!(3u8.overflowing_pow(6), (217, true));", $EndFeature, "
1484 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1485 #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
1486 #[must_use = "this returns the result of the operation, \
1487 without modifying the original"]
1489 pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
1493 let mut base = self;
1494 let mut acc: Self = 1;
1495 let mut overflown = false;
1496 // Scratch space for storing results of overflowing_mul.
1501 r = acc.overflowing_mul(base);
1506 r = base.overflowing_mul(base);
1511 // since exp!=0, finally the exp must be 1.
1512 // Deal with the final bit of the exponent separately, since
1513 // squaring the base afterwards is not necessary and may cause a
1514 // needless overflow.
1515 r = acc.overflowing_mul(base);
1523 concat!("Raises self to the power of `exp`, using exponentiation by squaring.
1530 ", $Feature, "assert_eq!(2", stringify!($SelfT), ".pow(5), 32);", $EndFeature, "
1532 #[stable(feature = "rust1", since = "1.0.0")]
1533 #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
1534 #[must_use = "this returns the result of the operation, \
1535 without modifying the original"]
1537 #[rustc_inherit_overflow_checks]
1538 pub const fn pow(self, mut exp: u32) -> Self {
1542 let mut base = self;
1553 // since exp!=0, finally the exp must be 1.
1554 // Deal with the final bit of the exponent separately, since
1555 // squaring the base afterwards is not necessary and may cause a
1556 // needless overflow.
1562 concat!("Performs Euclidean division.
1564 Since, for the positive integers, all common
1565 definitions of division are equal, this
1566 is exactly equal to `self / rhs`.
1570 This function will panic if `rhs` is 0.
1577 assert_eq!(7", stringify!($SelfT), ".div_euclid(4), 1); // or any other integer type
1579 #[stable(feature = "euclidean_division", since = "1.38.0")]
1580 #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1581 #[must_use = "this returns the result of the operation, \
1582 without modifying the original"]
1584 #[rustc_inherit_overflow_checks]
1585 pub const fn div_euclid(self, rhs: Self) -> Self {
1592 concat!("Calculates the least remainder of `self (mod rhs)`.
1594 Since, for the positive integers, all common
1595 definitions of division are equal, this
1596 is exactly equal to `self % rhs`.
1600 This function will panic if `rhs` is 0.
1607 assert_eq!(7", stringify!($SelfT), ".rem_euclid(4), 3); // or any other integer type
1609 #[stable(feature = "euclidean_division", since = "1.38.0")]
1610 #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")]
1611 #[must_use = "this returns the result of the operation, \
1612 without modifying the original"]
1614 #[rustc_inherit_overflow_checks]
1615 pub const fn rem_euclid(self, rhs: Self) -> Self {
1621 concat!("Returns `true` if and only if `self == 2^k` for some `k`.
1628 ", $Feature, "assert!(16", stringify!($SelfT), ".is_power_of_two());
1629 assert!(!10", stringify!($SelfT), ".is_power_of_two());", $EndFeature, "
1631 #[stable(feature = "rust1", since = "1.0.0")]
1632 #[rustc_const_stable(feature = "const_is_power_of_two", since = "1.32.0")]
1634 pub const fn is_power_of_two(self) -> bool {
1635 self.count_ones() == 1
1639 // Returns one less than next power of two.
1640 // (For 8u8 next power of two is 8u8 and for 6u8 it is 8u8)
1642 // 8u8.one_less_than_next_power_of_two() == 7
1643 // 6u8.one_less_than_next_power_of_two() == 7
1645 // This method cannot overflow, as in the `next_power_of_two`
1646 // overflow cases it instead ends up returning the maximum value
1647 // of the type, and can return 0 for 0.
1649 #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
1650 const fn one_less_than_next_power_of_two(self) -> Self {
1651 if self <= 1 { return 0; }
1654 // SAFETY: Because `p > 0`, it cannot consist entirely of leading zeros.
1655 // That means the shift is always in-bounds, and some processors
1656 // (such as intel pre-haswell) have more efficient ctlz
1657 // intrinsics when the argument is non-zero.
1658 let z = unsafe { intrinsics::ctlz_nonzero(p) };
1663 concat!("Returns the smallest power of two greater than or equal to `self`.
1665 When return value overflows (i.e., `self > (1 << (N-1))` for type
1666 `uN`), it panics in debug mode and return value is wrapped to 0 in
1667 release mode (the only situation in which method can return 0).
1674 ", $Feature, "assert_eq!(2", stringify!($SelfT), ".next_power_of_two(), 2);
1675 assert_eq!(3", stringify!($SelfT), ".next_power_of_two(), 4);", $EndFeature, "
1677 #[stable(feature = "rust1", since = "1.0.0")]
1678 #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
1680 #[rustc_inherit_overflow_checks]
1681 pub const fn next_power_of_two(self) -> Self {
1682 self.one_less_than_next_power_of_two() + 1
1687 concat!("Returns the smallest power of two greater than or equal to `n`. If
1688 the next power of two is greater than the type's maximum value,
1689 `None` is returned, otherwise the power of two is wrapped in `Some`.
1696 ", $Feature, "assert_eq!(2", stringify!($SelfT),
1697 ".checked_next_power_of_two(), Some(2));
1698 assert_eq!(3", stringify!($SelfT), ".checked_next_power_of_two(), Some(4));
1699 assert_eq!(", stringify!($SelfT), "::MAX.checked_next_power_of_two(), None);",
1703 #[stable(feature = "rust1", since = "1.0.0")]
1704 #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
1705 pub const fn checked_next_power_of_two(self) -> Option<Self> {
1706 self.one_less_than_next_power_of_two().checked_add(1)
1711 concat!("Returns the smallest power of two greater than or equal to `n`. If
1712 the next power of two is greater than the type's maximum value,
1713 the return value is wrapped to `0`.
1720 #![feature(wrapping_next_power_of_two)]
1722 assert_eq!(2", stringify!($SelfT), ".wrapping_next_power_of_two(), 2);
1723 assert_eq!(3", stringify!($SelfT), ".wrapping_next_power_of_two(), 4);
1724 assert_eq!(", stringify!($SelfT), "::MAX.wrapping_next_power_of_two(), 0);",
1727 #[unstable(feature = "wrapping_next_power_of_two", issue = "32463",
1728 reason = "needs decision on wrapping behaviour")]
1729 #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")]
1730 pub const fn wrapping_next_power_of_two(self) -> Self {
1731 self.one_less_than_next_power_of_two().wrapping_add(1)
1736 concat!("Return the memory representation of this integer as a byte array in
1737 big-endian (network) byte order.
1744 let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();
1745 assert_eq!(bytes, ", $be_bytes, ");
1747 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
1748 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
1750 pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
1751 self.to_be().to_ne_bytes()
1756 concat!("Return the memory representation of this integer as a byte array in
1757 little-endian byte order.
1764 let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();
1765 assert_eq!(bytes, ", $le_bytes, ");
1767 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
1768 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
1770 pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
1771 self.to_le().to_ne_bytes()
1777 Return the memory representation of this integer as a byte array in
1780 As the target platform's native endianness is used, portable code
1781 should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
1786 [`to_be_bytes`]: #method.to_be_bytes
1787 [`to_le_bytes`]: #method.to_le_bytes
1792 let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();
1795 if cfg!(target_endian = \"big\") {
1802 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
1803 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
1804 // SAFETY: const sound because integers are plain old datatypes so we can always
1805 // transmute them to arrays of bytes
1806 #[allow_internal_unstable(const_fn_transmute)]
1808 pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
1809 // SAFETY: integers are plain old datatypes so we can always transmute them to
1811 unsafe { mem::transmute(self) }
1816 concat!("Create a native endian integer value from its representation
1817 as a byte array in big endian.
1824 let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");
1825 assert_eq!(value, ", $swap_op, ");
1828 When starting from a slice rather than an array, fallible conversion APIs can be used:
1831 use std::convert::TryInto;
1833 fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {
1834 let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());
1836 ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())
1839 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
1840 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
1842 pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
1843 Self::from_be(Self::from_ne_bytes(bytes))
1849 Create a native endian integer value from its representation
1850 as a byte array in little endian.
1857 let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");
1858 assert_eq!(value, ", $swap_op, ");
1861 When starting from a slice rather than an array, fallible conversion APIs can be used:
1864 use std::convert::TryInto;
1866 fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {
1867 let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());
1869 ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())
1872 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
1873 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
1875 pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
1876 Self::from_le(Self::from_ne_bytes(bytes))
1881 concat!("Create a native endian integer value from its memory representation
1882 as a byte array in native endianness.
1884 As the target platform's native endianness is used, portable code
1885 likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
1886 appropriate instead.
1888 [`from_be_bytes`]: #method.from_be_bytes
1889 [`from_le_bytes`]: #method.from_le_bytes
1896 let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {
1901 assert_eq!(value, ", $swap_op, ");
1904 When starting from a slice rather than an array, fallible conversion APIs can be used:
1907 use std::convert::TryInto;
1909 fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {
1910 let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());
1912 ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())
1915 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
1916 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
1917 // SAFETY: const sound because integers are plain old datatypes so we can always
1918 // transmute to them
1919 #[allow_internal_unstable(const_fn_transmute)]
1921 pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
1922 // SAFETY: integers are plain old datatypes so we can always transmute to them
1923 unsafe { mem::transmute(bytes) }
1928 concat!("**This method is soft-deprecated.**
1930 Although using it won’t cause compilation warning,
1931 new code should use [`", stringify!($SelfT), "::MIN", "`](#associatedconstant.MIN) instead.
1933 Returns the smallest value that can be represented by this integer type."),
1934 #[stable(feature = "rust1", since = "1.0.0")]
1937 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
1938 pub const fn min_value() -> Self { Self::MIN }
1942 concat!("**This method is soft-deprecated.**
1944 Although using it won’t cause compilation warning,
1945 new code should use [`", stringify!($SelfT), "::MAX", "`](#associatedconstant.MAX) instead.
1947 Returns the largest value that can be represented by this integer type."),
1948 #[stable(feature = "rust1", since = "1.0.0")]
1951 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
1952 pub const fn max_value() -> Self { Self::MAX }