Skip to content

Commit 538f882

Browse files
committed
rustc_metadata: replace Lazy<[Table<T>]> with Lazy<Table<T>>.
1 parent 2212aa4 commit 538f882

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
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, NativeLibrary, ForeignModule};
44
use crate::schema::*;
5+
use crate::table::Table;
56

67
use rustc_data_structures::sync::{Lrc, ReadGuard};
78
use rustc::hir::map::{DefKey, DefPath, DefPathData, DefPathHash, Definitions};
@@ -27,7 +28,7 @@ use std::mem;
2728
use std::num::NonZeroUsize;
2829
use std::u32;
2930

30-
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
31+
use rustc_serialize::{Decodable, Decoder, Encodable, SpecializedDecoder, opaque};
3132
use syntax::attr;
3233
use syntax::ast::{self, Ident};
3334
use syntax::source_map;
@@ -128,15 +129,15 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'a, 'tcx, 'tcx>
128129
}
129130
}
130131

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

139-
impl<'a, 'tcx: 'a, T: Decodable> Lazy<[T]> {
140+
impl<'a, 'tcx: 'a, T: Encodable + Decodable> Lazy<[T]> {
140141
pub fn decode<M: Metadata<'a, 'tcx>>(
141142
self,
142143
meta: M,
@@ -236,13 +237,13 @@ impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {
236237
}
237238
}
238239

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

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

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

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

src/librustc_metadata/encoder.rs

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

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

