File tree Expand file tree Collapse file tree 2 files changed +13
-6
lines changed Expand file tree Collapse file tree 2 files changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -361,9 +361,7 @@ fn panic_expand(
361361 } ;
362362
363363 // FIXME(slow): quote! have a way to expand to builder to make this a vec!
364- let mut mutable_trees = std:: mem:: take ( & mut call. token_trees ) . into_vec ( ) ;
365- mutable_trees. push ( tt:: TokenTree :: Subtree ( subtree) ) ;
366- call. token_trees = mutable_trees. into_boxed_slice ( ) ;
364+ call. push ( tt:: TokenTree :: Subtree ( subtree) ) ;
367365
368366 ExpandResult :: ok ( call)
369367}
@@ -395,9 +393,7 @@ fn unreachable_expand(
395393 } ;
396394
397395 // FIXME(slow): quote! have a way to expand to builder to make this a vec!
398- let mut mutable_trees = std:: mem:: take ( & mut call. token_trees ) . into_vec ( ) ;
399- mutable_trees. push ( tt:: TokenTree :: Subtree ( subtree) ) ;
400- call. token_trees = mutable_trees. into_boxed_slice ( ) ;
396+ call. push ( tt:: TokenTree :: Subtree ( subtree) ) ;
401397
402398 ExpandResult :: ok ( call)
403399}
Original file line number Diff line number Diff line change @@ -77,6 +77,17 @@ impl<S: Span> Subtree<S> {
7777 Subtree { delimiter : Delimiter :: invisible_delim_spanned ( span) , token_trees : Box :: new ( [ ] ) }
7878 }
7979
80+ /// This is slow, and should be avoided, as it will always reallocate!
81+ pub fn push ( & mut self , subtree : TokenTree < S > ) {
82+ let mut mutable_trees = std:: mem:: take ( & mut self . token_trees ) . into_vec ( ) ;
83+
84+ // Reserve exactly space for one element, to avoid `into_boxed_slice` having to reallocate again.
85+ mutable_trees. reserve_exact ( 1 ) ;
86+ mutable_trees. push ( subtree) ;
87+
88+ self . token_trees = mutable_trees. into_boxed_slice ( ) ;
89+ }
90+
8091 pub fn visit_ids ( & mut self , f : & mut impl FnMut ( S ) -> S ) {
8192 self . delimiter . open = f ( self . delimiter . open ) ;
8293 self . delimiter . close = f ( self . delimiter . close ) ;
You can’t perform that action at this time.
0 commit comments