| 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_rules! maybe_ref { |
|
| 13 (ref, $x:expr) => { |
|
| 14 &$x |
|
| 15 }; |
|
| 16 (noref, $x:expr) => { |
|
| 17 $x |
|
| 18 }; |
|
| 19 (ref, $x:ty) => { |
|
| 20 &$x |
|
| 21 }; |
|
| 22 (noref, $x:ty) => { |
|
| 23 $x |
|
| 24 }; |
|
| 25 } |
|
| 26 |
|
| 27 /// Choose `a` if first argument is the literal `ref`, otherwise `b`. |
|
| 28 // macro_rules! ifref { |
|
| 29 // (noref, $a:expr, $b:expr) => { |
|
| 30 // $b |
|
| 31 // }; |
|
| 32 // (ref, $a:expr, $b:expr) => { |
|
| 33 // $a |
|
| 34 // }; |
|
| 35 // } |
|
| 36 |
|
| 37 /// Annotate `x` with a lifetime if the first parameter |
|
| 38 /// Typically to be used from another macro. |
|
| 39 /// |
|
| 40 /// ```ignore |
|
| 41 /// maybe_ref!(ref, &'a V) // ➡ &'a V |
|
| 42 /// maybe_ref!(noref, &'a V) // ➡ V |
|
| 43 /// ``` |
|
| 44 macro_rules! maybe_lifetime { |
|
| 45 (ref, $x:ty) => { |
|
| 46 $x |
|
| 47 }; |
|
| 48 (noref, &$lt:lifetime $x:ty) => { |
|
| 49 $x |
|
| 50 }; |
|
| 51 (noref, &$x:ty) => { |
|
| 52 $x |
|
| 53 }; |
|
| 54 } |
|