Skip to content

Commit c290293

Browse files
committed
Derive Rustc{En,De}codable for TokenStream.
`TokenStream` used to be a complex type, but it is now just a newtype around a `Lrc<Vec<TreeAndJoint>>`. Currently it uses custom encoding that discards the `IsJoint` and custom decoding that adds `NonJoint` back in for every token tree. This requires building intermediate `Vec<TokenTree>`s. This commit makes `TokenStream` derive `Rustc{En,De}codable`. This simplifies the code, and avoids the creation of the intermediate vectors, saving up to 3% on various benchmarks. It also changes the AST JSON output in one test.
1 parent c23a7aa commit c290293

File tree

2 files changed

+3
-16
lines changed

2 files changed

+3
-16
lines changed

src/libsyntax/tokenstream.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use syntax_pos::{BytePos, Span, DUMMY_SP};
1919
#[cfg(target_arch = "x86_64")]
2020
use rustc_data_structures::static_assert_size;
2121
use rustc_data_structures::sync::Lrc;
22-
use rustc_serialize::{Decoder, Decodable, Encoder, Encodable};
2322
use smallvec::{SmallVec, smallvec};
2423

2524
use std::{iter, mem};
@@ -136,7 +135,7 @@ impl TokenTree {
136135
/// The goal is for procedural macros to work with `TokenStream`s and `TokenTree`s
137136
/// instead of a representation of the abstract syntax tree.
138137
/// Today's `TokenTree`s can still contain AST via `token::Interpolated` for back-compat.
139-
#[derive(Clone, Debug, Default)]
138+
#[derive(Clone, Debug, Default, RustcEncodable, RustcDecodable)]
140139
pub struct TokenStream(pub Lrc<Vec<TreeAndJoint>>);
141140

142141
pub type TreeAndJoint = (TokenTree, IsJoint);
@@ -145,7 +144,7 @@ pub type TreeAndJoint = (TokenTree, IsJoint);
145144
#[cfg(target_arch = "x86_64")]
146145
static_assert_size!(TokenStream, 8);
147146

148-
#[derive(Clone, Copy, Debug, PartialEq)]
147+
#[derive(Clone, Copy, Debug, PartialEq, RustcEncodable, RustcDecodable)]
149148
pub enum IsJoint {
150149
Joint,
151150
NonJoint
@@ -460,18 +459,6 @@ impl Cursor {
460459
}
461460
}
462461

463-
impl Encodable for TokenStream {
464-
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), E::Error> {
465-
self.trees().collect::<Vec<_>>().encode(encoder)
466-
}
467-
}
468-
469-
impl Decodable for TokenStream {
470-
fn decode<D: Decoder>(decoder: &mut D) -> Result<TokenStream, D::Error> {
471-
Vec::<TokenTree>::decode(decoder).map(|vec| vec.into_iter().collect())
472-
}
473-
}
474-
475462
#[derive(Debug, Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
476463
pub struct DelimSpan {
477464
pub open: Span,
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"module":{"inner":{"lo":0,"hi":0},"items":[{"ident":{"name":"core","span":{"lo":0,"hi":0}},"attrs":[],"id":0,"kind":{"variant":"ExternCrate","fields":[null]},"vis":{"node":"Inherited","span":{"lo":0,"hi":0}},"span":{"lo":0,"hi":0},"tokens":[{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["extern",false]},"span":{"lo":0,"hi":0}}]},{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["crate",false]},"span":{"lo":0,"hi":0}}]},{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["core",false]},"span":{"lo":0,"hi":0}}]},{"variant":"Token","fields":[{"kind":"Semi","span":{"lo":0,"hi":0}}]}]}],"inline":true},"attrs":[],"span":{"lo":0,"hi":0}}
1+
{"module":{"inner":{"lo":0,"hi":0},"items":[{"ident":{"name":"core","span":{"lo":0,"hi":0}},"attrs":[],"id":0,"kind":{"variant":"ExternCrate","fields":[null]},"vis":{"node":"Inherited","span":{"lo":0,"hi":0}},"span":{"lo":0,"hi":0},"tokens":{"_field0":[[{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["extern",false]},"span":{"lo":0,"hi":0}}]},"NonJoint"],[{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["crate",false]},"span":{"lo":0,"hi":0}}]},"NonJoint"],[{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["core",false]},"span":{"lo":0,"hi":0}}]},"NonJoint"],[{"variant":"Token","fields":[{"kind":"Semi","span":{"lo":0,"hi":0}}]},"NonJoint"]]}}],"inline":true},"attrs":[],"span":{"lo":0,"hi":0}}

0 commit comments

Comments
 (0)