summaryrefslogtreecommitdiffstats
path: root/library/core/src/num
diff options
context:
space:
mode:
Diffstat (limited to 'library/core/src/num')
-rw-r--r--library/core/src/num/dec2flt/mod.rs17
-rw-r--r--library/core/src/num/error.rs57
-rw-r--r--library/core/src/num/int_log10.rs8
-rw-r--r--library/core/src/num/int_macros.rs80
-rw-r--r--library/core/src/num/mod.rs396
-rw-r--r--library/core/src/num/shells/i128.rs4
-rw-r--r--library/core/src/num/shells/i16.rs4
-rw-r--r--library/core/src/num/shells/i32.rs4
-rw-r--r--library/core/src/num/shells/i64.rs4
-rw-r--r--library/core/src/num/shells/i8.rs4
-rw-r--r--library/core/src/num/shells/isize.rs4
-rw-r--r--library/core/src/num/shells/u128.rs4
-rw-r--r--library/core/src/num/shells/u16.rs4
-rw-r--r--library/core/src/num/shells/u32.rs4
-rw-r--r--library/core/src/num/shells/u64.rs4
-rw-r--r--library/core/src/num/shells/u8.rs4
-rw-r--r--library/core/src/num/shells/usize.rs4
-rw-r--r--library/core/src/num/uint_macros.rs63
18 files changed, 462 insertions, 207 deletions
diff --git a/library/core/src/num/dec2flt/mod.rs b/library/core/src/num/dec2flt/mod.rs
index a888ced49..f8d493e8b 100644
--- a/library/core/src/num/dec2flt/mod.rs
+++ b/library/core/src/num/dec2flt/mod.rs
@@ -75,6 +75,7 @@
issue = "none"
)]
+use crate::error::Error;
use crate::fmt;
use crate::str::FromStr;
@@ -182,15 +183,10 @@ enum FloatErrorKind {
Invalid,
}
-impl ParseFloatError {
- #[unstable(
- feature = "int_error_internals",
- reason = "available through Error trait and this method should \
- not be exposed publicly",
- issue = "none"
- )]
- #[doc(hidden)]
- pub fn __description(&self) -> &str {
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Error for ParseFloatError {
+ #[allow(deprecated)]
+ fn description(&self) -> &str {
match self.kind {
FloatErrorKind::Empty => "cannot parse float from empty string",
FloatErrorKind::Invalid => "invalid float literal",
@@ -201,7 +197,8 @@ impl ParseFloatError {
#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for ParseFloatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- self.__description().fmt(f)
+ #[allow(deprecated)]
+ self.description().fmt(f)
}
}
diff --git a/library/core/src/num/error.rs b/library/core/src/num/error.rs
index 768dd8781..1bae4efe7 100644
--- a/library/core/src/num/error.rs
+++ b/library/core/src/num/error.rs
@@ -9,23 +9,19 @@ use crate::fmt;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct TryFromIntError(pub(crate) ());
-impl TryFromIntError {
- #[unstable(
- feature = "int_error_internals",
- reason = "available through Error trait and this method should \
- not be exposed publicly",
- issue = "none"
- )]
- #[doc(hidden)]
- pub fn __description(&self) -> &str {
- "out of range integral type conversion attempted"
+#[stable(feature = "try_from", since = "1.34.0")]
+impl fmt::Display for TryFromIntError {
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ #[allow(deprecated)]
+ self.description().fmt(fmt)
}
}
#[stable(feature = "try_from", since = "1.34.0")]
-impl fmt::Display for TryFromIntError {
- fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
- self.__description().fmt(fmt)
+impl Error for TryFromIntError {
+ #[allow(deprecated)]
+ fn description(&self) -> &str {
+ "out of range integral type conversion attempted"
}
}
@@ -121,28 +117,13 @@ impl ParseIntError {
pub fn kind(&self) -> &IntErrorKind {
&self.kind
}
- #[unstable(
- feature = "int_error_internals",
- reason = "available through Error trait and this method should \
- not be exposed publicly",
- issue = "none"
- )]
- #[doc(hidden)]
- pub fn __description(&self) -> &str {
- match self.kind {
- IntErrorKind::Empty => "cannot parse integer from empty string",
- IntErrorKind::InvalidDigit => "invalid digit found in string",
- IntErrorKind::PosOverflow => "number too large to fit in target type",
- IntErrorKind::NegOverflow => "number too small to fit in target type",
- IntErrorKind::Zero => "number would be zero for non-zero type",
- }
- }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for ParseIntError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- self.__description().fmt(f)
+ #[allow(deprecated)]
+ self.description().fmt(f)
}
}
@@ -150,14 +131,12 @@ impl fmt::Display for ParseIntError {
impl Error for ParseIntError {
#[allow(deprecated)]
fn description(&self) -> &str {
- self.__description()
- }
-}
-
-#[stable(feature = "try_from", since = "1.34.0")]
-impl Error for TryFromIntError {
- #[allow(deprecated)]
- fn description(&self) -> &str {
- self.__description()
+ match self.kind {
+ IntErrorKind::Empty => "cannot parse integer from empty string",
+ IntErrorKind::InvalidDigit => "invalid digit found in string",
+ IntErrorKind::PosOverflow => "number too large to fit in target type",
+ IntErrorKind::NegOverflow => "number too small to fit in target type",
+ IntErrorKind::Zero => "number would be zero for non-zero type",
+ }
}
}
diff --git a/library/core/src/num/int_log10.rs b/library/core/src/num/int_log10.rs
index 80472528f..0ce31b40a 100644
--- a/library/core/src/num/int_log10.rs
+++ b/library/core/src/num/int_log10.rs
@@ -138,3 +138,11 @@ pub const fn i64(val: i64) -> u32 {
pub const fn i128(val: i128) -> u32 {
u128(val as u128)
}
+
+/// Instantiate this panic logic once, rather than for all the ilog methods
+/// on every single primitive type.
+#[cold]
+#[track_caller]
+pub const fn panic_for_nonpositive_argument() -> ! {
+ panic!("argument of integer logarithm must be positive")
+}
diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs
index 2cae98b8e..aec15212d 100644
--- a/library/core/src/num/int_macros.rs
+++ b/library/core/src/num/int_macros.rs
@@ -1,11 +1,31 @@
macro_rules! int_impl {
- ($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, $BITS_MINUS_ONE:expr, $Min:expr, $Max:expr,
- $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
- $reversed:expr, $le_bytes:expr, $be_bytes:expr,
- $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr,
- $bound_condition:expr) => {
+ (
+ Self = $SelfT:ty,
+ ActualT = $ActualT:ident,
+ UnsignedT = $UnsignedT:ty,
+
+ // There are all for use *only* in doc comments.
+ // As such, they're all passed as literals -- passing them as a string
+ // literal is fine if they need to be multiple code tokens.
+ // In non-comments, use the associated constants rather than these.
+ BITS = $BITS:literal,
+ BITS_MINUS_ONE = $BITS_MINUS_ONE:literal,
+ Min = $Min:literal,
+ Max = $Max:literal,
+ rot = $rot:literal,
+ rot_op = $rot_op:literal,
+ rot_result = $rot_result:literal,
+ swap_op = $swap_op:literal,
+ swapped = $swapped:literal,
+ reversed = $reversed:literal,
+ le_bytes = $le_bytes:literal,
+ be_bytes = $be_bytes:literal,
+ to_xe_bytes_doc = $to_xe_bytes_doc:expr,
+ from_xe_bytes_doc = $from_xe_bytes_doc:expr,
+ bound_condition = $bound_condition:literal,
+ ) => {
/// The smallest value that can be represented by this integer type
- #[doc = concat!("(&minus;2<sup>", $BITS_MINUS_ONE, "</sup>", $bound_condition, ")")]
+ #[doc = concat!("(&minus;2<sup>", $BITS_MINUS_ONE, "</sup>", $bound_condition, ").")]
///
/// # Examples
///
@@ -15,10 +35,10 @@ macro_rules! int_impl {
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");")]
/// ```
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
- pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self;
+ pub const MIN: Self = !Self::MAX;
/// The largest value that can be represented by this integer type
- #[doc = concat!("(2<sup>", $BITS_MINUS_ONE, "</sup> &minus; 1", $bound_condition, ")")]
+ #[doc = concat!("(2<sup>", $BITS_MINUS_ONE, "</sup> &minus; 1", $bound_condition, ").")]
///
/// # Examples
///
@@ -28,7 +48,7 @@ macro_rules! int_impl {
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");")]
/// ```
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
- pub const MAX: Self = !Self::MIN;
+ pub const MAX: Self = (<$UnsignedT>::MAX >> 1) as Self;
/// The size of this integer type in bits.
///
@@ -38,7 +58,7 @@ macro_rules! int_impl {
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");")]
/// ```
#[stable(feature = "int_bits_const", since = "1.53.0")]
- pub const BITS: u32 = $BITS;
+ pub const BITS: u32 = <$UnsignedT>::BITS;
/// Converts a string slice in a given base to an integer.
///
@@ -1365,7 +1385,7 @@ macro_rules! int_impl {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds
unsafe {
- self.unchecked_shl(rhs & ($BITS - 1))
+ self.unchecked_shl(rhs & (Self::BITS - 1))
}
}
@@ -1395,7 +1415,7 @@ macro_rules! int_impl {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds
unsafe {
- self.unchecked_shr(rhs & ($BITS - 1))
+ self.unchecked_shr(rhs & (Self::BITS - 1))
}
}
@@ -1901,7 +1921,7 @@ macro_rules! int_impl {
without modifying the original"]
#[inline]
pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
- (self.wrapping_shl(rhs), (rhs > ($BITS - 1)))
+ (self.wrapping_shl(rhs), rhs >= Self::BITS)
}
/// Shifts self right by `rhs` bits.
@@ -1924,7 +1944,7 @@ macro_rules! int_impl {
without modifying the original"]
#[inline]
pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
- (self.wrapping_shr(rhs), (rhs > ($BITS - 1)))
+ (self.wrapping_shr(rhs), rhs >= Self::BITS)
}
/// Computes the absolute value of `self`.
@@ -2331,14 +2351,17 @@ macro_rules! int_impl {
/// ```
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
- #[rustc_allow_const_fn_unstable(const_option)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn ilog(self, base: Self) -> u32 {
assert!(base >= 2, "base of integer logarithm must be at least 2");
- self.checked_ilog(base).expect("argument of integer logarithm must be positive")
+ if let Some(log) = self.checked_ilog(base) {
+ log
+ } else {
+ int_log10::panic_for_nonpositive_argument()
+ }
}
/// Returns the base 2 logarithm of the number, rounded down.
@@ -2354,13 +2377,16 @@ macro_rules! int_impl {
/// ```
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
- #[rustc_allow_const_fn_unstable(const_option)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn ilog2(self) -> u32 {
- self.checked_ilog2().expect("argument of integer logarithm must be positive")
+ if let Some(log) = self.checked_ilog2() {
+ log
+ } else {
+ int_log10::panic_for_nonpositive_argument()
+ }
}
/// Returns the base 10 logarithm of the number, rounded down.
@@ -2376,13 +2402,16 @@ macro_rules! int_impl {
/// ```
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
- #[rustc_allow_const_fn_unstable(const_option)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn ilog10(self) -> u32 {
- self.checked_ilog10().expect("argument of integer logarithm must be positive")
+ if let Some(log) = self.checked_ilog10() {
+ log
+ } else {
+ int_log10::panic_for_nonpositive_argument()
+ }
}
/// Returns the logarithm of the number with respect to an arbitrary base,
@@ -2574,12 +2603,13 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
+ #[rustc_allow_const_fn_unstable(const_cmp)]
pub const fn signum(self) -> Self {
- match self {
- n if n > 0 => 1,
- 0 => 0,
- _ => -1,
- }
+ // Picking the right way to phrase this is complicated
+ // (<https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign>)
+ // so delegate it to `Ord` which is already producing -1/0/+1
+ // exactly like we need and can be the place to deal with the complexity.
+ self.cmp(&0) as _
}
/// Returns `true` if `self` is positive and `false` if the number is zero or
diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs
index ac7f579eb..a50c91579 100644
--- a/library/core/src/num/mod.rs
+++ b/library/core/src/num/mod.rs
@@ -9,9 +9,6 @@ use crate::mem;
use crate::ops::{Add, Mul, Sub};
use crate::str::FromStr;
-#[cfg(not(no_fp_fmt_parse))]
-use crate::error::Error;
-
// Used because the `?` operator is not allowed in a const context.
macro_rules! try_opt {
($e:expr) => {
@@ -61,15 +58,6 @@ pub use wrapping::Wrapping;
#[cfg(not(no_fp_fmt_parse))]
pub use dec2flt::ParseFloatError;
-#[cfg(not(no_fp_fmt_parse))]
-#[stable(feature = "rust1", since = "1.0.0")]
-impl Error for ParseFloatError {
- #[allow(deprecated)]
- fn description(&self) -> &str {
- self.__description()
- }
-}
-
#[stable(feature = "rust1", since = "1.0.0")]
pub use error::ParseIntError;
@@ -238,72 +226,217 @@ macro_rules! widening_impl {
}
impl i8 {
- int_impl! { i8, i8, u8, 8, 7, -128, 127, 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
- "[0x12]", "[0x12]", "", "", "" }
+ int_impl! {
+ Self = i8,
+ ActualT = i8,
+ UnsignedT = u8,
+ BITS = 8,
+ BITS_MINUS_ONE = 7,
+ Min = -128,
+ Max = 127,
+ rot = 2,
+ rot_op = "-0x7e",
+ rot_result = "0xa",
+ swap_op = "0x12",
+ swapped = "0x12",
+ reversed = "0x48",
+ le_bytes = "[0x12]",
+ be_bytes = "[0x12]",
+ to_xe_bytes_doc = "",
+ from_xe_bytes_doc = "",
+ bound_condition = "",
+ }
}
impl i16 {
- int_impl! { i16, i16, u16, 16, 15, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
- "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "", "" }
+ int_impl! {
+ Self = i16,
+ ActualT = i16,
+ UnsignedT = u16,
+ BITS = 16,
+ BITS_MINUS_ONE = 15,
+ Min = -32768,
+ Max = 32767,
+ rot = 4,
+ rot_op = "-0x5ffd",
+ rot_result = "0x3a",
+ swap_op = "0x1234",
+ swapped = "0x3412",
+ reversed = "0x2c48",
+ le_bytes = "[0x34, 0x12]",
+ be_bytes = "[0x12, 0x34]",
+ to_xe_bytes_doc = "",
+ from_xe_bytes_doc = "",
+ bound_condition = "",
+ }
}
impl i32 {
- int_impl! { i32, i32, u32, 32, 31, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
- "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
- "[0x12, 0x34, 0x56, 0x78]", "", "", "" }
+ int_impl! {
+ Self = i32,
+ ActualT = i32,
+ UnsignedT = u32,
+ BITS = 32,
+ BITS_MINUS_ONE = 31,
+ Min = -2147483648,
+ Max = 2147483647,
+ rot = 8,
+ rot_op = "0x10000b3",
+ rot_result = "0xb301",
+ swap_op = "0x12345678",
+ swapped = "0x78563412",
+ reversed = "0x1e6a2c48",
+ le_bytes = "[0x78, 0x56, 0x34, 0x12]",
+ be_bytes = "[0x12, 0x34, 0x56, 0x78]",
+ to_xe_bytes_doc = "",
+ from_xe_bytes_doc = "",
+ bound_condition = "",
+ }
}
impl i64 {
- int_impl! { i64, i64, u64, 64, 63, -9223372036854775808, 9223372036854775807, 12,
- "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
- "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
- "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", "", "", "" }
+ int_impl! {
+ Self = i64,
+ ActualT = i64,
+ UnsignedT = u64,
+ BITS = 64,
+ BITS_MINUS_ONE = 63,
+ Min = -9223372036854775808,
+ Max = 9223372036854775807,
+ rot = 12,
+ rot_op = "0xaa00000000006e1",
+ rot_result = "0x6e10aa",
+ swap_op = "0x1234567890123456",
+ swapped = "0x5634129078563412",
+ reversed = "0x6a2c48091e6a2c48",
+ le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
+ be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
+ to_xe_bytes_doc = "",
+ from_xe_bytes_doc = "",
+ bound_condition = "",
+ }
}
impl i128 {
- int_impl! { i128, i128, u128, 128, 127, -170141183460469231731687303715884105728,
- 170141183460469231731687303715884105727, 16,
- "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
- "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
- "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
- 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
- "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
- 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "", "" }
+ int_impl! {
+ Self = i128,
+ ActualT = i128,
+ UnsignedT = u128,
+ BITS = 128,
+ BITS_MINUS_ONE = 127,
+ Min = -170141183460469231731687303715884105728,
+ Max = 170141183460469231731687303715884105727,
+ rot = 16,
+ rot_op = "0x13f40000000000000000000000004f76",
+ rot_result = "0x4f7613f4",
+ swap_op = "0x12345678901234567890123456789012",
+ swapped = "0x12907856341290785634129078563412",
+ reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
+ le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
+ 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
+ be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
+ 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
+ to_xe_bytes_doc = "",
+ from_xe_bytes_doc = "",
+ bound_condition = "",
+ }
}
#[cfg(target_pointer_width = "16")]
impl isize {
- int_impl! { isize, i16, usize, 16, 15, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234",
- "0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]",
- usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
- " on 16-bit targets" }
+ int_impl! {
+ Self = isize,
+ ActualT = i16,
+ UnsignedT = usize,
+ BITS = 16,
+ BITS_MINUS_ONE = 15,
+ Min = -32768,
+ Max = 32767,
+ rot = 4,
+ rot_op = "-0x5ffd",
+ rot_result = "0x3a",
+ swap_op = "0x1234",
+ swapped = "0x3412",
+ reversed = "0x2c48",
+ le_bytes = "[0x34, 0x12]",
+ be_bytes = "[0x12, 0x34]",
+ to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
+ from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
+ bound_condition = " on 16-bit targets",
+ }
}
#[cfg(target_pointer_width = "32")]
impl isize {
- int_impl! { isize, i32, usize, 32, 31, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
- "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
- "[0x12, 0x34, 0x56, 0x78]",
- usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
- " on 32-bit targets" }
+ int_impl! {
+ Self = isize,
+ ActualT = i32,
+ UnsignedT = usize,
+ BITS = 32,
+ BITS_MINUS_ONE = 31,
+ Min = -2147483648,
+ Max = 2147483647,
+ rot = 8,
+ rot_op = "0x10000b3",
+ rot_result = "0xb301",
+ swap_op = "0x12345678",
+ swapped = "0x78563412",
+ reversed = "0x1e6a2c48",
+ le_bytes = "[0x78, 0x56, 0x34, 0x12]",
+ be_bytes = "[0x12, 0x34, 0x56, 0x78]",
+ to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
+ from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
+ bound_condition = " on 32-bit targets",
+ }
}
#[cfg(target_pointer_width = "64")]
impl isize {
- int_impl! { isize, i64, usize, 64, 63, -9223372036854775808, 9223372036854775807,
- 12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
- "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
- "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
- usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
- " on 64-bit targets" }
+ int_impl! {
+ Self = isize,
+ ActualT = i64,
+ UnsignedT = usize,
+ BITS = 64,
+ BITS_MINUS_ONE = 63,
+ Min = -9223372036854775808,
+ Max = 9223372036854775807,
+ rot = 12,
+ rot_op = "0xaa00000000006e1",
+ rot_result = "0x6e10aa",
+ swap_op = "0x1234567890123456",
+ swapped = "0x5634129078563412",
+ reversed = "0x6a2c48091e6a2c48",
+ le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
+ be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
+ to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
+ from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
+ bound_condition = " on 64-bit targets",
+ }
}
/// If 6th bit set ascii is upper case.
const ASCII_CASE_MASK: u8 = 0b0010_0000;
impl u8 {
- uint_impl! { u8, u8, i8, NonZeroU8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
- "[0x12]", "", "", "" }
+ uint_impl! {
+ Self = u8,
+ ActualT = u8,
+ SignedT = i8,
+ NonZeroT = NonZeroU8,
+ BITS = 8,
+ MAX = 255,
+ rot = 2,
+ rot_op = "0x82",
+ rot_result = "0xa",
+ swap_op = "0x12",
+ swapped = "0x12",
+ reversed = "0x48",
+ le_bytes = "[0x12]",
+ be_bytes = "[0x12]",
+ to_xe_bytes_doc = "",
+ from_xe_bytes_doc = "",
+ bound_condition = "",
+ }
widening_impl! { u8, u16, 8, unsigned }
/// Checks if the value is within the ASCII range.
@@ -887,8 +1020,25 @@ impl u8 {
}
impl u16 {
- uint_impl! { u16, u16, i16, NonZeroU16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
- "[0x34, 0x12]", "[0x12, 0x34]", "", "", "" }
+ uint_impl! {
+ Self = u16,
+ ActualT = u16,
+ SignedT = i16,
+ NonZeroT = NonZeroU16,
+ BITS = 16,
+ MAX = 65535,
+ rot = 4,
+ rot_op = "0xa003",
+ rot_result = "0x3a",
+ swap_op = "0x1234",
+ swapped = "0x3412",
+ reversed = "0x2c48",
+ le_bytes = "[0x34, 0x12]",
+ be_bytes = "[0x12, 0x34]",
+ to_xe_bytes_doc = "",
+ from_xe_bytes_doc = "",
+ bound_condition = "",
+ }
widening_impl! { u16, u32, 16, unsigned }
/// Checks if the value is a Unicode surrogate code point, which are disallowed values for [`char`].
@@ -918,56 +1068,144 @@ impl u16 {
}
impl u32 {
- uint_impl! { u32, u32, i32, NonZeroU32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
- "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "", "" }
+ uint_impl! {
+ Self = u32,
+ ActualT = u32,
+ SignedT = i32,
+ NonZeroT = NonZeroU32,
+ BITS = 32,
+ MAX = 4294967295,
+ rot = 8,
+ rot_op = "0x10000b3",
+ rot_result = "0xb301",
+ swap_op = "0x12345678",
+ swapped = "0x78563412",
+ reversed = "0x1e6a2c48",
+ le_bytes = "[0x78, 0x56, 0x34, 0x12]",
+ be_bytes = "[0x12, 0x34, 0x56, 0x78]",
+ to_xe_bytes_doc = "",
+ from_xe_bytes_doc = "",
+ bound_condition = "",
+ }
widening_impl! { u32, u64, 32, unsigned }
}
impl u64 {
- uint_impl! { u64, u64, i64, NonZeroU64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
- "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
- "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
- "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
- "", "", ""}
+ uint_impl! {
+ Self = u64,
+ ActualT = u64,
+ SignedT = i64,
+ NonZeroT = NonZeroU64,
+ BITS = 64,
+ MAX = 18446744073709551615,
+ rot = 12,
+ rot_op = "0xaa00000000006e1",
+ rot_result = "0x6e10aa",
+ swap_op = "0x1234567890123456",
+ swapped = "0x5634129078563412",
+ reversed = "0x6a2c48091e6a2c48",
+ le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
+ be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
+ to_xe_bytes_doc = "",
+ from_xe_bytes_doc = "",
+ bound_condition = "",
+ }
widening_impl! { u64, u128, 64, unsigned }
}
impl u128 {
- uint_impl! { u128, u128, i128, NonZeroU128, 128, 340282366920938463463374607431768211455, 16,
- "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
- "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
- "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
- 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
- "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
- 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
- "", "", ""}
+ uint_impl! {
+ Self = u128,
+ ActualT = u128,
+ SignedT = i128,
+ NonZeroT = NonZeroU128,
+ BITS = 128,
+ MAX = 340282366920938463463374607431768211455,
+ rot = 16,
+ rot_op = "0x13f40000000000000000000000004f76",
+ rot_result = "0x4f7613f4",
+ swap_op = "0x12345678901234567890123456789012",
+ swapped = "0x12907856341290785634129078563412",
+ reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
+ le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
+ 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
+ be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
+ 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
+ to_xe_bytes_doc = "",
+ from_xe_bytes_doc = "",
+ bound_condition = "",
+ }
}
#[cfg(target_pointer_width = "16")]
impl usize {
- uint_impl! { usize, u16, isize, NonZeroUsize, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
- "[0x34, 0x12]", "[0x12, 0x34]",
- usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
- " on 16-bit targets" }
+ uint_impl! {
+ Self = usize,
+ ActualT = u16,
+ SignedT = isize,
+ NonZeroT = NonZeroUsize,
+ BITS = 16,
+ MAX = 65535,
+ rot = 4,
+ rot_op = "0xa003",
+ rot_result = "0x3a",
+ swap_op = "0x1234",
+ swapped = "0x3412",
+ reversed = "0x2c48",
+ le_bytes = "[0x34, 0x12]",
+ be_bytes = "[0x12, 0x34]",
+ to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
+ from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
+ bound_condition = " on 16-bit targets",
+ }
widening_impl! { usize, u32, 16, unsigned }
}
+
#[cfg(target_pointer_width = "32")]
impl usize {
- uint_impl! { usize, u32, isize, NonZeroUsize, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
- "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
- usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
- " on 32-bit targets" }
+ uint_impl! {
+ Self = usize,
+ ActualT = u32,
+ SignedT = isize,
+ NonZeroT = NonZeroUsize,
+ BITS = 32,
+ MAX = 4294967295,
+ rot = 8,
+ rot_op = "0x10000b3",
+ rot_result = "0xb301",
+ swap_op = "0x12345678",
+ swapped = "0x78563412",
+ reversed = "0x1e6a2c48",
+ le_bytes = "[0x78, 0x56, 0x34, 0x12]",
+ be_bytes = "[0x12, 0x34, 0x56, 0x78]",
+ to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
+ from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
+ bound_condition = " on 32-bit targets",
+ }
widening_impl! { usize, u64, 32, unsigned }
}
#[cfg(target_pointer_width = "64")]
impl usize {
- uint_impl! { usize, u64, isize, NonZeroUsize, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
- "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
- "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
- "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
- usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
- " on 64-bit targets" }
+ uint_impl! {
+ Self = usize,
+ ActualT = u64,
+ SignedT = isize,
+ NonZeroT = NonZeroUsize,
+ BITS = 64,
+ MAX = 18446744073709551615,
+ rot = 12,
+ rot_op = "0xaa00000000006e1",
+ rot_result = "0x6e10aa",
+ swap_op = "0x1234567890123456",
+ swapped = "0x5634129078563412",
+ reversed = "0x6a2c48091e6a2c48",
+ le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
+ be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
+ to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
+ from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
+ bound_condition = " on 64-bit targets",
+ }
widening_impl! { usize, u128, 64, unsigned }
}
diff --git a/library/core/src/num/shells/i128.rs b/library/core/src/num/shells/i128.rs
index 7b048dc52..b3b3d3b48 100644
--- a/library/core/src/num/shells/i128.rs
+++ b/library/core/src/num/shells/i128.rs
@@ -1,6 +1,4 @@
-//! Constants for the 128-bit signed integer type.
-//!
-//! *[See also the `i128` primitive type][i128].*
+//! Redundant constants module for the [`i128` primitive type][i128].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/i16.rs b/library/core/src/num/shells/i16.rs
index 5c5812d5c..70a452e19 100644
--- a/library/core/src/num/shells/i16.rs
+++ b/library/core/src/num/shells/i16.rs
@@ -1,6 +1,4 @@
-//! Constants for the 16-bit signed integer type.
-//!
-//! *[See also the `i16` primitive type][i16].*
+//! Redundant constants module for the [`i16` primitive type][i16].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/i32.rs b/library/core/src/num/shells/i32.rs
index b283ac644..c30849e25 100644
--- a/library/core/src/num/shells/i32.rs
+++ b/library/core/src/num/shells/i32.rs
@@ -1,6 +1,4 @@
-//! Constants for the 32-bit signed integer type.
-//!
-//! *[See also the `i32` primitive type][i32].*
+//! Redundant constants module for the [`i32` primitive type][i32].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/i64.rs b/library/core/src/num/shells/i64.rs
index a416fa7e9..77d95d712 100644
--- a/library/core/src/num/shells/i64.rs
+++ b/library/core/src/num/shells/i64.rs
@@ -1,6 +1,4 @@
-//! Constants for the 64-bit signed integer type.
-//!
-//! *[See also the `i64` primitive type][i64].*
+//! Redundant constants module for the [`i64` primitive type][i64].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/i8.rs b/library/core/src/num/shells/i8.rs
index 02465013a..516ba8cde 100644
--- a/library/core/src/num/shells/i8.rs
+++ b/library/core/src/num/shells/i8.rs
@@ -1,6 +1,4 @@
-//! Constants for the 8-bit signed integer type.
-//!
-//! *[See also the `i8` primitive type][i8].*
+//! Redundant constants module for the [`i8` primitive type][i8].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/isize.rs b/library/core/src/num/shells/isize.rs
index 1579fbab6..828f7345b 100644
--- a/library/core/src/num/shells/isize.rs
+++ b/library/core/src/num/shells/isize.rs
@@ -1,6 +1,4 @@
-//! Constants for the pointer-sized signed integer type.
-//!
-//! *[See also the `isize` primitive type][isize].*
+//! Redundant constants module for the [`isize` primitive type][isize].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/u128.rs b/library/core/src/num/shells/u128.rs
index fe08cee58..b1e30e384 100644
--- a/library/core/src/num/shells/u128.rs
+++ b/library/core/src/num/shells/u128.rs
@@ -1,6 +1,4 @@
-//! Constants for the 128-bit unsigned integer type.
-//!
-//! *[See also the `u128` primitive type][u128].*
+//! Redundant constants module for the [`u128` primitive type][u128].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/u16.rs b/library/core/src/num/shells/u16.rs
index 36f8c6978..b203806f4 100644
--- a/library/core/src/num/shells/u16.rs
+++ b/library/core/src/num/shells/u16.rs
@@ -1,6 +1,4 @@
-//! Constants for the 16-bit unsigned integer type.
-//!
-//! *[See also the `u16` primitive type][u16].*
+//! Redundant constants module for the [`i16` primitive type][i16].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/u32.rs b/library/core/src/num/shells/u32.rs
index 1c369097d..4c84274e7 100644
--- a/library/core/src/num/shells/u32.rs
+++ b/library/core/src/num/shells/u32.rs
@@ -1,6 +1,4 @@
-//! Constants for the 32-bit unsigned integer type.
-//!
-//! *[See also the `u32` primitive type][u32].*
+//! Redundant constants module for the [`u32` primitive type][u32].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/u64.rs b/library/core/src/num/shells/u64.rs
index e8b691d15..47a95c682 100644
--- a/library/core/src/num/shells/u64.rs
+++ b/library/core/src/num/shells/u64.rs
@@ -1,6 +1,4 @@
-//! Constants for the 64-bit unsigned integer type.
-//!
-//! *[See also the `u64` primitive type][u64].*
+//! Redundant constants module for the [`u64` primitive type][u64].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/u8.rs b/library/core/src/num/shells/u8.rs
index 817c6a18a..360baef72 100644
--- a/library/core/src/num/shells/u8.rs
+++ b/library/core/src/num/shells/u8.rs
@@ -1,6 +1,4 @@
-//! Constants for the 8-bit unsigned integer type.
-//!
-//! *[See also the `u8` primitive type][u8].*
+//! Redundant constants module for the [`u8` primitive type][u8].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/shells/usize.rs b/library/core/src/num/shells/usize.rs
index 3e1bec5ec..44c24dfc2 100644
--- a/library/core/src/num/shells/usize.rs
+++ b/library/core/src/num/shells/usize.rs
@@ -1,6 +1,4 @@
-//! Constants for the pointer-sized unsigned integer type.
-//!
-//! *[See also the `usize` primitive type][usize].*
+//! Redundant constants module for the [`usize` primitive type][usize].
//!
//! New code should use the associated constants directly on the primitive type.
diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs
index 1c97c4686..932038a0b 100644
--- a/library/core/src/num/uint_macros.rs
+++ b/library/core/src/num/uint_macros.rs
@@ -1,10 +1,28 @@
macro_rules! uint_impl {
- ($SelfT:ty, $ActualT:ident, $SignedT:ident, $NonZeroT:ident,
- $BITS:expr, $MaxV:expr,
- $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
- $reversed:expr, $le_bytes:expr, $be_bytes:expr,
- $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr,
- $bound_condition:expr) => {
+ (
+ Self = $SelfT:ty,
+ ActualT = $ActualT:ident,
+ SignedT = $SignedT:ident,
+ NonZeroT = $NonZeroT:ident,
+
+ // There are all for use *only* in doc comments.
+ // As such, they're all passed as literals -- passing them as a string
+ // literal is fine if they need to be multiple code tokens.
+ // In non-comments, use the associated constants rather than these.
+ BITS = $BITS:literal,
+ MAX = $MaxV:literal,
+ rot = $rot:literal,
+ rot_op = $rot_op:literal,
+ rot_result = $rot_result:literal,
+ swap_op = $swap_op:literal,
+ swapped = $swapped:literal,
+ reversed = $reversed:literal,
+ le_bytes = $le_bytes:literal,
+ be_bytes = $be_bytes:literal,
+ to_xe_bytes_doc = $to_xe_bytes_doc:expr,
+ from_xe_bytes_doc = $from_xe_bytes_doc:expr,
+ bound_condition = $bound_condition:literal,
+ ) => {
/// The smallest value that can be represented by this integer type.
///
/// # Examples
@@ -18,7 +36,7 @@ macro_rules! uint_impl {
pub const MIN: Self = 0;
/// The largest value that can be represented by this integer type
- #[doc = concat!("(2<sup>", $BITS, "</sup> &minus; 1", $bound_condition, ")")]
+ #[doc = concat!("(2<sup>", $BITS, "</sup> &minus; 1", $bound_condition, ").")]
///
/// # Examples
///
@@ -38,7 +56,7 @@ macro_rules! uint_impl {
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");")]
/// ```
#[stable(feature = "int_bits_const", since = "1.53.0")]
- pub const BITS: u32 = $BITS;
+ pub const BITS: u32 = Self::MAX.count_ones();
/// Converts a string slice in a given base to an integer.
///
@@ -705,14 +723,17 @@ macro_rules! uint_impl {
/// ```
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
- #[rustc_allow_const_fn_unstable(const_option)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn ilog(self, base: Self) -> u32 {
assert!(base >= 2, "base of integer logarithm must be at least 2");
- self.checked_ilog(base).expect("argument of integer logarithm must be positive")
+ if let Some(log) = self.checked_ilog(base) {
+ log
+ } else {
+ int_log10::panic_for_nonpositive_argument()
+ }
}
/// Returns the base 2 logarithm of the number, rounded down.
@@ -728,13 +749,16 @@ macro_rules! uint_impl {
/// ```
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
- #[rustc_allow_const_fn_unstable(const_option)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn ilog2(self) -> u32 {
- self.checked_ilog2().expect("argument of integer logarithm must be positive")
+ if let Some(log) = self.checked_ilog2() {
+ log
+ } else {
+ int_log10::panic_for_nonpositive_argument()
+ }
}
/// Returns the base 10 logarithm of the number, rounded down.
@@ -750,13 +774,16 @@ macro_rules! uint_impl {
/// ```
#[stable(feature = "int_log", since = "1.67.0")]
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
- #[rustc_allow_const_fn_unstable(const_option)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[track_caller]
pub const fn ilog10(self) -> u32 {
- self.checked_ilog10().expect("argument of integer logarithm must be positive")
+ if let Some(log) = self.checked_ilog10() {
+ log
+ } else {
+ int_log10::panic_for_nonpositive_argument()
+ }
}
/// Returns the logarithm of the number with respect to an arbitrary base,
@@ -1381,7 +1408,7 @@ macro_rules! uint_impl {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds
unsafe {
- self.unchecked_shl(rhs & ($BITS - 1))
+ self.unchecked_shl(rhs & (Self::BITS - 1))
}
}
@@ -1414,7 +1441,7 @@ macro_rules! uint_impl {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds
unsafe {
- self.unchecked_shr(rhs & ($BITS - 1))
+ self.unchecked_shr(rhs & (Self::BITS - 1))
}
}
@@ -1838,7 +1865,7 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline(always)]
pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
- (self.wrapping_shl(rhs), (rhs > ($BITS - 1)))
+ (self.wrapping_shl(rhs), rhs >= Self::BITS)
}
/// Shifts self right by `rhs` bits.
@@ -1863,7 +1890,7 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline(always)]
pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
- (self.wrapping_shr(rhs), (rhs > ($BITS - 1)))
+ (self.wrapping_shr(rhs), rhs >= Self::BITS)
}
/// Raises self to the power of `exp`, using exponentiation by squaring.