Skip to content

Commit eb5d82b

Browse files
committed
Use named arguments for int_impl macro
This makes it easier to understand.
1 parent d3b46bb commit eb5d82b

File tree

2 files changed

+182
-39
lines changed

2 files changed

+182
-39
lines changed

library/core/src/num/int_macros.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
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-
$bound_condition:expr) => {
2+
(
3+
Self = $SelfT:ty,
4+
ActualT = $ActualT:ident,
5+
UnsignedT = $UnsignedT:ty,
6+
BITS = $BITS:expr,
7+
BITS_MINUS_ONE = $BITS_MINUS_ONE:expr,
8+
Min = $Min:expr,
9+
Max = $Max:expr,
10+
rot = $rot:expr,
11+
rot_op = $rot_op:expr,
12+
rot_result = $rot_result:expr,
13+
swap_op = $swap_op:expr,
14+
swapped = $swapped:expr,
15+
reversed = $reversed:expr,
16+
le_bytes = $le_bytes:expr,
17+
be_bytes = $be_bytes:expr,
18+
to_xe_bytes_doc = $to_xe_bytes_doc:expr,
19+
from_xe_bytes_doc = $from_xe_bytes_doc:expr,
20+
bound_condition = $bound_condition:expr,
21+
) => {
722
/// The smallest value that can be represented by this integer type
823
#[doc = concat!("(&minus;2<sup>", $BITS_MINUS_ONE, "</sup>", $bound_condition, ").")]
924
///

library/core/src/num/mod.rs

+162-34
Original file line numberDiff line numberDiff line change
@@ -226,64 +226,192 @@ macro_rules! widening_impl {
226226
}
227227

228228
impl i8 {
229-
int_impl! { i8, i8, u8, 8, 7, -128, 127, 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
230-
"[0x12]", "[0x12]", "", "", "" }
229+
int_impl! {
230+
Self = i8,
231+
ActualT = i8,
232+
UnsignedT = u8,
233+
BITS = 8,
234+
BITS_MINUS_ONE = 7,
235+
Min = -128,
236+
Max = 127,
237+
rot = 2,
238+
rot_op = "-0x7e",
239+
rot_result = "0xa",
240+
swap_op = "0x12",
241+
swapped = "0x12",
242+
reversed = "0x48",
243+
le_bytes = "[0x12]",
244+
be_bytes = "[0x12]",
245+
to_xe_bytes_doc = "",
246+
from_xe_bytes_doc = "",
247+
bound_condition = "",
248+
}
231249
}
232250

233251
impl i16 {
234-
int_impl! { i16, i16, u16, 16, 15, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
235-
"0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "", "" }
252+
int_impl! {
253+
Self = i16,
254+
ActualT = i16,
255+
UnsignedT = u16,
256+
BITS = 16,
257+
BITS_MINUS_ONE = 15,
258+
Min = -32768,
259+
Max = 32767,
260+
rot = 4,
261+
rot_op = "-0x5ffd",
262+
rot_result = "0x3a",
263+
swap_op = "0x1234",
264+
swapped = "0x3412",
265+
reversed = "0x2c48",
266+
le_bytes = "[0x34, 0x12]",
267+
be_bytes = "[0x12, 0x34]",
268+
to_xe_bytes_doc = "",
269+
from_xe_bytes_doc = "",
270+
bound_condition = "",
271+
}
236272
}
237273

238274
impl i32 {
239-
int_impl! { i32, i32, u32, 32, 31, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
240-
"0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
241-
"[0x12, 0x34, 0x56, 0x78]", "", "", "" }
275+
int_impl! {
276+
Self = i32,
277+
ActualT = i32,
278+
UnsignedT = u32,
279+
BITS = 32,
280+
BITS_MINUS_ONE = 31,
281+
Min = -2147483648,
282+
Max = 2147483647,
283+
rot = 8,
284+
rot_op = "0x10000b3",
285+
rot_result = "0xb301",
286+
swap_op = "0x12345678",
287+
swapped = "0x78563412",
288+
reversed = "0x1e6a2c48",
289+
le_bytes = "[0x78, 0x56, 0x34, 0x12]",
290+
be_bytes = "[0x12, 0x34, 0x56, 0x78]",
291+
to_xe_bytes_doc = "",
292+
from_xe_bytes_doc = "",
293+
bound_condition = "",
294+
}
242295
}
243296

244297
impl i64 {
245-
int_impl! { i64, i64, u64, 64, 63, -9223372036854775808, 9223372036854775807, 12,
246-
"0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
247-
"0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
248-
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", "", "", "" }
298+
int_impl! {
299+
Self = i64,
300+
ActualT = i64,
301+
UnsignedT = u64,
302+
BITS = 64,
303+
BITS_MINUS_ONE = 63,
304+
Min = -9223372036854775808,
305+
Max = 9223372036854775807,
306+
rot = 12,
307+
rot_op = "0xaa00000000006e1",
308+
rot_result = "0x6e10aa",
309+
swap_op = "0x1234567890123456",
310+
swapped = "0x5634129078563412",
311+
reversed = "0x6a2c48091e6a2c48",
312+
le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
313+
be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
314+
to_xe_bytes_doc = "",
315+
from_xe_bytes_doc = "",
316+
bound_condition = "",
317+
}
249318
}
250319

251320
impl i128 {
252-
int_impl! { i128, i128, u128, 128, 127, -170141183460469231731687303715884105728,
253-
170141183460469231731687303715884105727, 16,
254-
"0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
255-
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
256-
"[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
257-
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
258-
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
259-
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "", "" }
321+
int_impl! {
322+
Self = i128,
323+
ActualT = i128,
324+
UnsignedT = u128,
325+
BITS = 128,
326+
BITS_MINUS_ONE = 127,
327+
Min = -170141183460469231731687303715884105728,
328+
Max = 170141183460469231731687303715884105727,
329+
rot = 16,
330+
rot_op = "0x13f40000000000000000000000004f76",
331+
rot_result = "0x4f7613f4",
332+
swap_op = "0x12345678901234567890123456789012",
333+
swapped = "0x12907856341290785634129078563412",
334+
reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
335+
le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
336+
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
337+
be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
338+
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
339+
to_xe_bytes_doc = "",
340+
from_xe_bytes_doc = "",
341+
bound_condition = "",
342+
}
260343
}
261344

262345
#[cfg(target_pointer_width = "16")]
263346
impl isize {
264-
int_impl! { isize, i16, usize, 16, 15, -32768, 32767, 4, "-0x5ffd", "0x3a", "0x1234",
265-
"0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]",
266-
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
267-
" on 16-bit targets" }
347+
int_impl! {
348+
Self = isize,
349+
ActualT = i16,
350+
UnsignedT = usize,
351+
BITS = 16,
352+
BITS_MINUS_ONE = 15,
353+
Min = -32768,
354+
Max = 32767,
355+
rot = 4,
356+
rot_op = "-0x5ffd",
357+
rot_result = "0x3a",
358+
swap_op = "0x1234",
359+
swapped = "0x3412",
360+
reversed = "0x2c48",
361+
le_bytes = "[0x34, 0x12]",
362+
be_bytes = "[0x12, 0x34]",
363+
to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
364+
from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
365+
bound_condition = " on 16-bit targets",
366+
}
268367
}
269368

270369
#[cfg(target_pointer_width = "32")]
271370
impl isize {
272-
int_impl! { isize, i32, usize, 32, 31, -2147483648, 2147483647, 8, "0x10000b3", "0xb301",
273-
"0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
274-
"[0x12, 0x34, 0x56, 0x78]",
275-
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
276-
" on 32-bit targets" }
371+
int_impl! {
372+
Self = isize,
373+
ActualT = i32,
374+
UnsignedT = usize,
375+
BITS = 32,
376+
BITS_MINUS_ONE = 31,
377+
Min = -2147483648,
378+
Max = 2147483647,
379+
rot = 8,
380+
rot_op = "0x10000b3",
381+
rot_result = "0xb301",
382+
swap_op = "0x12345678",
383+
swapped = "0x78563412",
384+
reversed = "0x1e6a2c48",
385+
le_bytes = "[0x78, 0x56, 0x34, 0x12]",
386+
be_bytes = "[0x12, 0x34, 0x56, 0x78]",
387+
to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
388+
from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
389+
bound_condition = " on 32-bit targets",
390+
}
277391
}
278392

279393
#[cfg(target_pointer_width = "64")]
280394
impl isize {
281-
int_impl! { isize, i64, usize, 64, 63, -9223372036854775808, 9223372036854775807,
282-
12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
283-
"0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
284-
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
285-
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
286-
" on 64-bit targets" }
395+
int_impl! {
396+
Self = isize,
397+
ActualT = i64,
398+
UnsignedT = usize,
399+
BITS = 64,
400+
BITS_MINUS_ONE = 63,
401+
Min = -9223372036854775808,
402+
Max = 9223372036854775807,
403+
rot = 12,
404+
rot_op = "0xaa00000000006e1",
405+
rot_result = "0x6e10aa",
406+
swap_op = "0x1234567890123456",
407+
swapped = "0x5634129078563412",
408+
reversed = "0x6a2c48091e6a2c48",
409+
le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
410+
be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
411+
to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
412+
from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
413+
bound_condition = " on 64-bit targets",
414+
}
287415
}
288416

289417
/// If 6th bit set ascii is upper case.

0 commit comments

Comments
 (0)