@@ -47,6 +47,7 @@ use core::ops::BitOr;
4747use std:: ffi:: CStr ;
4848use std:: ops:: { Range , RangeBounds } ;
4949use std:: path:: PathBuf ;
50+ use std:: rc:: Rc ;
5051use std:: str:: FromStr ;
5152use std:: { error, fmt} ;
5253
@@ -99,7 +100,7 @@ pub fn is_available() -> bool {
99100#[ cfg_attr( feature = "rustc-dep-of-std" , rustc_diagnostic_item = "TokenStream" ) ]
100101#[ stable( feature = "proc_macro_lib" , since = "1.15.0" ) ]
101102#[ derive( Clone ) ]
102- pub struct TokenStream ( Vec < bridge:: TokenTree < bridge:: client:: Span , bridge:: client:: Symbol > > ) ;
103+ pub struct TokenStream ( Rc < Vec < bridge:: TokenTree < bridge:: client:: Span , bridge:: client:: Symbol > > > ) ;
103104
104105#[ stable( feature = "proc_macro_lib" , since = "1.15.0" ) ]
105106impl !Send for TokenStream { }
@@ -153,7 +154,7 @@ impl TokenStream {
153154 /// Returns an empty `TokenStream` containing no token trees.
154155 #[ stable( feature = "proc_macro_lib2" , since = "1.29.0" ) ]
155156 pub fn new ( ) -> TokenStream {
156- TokenStream ( Vec :: new ( ) )
157+ TokenStream ( Rc :: new ( Vec :: new ( ) ) )
157158 }
158159
159160 /// Checks if this `TokenStream` is empty.
@@ -175,7 +176,7 @@ impl TokenStream {
175176 #[ unstable( feature = "proc_macro_expand" , issue = "90765" ) ]
176177 pub fn expand_expr ( & self ) -> Result < TokenStream , ExpandError > {
177178 match BridgeMethods :: ts_expand_expr ( stream_to_bridge_stream ( self . clone ( ) ) ) {
178- Ok ( stream) => Ok ( TokenStream ( stream. trees ) ) ,
179+ Ok ( stream) => Ok ( TokenStream ( Rc :: new ( stream. trees ) ) ) ,
179180 Err ( _) => Err ( ExpandError ) ,
180181 }
181182 }
@@ -193,7 +194,7 @@ impl FromStr for TokenStream {
193194 type Err = LexError ;
194195
195196 fn from_str ( src : & str ) -> Result < TokenStream , LexError > {
196- Ok ( TokenStream ( BridgeMethods :: ts_from_str ( src) . trees ) )
197+ Ok ( TokenStream ( Rc :: new ( BridgeMethods :: ts_from_str ( src) . trees ) ) )
197198 }
198199}
199200
@@ -237,7 +238,7 @@ pub use quote::{HasIterator, RepInterp, ThereIsNoIteratorInRepetition, ext, quot
237238fn stream_to_bridge_stream (
238239 stream : TokenStream ,
239240) -> bridge:: TokenStream < bridge:: client:: Span , bridge:: client:: Symbol > {
240- bridge:: TokenStream { trees : stream. 0 }
241+ bridge:: TokenStream { trees : stream. 0 . to_vec ( ) }
241242}
242243
243244fn tree_to_bridge_tree (
@@ -255,7 +256,7 @@ fn tree_to_bridge_tree(
255256#[ stable( feature = "proc_macro_lib2" , since = "1.29.0" ) ]
256257impl From < TokenTree > for TokenStream {
257258 fn from ( tree : TokenTree ) -> TokenStream {
258- TokenStream ( vec ! [ tree_to_bridge_tree( tree) ] )
259+ TokenStream ( Rc :: new ( vec ! [ tree_to_bridge_tree( tree) ] ) )
259260 }
260261}
261262
@@ -275,14 +276,7 @@ impl ConcatTreesHelper {
275276 }
276277
277278 fn build ( self ) -> TokenStream {
278- TokenStream ( self . trees )
279- }
280-
281- fn append_to ( mut self , stream : & mut TokenStream ) {
282- if self . trees . is_empty ( ) {
283- return ;
284- }
285- stream. 0 . append ( & mut self . trees ) ;
279+ TokenStream ( Rc :: new ( self . trees ) )
286280 }
287281}
288282
@@ -308,15 +302,6 @@ impl ConcatStreamsHelper {
308302 }
309303 stream
310304 }
311-
312- fn append_to ( self , stream : & mut TokenStream ) {
313- if self . streams . is_empty ( ) {
314- return ;
315- }
316- for mut s in self . streams {
317- stream. 0 . append ( & mut s. 0 ) ;
318- }
319- }
320305}
321306
322307/// Collects a number of token trees into a single stream.
@@ -348,7 +333,12 @@ impl Extend<TokenTree> for TokenStream {
348333 let iter = trees. into_iter ( ) ;
349334 let mut builder = ConcatTreesHelper :: new ( iter. size_hint ( ) . 0 ) ;
350335 iter. for_each ( |tree| builder. push ( tree) ) ;
351- builder. append_to ( self ) ;
336+ if builder. trees . is_empty ( ) {
337+ return ;
338+ }
339+ let mut old = ( * self . 0 ) . clone ( ) ;
340+ old. append ( & mut builder. trees ) ;
341+ self . 0 = Rc :: new ( old) ;
352342 }
353343}
354344
@@ -358,7 +348,11 @@ impl Extend<TokenStream> for TokenStream {
358348 let iter = streams. into_iter ( ) ;
359349 let mut builder = ConcatStreamsHelper :: new ( iter. size_hint ( ) . 0 ) ;
360350 iter. for_each ( |stream| builder. push ( stream) ) ;
361- builder. append_to ( self ) ;
351+ for s in builder. streams {
352+ let mut old = ( * self . 0 ) . clone ( ) ;
353+ old. append ( & mut Rc :: unwrap_or_clone ( s. 0 ) ) ;
354+ self . 0 = Rc :: new ( old) ;
355+ }
362356 }
363357}
364358
@@ -380,6 +374,8 @@ extend_items!(Group Literal Punct Ident);
380374/// Public implementation details for the `TokenStream` type, such as iterators.
381375#[ stable( feature = "proc_macro_lib2" , since = "1.29.0" ) ]
382376pub mod token_stream {
377+ use std:: rc:: Rc ;
378+
383379 use crate :: { Group , Ident , Literal , Punct , TokenStream , TokenTree , bridge} ;
384380
385381 /// An iterator over `TokenStream`'s `TokenTree`s.
@@ -419,7 +415,7 @@ pub mod token_stream {
419415 type IntoIter = IntoIter ;
420416
421417 fn into_iter ( self ) -> IntoIter {
422- IntoIter ( self . 0 . into_iter ( ) )
418+ IntoIter ( Rc :: unwrap_or_clone ( self . 0 ) . into_iter ( ) )
423419 }
424420 }
425421}
@@ -815,8 +811,8 @@ impl Group {
815811 #[ stable( feature = "proc_macro_lib2" , since = "1.29.0" ) ]
816812 pub fn stream ( & self ) -> TokenStream {
817813 match & self . 0 . stream {
818- Some ( stream) => TokenStream ( stream. trees . clone ( ) ) ,
819- None => TokenStream ( vec ! [ ] ) ,
814+ Some ( stream) => TokenStream ( Rc :: new ( stream. trees . clone ( ) ) ) ,
815+ None => TokenStream ( Rc :: new ( vec ! [ ] ) ) ,
820816 }
821817 }
822818
0 commit comments