|
2 | 2 |
|
3 | 3 | use proc_macro::{TokenStream, TokenTree}; |
4 | 4 |
|
5 | | -#[allow(dead_code)] |
6 | 5 | pub(crate) trait ToTokens { |
7 | 6 | fn to_tokens(&self, tokens: &mut TokenStream); |
8 | 7 | } |
@@ -47,121 +46,116 @@ impl ToTokens for TokenStream { |
47 | 46 | /// `quote` crate but provides only just enough functionality needed by the current `macros` crate. |
48 | 47 | macro_rules! quote_spanned { |
49 | 48 | ($span:expr => $($tt:tt)*) => {{ |
50 | | - let mut tokens: ::std::vec::Vec<::proc_macro::TokenTree>; |
51 | | - #[allow(clippy::vec_init_then_push)] |
| 49 | + let mut tokens = ::proc_macro::TokenStream::new(); |
52 | 50 | { |
53 | | - tokens = ::std::vec::Vec::new(); |
54 | 51 | let span = $span; |
55 | 52 | quote_spanned!(@proc tokens span $($tt)*); |
56 | 53 | } |
57 | | - ::proc_macro::TokenStream::from_iter(tokens) |
| 54 | + tokens |
58 | 55 | }}; |
59 | 56 | (@proc $v:ident $span:ident) => {}; |
60 | 57 | (@proc $v:ident $span:ident #$id:ident $($tt:tt)*) => { |
61 | | - let mut ts = ::proc_macro::TokenStream::new(); |
62 | | - $crate::quote::ToTokens::to_tokens(&$id, &mut ts); |
63 | | - $v.extend(ts); |
| 58 | + $crate::quote::ToTokens::to_tokens(&$id, &mut $v); |
64 | 59 | quote_spanned!(@proc $v $span $($tt)*); |
65 | 60 | }; |
66 | 61 | (@proc $v:ident $span:ident #(#$id:ident)* $($tt:tt)*) => { |
67 | 62 | for token in $id { |
68 | | - let mut ts = ::proc_macro::TokenStream::new(); |
69 | | - $crate::quote::ToTokens::to_tokens(&token, &mut ts); |
70 | | - $v.extend(ts); |
| 63 | + $crate::quote::ToTokens::to_tokens(&token, &mut $v); |
71 | 64 | } |
72 | 65 | quote_spanned!(@proc $v $span $($tt)*); |
73 | 66 | }; |
74 | 67 | (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => { |
75 | 68 | #[allow(unused_mut)] |
76 | | - let mut tokens = ::std::vec::Vec::<::proc_macro::TokenTree>::new(); |
| 69 | + let mut tokens = ::proc_macro::TokenStream::new(); |
77 | 70 | quote_spanned!(@proc tokens $span $($inner)*); |
78 | | - $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new( |
| 71 | + $v.extend([::proc_macro::TokenTree::Group(::proc_macro::Group::new( |
79 | 72 | ::proc_macro::Delimiter::Parenthesis, |
80 | | - ::proc_macro::TokenStream::from_iter(tokens) |
81 | | - ))); |
| 73 | + tokens, |
| 74 | + ))]); |
82 | 75 | quote_spanned!(@proc $v $span $($tt)*); |
83 | 76 | }; |
84 | 77 | (@proc $v:ident $span:ident [ $($inner:tt)* ] $($tt:tt)*) => { |
85 | | - let mut tokens = ::std::vec::Vec::new(); |
| 78 | + let mut tokens = ::proc_macro::TokenStream::new(); |
86 | 79 | quote_spanned!(@proc tokens $span $($inner)*); |
87 | | - $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new( |
| 80 | + $v.extend([::proc_macro::TokenTree::Group(::proc_macro::Group::new( |
88 | 81 | ::proc_macro::Delimiter::Bracket, |
89 | | - ::proc_macro::TokenStream::from_iter(tokens) |
90 | | - ))); |
| 82 | + tokens, |
| 83 | + ))]); |
91 | 84 | quote_spanned!(@proc $v $span $($tt)*); |
92 | 85 | }; |
93 | 86 | (@proc $v:ident $span:ident { $($inner:tt)* } $($tt:tt)*) => { |
94 | | - let mut tokens = ::std::vec::Vec::new(); |
| 87 | + let mut tokens = ::proc_macro::TokenStream::new(); |
95 | 88 | quote_spanned!(@proc tokens $span $($inner)*); |
96 | | - $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new( |
| 89 | + $v.extend([::proc_macro::TokenTree::Group(::proc_macro::Group::new( |
97 | 90 | ::proc_macro::Delimiter::Brace, |
98 | | - ::proc_macro::TokenStream::from_iter(tokens) |
99 | | - ))); |
| 91 | + tokens, |
| 92 | + ))]); |
100 | 93 | quote_spanned!(@proc $v $span $($tt)*); |
101 | 94 | }; |
102 | 95 | (@proc $v:ident $span:ident :: $($tt:tt)*) => { |
103 | | - $v.push(::proc_macro::TokenTree::Punct( |
104 | | - ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Joint) |
105 | | - )); |
106 | | - $v.push(::proc_macro::TokenTree::Punct( |
107 | | - ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone) |
108 | | - )); |
| 96 | + $v.extend([::proc_macro::Spacing::Joint, ::proc_macro::Spacing::Alone].map(|spacing| { |
| 97 | + ::proc_macro::TokenTree::Punct(::proc_macro::Punct::new(':', spacing)) |
| 98 | + })); |
109 | 99 | quote_spanned!(@proc $v $span $($tt)*); |
110 | 100 | }; |
111 | 101 | (@proc $v:ident $span:ident : $($tt:tt)*) => { |
112 | | - $v.push(::proc_macro::TokenTree::Punct( |
113 | | - ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone) |
114 | | - )); |
| 102 | + $v.extend([::proc_macro::TokenTree::Punct( |
| 103 | + ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone), |
| 104 | + )]); |
115 | 105 | quote_spanned!(@proc $v $span $($tt)*); |
116 | 106 | }; |
117 | 107 | (@proc $v:ident $span:ident , $($tt:tt)*) => { |
118 | | - $v.push(::proc_macro::TokenTree::Punct( |
119 | | - ::proc_macro::Punct::new(',', ::proc_macro::Spacing::Alone) |
120 | | - )); |
| 108 | + $v.extend([::proc_macro::TokenTree::Punct( |
| 109 | + ::proc_macro::Punct::new(',', ::proc_macro::Spacing::Alone), |
| 110 | + )]); |
121 | 111 | quote_spanned!(@proc $v $span $($tt)*); |
122 | 112 | }; |
123 | 113 | (@proc $v:ident $span:ident @ $($tt:tt)*) => { |
124 | | - $v.push(::proc_macro::TokenTree::Punct( |
125 | | - ::proc_macro::Punct::new('@', ::proc_macro::Spacing::Alone) |
126 | | - )); |
| 114 | + $v.extend([::proc_macro::TokenTree::Punct( |
| 115 | + ::proc_macro::Punct::new('@', ::proc_macro::Spacing::Alone), |
| 116 | + )]); |
127 | 117 | quote_spanned!(@proc $v $span $($tt)*); |
128 | 118 | }; |
129 | 119 | (@proc $v:ident $span:ident ! $($tt:tt)*) => { |
130 | | - $v.push(::proc_macro::TokenTree::Punct( |
131 | | - ::proc_macro::Punct::new('!', ::proc_macro::Spacing::Alone) |
132 | | - )); |
| 120 | + $v.extend([::proc_macro::TokenTree::Punct( |
| 121 | + ::proc_macro::Punct::new('!', ::proc_macro::Spacing::Alone), |
| 122 | + )]); |
133 | 123 | quote_spanned!(@proc $v $span $($tt)*); |
134 | 124 | }; |
135 | 125 | (@proc $v:ident $span:ident ; $($tt:tt)*) => { |
136 | | - $v.push(::proc_macro::TokenTree::Punct( |
137 | | - ::proc_macro::Punct::new(';', ::proc_macro::Spacing::Alone) |
138 | | - )); |
| 126 | + $v.extend([::proc_macro::TokenTree::Punct( |
| 127 | + ::proc_macro::Punct::new(';', ::proc_macro::Spacing::Alone), |
| 128 | + )]); |
139 | 129 | quote_spanned!(@proc $v $span $($tt)*); |
140 | 130 | }; |
141 | 131 | (@proc $v:ident $span:ident + $($tt:tt)*) => { |
142 | | - $v.push(::proc_macro::TokenTree::Punct( |
143 | | - ::proc_macro::Punct::new('+', ::proc_macro::Spacing::Alone) |
144 | | - )); |
| 132 | + $v.extend([::proc_macro::TokenTree::Punct( |
| 133 | + ::proc_macro::Punct::new('+', ::proc_macro::Spacing::Alone), |
| 134 | + )]); |
145 | 135 | quote_spanned!(@proc $v $span $($tt)*); |
146 | 136 | }; |
147 | 137 | (@proc $v:ident $span:ident = $($tt:tt)*) => { |
148 | | - $v.push(::proc_macro::TokenTree::Punct( |
149 | | - ::proc_macro::Punct::new('=', ::proc_macro::Spacing::Alone) |
150 | | - )); |
| 138 | + $v.extend([::proc_macro::TokenTree::Punct( |
| 139 | + ::proc_macro::Punct::new('=', ::proc_macro::Spacing::Alone), |
| 140 | + )]); |
151 | 141 | quote_spanned!(@proc $v $span $($tt)*); |
152 | 142 | }; |
153 | 143 | (@proc $v:ident $span:ident # $($tt:tt)*) => { |
154 | | - $v.push(::proc_macro::TokenTree::Punct( |
155 | | - ::proc_macro::Punct::new('#', ::proc_macro::Spacing::Alone) |
156 | | - )); |
| 144 | + $v.extend([::proc_macro::TokenTree::Punct( |
| 145 | + ::proc_macro::Punct::new('#', ::proc_macro::Spacing::Alone), |
| 146 | + )]); |
157 | 147 | quote_spanned!(@proc $v $span $($tt)*); |
158 | 148 | }; |
159 | 149 | (@proc $v:ident $span:ident _ $($tt:tt)*) => { |
160 | | - $v.push(::proc_macro::TokenTree::Ident(::proc_macro::Ident::new("_", $span))); |
| 150 | + $v.extend([::proc_macro::TokenTree::Ident( |
| 151 | + ::proc_macro::Ident::new("_", $span), |
| 152 | + )]); |
161 | 153 | quote_spanned!(@proc $v $span $($tt)*); |
162 | 154 | }; |
163 | 155 | (@proc $v:ident $span:ident $id:ident $($tt:tt)*) => { |
164 | | - $v.push(::proc_macro::TokenTree::Ident(::proc_macro::Ident::new(stringify!($id), $span))); |
| 156 | + $v.extend([::proc_macro::TokenTree::Ident( |
| 157 | + ::proc_macro::Ident::new(stringify!($id), $span), |
| 158 | + )]); |
165 | 159 | quote_spanned!(@proc $v $span $($tt)*); |
166 | 160 | }; |
167 | 161 | } |
|
0 commit comments