Skip to content

Commit dd1264e

Browse files
committed
rustc_metadata: replace Lazy<[Table<T>]> with Lazy<Table<T>>.
1 parent ffd18fc commit dd1264e

File tree

4 files changed

+46
-18
lines changed

4 files changed

+46
-18
lines changed

src/librustc_metadata/decoder.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use crate::cstore::{self, CrateMetadata, MetadataBlob};
44
use crate::schema::*;
5+
use crate::table::Table;
56

67
use rustc_index::vec::IndexVec;
78
use rustc_data_structures::sync::{Lrc, ReadGuard};
@@ -28,7 +29,7 @@ use std::mem;
2829
use std::num::NonZeroUsize;
2930
use std::u32;
3031

31-
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
32+
use rustc_serialize::{Decodable, Decoder, Encodable, SpecializedDecoder, opaque};
3233
use syntax::attr;
3334
use syntax::ast::{self, Ident};
3435
use syntax::source_map::{self, respan, Spanned};
@@ -130,15 +131,15 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'tcx>) {
130131
}
131132
}
132133

133-
impl<'a, 'tcx, T: Decodable> Lazy<T> {
134+
impl<'a, 'tcx, T: Encodable + Decodable> Lazy<T> {
134135
crate fn decode<M: Metadata<'a, 'tcx>>(self, meta: M) -> T {
135136
let mut dcx = meta.decoder(self.position.get());
136137
dcx.lazy_state = LazyState::NodeStart(self.position);
137138
T::decode(&mut dcx).unwrap()
138139
}
139140
}
140141

141-
impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable> Lazy<[T]> {
142+
impl<'a: 'x, 'tcx: 'x, 'x, T: Encodable + Decodable> Lazy<[T]> {
142143
crate fn decode<M: Metadata<'a, 'tcx>>(
143144
self,
144145
meta: M,
@@ -237,13 +238,13 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
237238
}
238239
}
239240

240-
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
241+
impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
241242
fn specialized_decode(&mut self) -> Result<Lazy<T>, Self::Error> {
242243
self.read_lazy_with_meta(())
243244
}
244245
}
245246

246-
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
247+
impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
247248
fn specialized_decode(&mut self) -> Result<Lazy<[T]>, Self::Error> {
248249
let len = self.read_usize()?;
249250
if len == 0 {
@@ -254,6 +255,14 @@ impl<'a, 'tcx, T> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
254255
}
255256
}
256257

258+
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<Table<T>>> for DecodeContext<'a, 'tcx>
259+
where T: LazyMeta<Meta = ()>,
260+
{
261+
fn specialized_decode(&mut self) -> Result<Lazy<Table<T>>, Self::Error> {
262+
let len = self.read_usize()?;
263+
self.read_lazy_with_meta(len)
264+
}
265+
}
257266

258267
impl<'a, 'tcx> SpecializedDecoder<DefId> for DecodeContext<'a, 'tcx> {
259268
#[inline]

src/librustc_metadata/encoder.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ impl<'tcx> Encoder for EncodeContext<'tcx> {
9898
}
9999
}
100100

101-
impl<'tcx, T> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> {
101+
impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> {
102102
fn specialized_encode(&mut self, lazy: &Lazy<T>) -> Result<(), Self::Error> {
103103
self.emit_lazy_distance(*lazy)
104104
}
105105
}
106106

107-
impl<'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
107+
impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
108108
fn specialized_encode(&mut self, lazy: &Lazy<[T]>) -> Result<(), Self::Error> {
109109
self.emit_usize(lazy.meta)?;
110110
if lazy.meta == 0 {
@@ -114,6 +114,15 @@ impl<'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
114114
}
115115
}
116116

