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`.
This commit is contained in:
Nicholas Nethercote 2024-06-27 11:15:38 +10:00
parent 0cfd2473be
commit 7416c20cfd

View File

@ -23,7 +23,6 @@
use rustc_macros::{Decodable, Encodable, HashStable_Generic}; use rustc_macros::{Decodable, Encodable, HashStable_Generic};
use rustc_serialize::{Decodable, Encodable}; use rustc_serialize::{Decodable, Encodable};
use rustc_span::{sym, Span, SpanDecoder, SpanEncoder, Symbol, DUMMY_SP}; use rustc_span::{sym, Span, SpanDecoder, SpanEncoder, Symbol, DUMMY_SP};
use smallvec::{smallvec, SmallVec};
use std::borrow::Cow; use std::borrow::Cow;
use std::{cmp, fmt, iter}; use std::{cmp, fmt, iter};
@ -186,20 +185,19 @@ pub fn new(tokens: Vec<AttrTokenTree>) -> AttrTokenStream {
/// If there are inner attributes, they are inserted into the proper /// If there are inner attributes, they are inserted into the proper
/// place in the attribute target tokens. /// place in the attribute target tokens.
pub fn to_token_trees(&self) -> Vec<TokenTree> { pub fn to_token_trees(&self) -> Vec<TokenTree> {
self.0 let mut res = Vec::with_capacity(self.0.len());
.iter() for tree in self.0.iter() {
.flat_map(|tree| match &tree { match tree {
AttrTokenTree::Token(inner, spacing) => { AttrTokenTree::Token(inner, spacing) => {
smallvec![TokenTree::Token(inner.clone(), *spacing)].into_iter() res.push(TokenTree::Token(inner.clone(), *spacing));
} }
AttrTokenTree::Delimited(span, spacing, delim, stream) => { AttrTokenTree::Delimited(span, spacing, delim, stream) => {
smallvec![TokenTree::Delimited( res.push(TokenTree::Delimited(
*span, *span,
*spacing, *spacing,
*delim, *delim,
TokenStream::new(stream.to_token_trees()) TokenStream::new(stream.to_token_trees()),
),] ))
.into_iter()
} }
AttrTokenTree::Attributes(data) => { AttrTokenTree::Attributes(data) => {
let idx = data let idx = data
@ -243,16 +241,14 @@ pub fn to_token_trees(&self) -> Vec<TokenTree> {
"Failed to find trailing delimited group in: {target_tokens:?}" "Failed to find trailing delimited group in: {target_tokens:?}"
); );
} }
let mut flat: SmallVec<[_; 1]> =
SmallVec::with_capacity(target_tokens.len() + outer_attrs.len());
for attr in outer_attrs { for attr in outer_attrs {
flat.extend(attr.tokens().0.iter().cloned()); res.extend(attr.tokens().0.iter().cloned());
} }
flat.extend(target_tokens); res.extend(target_tokens);
flat.into_iter()
} }
}) }
.collect() }
res
} }
} }