src/metaprogramming.rs

changeset 94
1f19c6bbf07b
parent 75
e9f4550cfa18
equal deleted inserted replaced
93:123f7f38e161 94:1f19c6bbf07b
7 /// 7 ///
8 /// ```ignore 8 /// ```ignore
9 /// maybe_ref!(ref, V) // ➡ &V 9 /// maybe_ref!(ref, V) // ➡ &V
10 /// maybe_ref!(noref, V) // ➡ V 10 /// maybe_ref!(noref, V) // ➡ V
11 /// ``` 11 /// ```
12 #[macro_export]
13 macro_rules! maybe_ref { 12 macro_rules! maybe_ref {
14 (ref, $x:expr) => { &$x }; 13 (ref, $x:expr) => {
15 (noref, $x:expr) => { $x }; 14 &$x
16 (ref, $x:ty) => { &$x }; 15 };
17 (noref, $x:ty) => { $x }; 16 (noref, $x:expr) => {
17 $x
18 };
19 (ref, $x:ty) => {
20 &$x
21 };
22 (noref, $x:ty) => {
23 $x
24 };
18 } 25 }
19 26
20 /// Choose `a` if first argument is the literal `ref`, otherwise `b`. 27 /// Choose `a` if first argument is the literal `ref`, otherwise `b`.
21 #[macro_export] 28 // macro_rules! ifref {
22 macro_rules! ifref { 29 // (noref, $a:expr, $b:expr) => {
23 (noref, $a:expr, $b:expr) => { $b }; 30 // $b
24 (ref, $a:expr, $b:expr) => { $a }; 31 // };
25 } 32 // (ref, $a:expr, $b:expr) => {
26 33 // $a
34 // };
35 // }
27 36
28 /// Annotate `x` with a lifetime if the first parameter 37 /// Annotate `x` with a lifetime if the first parameter
29 /// Typically to be used from another macro. 38 /// Typically to be used from another macro.
30 /// 39 ///
31 /// ```ignore 40 /// ```ignore
32 /// maybe_ref!(ref, &'a V) // ➡ &'a V 41 /// maybe_ref!(ref, &'a V) // ➡ &'a V
33 /// maybe_ref!(noref, &'a V) // ➡ V 42 /// maybe_ref!(noref, &'a V) // ➡ V
34 /// ``` 43 /// ```
35 #[macro_export]
36 macro_rules! maybe_lifetime { 44 macro_rules! maybe_lifetime {
37 (ref, $x:ty) => { $x }; 45 (ref, $x:ty) => {
38 (noref, &$lt:lifetime $x:ty) => { $x }; 46 $x
39 (noref, &$x:ty) => { $x }; 47 };
48 (noref, &$lt:lifetime $x:ty) => {
49 $x
50 };
51 (noref, &$x:ty) => {
52 $x
53 };
40 } 54 }
41

mercurial