rs_utils/macros.rs
1//! Utility macros
2
3/// Expands into a series of for loops with the given block of code which should
4/// treat each type uniformly.
5///
6/// This is to avoid dynamic dispatch when chaining iterators over generic
7/// collections when the types are known.
8pub macro for_sequence {
9 ( $pattern:pat in ($($iter:expr),+) $do:block
10 ) => {
11 $(for $pattern in $iter $do)+
12 }
13}
14
15/// Print the stringified expression followed by its debug formatting
16pub macro show {
17 ($e:expr) => {
18 println!("{}: {:?}", stringify!($e), $e);
19 }
20}
21
22/// Print the stringified expression followed by its pretty debug formatting
23pub macro pretty {
24 ($e:expr) => {
25 println!("{}: {:#?}", stringify!($e), $e);
26 }
27}
28
29/// Print the stringified expression followed by its display formatting
30pub macro display {
31 ($e:expr) => {
32 println!("{}: {}", stringify!($e), $e);
33 }
34}
35
36/// Print the stringified expression followed by its bitstring formatting
37pub macro bits {
38 ($e:expr) => {
39 let e = $e;
40 println!("{}: {:02$b}", stringify!($e), e, 8 * std::mem::size_of_val (&e));
41 }
42}
43
44/// Print the stringified expression followed by its hexadecimal formatting
45pub macro hex {
46 ($e:expr) => {
47 println!("{}: {:x}", stringify!($e), $e);
48 }
49}
50
51/// Print the stringified expression followed by its pointer formatting
52pub macro address {
53 ($e:expr) => {
54 println!("{}: {:p}", stringify!($e), $e);
55 }
56}