|
2 | 2 |
|
3 | 3 | use std::str; |
4 | 4 |
|
| 5 | +use itertools::intersperse; |
5 | 6 | use proc_macro2::{Literal, Punct, Spacing, Span, TokenStream, TokenTree}; |
6 | 7 | use std::default::Default; |
7 | 8 | use std::iter::FromIterator; |
@@ -160,22 +161,14 @@ fn punct<T, P: Default>(x: Vec<T>) -> Punctuated<T, P> { |
160 | 161 | fn punct_box<T, P: Default>(x: Vec<Box<T>>) -> Punctuated<T, P> { |
161 | 162 | Punctuated::from_iter(x.into_iter().map(|x| *x)) |
162 | 163 | } |
163 | | - |
164 | | -fn comma_separated<T, F>(items: Vec<T>, f: F) -> TokenStream |
| 164 | +fn comma_separated<I, T>(items: I) -> TokenStream |
165 | 165 | where |
166 | | - F: Fn(&mut TokenStream, T), |
| 166 | + I: Iterator<Item = T>, |
| 167 | + T: ToTokens + Clone, |
167 | 168 | { |
168 | | - let mut tokens = TokenStream::new(); |
169 | | - let mut first = true; |
170 | | - for item in items { |
171 | | - if !first { |
172 | | - TokenTree::Punct(Punct::new(',', Spacing::Alone)).to_tokens(&mut tokens); |
173 | | - } else { |
174 | | - first = false; |
175 | | - } |
176 | | - f(&mut tokens, item); |
177 | | - } |
178 | | - tokens |
| 169 | + let items = items.map(|items| items.to_token_stream()); |
| 170 | + let comma = TokenTree::Punct(Punct::new(',', Spacing::Alone)).into_token_stream(); |
| 171 | + intersperse(items, comma).collect() |
179 | 172 | } |
180 | 173 |
|
181 | 174 | pub trait Make<T> { |
@@ -286,30 +279,21 @@ impl Make<TokenStream> for Vec<TokenTree> { |
286 | 279 |
|
287 | 280 | impl Make<TokenStream> for Vec<&str> { |
288 | 281 | fn make(self, _mk: &Builder) -> TokenStream { |
289 | | - comma_separated(self, |tokens, s| { |
290 | | - let tt = TokenTree::Ident(Ident::new(s, Span::call_site())); |
291 | | - tt.to_tokens(tokens); |
292 | | - }) |
| 282 | + comma_separated(self.iter().map(|&s| Ident::new(s, Span::call_site()))) |
293 | 283 | } |
294 | 284 | } |
295 | 285 |
|
296 | 286 | impl Make<TokenStream> for Vec<u64> { |
297 | 287 | fn make(self, _mk: &Builder) -> TokenStream { |
298 | | - comma_separated(self, |tokens, s| { |
299 | | - let tt = TokenTree::Literal(Literal::u64_unsuffixed(s)); |
300 | | - tt.to_tokens(tokens); |
301 | | - }) |
| 288 | + comma_separated(self.iter().map(|&s| Literal::u64_unsuffixed(s))) |
302 | 289 | } |
303 | 290 | } |
304 | 291 |
|
305 | 292 | impl Make<TokenStream> for Vec<Meta> { |
306 | 293 | fn make(self, _mk: &Builder) -> TokenStream { |
307 | | - comma_separated(self, |tokens, s| { |
308 | | - s.to_tokens(tokens); |
309 | | - }) |
| 294 | + comma_separated(self.iter()) |
310 | 295 | } |
311 | 296 | } |
312 | | - |
313 | 297 | impl Make<PathArguments> for AngleBracketedGenericArguments { |
314 | 298 | fn make(self, _mk: &Builder) -> PathArguments { |
315 | 299 | PathArguments::AngleBracketed(self) |
|
0 commit comments