@@ -22,7 +22,7 @@ use crate::ptr::P;
22
22
use crate :: sess:: ParseSess ;
23
23
use crate :: symbol:: { sym, Symbol } ;
24
24
use crate :: ThinVec ;
25
- use crate :: tokenstream:: { TokenStream , TokenTree , DelimSpan } ;
25
+ use crate :: tokenstream:: { DelimSpan , TokenStream , TokenTree , TreeAndJoint } ;
26
26
use crate :: GLOBALS ;
27
27
28
28
use log:: debug;
@@ -463,7 +463,7 @@ pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option
463
463
}
464
464
465
465
impl MetaItem {
466
- fn tokens ( & self ) -> TokenStream {
466
+ fn token_trees_and_joints ( & self ) -> Vec < TreeAndJoint > {
467
467
let mut idents = vec ! [ ] ;
468
468
let mut last_pos = BytePos ( 0 as u32 ) ;
469
469
for ( i, segment) in self . path . segments . iter ( ) . enumerate ( ) {
@@ -477,8 +477,8 @@ impl MetaItem {
477
477
idents. push ( TokenTree :: Token ( Token :: from_ast_ident ( segment. ident ) ) . into ( ) ) ;
478
478
last_pos = segment. ident . span . hi ( ) ;
479
479
}
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
482
482
}
483
483
484
484
fn from_tokens < I > ( tokens : & mut iter:: Peekable < I > ) -> Option < MetaItem >
@@ -537,32 +537,41 @@ impl MetaItem {
537
537
}
538
538
539
539
impl MetaItemKind {
540
- pub fn tokens ( & self , span : Span ) -> TokenStream {
540
+ pub fn token_trees_and_joints ( & self , span : Span ) -> Vec < TreeAndJoint > {
541
541
match * self {
542
- MetaItemKind :: Word => TokenStream :: default ( ) ,
542
+ MetaItemKind :: Word => vec ! [ ] ,
543
543
MetaItemKind :: NameValue ( ref lit) => {
544
- TokenStream :: new ( vec ! [
544
+ vec ! [
545
545
TokenTree :: token( token:: Eq , span) . into( ) ,
546
546
lit. token_tree( ) . into( ) ,
547
- ] )
547
+ ]
548
548
}
549
549
MetaItemKind :: List ( ref list) => {
550
550
let mut tokens = Vec :: new ( ) ;
551
551
for ( i, item) in list. iter ( ) . enumerate ( ) {
552
552
if i > 0 {
553
553
tokens. push ( TokenTree :: token ( token:: Comma , span) . into ( ) ) ;
554
554
}
555
- item . tokens ( ) . append_to_tree_and_joint_vec ( & mut tokens ) ;
555
+ tokens. extend ( item . token_trees_and_joints ( ) )
556
556
}
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
+ ]
562
564
}
563
565
}
564
566
}
565
567
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
+
566
575
fn from_tokens < I > ( tokens : & mut iter:: Peekable < I > ) -> Option < MetaItemKind >
567
576
where I : Iterator < Item = TokenTree > ,
568
577
{
@@ -604,10 +613,10 @@ impl NestedMetaItem {
604
613
}
605
614
}
606
615
607
- fn tokens ( & self ) -> TokenStream {
616
+ fn token_trees_and_joints ( & self ) -> Vec < TreeAndJoint > {
608
617
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( ) ] ,
611
620
}
612
621
}
613
622
0 commit comments