Use named arguments for uint_impl macro

This makes it easier to understand.
This commit is contained in:
Nilstrieb 2023-02-20 18:10:51 +00:00
parent 267cd1d2c5
commit d3b46bb74e
2 changed files with 174 additions and 39 deletions

View File

@ -290,8 +290,25 @@ impl isize {
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.
@ -875,8 +892,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`].
@ -906,56 +940,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 }
}

View File

@ -1,10 +1,23 @@
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,
BITS = $BITS:expr,
MAX = $MaxV:expr,
rot = $rot:expr,
rot_op = $rot_op:expr,
rot_result = $rot_result:expr,
swap_op = $swap_op:expr,
swapped = $swapped:expr,
reversed = $reversed:expr,
le_bytes = $le_bytes:expr,
be_bytes = $be_bytes:expr,
to_xe_bytes_doc = $to_xe_bytes_doc:expr,
from_xe_bytes_doc = $from_xe_bytes_doc:expr,
bound_condition = $bound_condition:expr,
) => {
/// The smallest value that can be represented by this integer type.
///
/// # Examples