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 followed by its pretty debug formatting
22pub macro pretty {
23  ($e:expr) => {
24    println!("{}: {:#?}", stringify!($e), $e);
25  }
26}
27
28/// Print the stringified expression followed by its display formatting
29pub macro display {
30  ($e:expr) => {
31    println!("{}: {}", stringify!($e), $e);
32  }
33}
34
35/// Print the stringified expression followed by its bitstring formatting
36pub macro bits {
37  ($e:expr) => {
38    let e = $e;
39    println!("{}: {:02$b}", stringify!($e), e, 8 * std::mem::size_of_val (&e));
40  }
41}
42
43/// Print the stringified expression followed by its hexadecimal formatting
44pub macro hex {
45  ($e:expr) => {
46    println!("{}: {:x}", stringify!($e), $e);
47  }
48}
49
50/// Print the stringified expression followed by its pointer formatting
51pub macro address {
52  ($e:expr) => {
53    println!("{}: {:p}", stringify!($e), $e);
54  }
55}