|
2 | 2 |
|
3 | 3 | #![feature(macro_metavar_expr)]
|
4 | 4 |
|
5 |
| -/// Count the number of idents in a macro repetition. |
6 |
| -macro_rules! count_idents { |
7 |
| - ( $( $i:ident ),* ) => { |
8 |
| - ${count($i)} |
9 |
| - }; |
10 |
| -} |
11 |
| - |
12 |
| -/// Count the number of idents in a 2-dimensional macro repetition. |
13 |
| -macro_rules! count_idents_2 { |
14 |
| - ( $( [ $( $i:ident ),* ] ),* ) => { |
15 |
| - ${count($i)} |
16 |
| - }; |
17 |
| -} |
18 |
| - |
19 |
| -/// Mostly counts the number of OUTER-MOST repetitions |
20 |
| -macro_rules! count_depth_limits { |
21 |
| - ( $( { $( [ $( $outer:ident : ( $( $inner:ident )* ) )* ] )* } )* ) => { |
22 |
| - ( |
23 |
| - ( |
24 |
| - ${count($inner)}, |
25 |
| - ${count($inner, 0)}, |
26 |
| - ${count($inner, 1)}, |
27 |
| - ${count($inner, 2)}, |
28 |
| - ${count($inner, 3)}, |
29 |
| - ), |
30 |
| - ( |
31 |
| - ${count($outer)}, |
32 |
| - ${count($outer, 0)}, |
33 |
| - ${count($outer, 1)}, |
34 |
| - ${count($outer, 2)}, |
35 |
| - ), |
36 |
| - ) |
37 |
| - }; |
38 |
| -} |
39 |
| - |
40 |
| -/// Produce (index, length) pairs for literals in a macro repetition. |
41 |
| -/// The literal is not included in the output, so this macro uses the |
42 |
| -/// `ignore` meta-variable expression to create a non-expanding |
43 |
| -/// repetition binding. |
44 |
| -macro_rules! enumerate_literals { |
45 |
| - ( $( ($l:stmt) ),* ) => { |
46 |
| - [$( ${ignore($l)} (${index()}, ${length()}) ),*] |
47 |
| - }; |
48 |
| -} |
49 |
| - |
50 |
| -/// Produce index and length tuples for literals in a 2-dimensional |
51 |
| -/// macro repetition. |
52 |
| -macro_rules! enumerate_literals_2 { |
53 |
| - ( $( [ $( ($l:literal) ),* ] ),* ) => { |
54 |
| - [ |
55 |
| - $( |
56 |
| - $( |
57 |
| - ( |
58 |
| - ${index(1)}, |
59 |
| - ${length(1)}, |
60 |
| - ${index(0)}, |
61 |
| - ${length(0)}, |
62 |
| - $l |
63 |
| - ), |
64 |
| - )* |
65 |
| - )* |
66 |
| - ] |
67 |
| - }; |
68 |
| -} |
69 |
| - |
70 | 5 | /// Generate macros that count idents and then add a constant number
|
71 | 6 | /// to the count.
|
72 | 7 | ///
|
@@ -108,39 +43,6 @@ make_picker!(first => a, b; a);
|
108 | 43 | make_picker!(second => a, b; b);
|
109 | 44 |
|
110 | 45 | fn main() {
|
111 |
| - assert_eq!(count_idents!(a, b, c), 3); |
112 |
| - assert_eq!(count_idents_2!([a, b, c], [d, e], [f]), 6); |
113 |
| - assert_eq!( |
114 |
| - count_depth_limits! { |
115 |
| - { |
116 |
| - [ A: (a b c) D: (d e f) ] |
117 |
| - [ G: (g h) I: (i j k l m) ] |
118 |
| - [ N: (n) ] |
119 |
| - } |
120 |
| - { |
121 |
| - [ O: (o) P: (p q) R: (r s) ] |
122 |
| - [ T: (t u v w x y z) ] |
123 |
| - } |
124 |
| - }, |
125 |
| - ((26, 26, 9, 5, 2), (9, 9, 5, 2)) |
126 |
| - ); |
127 |
| - assert_eq!(enumerate_literals![("foo"), ("bar")], [(0, 2), (1, 2)]); |
128 |
| - assert_eq!( |
129 |
| - enumerate_literals_2![ |
130 |
| - [("foo"), ("bar"), ("baz")], |
131 |
| - [("qux"), ("quux"), ("quuz"), ("xyzzy")] |
132 |
| - ], |
133 |
| - [ |
134 |
| - (0, 2, 0, 3, "foo"), |
135 |
| - (0, 2, 1, 3, "bar"), |
136 |
| - (0, 2, 2, 3, "baz"), |
137 |
| - |
138 |
| - (1, 2, 0, 4, "qux"), |
139 |
| - (1, 2, 1, 4, "quux"), |
140 |
| - (1, 2, 2, 4, "quuz"), |
141 |
| - (1, 2, 3, 4, "xyzzy"), |
142 |
| - ] |
143 |
| - ); |
144 | 46 | assert_eq!(plus_one!(a, b, c), 4);
|
145 | 47 | assert_eq!(plus_five!(a, b), 7);
|
146 | 48 | assert_eq!(first!(1, 2), 1);
|
|
0 commit comments