| |
1 /*! |
| |
2 Metaprogramming tools |
| |
3 */ |
| |
4 |
| |
5 /// Reference `x` if so indicated by the first parameter. |
| |
6 /// Typically to be used from another macro. |
| |
7 /// |
| |
8 /// ```ignore |
| |
9 /// maybe_ref!(ref, V) // ➡ &V |
| |
10 /// maybe_ref!(noref, V) // ➡ V |
| |
11 /// ``` |
| |
12 #[macro_export] |
| |
13 macro_rules! maybe_ref { |
| |
14 (ref, $x:expr) => { &$x }; |
| |
15 (noref, $x:expr) => { $x }; |
| |
16 (ref, $x:ty) => { &$x }; |
| |
17 (noref, $x:ty) => { $x }; |
| |
18 } |
| |
19 |
| |
20 /// Choose `a` if first argument is the literal `ref`, otherwise `b`. |
| |
21 #[macro_export] |
| |
22 macro_rules! ifref { |
| |
23 (noref, $a:expr, $b:expr) => { $b }; |
| |
24 (ref, $a:expr, $b:expr) => { $a }; |
| |
25 } |
| |
26 |
| |
27 |
| |
28 /// Annotate `x` with a lifetime if the first parameter |
| |
29 /// Typically to be used from another macro. |
| |
30 /// |
| |
31 /// ```ignore |
| |
32 /// maybe_ref!(ref, &'a V) // ➡ &'a V |
| |
33 /// maybe_ref!(noref, &'a V) // ➡ V |
| |
34 /// ``` |
| |
35 #[macro_export] |
| |
36 macro_rules! maybe_lifetime { |
| |
37 (ref, $x:ty) => { $x }; |
| |
38 (noref, &$lt:lifetime $x:ty) => { $x }; |
| |
39 (noref, &$x:ty) => { $x }; |
| |
40 } |
| |
41 |