117+
impl<'tcx, T> SpecializedEncoder<Lazy<Table<T>>> for EncodeContext<'tcx>
118+
where T: LazyMeta<Meta = ()>,
119+
{
120+
fn specialized_encode(&mut self, lazy: &Lazy<Table<T>>) -> Result<(), Self::Error> {
121+
self.emit_usize(lazy.meta)?;
122+
self.emit_lazy_distance(*lazy)
123+
}
124+
}
125+
117126
impl<'tcx> SpecializedEncoder<CrateNum> for EncodeContext<'tcx> {
118127
#[inline]
119128
fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> {
@@ -258,7 +267,7 @@ impl<T: Encodable> EncodeContentsForLazy<T> for T {
258267
}
259268
}
260269

261-
impl<I, T> EncodeContentsForLazy<[T]> for I
270+
impl<I, T: Encodable> EncodeContentsForLazy<[T]> for I
262271
where I: IntoIterator,
263272
I::Item: EncodeContentsForLazy<T>,
264273
{

src/librustc_metadata/schema.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_target::spec::{PanicStrategy, TargetTriple};
1414
use rustc_index::vec::IndexVec;
1515
use rustc_data_structures::svh::Svh;
1616

17+
use rustc_serialize::Encodable;
1718
use syntax::{ast, attr};
1819
use syntax::edition::Edition;
1920
use syntax::symbol::Symbol;
@@ -53,7 +54,7 @@ crate trait LazyMeta {
5354
fn min_size(meta: Self::Meta) -> usize;
5455
}
5556

56-
impl<T> LazyMeta for T {
57+
impl<T: Encodable> LazyMeta for T {
5758
type Meta = ();
5859

5960
fn min_size(_: ()) -> usize {
@@ -62,7 +63,7 @@ impl<T> LazyMeta for T {
6263
}
6364
}
6465

65-
impl<T> LazyMeta for [T] {
66+
impl<T: Encodable> LazyMeta for [T] {
6667
type Meta = usize;
6768

6869
fn min_size(len: usize) -> usize {
@@ -118,13 +119,13 @@ impl<T: ?Sized + LazyMeta> Lazy<T> {
118119
}
119120
}
120121

121-
impl<T> Lazy<T> {
122+
impl<T: Encodable> Lazy<T> {
122123
crate fn from_position(position: NonZeroUsize) -> Lazy<T> {
123124
Lazy::from_position_and_meta(position, ())
124125
}
125126
}
126127

127-
impl<T> Lazy<[T]> {
128+
impl<T: Encodable> Lazy<[T]> {
128129
crate fn empty() -> Lazy<[T]> {
129130
Lazy::from_position_and_meta(NonZeroUsize::new(1).unwrap(), 0)
130131
}
@@ -160,6 +161,7 @@ crate enum LazyState {
160161
// manually, instead of relying on the default, to get the correct variance.
161162
// Only needed when `T` itself contains a parameter (e.g. `'tcx`).
162163
macro_rules! Lazy {
164+
(Table<$T:ty>) => {Lazy<Table<$T>, usize>};
163165
([$T:ty]) => {Lazy<[$T], usize>};
164166
($T:ty) => {Lazy<$T, ()>};
165167
}
@@ -194,7 +196,7 @@ crate struct CrateRoot<'tcx> {
194196
pub exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportLevel)]),
195197
pub interpret_alloc_index: Lazy<[u32]>,
196198

197-
pub entries_table: Lazy!([Table<Entry<'tcx>>]),
199+
pub entries_table: Lazy!(Table<Entry<'tcx>>),
198200

199201
/// The DefIndex's of any proc macros delcared by
200202
/// this crate

src/librustc_metadata/table.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::schema::*;
22

33
use rustc::hir::def_id::{DefId, DefIndex};
4-
use rustc_serialize::opaque::Encoder;
4+
use rustc_serialize::{Encodable, opaque::Encoder};
55
use std::convert::TryInto;
66
use std::marker::PhantomData;
77
use std::num::NonZeroUsize;
@@ -73,12 +73,12 @@ impl FixedSizeEncoding for u32 {
7373
/// (e.g. while visiting the definitions of a crate), and on-demand decoding
7474
/// of specific indices (e.g. queries for per-definition data).
7575
/// Similar to `Vec<Lazy<T>>`, but with zero-copy decoding.
76-
crate struct Table<T> {
76+
crate struct Table<T: LazyMeta<Meta = ()>> {
7777
positions: Vec<u8>,
7878
_marker: PhantomData<T>,
7979
}
8080

81-
impl<T> Table<T> {
81+
impl<T: LazyMeta<Meta = ()>> Table<T> {
8282
crate fn new(max_index: usize) -> Self {
8383
Table {
8484
positions: vec![0; max_index * 4],
@@ -105,7 +105,7 @@ impl<T> Table<T> {
105105
position.write_to_bytes_at(positions, array_index)
106106
}
107107

108-
crate fn encode(&self, buf: &mut Encoder) -> Lazy<[Self]> {
108+
crate fn encode(&self, buf: &mut Encoder) -> Lazy<Self> {
109109
let pos = buf.position();
110110
buf.emit_raw_bytes(&self.positions);
111111
Lazy::from_position_and_meta(
@@ -115,7 +115,15 @@ impl<T> Table<T> {
115115
}
116116
}
117117

118-
impl<T> Lazy<[Table<T>]> {
118+
impl<T: LazyMeta<Meta = ()>> LazyMeta for Table<T> {
119+
type Meta = usize;
120+
121+
fn min_size(len: usize) -> usize {
122+
len * 4
123+
}
124+
}
125+
126+
impl<T: Encodable> Lazy<Table<T>> {
119127
/// Given the metadata, extract out the offset of a particular
120128
/// DefIndex (if any).
121129
#[inline(never)]

0 commit comments

Comments
 (0)