Skip to main content

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    $(for $pattern in $iter $do)+
11  }
12}
13
14/// Print the stringified expression followed by its debug formatting
15pub macro show {
16  ($e:expr) => {
17    println!("{}: {:?}", stringify!($e), $e);
18  }
19}
20
21/// Print the stringified expression to stderr followed by its debug formatting
22pub macro eshow {
23  ($e:expr) => {
24    eprintln!("{}: {:?}", stringify!($e), $e);
25  }
26}
27
28/// Print the stringified expression followed by its pretty debug formatting
29pub macro pretty {
30  ($e:expr) => {
31    println!("{}: {:#?}", stringify!($e), $e);
32  }
33}
34
35/// Print the stringified expression to stderr followed by its pretty debug formatting
36pub macro epretty {
37  ($e:expr) => {
38    eprintln!("{}: {:#?}", stringify!($e), $e);
39  }
40}
41
42/// Print the stringified expression followed by its display formatting
43pub macro display {
44  ($e:expr) => {
45    println!("{}: {}", stringify!($e), $e);
46  }
47}
48
49/// Print the stringified expression to stderr followed by its display formatting
50pub macro edisplay {
51  ($e:expr) => {
52    eprintln!("{}: {}", stringify!($e), $e);
53  }
54}
55
56/// Print the stringified expression followed by its bitstring formatting
57pub macro bits {
58  ($e:expr) => {
59    let e = $e;
60    println!("{}: {:02$b}", stringify!($e), e, 8 * core::mem::size_of_val (&e));
61  }
62}
63
64/// Print the stringified expression to stderr followed by its bitstring formatting
65pub macro ebits {
66  ($e:expr) => {
67    let e = $e;
68    eprintln!("{}: {:02$b}", stringify!($e), e, 8 * core::mem::size_of_val (&e));
69  }
70}
71
72/// Print the stringified expression followed by its hexadecimal formatting
73pub macro hex {
74  ($e:expr) => {
75    println!("{}: {:x}", stringify!($e), $e);
76  }
77}
78
79/// Print the stringified expression to stderr followed by its hexadecimal formatting
80pub macro ehex {
81  ($e:expr) => {
82    println!("{}: {:x}", stringify!($e), $e);
83  }
84}
85
86/// Print the stringified expression followed by its pointer formatting
87pub macro address {
88  ($e:expr) => {
89    println!("{}: {:p}", stringify!($e), $e);
90  }
91}
92
93/// Print the stringified expression to stderr followed by its pointer formatting
94pub macro eaddress {
95  ($e:expr) => {
96    eprintln!("{}: {:p}", stringify!($e), $e);
97  }
98}