rust_utils/macros.rs
1/// Convenience macro to create a `HashMap` from a predfined set of key - value pairs
2///
3/// # Example
4/// ```
5/// let map = hash_map! {
6/// "yes" => 0.,
7/// "no" => 1.,
8/// "pi" => std::f64::consts::PI
9/// };
10///
11/// for (key, val) in map.iter() {
12/// println!("Key: {key}, Value: {val}");
13/// }
14/// ```
15#[macro_export]
16macro_rules! hash_map {
17 ($($key:expr => $val:expr), *) => {
18 {
19 let mut map = std::collections::HashMap::new();
20 $( map.insert($key, $val); )*
21 map
22 }
23 }
24}
25
26/// Convenience macro for generating a test module from a set of functions
27///
28/// It also pre-imports everything from the outer module
29#[macro_export]
30macro_rules! tests {
31 ($($(#[$attr:meta]) * fn $name:ident $params:tt $body:tt) *) => {
32 #[cfg(test)]
33 mod tests {
34 use super::*;
35
36 $(
37 $(
38 #[$attr]
39 )*
40 #[test]
41 fn $name$params$body
42 )*
43 }
44 }
45}