Skip to content

Commit 7416c20

Browse files
committed
Just push in AttrTokenStream::to_token_trees.
Currently it uses a mixture of functional style (`flat_map`) and imperative style (`push`), which is a bit hard to read. This commit converts it to fully imperative, which is more concise and avoids the need for `smallvec`.
1 parent 0cfd247 commit 7416c20

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_data_structures::sync::{self, Lrc};
2323
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
2424
use rustc_serialize::{Decodable, Encodable};
2525
use rustc_span::{sym, Span, SpanDecoder, SpanEncoder, Symbol, DUMMY_SP};
26-
use smallvec::{smallvec, SmallVec};
2726

2827
use std::borrow::Cow;
2928
use std::{cmp, fmt, iter};
@@ -186,20 +185,19 @@ impl AttrTokenStream {
186185
/// If there are inner attributes, they are inserted into the proper
187186
/// place in the attribute target tokens.
188187
pub fn to_token_trees(&self) -> Vec<TokenTree> {
189-
self.0
190-
.iter()
191-
.flat_map(|tree| match &tree {
188+
let mut res = Vec::with_capacity(self.0.len());
189+
for tree in self.0.iter() {
190+
match tree {
192191
AttrTokenTree::Token(inner, spacing) => {
193-
smallvec![TokenTree::Token(inner.clone(), *spacing)].into_iter()
192+
res.push(TokenTree::Token(inner.clone(), *spacing));
194193
}
195194
AttrTokenTree::Delimited(span, spacing, delim, stream) => {
196-
smallvec![TokenTree::Delimited(
195+
res.push(TokenTree::Delimited(
197196
*span,
198197
*spacing,
199198
*delim,
200-
TokenStream::new(stream.to_token_trees())
201-
),]
202-
.into_iter()
199+
TokenStream::new(stream.to_token_trees()),
200+
))
203201
}
204202
AttrTokenTree::Attributes(data) => {
205203
let idx = data
@@ -243,16 +241,14 @@ impl AttrTokenStream {
243241
"Failed to find trailing delimited group in: {target_tokens:?}"
244242
);
245243
}
246-
let mut flat: SmallVec<[_; 1]> =
247-
SmallVec::with_capacity(target_tokens.len() + outer_attrs.len());
248244
for attr in outer_attrs {
249-
flat.extend(attr.tokens().0.iter().cloned());
245+
res.extend(attr.tokens().0.iter().cloned());
250246
}
251-
flat.extend(target_tokens);
252-
flat.into_iter()
247+
res.extend(target_tokens);
253248
}
254-
})
255-
.collect()
249+
}
250+
}
251+
res
256252
}
257253
}
258254

0 commit comments

Comments
 (0)