From 7abbc6e3c556eb1a46f8de64c6acd31ea2c0c637 Mon Sep 17 00:00:00 2001 From: Smitty Date: Mon, 26 Jul 2021 20:22:44 -0400 Subject: [PATCH 1/2] Document math behind MIN/MAX consts on integers --- library/core/src/num/int_macros.rs | 8 +++++--- library/core/src/num/mod.rs | 16 ++++++++-------- library/core/src/num/uint_macros.rs | 3 ++- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index 982729388c8..8bf2d9a6d14 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -1,9 +1,10 @@ macro_rules! int_impl { - ($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, $Min:expr, $Max:expr, + ($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) => { - /// The smallest value that can be represented by this integer type. + /// The smallest value that can be represented by this integer type, + #[doc = concat!("-2", stringify!($BITS_MINUS_ONE), ".")] /// /// # Examples /// @@ -15,7 +16,8 @@ macro_rules! int_impl { #[stable(feature = "assoc_int_consts", since = "1.43.0")] pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self; - /// The largest value that can be represented by this integer type. + /// The largest value that can be represented by this integer type, + #[doc = concat!("2", stringify!($BITS_MINUS_ONE), " - 1.")] /// /// # Examples /// diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 26d84a60702..6c43d6d6212 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -91,26 +91,26 @@ depending on the target pointer size. #[lang = "i8"] impl i8 { - int_impl! { i8, i8, u8, 8, -128, 127, 2, "-0x7e", "0xa", "0x12", "0x12", "0x48", + int_impl! { i8, i8, u8, 8, 7, -128, 127, 2, "-0x7e", "0xa", "0x12", "0x12", "0x48", "[0x12]", "[0x12]", "", "" } } #[lang = "i16"] impl i16 { - int_impl! { i16, i16, u16, 16, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234", "0x3412", + int_impl! { i16, i16, u16, 16, 15, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234", "0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "" } } #[lang = "i32"] impl i32 { - int_impl! { i32, i32, u32, 32, -2147483648, 2147483647, 8, "0x10000b3", "0xb301", + int_impl! { i32, i32, u32, 32, 31, -2147483648, 2147483647, 8, "0x10000b3", "0xb301", "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "" } } #[lang = "i64"] impl i64 { - int_impl! { i64, i64, u64, 64, -9223372036854775808, 9223372036854775807, 12, + 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]", "", "" } @@ -118,7 +118,7 @@ impl i64 { #[lang = "i128"] impl i128 { - int_impl! { i128, i128, u128, 128, -170141183460469231731687303715884105728, + int_impl! { i128, i128, u128, 128, 127, -170141183460469231731687303715884105728, 170141183460469231731687303715884105727, 16, "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012", "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48", @@ -131,7 +131,7 @@ impl i128 { #[cfg(target_pointer_width = "16")] #[lang = "isize"] impl isize { - int_impl! { isize, i16, usize, 16, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234", + 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!() } } @@ -139,7 +139,7 @@ impl isize { #[cfg(target_pointer_width = "32")] #[lang = "isize"] impl isize { - int_impl! { isize, i32, usize, 32, -2147483648, 2147483647, 8, "0x10000b3", "0xb301", + 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!() } @@ -148,7 +148,7 @@ impl isize { #[cfg(target_pointer_width = "64")] #[lang = "isize"] impl isize { - int_impl! { isize, i64, usize, 64, -9223372036854775808, 9223372036854775807, + 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]", diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index ca1b05fdfbe..0d04d1b3b42 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -15,7 +15,8 @@ macro_rules! uint_impl { #[stable(feature = "assoc_int_consts", since = "1.43.0")] pub const MIN: Self = 0; - /// The largest value that can be represented by this integer type. + /// The largest value that can be represented by this integer type, + #[doc = concat!("2", stringify!($BITS), " - 1.")] /// /// # Examples /// From 0e017496eb588ac9cfe3e892fc7083996f5f3397 Mon Sep 17 00:00:00 2001 From: Smitty Date: Tue, 27 Jul 2021 16:37:18 -0400 Subject: [PATCH 2/2] remove unneeded stringify --- library/core/src/num/int_macros.rs | 4 ++-- library/core/src/num/uint_macros.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index 8bf2d9a6d14..e54aaae86bf 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -4,7 +4,7 @@ macro_rules! int_impl { $reversed:expr, $le_bytes:expr, $be_bytes:expr, $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => { /// The smallest value that can be represented by this integer type, - #[doc = concat!("-2", stringify!($BITS_MINUS_ONE), ".")] + #[doc = concat!("-2", $BITS_MINUS_ONE, ".")] /// /// # Examples /// @@ -17,7 +17,7 @@ macro_rules! int_impl { pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self; /// The largest value that can be represented by this integer type, - #[doc = concat!("2", stringify!($BITS_MINUS_ONE), " - 1.")] + #[doc = concat!("2", $BITS_MINUS_ONE, " - 1.")] /// /// # Examples /// diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 0d04d1b3b42..86450712270 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -16,7 +16,7 @@ macro_rules! uint_impl { pub const MIN: Self = 0; /// The largest value that can be represented by this integer type, - #[doc = concat!("2", stringify!($BITS), " - 1.")] + #[doc = concat!("2", $BITS, " - 1.")] /// /// # Examples ///