Skip to content

Commit e4ec4a6

Browse files
committed
Change MetaItem::tokens() to MetaItem::token_trees_and_joints().
Likewise for `NestedMetaItem::tokens()`. Also, add `MetaItemKind::token_trees_and_joints()`, which `MetaItemKind::tokens()` now calls. This avoids some unnecessary `TokenTree` to `TokenStream` conversions, and removes the need for the clumsy `TokenStream::append_to_tree_and_joint_vec()`.
1 parent 212ae58 commit e4ec4a6

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/libsyntax/attr/mod.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::ptr::P;
2222
use crate::sess::ParseSess;
2323
use crate::symbol::{sym, Symbol};
2424
use crate::ThinVec;
25-
use crate::tokenstream::{TokenStream, TokenTree, DelimSpan};
25+
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint};
2626
use crate::GLOBALS;
2727

2828
use log::debug;
@@ -463,7 +463,7 @@ pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option
463463
}
464464

465465
impl MetaItem {
466-
fn tokens(&self) -> TokenStream {
466+
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
467467
let mut idents = vec![];
468468
let mut last_pos = BytePos(0 as u32);
469469
for (i, segment) in self.path.segments.iter().enumerate() {
@@ -477,8 +477,8 @@ impl MetaItem {
477477
idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident)).into());
478478
last_pos = segment.ident.span.hi();
479479
}
480-
self.kind.tokens(self.span).append_to_tree_and_joint_vec(&mut idents);
481-
TokenStream::new(idents)
480+
idents.extend(self.kind.token_trees_and_joints(self.span));
481+
idents
482482
}
483483

484484
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
@@ -537,32 +537,41 @@ impl MetaItem {
537537
}
538538

539539
impl MetaItemKind {
540-
pub fn tokens(&self, span: Span) -> TokenStream {
540+
pub fn token_trees_and_joints(&self, span: Span) -> Vec<TreeAndJoint> {
541541
match *self {
542-
MetaItemKind::Word => TokenStream::default(),
542+
MetaItemKind::Word => vec![],
543543
MetaItemKind::NameValue(ref lit) => {
544-
TokenStream::new(vec![
544+
vec![
545545
TokenTree::token(token::Eq, span).into(),
546546
lit.token_tree().into(),
547-
])
547+
]
548548
}
549549
MetaItemKind::List(ref list) => {
550550
let mut tokens = Vec::new();
551551
for (i, item) in list.iter().enumerate() {
552552
if i > 0 {
553553
tokens.push(TokenTree::token(token::Comma, span).into());
554554
}
555-
item.tokens().append_to_tree_and_joint_vec(&mut tokens);
555+
tokens.extend(item.token_trees_and_joints())
556556
}
557-
TokenTree::Delimited(
558-
DelimSpan::from_single(span),
559-
token::Paren,
560-
TokenStream::new(tokens).into(),
561-
).into()
557+
vec![
558+
TokenTree::Delimited(
559+
DelimSpan::from_single(span),
560+
token::Paren,
561+
TokenStream::new(tokens).into(),
562+
).into()
563+
]
562564
}
563565
}
564566
}
565567

568+
// Premature conversions of `TokenTree`s to `TokenStream`s can hurt
569+
// performance. Do not use this function if `token_trees_and_joints()` can
570+
// be used instead.
571+
pub fn tokens(&self, span: Span) -> TokenStream {
572+
TokenStream::new(self.token_trees_and_joints(span))
573+
}
574+
566575
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItemKind>
567576
where I: Iterator<Item = TokenTree>,
568577
{
@@ -604,10 +613,10 @@ impl NestedMetaItem {
604613
}
605614
}
606615

607-
fn tokens(&self) -> TokenStream {
616+
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
608617
match *self {
609-
NestedMetaItem::MetaItem(ref item) => item.tokens(),
610-
NestedMetaItem::Literal(ref lit) => lit.token_tree().into(),
618+
NestedMetaItem::MetaItem(ref item) => item.token_trees_and_joints(),
619+
NestedMetaItem::Literal(ref lit) => vec![lit.token_tree().into()],
611620
}
612621
}
613622

src/libsyntax/tokenstream.rs

-4
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,6 @@ impl TokenStream {
271271
}
272272
}
273273

274-
pub fn append_to_tree_and_joint_vec(self, vec: &mut Vec<TreeAndJoint>) {
275-
vec.extend(self.0.iter().cloned());
276-
}
277-
278274
pub fn trees(&self) -> Cursor {
279275
self.clone().into_trees()
280276
}

0 commit comments

Comments
 (0)