1 macro_rules! int_impl {
2 ($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, $BITS_MINUS_ONE:expr, $Min:expr, $Max: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) => {
6 /// The smallest value that can be represented by this integer type,
7 #[doc = concat!("-2<sup>", $BITS_MINUS_ONE, "</sup>.")]
14 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");")]
16 #[stable(feature = "assoc_int_consts", since = "1.43.0")]
17 pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self;
19 /// The largest value that can be represented by this integer type,
20 #[doc = concat!("2<sup>", $BITS_MINUS_ONE, "</sup> - 1.")]
27 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");")]
29 #[stable(feature = "assoc_int_consts", since = "1.43.0")]
30 pub const MAX: Self = !Self::MIN;
32 /// The size of this integer type in bits.
37 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");")]
39 #[stable(feature = "int_bits_const", since = "1.53.0")]
40 pub const BITS: u32 = $BITS;
42 /// Converts a string slice in a given base to an integer.
44 /// The string is expected to be an optional `+` or `-` sign followed by digits.
45 /// Leading and trailing whitespace represent an error. Digits are a subset of these characters,
46 /// depending on `radix`:
54 /// This function panics if `radix` is not in the range from 2 to 36.
61 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::from_str_radix(\"A\", 16), Ok(10));")]
63 #[stable(feature = "rust1", since = "1.0.0")]
64 pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
65 from_str_radix(src, radix)
68 /// Returns the number of ones in the binary representation of `self`.
75 #[doc = concat!("let n = 0b100_0000", stringify!($SelfT), ";")]
77 /// assert_eq!(n.count_ones(), 1);
80 #[stable(feature = "rust1", since = "1.0.0")]
81 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
82 #[doc(alias = "popcount")]
83 #[doc(alias = "popcnt")]
85 pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
87 /// Returns the number of zeros in the binary representation of `self`.
94 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 1);")]
96 #[stable(feature = "rust1", since = "1.0.0")]
97 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
99 pub const fn count_zeros(self) -> u32 {
103 /// Returns the number of leading zeros in the binary representation of `self`.
110 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
112 /// assert_eq!(n.leading_zeros(), 0);
114 #[stable(feature = "rust1", since = "1.0.0")]
115 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
117 pub const fn leading_zeros(self) -> u32 {
118 (self as $UnsignedT).leading_zeros()
121 /// Returns the number of trailing zeros in the binary representation of `self`.
128 #[doc = concat!("let n = -4", stringify!($SelfT), ";")]
130 /// assert_eq!(n.trailing_zeros(), 2);
132 #[stable(feature = "rust1", since = "1.0.0")]
133 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
135 pub const fn trailing_zeros(self) -> u32 {
136 (self as $UnsignedT).trailing_zeros()
139 /// Returns the number of leading ones in the binary representation of `self`.
146 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
148 #[doc = concat!("assert_eq!(n.leading_ones(), ", stringify!($BITS), ");")]
150 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
151 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
153 pub const fn leading_ones(self) -> u32 {
154 (self as $UnsignedT).leading_ones()
157 /// Returns the number of trailing ones in the binary representation of `self`.
164 #[doc = concat!("let n = 3", stringify!($SelfT), ";")]
166 /// assert_eq!(n.trailing_ones(), 2);
168 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
169 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
171 pub const fn trailing_ones(self) -> u32 {
172 (self as $UnsignedT).trailing_ones()
175 /// Shifts the bits to the left by a specified amount, `n`,
176 /// wrapping the truncated bits to the end of the resulting integer.
178 /// Please note this isn't the same operation as the `<<` shifting operator!
185 #[doc = concat!("let n = ", $rot_op, stringify!($SelfT), ";")]
186 #[doc = concat!("let m = ", $rot_result, ";")]
188 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
190 #[stable(feature = "rust1", since = "1.0.0")]
191 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
192 #[must_use = "this returns the result of the operation, \
193 without modifying the original"]
195 pub const fn rotate_left(self, n: u32) -> Self {
196 (self as $UnsignedT).rotate_left(n) as Self
199 /// Shifts the bits to the right by a specified amount, `n`,
200 /// wrapping the truncated bits to the beginning of the resulting
203 /// Please note this isn't the same operation as the `>>` shifting operator!
210 #[doc = concat!("let n = ", $rot_result, stringify!($SelfT), ";")]
211 #[doc = concat!("let m = ", $rot_op, ";")]
213 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
215 #[stable(feature = "rust1", since = "1.0.0")]
216 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
217 #[must_use = "this returns the result of the operation, \
218 without modifying the original"]
220 pub const fn rotate_right(self, n: u32) -> Self {
221 (self as $UnsignedT).rotate_right(n) as Self
224 /// Reverses the byte order of the integer.
231 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
233 /// let m = n.swap_bytes();
235 #[doc = concat!("assert_eq!(m, ", $swapped, ");")]
237 #[stable(feature = "rust1", since = "1.0.0")]
238 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
240 pub const fn swap_bytes(self) -> Self {
241 (self as $UnsignedT).swap_bytes() as Self
244 /// Reverses the order of bits in the integer. The least significant bit becomes the most significant bit,
245 /// second least-significant bit becomes second most-significant bit, etc.
252 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
253 /// let m = n.reverse_bits();
255 #[doc = concat!("assert_eq!(m, ", $reversed, ");")]
256 #[doc = concat!("assert_eq!(0, 0", stringify!($SelfT), ".reverse_bits());")]
258 #[stable(feature = "reverse_bits", since = "1.37.0")]
259 #[rustc_const_stable(feature = "const_int_methods", since = "1.37.0")]
262 pub const fn reverse_bits(self) -> Self {
263 (self as $UnsignedT).reverse_bits() as Self
266 /// Converts an integer from big endian to the target's endianness.
268 /// On big endian this is a no-op. On little endian the bytes are swapped.
275 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
277 /// if cfg!(target_endian = "big") {
278 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n)")]
280 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())")]
283 #[stable(feature = "rust1", since = "1.0.0")]
284 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
286 pub const fn from_be(x: Self) -> Self {
287 #[cfg(target_endian = "big")]
291 #[cfg(not(target_endian = "big"))]
297 /// Converts an integer from little endian to the target's endianness.
299 /// On little endian this is a no-op. On big endian the bytes are swapped.
306 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
308 /// if cfg!(target_endian = "little") {
309 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n)")]
311 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())")]
314 #[stable(feature = "rust1", since = "1.0.0")]
315 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
317 pub const fn from_le(x: Self) -> Self {
318 #[cfg(target_endian = "little")]
322 #[cfg(not(target_endian = "little"))]
328 /// Converts `self` to big endian from the target's endianness.
330 /// On big endian this is a no-op. On little endian the bytes are swapped.
337 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
339 /// if cfg!(target_endian = "big") {
340 /// assert_eq!(n.to_be(), n)
342 /// assert_eq!(n.to_be(), n.swap_bytes())
345 #[stable(feature = "rust1", since = "1.0.0")]
346 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
348 pub const fn to_be(self) -> Self { // or not to be?
349 #[cfg(target_endian = "big")]
353 #[cfg(not(target_endian = "big"))]
359 /// Converts `self` to little endian from the target's endianness.
361 /// On little endian this is a no-op. On big endian the bytes are swapped.
368 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
370 /// if cfg!(target_endian = "little") {
371 /// assert_eq!(n.to_le(), n)
373 /// assert_eq!(n.to_le(), n.swap_bytes())
376 #[stable(feature = "rust1", since = "1.0.0")]
377 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
379 pub const fn to_le(self) -> Self {
380 #[cfg(target_endian = "little")]
384 #[cfg(not(target_endian = "little"))]
390 /// Checked integer addition. Computes `self + rhs`, returning `None`
391 /// if overflow occurred.
398 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(1), Some(", stringify!($SelfT), "::MAX - 1));")]
399 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);")]
401 #[stable(feature = "rust1", since = "1.0.0")]
402 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
403 #[must_use = "this returns the result of the operation, \
404 without modifying the original"]
406 pub const fn checked_add(self, rhs: Self) -> Option<Self> {
407 let (a, b) = self.overflowing_add(rhs);
408 if unlikely!(b) {None} else {Some(a)}
411 /// Unchecked integer addition. Computes `self + rhs`, assuming overflow
416 /// This results in undefined behavior when
417 #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
418 /// i.e. when [`checked_add`] would return `None`.
420 #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
422 feature = "unchecked_math",
423 reason = "niche optimization path",
426 #[must_use = "this returns the result of the operation, \
427 without modifying the original"]
428 #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
430 pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
431 // SAFETY: the caller must uphold the safety contract for
433 unsafe { intrinsics::unchecked_add(self, rhs) }
436 /// Checked integer subtraction. Computes `self - rhs`, returning `None` if
437 /// overflow occurred.
444 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(1), Some(", stringify!($SelfT), "::MIN + 1));")]
445 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(3), None);")]
447 #[stable(feature = "rust1", since = "1.0.0")]
448 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
449 #[must_use = "this returns the result of the operation, \
450 without modifying the original"]
452 pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
453 let (a, b) = self.overflowing_sub(rhs);
454 if unlikely!(b) {None} else {Some(a)}
457 /// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
462 /// This results in undefined behavior when
463 #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
464 /// i.e. when [`checked_sub`] would return `None`.
466 #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
468 feature = "unchecked_math",
469 reason = "niche optimization path",
472 #[must_use = "this returns the result of the operation, \
473 without modifying the original"]
474 #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
476 pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
477 // SAFETY: the caller must uphold the safety contract for
479 unsafe { intrinsics::unchecked_sub(self, rhs) }
482 /// Checked integer multiplication. Computes `self * rhs`, returning `None` if
483 /// overflow occurred.
490 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(1), Some(", stringify!($SelfT), "::MAX));")]
491 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);")]
493 #[stable(feature = "rust1", since = "1.0.0")]
494 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
495 #[must_use = "this returns the result of the operation, \
496 without modifying the original"]
498 pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
499 let (a, b) = self.overflowing_mul(rhs);
500 if unlikely!(b) {None} else {Some(a)}
503 /// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
508 /// This results in undefined behavior when
509 #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
510 /// i.e. when [`checked_mul`] would return `None`.
512 #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
514 feature = "unchecked_math",
515 reason = "niche optimization path",
518 #[must_use = "this returns the result of the operation, \
519 without modifying the original"]
520 #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
522 pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
523 // SAFETY: the caller must uphold the safety contract for
525 unsafe { intrinsics::unchecked_mul(self, rhs) }
528 /// Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0`
529 /// or the division results in overflow.
536 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div(-1), Some(", stringify!($Max), "));")]
537 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div(-1), None);")]
538 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);")]
540 #[stable(feature = "rust1", since = "1.0.0")]
541 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.52.0")]
542 #[must_use = "this returns the result of the operation, \
543 without modifying the original"]
545 pub const fn checked_div(self, rhs: Self) -> Option<Self> {
546 if unlikely!(rhs == 0 || (self == Self::MIN && rhs == -1)) {
549 // SAFETY: div by zero and by INT_MIN have been checked above
550 Some(unsafe { intrinsics::unchecked_div(self, rhs) })
554 /// Checked Euclidean division. Computes `self.div_euclid(rhs)`,
555 /// returning `None` if `rhs == 0` or the division results in overflow.
562 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_euclid(-1), Some(", stringify!($Max), "));")]
563 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_euclid(-1), None);")]
564 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);")]
566 #[stable(feature = "euclidean_division", since = "1.38.0")]
567 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
568 #[must_use = "this returns the result of the operation, \
569 without modifying the original"]
571 pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
572 if unlikely!(rhs == 0 || (self == Self::MIN && rhs == -1)) {
575 Some(self.div_euclid(rhs))
579 /// Checked integer remainder. Computes `self % rhs`, returning `None` if
580 /// `rhs == 0` or the division results in overflow.
588 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));")]
589 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
590 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);")]
592 #[stable(feature = "wrapping", since = "1.7.0")]
593 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.52.0")]
594 #[must_use = "this returns the result of the operation, \
595 without modifying the original"]
597 pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
598 if unlikely!(rhs == 0 || (self == Self::MIN && rhs == -1)) {
601 // SAFETY: div by zero and by INT_MIN have been checked above
602 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
606 /// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None`
607 /// if `rhs == 0` or the division results in overflow.
614 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));")]
615 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
616 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);")]
618 #[stable(feature = "euclidean_division", since = "1.38.0")]
619 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
620 #[must_use = "this returns the result of the operation, \
621 without modifying the original"]
623 pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
624 if unlikely!(rhs == 0 || (self == Self::MIN && rhs == -1)) {
627 Some(self.rem_euclid(rhs))
631 /// Checked negation. Computes `-self`, returning `None` if `self == MIN`.
639 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5));")]
640 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);")]
642 #[stable(feature = "wrapping", since = "1.7.0")]
643 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
645 pub const fn checked_neg(self) -> Option<Self> {
646 let (a, b) = self.overflowing_neg();
647 if unlikely!(b) {None} else {Some(a)}
650 /// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger
651 /// than or equal to the number of bits in `self`.
658 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));")]
659 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);")]
661 #[stable(feature = "wrapping", since = "1.7.0")]
662 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
663 #[must_use = "this returns the result of the operation, \
664 without modifying the original"]
666 pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
667 let (a, b) = self.overflowing_shl(rhs);
668 if unlikely!(b) {None} else {Some(a)}
671 /// Unchecked shift left. Computes `self << rhs`, assuming that
672 /// `rhs` is less than the number of bits in `self`.
676 /// This results in undefined behavior if `rhs` is larger than
677 /// or equal to the number of bits in `self`,
678 /// i.e. when [`checked_shl`] would return `None`.
680 #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
682 feature = "unchecked_math",
683 reason = "niche optimization path",
686 #[must_use = "this returns the result of the operation, \
687 without modifying the original"]
688 #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
690 pub const unsafe fn unchecked_shl(self, rhs: Self) -> Self {
691 // SAFETY: the caller must uphold the safety contract for
693 unsafe { intrinsics::unchecked_shl(self, rhs) }
696 /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
697 /// larger than or equal to the number of bits in `self`.
704 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
705 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
707 #[stable(feature = "wrapping", since = "1.7.0")]
708 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
709 #[must_use = "this returns the result of the operation, \
710 without modifying the original"]
712 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
713 let (a, b) = self.overflowing_shr(rhs);
714 if unlikely!(b) {None} else {Some(a)}
717 /// Unchecked shift right. Computes `self >> rhs`, assuming that
718 /// `rhs` is less than the number of bits in `self`.
722 /// This results in undefined behavior if `rhs` is larger than
723 /// or equal to the number of bits in `self`,
724 /// i.e. when [`checked_shr`] would return `None`.
726 #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
728 feature = "unchecked_math",
729 reason = "niche optimization path",
732 #[must_use = "this returns the result of the operation, \
733 without modifying the original"]
734 #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
736 pub const unsafe fn unchecked_shr(self, rhs: Self) -> Self {
737 // SAFETY: the caller must uphold the safety contract for
739 unsafe { intrinsics::unchecked_shr(self, rhs) }
742 /// Checked absolute value. Computes `self.abs()`, returning `None` if
751 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
752 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
754 #[stable(feature = "no_panic_abs", since = "1.13.0")]
755 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
757 pub const fn checked_abs(self) -> Option<Self> {
758 if self.is_negative() {
765 /// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
766 /// overflow occurred.
773 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
774 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
777 #[stable(feature = "no_panic_pow", since = "1.34.0")]
778 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
779 #[must_use = "this returns the result of the operation, \
780 without modifying the original"]
782 pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
787 let mut acc: Self = 1;
791 acc = try_opt!(acc.checked_mul(base));
794 base = try_opt!(base.checked_mul(base));
796 // since exp!=0, finally the exp must be 1.
797 // Deal with the final bit of the exponent separately, since
798 // squaring the base afterwards is not necessary and may cause a
799 // needless overflow.
800 Some(try_opt!(acc.checked_mul(base)))
803 /// Saturating integer addition. Computes `self + rhs`, saturating at the numeric
804 /// bounds instead of overflowing.
811 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
812 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
813 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
816 #[stable(feature = "rust1", since = "1.0.0")]
817 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
818 #[must_use = "this returns the result of the operation, \
819 without modifying the original"]
821 pub const fn saturating_add(self, rhs: Self) -> Self {
822 intrinsics::saturating_add(self, rhs)
825 /// Saturating integer subtraction. Computes `self - rhs`, saturating at the
826 /// numeric bounds instead of overflowing.
833 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
834 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
835 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
837 #[stable(feature = "rust1", since = "1.0.0")]
838 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
839 #[must_use = "this returns the result of the operation, \
840 without modifying the original"]
842 pub const fn saturating_sub(self, rhs: Self) -> Self {
843 intrinsics::saturating_sub(self, rhs)
846 /// Saturating integer negation. Computes `-self`, returning `MAX` if `self == MIN`
847 /// instead of overflowing.
854 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
855 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
856 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
857 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
860 #[stable(feature = "saturating_neg", since = "1.45.0")]
861 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
863 pub const fn saturating_neg(self) -> Self {
864 intrinsics::saturating_sub(0, self)
867 /// Saturating absolute value. Computes `self.abs()`, returning `MAX` if `self ==
868 /// MIN` instead of overflowing.
875 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
876 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
877 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
878 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
881 #[stable(feature = "saturating_neg", since = "1.45.0")]
882 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
884 pub const fn saturating_abs(self) -> Self {
885 if self.is_negative() {
886 self.saturating_neg()
892 /// Saturating integer multiplication. Computes `self * rhs`, saturating at the
893 /// numeric bounds instead of overflowing.
901 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
902 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
903 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
905 #[stable(feature = "wrapping", since = "1.7.0")]
906 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
907 #[must_use = "this returns the result of the operation, \
908 without modifying the original"]
910 pub const fn saturating_mul(self, rhs: Self) -> Self {
911 match self.checked_mul(rhs) {
913 None => if (self < 0) == (rhs < 0) {
921 /// Saturating integer exponentiation. Computes `self.pow(exp)`,
922 /// saturating at the numeric bounds instead of overflowing.
930 #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
931 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
932 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
934 #[stable(feature = "no_panic_pow", since = "1.34.0")]
935 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
936 #[must_use = "this returns the result of the operation, \
937 without modifying the original"]
939 pub const fn saturating_pow(self, exp: u32) -> Self {
940 match self.checked_pow(exp) {
942 None if self < 0 && exp % 2 == 1 => Self::MIN,
947 /// Wrapping (modular) addition. Computes `self + rhs`, wrapping around at the
948 /// boundary of the type.
955 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
956 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
958 #[stable(feature = "rust1", since = "1.0.0")]
959 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
960 #[must_use = "this returns the result of the operation, \
961 without modifying the original"]
963 pub const fn wrapping_add(self, rhs: Self) -> Self {
964 intrinsics::wrapping_add(self, rhs)
967 /// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the
968 /// boundary of the type.
975 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
976 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
978 #[stable(feature = "rust1", since = "1.0.0")]
979 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
980 #[must_use = "this returns the result of the operation, \
981 without modifying the original"]
983 pub const fn wrapping_sub(self, rhs: Self) -> Self {
984 intrinsics::wrapping_sub(self, rhs)
987 /// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at
988 /// the boundary of the type.
995 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
996 /// assert_eq!(11i8.wrapping_mul(12), -124);
998 #[stable(feature = "rust1", since = "1.0.0")]
999 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1000 #[must_use = "this returns the result of the operation, \
1001 without modifying the original"]
1003 pub const fn wrapping_mul(self, rhs: Self) -> Self {
1004 intrinsics::wrapping_mul(self, rhs)
1007 /// Wrapping (modular) division. Computes `self / rhs`, wrapping around at the
1008 /// boundary of the type.
1010 /// The only case where such wrapping can occur is when one divides `MIN / -1` on a signed type (where
1011 /// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
1012 /// that is too large to represent in the type. In such a case, this function returns `MIN` itself.
1016 /// This function will panic if `rhs` is 0.
1023 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
1024 /// assert_eq!((-128i8).wrapping_div(-1), -128);
1026 #[stable(feature = "num_wrapping", since = "1.2.0")]
1027 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
1028 #[must_use = "this returns the result of the operation, \
1029 without modifying the original"]
1031 pub const fn wrapping_div(self, rhs: Self) -> Self {
1032 self.overflowing_div(rhs).0
1035 /// Wrapping Euclidean division. Computes `self.div_euclid(rhs)`,
1036 /// wrapping around at the boundary of the type.
1038 /// Wrapping will only occur in `MIN / -1` on a signed type (where `MIN` is the negative minimal value
1039 /// for the type). This is equivalent to `-MIN`, a positive value that is too large to represent in the
1040 /// type. In this case, this method returns `MIN` itself.
1044 /// This function will panic if `rhs` is 0.
1051 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
1052 /// assert_eq!((-128i8).wrapping_div_euclid(-1), -128);
1054 #[stable(feature = "euclidean_division", since = "1.38.0")]
1055 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1056 #[must_use = "this returns the result of the operation, \
1057 without modifying the original"]
1059 pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
1060 self.overflowing_div_euclid(rhs).0
1063 /// Wrapping (modular) remainder. Computes `self % rhs`, wrapping around at the
1064 /// boundary of the type.
1066 /// Such wrap-around never actually occurs mathematically; implementation artifacts make `x % y`
1067 /// invalid for `MIN / -1` on a signed type (where `MIN` is the negative minimal value). In such a case,
1068 /// this function returns `0`.
1072 /// This function will panic if `rhs` is 0.
1079 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
1080 /// assert_eq!((-128i8).wrapping_rem(-1), 0);
1082 #[stable(feature = "num_wrapping", since = "1.2.0")]
1083 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
1084 #[must_use = "this returns the result of the operation, \
1085 without modifying the original"]
1087 pub const fn wrapping_rem(self, rhs: Self) -> Self {
1088 self.overflowing_rem(rhs).0
1091 /// Wrapping Euclidean remainder. Computes `self.rem_euclid(rhs)`, wrapping around
1092 /// at the boundary of the type.
1094 /// Wrapping will only occur in `MIN % -1` on a signed type (where `MIN` is the negative minimal value
1095 /// for the type). In this case, this method returns 0.
1099 /// This function will panic if `rhs` is 0.
1106 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
1107 /// assert_eq!((-128i8).wrapping_rem_euclid(-1), 0);
1109 #[stable(feature = "euclidean_division", since = "1.38.0")]
1110 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1111 #[must_use = "this returns the result of the operation, \
1112 without modifying the original"]
1114 pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
1115 self.overflowing_rem_euclid(rhs).0
1118 /// Wrapping (modular) negation. Computes `-self`, wrapping around at the boundary
1121 /// The only case where such wrapping can occur is when one negates `MIN` on a signed type (where `MIN`
1122 /// is the negative minimal value for the type); this is a positive value that is too large to represent
1123 /// in the type. In such a case, this function returns `MIN` itself.
1130 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
1131 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
1133 #[stable(feature = "num_wrapping", since = "1.2.0")]
1134 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1136 pub const fn wrapping_neg(self) -> Self {
1137 (0 as $SelfT).wrapping_sub(self)
1140 /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where `mask` removes
1141 /// any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
1143 /// Note that this is *not* the same as a rotate-left; the RHS of a wrapping shift-left is restricted to
1144 /// the range of the type, rather than the bits shifted out of the LHS being returned to the other end.
1145 /// The primitive integer types all implement a [`rotate_left`](Self::rotate_left) function,
1146 /// which may be what you want instead.
1153 #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(7), -128);")]
1154 #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(128), -1);")]
1156 #[stable(feature = "num_wrapping", since = "1.2.0")]
1157 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1158 #[must_use = "this returns the result of the operation, \
1159 without modifying the original"]
1161 pub const fn wrapping_shl(self, rhs: u32) -> Self {
1162 // SAFETY: the masking by the bitsize of the type ensures that we do not shift
1165 intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
1169 /// Panic-free bitwise shift-right; yields `self >> mask(rhs)`, where `mask`
1170 /// removes any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
1172 /// Note that this is *not* the same as a rotate-right; the RHS of a wrapping shift-right is restricted
1173 /// to the range of the type, rather than the bits shifted out of the LHS being returned to the other
1174 /// end. The primitive integer types all implement a [`rotate_right`](Self::rotate_right) function,
1175 /// which may be what you want instead.
1182 #[doc = concat!("assert_eq!((-128", stringify!($SelfT), ").wrapping_shr(7), -1);")]
1183 /// assert_eq!((-128i16).wrapping_shr(64), -128);
1185 #[stable(feature = "num_wrapping", since = "1.2.0")]
1186 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1187 #[must_use = "this returns the result of the operation, \
1188 without modifying the original"]
1190 pub const fn wrapping_shr(self, rhs: u32) -> Self {
1191 // SAFETY: the masking by the bitsize of the type ensures that we do not shift
1194 intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
1198 /// Wrapping (modular) absolute value. Computes `self.abs()`, wrapping around at
1199 /// the boundary of the type.
1201 /// The only case where such wrapping can occur is when one takes the absolute value of the negative
1202 /// minimal value for the type; this is a positive value that is too large to represent in the type. In
1203 /// such a case, this function returns `MIN` itself.
1210 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
1211 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
1212 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
1213 /// assert_eq!((-128i8).wrapping_abs() as u8, 128);
1215 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1216 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1217 #[allow(unused_attributes)]
1219 pub const fn wrapping_abs(self) -> Self {
1220 if self.is_negative() {
1227 /// Computes the absolute value of `self` without any wrapping
1236 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
1237 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
1238 /// assert_eq!((-128i8).unsigned_abs(), 128u8);
1240 #[stable(feature = "unsigned_abs", since = "1.51.0")]
1241 #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
1243 pub const fn unsigned_abs(self) -> $UnsignedT {
1244 self.wrapping_abs() as $UnsignedT
1247 /// Wrapping (modular) exponentiation. Computes `self.pow(exp)`,
1248 /// wrapping around at the boundary of the type.
1255 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
1256 /// assert_eq!(3i8.wrapping_pow(5), -13);
1257 /// assert_eq!(3i8.wrapping_pow(6), -39);
1259 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1260 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1261 #[must_use = "this returns the result of the operation, \
1262 without modifying the original"]
1264 pub const fn wrapping_pow(self, mut exp: u32) -> Self {
1268 let mut base = self;
1269 let mut acc: Self = 1;
1273 acc = acc.wrapping_mul(base);
1276 base = base.wrapping_mul(base);
1279 // since exp!=0, finally the exp must be 1.
1280 // Deal with the final bit of the exponent separately, since
1281 // squaring the base afterwards is not necessary and may cause a
1282 // needless overflow.
1283 acc.wrapping_mul(base)
1286 /// Calculates `self` + `rhs`
1288 /// Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would
1289 /// occur. If an overflow would have occurred then the wrapped value is returned.
1297 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
1298 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
1300 #[stable(feature = "wrapping", since = "1.7.0")]
1301 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1302 #[must_use = "this returns the result of the operation, \
1303 without modifying the original"]
1305 pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1306 let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
1310 /// Calculates `self` - `rhs`
1312 /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow
1313 /// would occur. If an overflow would have occurred then the wrapped value is returned.
1321 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
1322 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
1324 #[stable(feature = "wrapping", since = "1.7.0")]
1325 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1326 #[must_use = "this returns the result of the operation, \
1327 without modifying the original"]
1329 pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
1330 let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
1334 /// Calculates the multiplication of `self` and `rhs`.
1336 /// Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic overflow
1337 /// would occur. If an overflow would have occurred then the wrapped value is returned.
1344 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
1345 /// assert_eq!(1_000_000_000i32.overflowing_mul(10), (1410065408, true));
1347 #[stable(feature = "wrapping", since = "1.7.0")]
1348 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1349 #[must_use = "this returns the result of the operation, \
1350 without modifying the original"]
1352 pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
1353 let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
1357 /// Calculates the divisor when `self` is divided by `rhs`.
1359 /// Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would
1360 /// occur. If an overflow would occur then self is returned.
1364 /// This function will panic if `rhs` is 0.
1372 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
1373 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
1376 #[stable(feature = "wrapping", since = "1.7.0")]
1377 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
1378 #[must_use = "this returns the result of the operation, \
1379 without modifying the original"]
1380 pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1381 if unlikely!(self == Self::MIN && rhs == -1) {
1388 /// Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
1390 /// Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would
1391 /// occur. If an overflow would occur then `self` is returned.
1395 /// This function will panic if `rhs` is 0.
1402 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
1403 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
1406 #[stable(feature = "euclidean_division", since = "1.38.0")]
1407 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1408 #[must_use = "this returns the result of the operation, \
1409 without modifying the original"]
1410 pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
1411 if unlikely!(self == Self::MIN && rhs == -1) {
1414 (self.div_euclid(rhs), false)
1418 /// Calculates the remainder when `self` is divided by `rhs`.
1420 /// Returns a tuple of the remainder after dividing along with a boolean indicating whether an
1421 /// arithmetic overflow would occur. If an overflow would occur then 0 is returned.
1425 /// This function will panic if `rhs` is 0.
1433 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
1434 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
1437 #[stable(feature = "wrapping", since = "1.7.0")]
1438 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
1439 #[must_use = "this returns the result of the operation, \
1440 without modifying the original"]
1441 pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1442 if unlikely!(self == Self::MIN && rhs == -1) {
1450 /// Overflowing Euclidean remainder. Calculates `self.rem_euclid(rhs)`.
1452 /// Returns a tuple of the remainder after dividing along with a boolean indicating whether an
1453 /// arithmetic overflow would occur. If an overflow would occur then 0 is returned.
1457 /// This function will panic if `rhs` is 0.
1464 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
1465 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
1467 #[stable(feature = "euclidean_division", since = "1.38.0")]
1468 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1469 #[must_use = "this returns the result of the operation, \
1470 without modifying the original"]
1472 pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1473 if unlikely!(self == Self::MIN && rhs == -1) {
1476 (self.rem_euclid(rhs), false)
1481 /// Negates self, overflowing if this is equal to the minimum value.
1483 /// Returns a tuple of the negated version of self along with a boolean indicating whether an overflow
1484 /// happened. If `self` is the minimum value (e.g., `i32::MIN` for values of type `i32`), then the
1485 /// minimum value will be returned again and `true` will be returned for an overflow happening.
1492 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
1493 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
1496 #[stable(feature = "wrapping", since = "1.7.0")]
1497 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1498 #[allow(unused_attributes)]
1499 pub const fn overflowing_neg(self) -> (Self, bool) {
1500 if unlikely!(self == Self::MIN) {
1507 /// Shifts self left by `rhs` bits.
1509 /// Returns a tuple of the shifted version of self along with a boolean indicating whether the shift
1510 /// value was larger than or equal to the number of bits. If the shift value is too large, then value is
1511 /// masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
1518 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
1519 /// assert_eq!(0x1i32.overflowing_shl(36), (0x10, true));
1521 #[stable(feature = "wrapping", since = "1.7.0")]
1522 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1523 #[must_use = "this returns the result of the operation, \
1524 without modifying the original"]
1526 pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1527 (self.wrapping_shl(rhs), (rhs > ($BITS - 1)))
1530 /// Shifts self right by `rhs` bits.
1532 /// Returns a tuple of the shifted version of self along with a boolean indicating whether the shift
1533 /// value was larger than or equal to the number of bits. If the shift value is too large, then value is
1534 /// masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
1541 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
1542 /// assert_eq!(0x10i32.overflowing_shr(36), (0x1, true));
1544 #[stable(feature = "wrapping", since = "1.7.0")]
1545 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1546 #[must_use = "this returns the result of the operation, \
1547 without modifying the original"]
1549 pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1550 (self.wrapping_shr(rhs), (rhs > ($BITS - 1)))
1553 /// Computes the absolute value of `self`.
1555 /// Returns a tuple of the absolute version of self along with a boolean indicating whether an overflow
1556 /// happened. If self is the minimum value
1557 #[doc = concat!("(e.g., ", stringify!($SelfT), "::MIN for values of type ", stringify!($SelfT), "),")]
1558 /// then the minimum value will be returned again and true will be returned
1559 /// for an overflow happening.
1566 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
1567 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
1568 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
1570 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1571 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1573 pub const fn overflowing_abs(self) -> (Self, bool) {
1574 (self.wrapping_abs(), self == Self::MIN)
1577 /// Raises self to the power of `exp`, using exponentiation by squaring.
1579 /// Returns a tuple of the exponentiation along with a bool indicating
1580 /// whether an overflow happened.
1587 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
1588 /// assert_eq!(3i8.overflowing_pow(5), (-13, true));
1590 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1591 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1592 #[must_use = "this returns the result of the operation, \
1593 without modifying the original"]
1595 pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
1599 let mut base = self;
1600 let mut acc: Self = 1;
1601 let mut overflown = false;
1602 // Scratch space for storing results of overflowing_mul.
1607 r = acc.overflowing_mul(base);
1612 r = base.overflowing_mul(base);
1617 // since exp!=0, finally the exp must be 1.
1618 // Deal with the final bit of the exponent separately, since
1619 // squaring the base afterwards is not necessary and may cause a
1620 // needless overflow.
1621 r = acc.overflowing_mul(base);
1626 /// Raises self to the power of `exp`, using exponentiation by squaring.
1633 #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
1635 /// assert_eq!(x.pow(5), 32);
1637 #[stable(feature = "rust1", since = "1.0.0")]
1638 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1639 #[must_use = "this returns the result of the operation, \
1640 without modifying the original"]
1642 #[rustc_inherit_overflow_checks]
1643 pub const fn pow(self, mut exp: u32) -> Self {
1647 let mut base = self;
1658 // since exp!=0, finally the exp must be 1.
1659 // Deal with the final bit of the exponent separately, since
1660 // squaring the base afterwards is not necessary and may cause a
1661 // needless overflow.
1665 /// Calculates the quotient of Euclidean division of `self` by `rhs`.
1667 /// This computes the integer `q` such that `self = q * rhs + r`, with
1668 /// `r = self.rem_euclid(rhs)` and `0 <= r < abs(rhs)`.
1670 /// In other words, the result is `self / rhs` rounded to the integer `q`
1671 /// such that `self >= q * rhs`.
1672 /// If `self > 0`, this is equal to round towards zero (the default in Rust);
1673 /// if `self < 0`, this is equal to round towards +/- infinity.
1677 /// This function will panic if `rhs` is 0 or the division results in overflow.
1684 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
1687 /// assert_eq!(a.div_euclid(b), 1); // 7 >= 4 * 1
1688 /// assert_eq!(a.div_euclid(-b), -1); // 7 >= -4 * -1
1689 /// assert_eq!((-a).div_euclid(b), -2); // -7 >= 4 * -2
1690 /// assert_eq!((-a).div_euclid(-b), 2); // -7 >= -4 * 2
1692 #[stable(feature = "euclidean_division", since = "1.38.0")]
1693 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1694 #[must_use = "this returns the result of the operation, \
1695 without modifying the original"]
1697 #[rustc_inherit_overflow_checks]
1698 pub const fn div_euclid(self, rhs: Self) -> Self {
1701 return if rhs > 0 { q - 1 } else { q + 1 }
1707 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
1709 /// This is done as if by the Euclidean division algorithm -- given
1710 /// `r = self.rem_euclid(rhs)`, `self = rhs * self.div_euclid(rhs) + r`, and
1711 /// `0 <= r < abs(rhs)`.
1715 /// This function will panic if `rhs` is 0 or the division results in overflow.
1722 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
1725 /// assert_eq!(a.rem_euclid(b), 3);
1726 /// assert_eq!((-a).rem_euclid(b), 1);
1727 /// assert_eq!(a.rem_euclid(-b), 3);
1728 /// assert_eq!((-a).rem_euclid(-b), 1);
1730 #[stable(feature = "euclidean_division", since = "1.38.0")]
1731 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1732 #[must_use = "this returns the result of the operation, \
1733 without modifying the original"]
1735 #[rustc_inherit_overflow_checks]
1736 pub const fn rem_euclid(self, rhs: Self) -> Self {
1749 /// Returns the logarithm of the number with respect to an arbitrary base.
1751 /// This method might not be optimized owing to implementation details;
1752 /// `log2` can produce results more efficiently for base 2, and `log10`
1753 /// can produce results more efficiently for base 10.
1757 /// When the number is zero, or if the base is not at least 2; it
1758 /// panics in debug mode and the return value is wrapped to 0 in release
1759 /// mode (the only situation in which the method can return 0).
1764 /// #![feature(int_log)]
1765 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".log(5), 1);")]
1767 #[unstable(feature = "int_log", issue = "70887")]
1768 #[must_use = "this returns the result of the operation, \
1769 without modifying the original"]
1772 #[rustc_inherit_overflow_checks]
1773 #[allow(arithmetic_overflow)]
1774 pub const fn log(self, base: Self) -> Self {
1775 match self.checked_log(base) {
1778 // In debug builds, trigger a panic on None.
1779 // This should optimize completely out in release builds.
1780 let _ = Self::MAX + 1;
1787 /// Returns the base 2 logarithm of the number.
1791 /// When the number is zero it panics in debug mode and the return value
1792 /// is wrapped to 0 in release mode (the only situation in which the
1793 /// method can return 0).
1798 /// #![feature(int_log)]
1799 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".log2(), 1);")]
1801 #[unstable(feature = "int_log", issue = "70887")]
1802 #[must_use = "this returns the result of the operation, \
1803 without modifying the original"]
1806 #[rustc_inherit_overflow_checks]
1807 #[allow(arithmetic_overflow)]
1808 pub const fn log2(self) -> Self {
1809 match self.checked_log2() {
1812 // In debug builds, trigger a panic on None.
1813 // This should optimize completely out in release builds.
1814 let _ = Self::MAX + 1;
1821 /// Returns the base 10 logarithm of the number.
1825 /// When the number is zero it panics in debug mode and the return value
1826 /// is wrapped to 0 in release mode (the only situation in which the
1827 /// method can return 0).
1832 /// #![feature(int_log)]
1833 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".log10(), 1);")]
1835 #[unstable(feature = "int_log", issue = "70887")]
1836 #[must_use = "this returns the result of the operation, \
1837 without modifying the original"]
1840 #[rustc_inherit_overflow_checks]
1841 #[allow(arithmetic_overflow)]
1842 pub const fn log10(self) -> Self {
1843 match self.checked_log10() {
1846 // In debug builds, trigger a panic on None.
1847 // This should optimize completely out in release builds.
1848 let _ = Self::MAX + 1;
1855 /// Returns the logarithm of the number with respect to an arbitrary base.
1857 /// Returns `None` if the number is negative or zero, or if the base is not at least 2.
1859 /// This method might not be optimized owing to implementation details;
1860 /// `checked_log2` can produce results more efficiently for base 2, and
1861 /// `checked_log10` can produce results more efficiently for base 10.
1866 /// #![feature(int_log)]
1867 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_log(5), Some(1));")]
1869 #[unstable(feature = "int_log", issue = "70887")]
1870 #[must_use = "this returns the result of the operation, \
1871 without modifying the original"]
1873 pub const fn checked_log(self, base: Self) -> Option<Self> {
1874 if self <= 0 || base <= 1 {
1880 // Optimization for 128 bit wide integers.
1881 if Self::BITS == 128 {
1882 let b = Self::log2(self) / (Self::log2(base) + 1);
1884 r /= base.pow(b as u32);
1895 /// Returns the base 2 logarithm of the number.
1897 /// Returns `None` if the number is negative or zero.
1902 /// #![feature(int_log)]
1903 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_log2(), Some(1));")]
1905 #[unstable(feature = "int_log", issue = "70887")]
1906 #[must_use = "this returns the result of the operation, \
1907 without modifying the original"]
1909 pub const fn checked_log2(self) -> Option<Self> {
1913 // SAFETY: We just checked that this number is positive
1914 let log = (Self::BITS - 1) as Self - unsafe { intrinsics::ctlz_nonzero(self) };
1919 /// Returns the base 10 logarithm of the number.
1921 /// Returns `None` if the number is negative or zero.
1926 /// #![feature(int_log)]
1927 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_log10(), Some(1));")]
1929 #[unstable(feature = "int_log", issue = "70887")]
1930 #[must_use = "this returns the result of the operation, \
1931 without modifying the original"]
1933 pub const fn checked_log10(self) -> Option<Self> {
1934 match int_log10::$ActualT(self as $ActualT) {
1935 Some(s) => Some(s as Self),
1940 /// Computes the absolute value of `self`.
1942 /// # Overflow behavior
1944 /// The absolute value of
1945 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
1946 /// cannot be represented as an
1947 #[doc = concat!("`", stringify!($SelfT), "`,")]
1948 /// and attempting to calculate it will cause an overflow. This means
1949 /// that code in debug mode will trigger a panic on this case and
1950 /// optimized code will return
1951 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
1952 /// without a panic.
1959 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
1960 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
1962 #[stable(feature = "rust1", since = "1.0.0")]
1963 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1964 #[allow(unused_attributes)]
1966 #[rustc_inherit_overflow_checks]
1967 pub const fn abs(self) -> Self {
1968 // Note that the #[rustc_inherit_overflow_checks] and #[inline]
1969 // above mean that the overflow semantics of the subtraction
1970 // depend on the crate we're being called from.
1971 if self.is_negative() {
1978 /// Returns a number representing sign of `self`.
1980 /// - `0` if the number is zero
1981 /// - `1` if the number is positive
1982 /// - `-1` if the number is negative
1989 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
1990 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
1991 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
1993 #[stable(feature = "rust1", since = "1.0.0")]
1994 #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
1996 pub const fn signum(self) -> Self {
2004 /// Returns `true` if `self` is positive and `false` if the number is zero or
2012 #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
2013 #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
2015 #[stable(feature = "rust1", since = "1.0.0")]
2016 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2018 pub const fn is_positive(self) -> bool { self > 0 }
2020 /// Returns `true` if `self` is negative and `false` if the number is zero or
2028 #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
2029 #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
2031 #[stable(feature = "rust1", since = "1.0.0")]
2032 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2034 pub const fn is_negative(self) -> bool { self < 0 }
2036 /// Return the memory representation of this integer as a byte array in
2037 /// big-endian (network) byte order.
2039 #[doc = $to_xe_bytes_doc]
2044 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
2045 #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
2047 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2048 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2050 pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
2051 self.to_be().to_ne_bytes()
2054 /// Return the memory representation of this integer as a byte array in
2055 /// little-endian byte order.
2057 #[doc = $to_xe_bytes_doc]
2062 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
2063 #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
2065 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2066 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2068 pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
2069 self.to_le().to_ne_bytes()
2072 /// Return the memory representation of this integer as a byte array in
2073 /// native byte order.
2075 /// As the target platform's native endianness is used, portable code
2076 /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
2079 #[doc = $to_xe_bytes_doc]
2081 /// [`to_be_bytes`]: Self::to_be_bytes
2082 /// [`to_le_bytes`]: Self::to_le_bytes
2087 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
2090 /// if cfg!(target_endian = "big") {
2091 #[doc = concat!(" ", $be_bytes)]
2093 #[doc = concat!(" ", $le_bytes)]
2097 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2098 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2099 // SAFETY: const sound because integers are plain old datatypes so we can always
2100 // transmute them to arrays of bytes
2101 #[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
2103 pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
2104 // SAFETY: integers are plain old datatypes so we can always transmute them to
2106 unsafe { mem::transmute(self) }
2109 /// Create an integer value from its representation as a byte array in
2112 #[doc = $to_xe_bytes_doc]
2117 #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
2118 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2121 /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2124 /// use std::convert::TryInto;
2126 #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2127 #[doc = concat!(" let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2129 #[doc = concat!(" ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
2132 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2133 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2135 pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2136 Self::from_be(Self::from_ne_bytes(bytes))
2139 /// Create an integer value from its representation as a byte array in
2142 #[doc = $to_xe_bytes_doc]
2147 #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
2148 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2151 /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2154 /// use std::convert::TryInto;
2156 #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2157 #[doc = concat!(" let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2159 #[doc = concat!(" ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
2162 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2163 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2165 pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2166 Self::from_le(Self::from_ne_bytes(bytes))
2169 /// Create an integer value from its memory representation as a byte
2170 /// array in native endianness.
2172 /// As the target platform's native endianness is used, portable code
2173 /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
2174 /// appropriate instead.
2176 /// [`from_be_bytes`]: Self::from_be_bytes
2177 /// [`from_le_bytes`]: Self::from_le_bytes
2179 #[doc = $to_xe_bytes_doc]
2184 #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
2185 #[doc = concat!(" ", $be_bytes)]
2187 #[doc = concat!(" ", $le_bytes)]
2189 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2192 /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2195 /// use std::convert::TryInto;
2197 #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2198 #[doc = concat!(" let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2200 #[doc = concat!(" ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
2203 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2204 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2205 // SAFETY: const sound because integers are plain old datatypes so we can always
2206 // transmute to them
2207 #[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
2209 pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2210 // SAFETY: integers are plain old datatypes so we can always transmute to them
2211 unsafe { mem::transmute(bytes) }
2214 /// New code should prefer to use
2215 #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
2217 /// Returns the smallest value that can be represented by this integer type.
2218 #[stable(feature = "rust1", since = "1.0.0")]
2221 #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
2222 #[rustc_deprecated(since = "TBD", reason = "replaced by the `MIN` associated constant on this type")]
2223 pub const fn min_value() -> Self {
2227 /// New code should prefer to use
2228 #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
2230 /// Returns the largest value that can be represented by this integer type.
2231 #[stable(feature = "rust1", since = "1.0.0")]
2234 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
2235 #[rustc_deprecated(since = "TBD", reason = "replaced by the `MAX` associated constant on this type")]
2236 pub const fn max_value() -> Self {