Skip to content

Commit 0090eed

Browse files
committed
use rc, take 2
1 parent 670f389 commit 0090eed

3 files changed

Lines changed: 41 additions & 30 deletions

File tree

library/proc_macro/src/bridge/client.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::cell::RefCell;
44
use std::marker::PhantomData;
5+
use std::rc::Rc;
56
use std::sync::atomic::AtomicU32;
67

78
use super::*;
@@ -256,7 +257,9 @@ impl Client<crate::TokenStream, crate::TokenStream> {
256257
Client {
257258
handle_counters: &COUNTERS,
258259
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
259-
run_client(bridge, |input| f(crate::TokenStream(input)).0)
260+
run_client(bridge, |input| {
261+
Rc::unwrap_or_clone(f(crate::TokenStream(Rc::new(input))).0)
262+
})
260263
}),
261264
_marker: PhantomData,
262265
}
@@ -271,7 +274,10 @@ impl Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream> {
271274
handle_counters: &COUNTERS,
272275
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
273276
run_client(bridge, |(input, input2)| {
274-
f(crate::TokenStream(input), crate::TokenStream(input2)).0
277+
Rc::unwrap_or_clone(
278+
f(crate::TokenStream(Rc::new(input)), crate::TokenStream(Rc::new(input2)))
279+
.0,
280+
)
275281
})
276282
}),
277283
_marker: PhantomData,

library/proc_macro/src/bridge/rpc.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,18 @@ impl<'a, S, T: for<'s> Decode<'a, 's, S>> Decode<'a, '_, S> for Vec<T> {
210210
vec
211211
}
212212
}
213+
/*
214+
impl<S, T: Encode<S>> Encode<S> for Rc<T> {
215+
fn encode(self, w: &mut Buffer, s: &mut S) {
216+
T::encode(Rc::clone(self), w, s);
217+
}
218+
}
213219
220+
impl<'a, S, T: for<'s> Decode<'a, 's, S>> Decode<'a, '_, S> for Rc<T> {
221+
fn decode(r: &mut &'a [u8], s: &mut S) -> Self {
222+
Rc::new(T::decode(r, s))
223+
}
224+
}*/
214225
/// Simplified version of panic payloads, ignoring
215226
/// types other than `&'static str` and `String`.
216227
pub enum PanicMessage {

library/proc_macro/src/lib.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use core::ops::BitOr;
4747
use std::ffi::CStr;
4848
use std::ops::{Range, RangeBounds};
4949
use std::path::PathBuf;
50+
use std::rc::Rc;
5051
use std::str::FromStr;
5152
use 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")]
105106
impl !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
237238
fn 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

243244
fn tree_to_bridge_tree(
@@ -255,7 +256,7 @@ fn tree_to_bridge_tree(
255256
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
256257
impl 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,11 @@ 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 old = Rc::make_mut(&mut self.0);
340+
old.append(&mut builder.trees);
352341
}
353342
}
354343

@@ -358,7 +347,10 @@ impl Extend<TokenStream> for TokenStream {
358347
let iter = streams.into_iter();
359348
let mut builder = ConcatStreamsHelper::new(iter.size_hint().0);
360349
iter.for_each(|stream| builder.push(stream));
361-
builder.append_to(self);
350+
for mut s in builder.streams {
351+
let old = Rc::make_mut(&mut self.0);
352+
old.append(Rc::make_mut(&mut s.0));
353+
}
362354
}
363355
}
364356

@@ -380,6 +372,8 @@ extend_items!(Group Literal Punct Ident);
380372
/// Public implementation details for the `TokenStream` type, such as iterators.
381373
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
382374
pub mod token_stream {
375+
use std::rc::Rc;
376+
383377
use crate::{Group, Ident, Literal, Punct, TokenStream, TokenTree, bridge};
384378

385379
/// An iterator over `TokenStream`'s `TokenTree`s.
@@ -419,7 +413,7 @@ pub mod token_stream {
419413
type IntoIter = IntoIter;
420414

421415
fn into_iter(self) -> IntoIter {
422-
IntoIter(self.0.into_iter())
416+
IntoIter(Rc::unwrap_or_clone(self.0).into_iter())
423417
}
424418
}
425419
}
@@ -815,8 +809,8 @@ impl Group {
815809
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
816810
pub fn stream(&self) -> TokenStream {
817811
match &self.0.stream {
818-
Some(stream) => TokenStream(stream.trees.clone()),
819-
None => TokenStream(vec![]),
812+
Some(stream) => TokenStream(Rc::new(stream.trees.clone())),
813+
None => TokenStream(Rc::new(vec![])),
820814
}
821815
}
822816

0 commit comments

Comments
 (0)