Skip to content

Commit c5db3d6

Browse files
committed
Reduce make_mut calls in FromIterator and Extend impls
1 parent f66898b commit c5db3d6

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

src/fallback.rs

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,47 +65,45 @@ impl TokenStream {
6565
fn take_inner(&mut self) -> Vec<TokenTree> {
6666
mem::replace(Rc::make_mut(&mut self.inner), Vec::new())
6767
}
68+
}
6869

69-
fn push_token_from_proc_macro(&mut self, token: TokenTree) {
70-
// https://github.com/dtolnay/proc-macro2/issues/235
71-
match token {
72-
#[cfg(not(no_bind_by_move_pattern_guard))]
73-
TokenTree::Literal(crate::Literal {
74-
#[cfg(wrap_proc_macro)]
75-
inner: crate::imp::Literal::Fallback(literal),
76-
#[cfg(not(wrap_proc_macro))]
77-
inner: literal,
78-
..
79-
}) if literal.repr.starts_with('-') => {
80-
push_negative_literal(self, literal);
81-
}
82-
#[cfg(no_bind_by_move_pattern_guard)]
83-
TokenTree::Literal(crate::Literal {
84-
#[cfg(wrap_proc_macro)]
85-
inner: crate::imp::Literal::Fallback(literal),
86-
#[cfg(not(wrap_proc_macro))]
87-
inner: literal,
88-
..
89-
}) => {
90-
if literal.repr.starts_with('-') {
91-
push_negative_literal(self, literal);
92-
} else {
93-
Rc::make_mut(&mut self.inner)
94-
.push(TokenTree::Literal(crate::Literal::_new_stable(literal)));
95-
}
70+
fn push_token_from_proc_macro(vec: &mut Vec<TokenTree>, token: TokenTree) {
71+
// https://github.com/dtolnay/proc-macro2/issues/235
72+
match token {
73+
#[cfg(not(no_bind_by_move_pattern_guard))]
74+
TokenTree::Literal(crate::Literal {
75+
#[cfg(wrap_proc_macro)]
76+
inner: crate::imp::Literal::Fallback(literal),
77+
#[cfg(not(wrap_proc_macro))]
78+
inner: literal,
79+
..
80+
}) if literal.repr.starts_with('-') => {
81+
push_negative_literal(vec, literal);
82+
}
83+
#[cfg(no_bind_by_move_pattern_guard)]
84+
TokenTree::Literal(crate::Literal {
85+
#[cfg(wrap_proc_macro)]
86+
inner: crate::imp::Literal::Fallback(literal),
87+
#[cfg(not(wrap_proc_macro))]
88+
inner: literal,
89+
..
90+
}) => {
91+
if literal.repr.starts_with('-') {
92+
push_negative_literal(vec, literal);
93+
} else {
94+
vec.push(TokenTree::Literal(crate::Literal::_new_stable(literal)));
9695
}
97-
_ => Rc::make_mut(&mut self.inner).push(token),
9896
}
97+
_ => vec.push(token),
98+
}
9999

100-
#[cold]
101-
fn push_negative_literal(stream: &mut TokenStream, mut literal: Literal) {
102-
literal.repr.remove(0);
103-
let mut punct = crate::Punct::new('-', Spacing::Alone);
104-
punct.set_span(crate::Span::_new_stable(literal.span));
105-
let inner = Rc::make_mut(&mut stream.inner);
106-
inner.push(TokenTree::Punct(punct));
107-
inner.push(TokenTree::Literal(crate::Literal::_new_stable(literal)));
108-
}
100+
#[cold]
101+
fn push_negative_literal(vec: &mut Vec<TokenTree>, mut literal: Literal) {
102+
literal.repr.remove(0);
103+
let mut punct = crate::Punct::new('-', Spacing::Alone);
104+
punct.set_span(crate::Span::_new_stable(literal.span));
105+
vec.push(TokenTree::Punct(punct));
106+
vec.push(TokenTree::Literal(crate::Literal::_new_stable(literal)));
109107
}
110108
}
111109

@@ -246,9 +244,9 @@ impl From<TokenStream> for proc_macro::TokenStream {
246244

247245
impl From<TokenTree> for TokenStream {
248246
fn from(tree: TokenTree) -> TokenStream {
249-
let mut stream = TokenStream::new();
250-
stream.push_token_from_proc_macro(tree);
251-
stream
247+
let mut stream = TokenStreamBuilder::new();
248+
push_token_from_proc_macro(&mut stream.inner, tree);
249+
stream.build()
252250
}
253251
}
254252

@@ -274,9 +272,10 @@ impl FromIterator<TokenStream> for TokenStream {
274272

275273
impl Extend<TokenTree> for TokenStream {
276274
fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, tokens: I) {
275+
let vec = Rc::make_mut(&mut self.inner);
277276
tokens
278277
.into_iter()
279-
.for_each(|token| self.push_token_from_proc_macro(token));
278+
.for_each(|token| push_token_from_proc_macro(vec, token));
280279
}
281280
}
282281

0 commit comments

Comments
 (0)