Skip to content

Commit d3b46bb

Browse files
committed
Use named arguments for uint_impl macro
This makes it easier to understand.
1 parent 267cd1d commit d3b46bb

File tree

2 files changed

+174
-39
lines changed

2 files changed

+174
-39
lines changed

library/core/src/num/mod.rs

+155-33
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,25 @@ impl isize {
290290
const ASCII_CASE_MASK: u8 = 0b0010_0000;
291291

292292
impl u8 {
293-
uint_impl! { u8, u8, i8, NonZeroU8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
294-
"[0x12]", "", "", "" }
293+
uint_impl! {
294+
Self = u8,
295+
ActualT = u8,
296+
SignedT = i8,
297+
NonZeroT = NonZeroU8,
298+
BITS = 8,
299+
MAX = 255,
300+
rot = 2,
301+
rot_op = "0x82",
302+
rot_result = "0xa",
303+
swap_op = "0x12",
304+
swapped = "0x12",
305+
reversed = "0x48",
306+
le_bytes = "[0x12]",
307+
be_bytes = "[0x12]",
308+
to_xe_bytes_doc = "",
309+
from_xe_bytes_doc = "",
310+
bound_condition = "",
311+
}
295312
widening_impl! { u8, u16, 8, unsigned }
296313

297314
/// Checks if the value is within the ASCII range.
@@ -875,8 +892,25 @@ impl u8 {
875892
}
876893

877894
impl u16 {
878-
uint_impl! { u16, u16, i16, NonZeroU16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
879-
"[0x34, 0x12]", "[0x12, 0x34]", "", "", "" }
895+
uint_impl! {
896+
Self = u16,
897+
ActualT = u16,
898+
SignedT = i16,
899+
NonZeroT = NonZeroU16,
900+
BITS = 16,
901+
MAX = 65535,
902+
rot = 4,
903+
rot_op = "0xa003",
904+
rot_result = "0x3a",
905+
swap_op = "0x1234",
906+
swapped = "0x3412",
907+
reversed = "0x2c48",
908+
le_bytes = "[0x34, 0x12]",
909+
be_bytes = "[0x12, 0x34]",
910+
to_xe_bytes_doc = "",
911+
from_xe_bytes_doc = "",
912+
bound_condition = "",
913+
}
880914
widening_impl! { u16, u32, 16, unsigned }
881915

882916
/// Checks if the value is a Unicode surrogate code point, which are disallowed values for [`char`].
@@ -906,56 +940,144 @@ impl u16 {
906940
}
907941

908942
impl u32 {
909-
uint_impl! { u32, u32, i32, NonZeroU32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
910-
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "", "" }
943+
uint_impl! {
944+
Self = u32,
945+
ActualT = u32,
946+
SignedT = i32,
947+
NonZeroT = NonZeroU32,
948+
BITS = 32,
949+
MAX = 4294967295,
950+
rot = 8,
951+
rot_op = "0x10000b3",
952+
rot_result = "0xb301",
953+
swap_op = "0x12345678",
954+
swapped = "0x78563412",
955+
reversed = "0x1e6a2c48",
956+
le_bytes = "[0x78, 0x56, 0x34, 0x12]",
957+
be_bytes = "[0x12, 0x34, 0x56, 0x78]",
958+
to_xe_bytes_doc = "",
959+
from_xe_bytes_doc = "",
960+
bound_condition = "",
961+
}
911962
widening_impl! { u32, u64, 32, unsigned }
912963
}
913964

914965
impl u64 {
915-
uint_impl! { u64, u64, i64, NonZeroU64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
916-
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
917-
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
918-
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
919-
"", "", ""}
966+
uint_impl! {
967+
Self = u64,
968+
ActualT = u64,
969+
SignedT = i64,
970+
NonZeroT = NonZeroU64,
971+
BITS = 64,
972+
MAX = 18446744073709551615,
973+
rot = 12,
974+
rot_op = "0xaa00000000006e1",
975+
rot_result = "0x6e10aa",
976+
swap_op = "0x1234567890123456",
977+
swapped = "0x5634129078563412",
978+
reversed = "0x6a2c48091e6a2c48",
979+
le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
980+
be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
981+
to_xe_bytes_doc = "",
982+
from_xe_bytes_doc = "",
983+
bound_condition = "",
984+
}
920985
widening_impl! { u64, u128, 64, unsigned }
921986
}
922987

923988
impl u128 {
924-
uint_impl! { u128, u128, i128, NonZeroU128, 128, 340282366920938463463374607431768211455, 16,
925-
"0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
926-
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
927-
"[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
928-
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
929-
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
930-
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
931-
"", "", ""}
989+
uint_impl! {
990+
Self = u128,
991+
ActualT = u128,
992+
SignedT = i128,
993+
NonZeroT = NonZeroU128,
994+
BITS = 128,
995+
MAX = 340282366920938463463374607431768211455,
996+
rot = 16,
997+
rot_op = "0x13f40000000000000000000000004f76",
998+
rot_result = "0x4f7613f4",
999+
swap_op = "0x12345678901234567890123456789012",
1000+
swapped = "0x12907856341290785634129078563412",
1001+
reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
1002+
le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
1003+
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
1004+
be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
1005+
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
1006+
to_xe_bytes_doc = "",
1007+
from_xe_bytes_doc = "",
1008+
bound_condition = "",
1009+
}
9321010
}
9331011

9341012
#[cfg(target_pointer_width = "16")]
9351013
impl usize {
936-
uint_impl! { usize, u16, isize, NonZeroUsize, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
937-
"[0x34, 0x12]", "[0x12, 0x34]",
938-
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
939-
" on 16-bit targets" }
1014+
uint_impl! {
1015+
Self = usize,
1016+
ActualT = u16,
1017+
SignedT = isize,
1018+
NonZeroT = NonZeroUsize,
1019+
BITS = 16,
1020+
MAX = 65535,
1021+
rot = 4,
1022+
rot_op = "0xa003",
1023+
rot_result = "0x3a",
1024+
swap_op = "0x1234",
1025+
swapped = "0x3412",
1026+
reversed = "0x2c48",
1027+
le_bytes = "[0x34, 0x12]",
1028+
be_bytes = "[0x12, 0x34]",
1029+
to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
1030+
from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
1031+
bound_condition = " on 16-bit targets",
1032+
}
9401033
widening_impl! { usize, u32, 16, unsigned }
9411034
}
1035+
9421036
#[cfg(target_pointer_width = "32")]
9431037
impl usize {
944-
uint_impl! { usize, u32, isize, NonZeroUsize, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
945-
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
946-
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
947-
" on 32-bit targets" }
1038+
uint_impl! {
1039+
Self = usize,
1040+
ActualT = u32,
1041+
SignedT = isize,
1042+
NonZeroT = NonZeroUsize,
1043+
BITS = 32,
1044+
MAX = 4294967295,
1045+
rot = 8,
1046+
rot_op = "0x10000b3",
1047+
rot_result = "0xb301",
1048+
swap_op = "0x12345678",
1049+
swapped = "0x78563412",
1050+
reversed = "0x1e6a2c48",
1051+
le_bytes = "[0x78, 0x56, 0x34, 0x12]",
1052+
be_bytes = "[0x12, 0x34, 0x56, 0x78]",
1053+
to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
1054+
from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
1055+
bound_condition = " on 32-bit targets",
1056+
}
9481057
widening_impl! { usize, u64, 32, unsigned }
9491058
}
9501059

9511060
#[cfg(target_pointer_width = "64")]
9521061
impl usize {
953-
uint_impl! { usize, u64, isize, NonZeroUsize, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
954-
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
955-
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
956-
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
957-
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
958-
" on 64-bit targets" }
1062+
uint_impl! {
1063+
Self = usize,
1064+
ActualT = u64,
1065+
SignedT = isize,
1066+
NonZeroT = NonZeroUsize,
1067+
BITS = 64,
1068+
MAX = 18446744073709551615,
1069+
rot = 12,
1070+
rot_op = "0xaa00000000006e1",
1071+
rot_result = "0x6e10aa",
1072+
swap_op = "0x1234567890123456",
1073+
swapped = "0x5634129078563412",
1074+
reversed = "0x6a2c48091e6a2c48",
1075+
le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
1076+
be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
1077+
to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
1078+
from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
1079+
bound_condition = " on 64-bit targets",
1080+
}
9591081
widening_impl! { usize, u128, 64, unsigned }
9601082
}
9611083

library/core/src/num/uint_macros.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
macro_rules! uint_impl {
2-
($SelfT:ty, $ActualT:ident, $SignedT:ident, $NonZeroT:ident,
3-
$BITS:expr, $MaxV:expr,
4-
$rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
5-
$reversed:expr, $le_bytes:expr, $be_bytes:expr,
6-
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr,
7-
$bound_condition:expr) => {
2+
(
3+
Self = $SelfT:ty,
4+
ActualT = $ActualT:ident,
5+
SignedT = $SignedT:ident,
6+
NonZeroT = $NonZeroT:ident,
7+
BITS = $BITS:expr,
8+
MAX = $MaxV:expr,
9+
rot = $rot:expr,
10+
rot_op = $rot_op:expr,
11+
rot_result = $rot_result:expr,
12+
swap_op = $swap_op:expr,
13+
swapped = $swapped:expr,
14+
reversed = $reversed:expr,
15+
le_bytes = $le_bytes:expr,
16+
be_bytes = $be_bytes:expr,
17+
to_xe_bytes_doc = $to_xe_bytes_doc:expr,
18+
from_xe_bytes_doc = $from_xe_bytes_doc:expr,
19+
bound_condition = $bound_condition:expr,
20+
) => {
821
/// The smallest value that can be represented by this integer type.
922
///
1023
/// # Examples

0 commit comments

Comments
 (0)