105-
impl<'a, 'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'a, 'tcx> {
105+
impl<'a, 'tcx, T: Encodable> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'a, 'tcx> {
106106
fn specialized_encode(&mut self, lazy: &Lazy<[T]>) -> Result<(), Self::Error> {
107107
self.emit_usize(lazy.meta)?;
108108
if lazy.meta == 0 {
@@ -112,6 +112,15 @@ impl<'a, 'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'a, 'tcx> {
112112
}
113113
}
114114

115+
impl<'a, 'tcx, T> SpecializedEncoder<Lazy<Table<T>>> for EncodeContext<'a, 'tcx>
116+
where T: LazyMeta<Meta = ()>,
117+
{
118+
fn specialized_encode(&mut self, lazy: &Lazy<Table<T>>) -> Result<(), Self::Error> {
119+
self.emit_usize(lazy.meta)?;
120+
self.emit_lazy_distance(*lazy)
121+
}
122+
}
123+
115124
impl<'a, 'tcx> SpecializedEncoder<CrateNum> for EncodeContext<'a, 'tcx> {
116125
#[inline]
117126
fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> {
@@ -250,7 +259,7 @@ impl<T: Encodable> EncodeContentsForLazy<T> for T {
250259
}
251260
}
252261

253-
impl<I, T> EncodeContentsForLazy<[T]> for I
262+
impl<I, T: Encodable> EncodeContentsForLazy<[T]> for I
254263
where I: IntoIterator,
255264
I::Item: EncodeContentsForLazy<T>,
256265
{

src/librustc_metadata/schema.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc::ty::{self, Ty, ReprOptions};
1212
use rustc_target::spec::{PanicStrategy, TargetTriple};
1313
use rustc_data_structures::svh::Svh;
1414

15-
use rustc_serialize as serialize;
15+
use rustc_serialize::{self as serialize, Encodable};
1616
use syntax::{ast, attr};
1717
use syntax::edition::Edition;
1818
use syntax::symbol::Symbol;
@@ -52,7 +52,7 @@ pub trait LazyMeta {
5252
fn min_size(meta: Self::Meta) -> usize;
5353
}
5454

55-
impl<T> LazyMeta for T {
55+
impl<T: Encodable> LazyMeta for T {
5656
type Meta = ();
5757

5858
fn min_size(_: ()) -> usize {
@@ -61,7 +61,7 @@ impl<T> LazyMeta for T {
6161
}
6262
}
6363

64-
impl<T> LazyMeta for [T] {
64+
impl<T: Encodable> LazyMeta for [T] {
6565
type Meta = usize;
6666

6767
fn min_size(len: usize) -> usize {
@@ -117,13 +117,13 @@ impl<T: ?Sized + LazyMeta> Lazy<T> {
117117
}
118118
}
119119

120-
impl<T> Lazy<T> {
120+
impl<T: Encodable> Lazy<T> {
121121
pub fn from_position(position: NonZeroUsize) -> Lazy<T> {
122122
Lazy::from_position_and_meta(position, ())
123123
}
124124
}
125125

126-
impl<T> Lazy<[T]> {
126+
impl<T: Encodable> Lazy<[T]> {
127127
pub fn empty() -> Lazy<[T]> {
128128
Lazy::from_position_and_meta(NonZeroUsize::new(1).unwrap(), 0)
129129
}
@@ -159,6 +159,7 @@ pub enum LazyState {
159159
// manually, instead of relying on the default, to get the correct variance.
160160
// Only needed when `T` itself contains a parameter (e.g. `'tcx`).
161161
macro_rules! Lazy {
162+
(Table<$T:ty>) => {Lazy<Table<$T>, usize>};
162163
([$T:ty]) => {Lazy<[$T], usize>};
163164
($T:ty) => {Lazy<$T, ()>};
164165
}
@@ -192,7 +193,7 @@ pub struct CrateRoot<'tcx> {
192193
pub exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportLevel)]),
193194
pub interpret_alloc_index: Lazy<[u32]>,
194195

195-
pub entries_table: Lazy!([Table<Entry<'tcx>>]),
196+
pub entries_table: Lazy!(Table<Entry<'tcx>>),
196197

197198
pub compiler_builtins: bool,
198199
pub needs_allocator: bool,

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, DefIndexAddressSpace};
4-
use rustc_serialize::opaque::Encoder;
4+
use rustc_serialize::{Encodable, opaque::Encoder};
55
use std::marker::PhantomData;
66
use std::num::NonZeroUsize;
77
use log::debug;
@@ -72,12 +72,12 @@ impl FixedSizeEncoding for u32 {
7272
/// (e.g. while visiting the definitions of a crate), and on-demand decoding
7373
/// of specific indices (e.g. queries for per-definition data).
7474
/// Similar to `Vec<Lazy<T>>`, but with zero-copy decoding.
75-
pub struct Table<T> {
75+
pub struct Table<T: LazyMeta<Meta = ()>> {
7676
positions: [Vec<u8>; 2],
7777
_marker: PhantomData<T>,
7878
}
7979

80-
impl<T> Table<T> {
80+
impl<T: LazyMeta<Meta = ()>> Table<T> {
8181
pub fn new((max_index_lo, max_index_hi): (usize, usize)) -> Self {
8282
Table {
8383
positions: [vec![0; max_index_lo * 4],
@@ -107,7 +107,7 @@ impl<T> Table<T> {
107107
position.write_to_bytes_at(positions, array_index)
108108
}
109109

110-
pub fn encode(&self, buf: &mut Encoder) -> Lazy<[Self]> {
110+
pub fn encode(&self, buf: &mut Encoder) -> Lazy<Self> {
111111
let pos = buf.position();
112112

113113
// First we write the length of the lower range ...
@@ -123,7 +123,15 @@ impl<T> Table<T> {
123123
}
124124
}
125125

126-
impl<T> Lazy<[Table<T>]> {
126+
impl<T: LazyMeta<Meta = ()>> LazyMeta for Table<T> {
127+
type Meta = usize;
128+
129+
fn min_size(len: usize) -> usize {
130+
len * 4
131+
}
132+
}
133+
134+
impl<T: Encodable> Lazy<Table<T>> {
127135
/// Given the metadata, extract out the offset of a particular
128136
/// DefIndex (if any).
129137
#[inline(never)]

0 commit comments

Comments
 (0)