diff options
| author | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2019-04-11 23:36:00 +0300 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2019-10-15 18:23:51 +0300 |
| commit | dd1264e90a47d9818d868276cd09363fe78056c4 (patch) | |
| tree | 2ad33940cbfb198197697283718b3e98a77766e1 | |
| parent | ffd18fc22c7dcfd59bb09567217046f6e0b71e7f (diff) | |
| download | rust-dd1264e90a47d9818d868276cd09363fe78056c4.tar.gz rust-dd1264e90a47d9818d868276cd09363fe78056c4.zip | |
rustc_metadata: replace Lazy<[Table<T>]> with Lazy<Table<T>>.
| -rw-r--r-- | src/librustc_metadata/decoder.rs | 19 | ||||
| -rw-r--r-- | src/librustc_metadata/encoder.rs | 15 | ||||
| -rw-r--r-- | src/librustc_metadata/schema.rs | 12 | ||||
| -rw-r--r-- | src/librustc_metadata/table.rs | 18 |
4 files changed, 46 insertions, 18 deletions
diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index 684dfd69d2d..c2ec9115d31 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -2,6 +2,7 @@ use crate::cstore::{self, CrateMetadata, MetadataBlob}; use crate::schema::*; +use crate::table::Table; use rustc_index::vec::IndexVec; use rustc_data_structures::sync::{Lrc, ReadGuard}; @@ -28,7 +29,7 @@ use std::mem; use std::num::NonZeroUsize; use std::u32; -use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque}; +use rustc_serialize::{Decodable, Decoder, Encodable, SpecializedDecoder, opaque}; use syntax::attr; use syntax::ast::{self, Ident}; use syntax::source_map::{self, respan, Spanned}; @@ -130,7 +131,7 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'tcx>) { } } -impl<'a, 'tcx, T: Decodable> Lazy<T> { +impl<'a, 'tcx, T: Encodable + Decodable> Lazy<T> { crate fn decode<M: Metadata<'a, 'tcx>>(self, meta: M) -> T { let mut dcx = meta.decoder(self.position.get()); dcx.lazy_state = LazyState::NodeStart(self.position); @@ -138,7 +139,7 @@ impl<'a, 'tcx, T: Decodable> Lazy<T> { } } -impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable> Lazy<[T]> { +impl<'a: 'x, 'tcx: 'x, 'x, T: Encodable + Decodable> Lazy<[T]> { crate fn decode<M: Metadata<'a, 'tcx>>( self, meta: M, @@ -237,13 +238,13 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> { } } -impl<'a, 'tcx, T> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> { +impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> { fn specialized_decode(&mut self) -> Result<Lazy<T>, Self::Error> { self.read_lazy_with_meta(()) } } -impl<'a, 'tcx, T> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> { +impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> { fn specialized_decode(&mut self) -> Result<Lazy<[T]>, Self::Error> { let len = self.read_usize()?; if len == 0 { @@ -254,6 +255,14 @@ impl<'a, 'tcx, T> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> { } } +impl<'a, 'tcx, T> SpecializedDecoder<Lazy<Table<T>>> for DecodeContext<'a, 'tcx> + where T: LazyMeta<Meta = ()>, +{ + fn specialized_decode(&mut self) -> Result<Lazy<Table<T>>, Self::Error> { + let len = self.read_usize()?; + self.read_lazy_with_meta(len) + } +} impl<'a, 'tcx> SpecializedDecoder<DefId> for DecodeContext<'a, 'tcx> { #[inline] diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index ef11ebdae43..53d3039a284 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -98,13 +98,13 @@ impl<'tcx> Encoder for EncodeContext<'tcx> { } } -impl<'tcx, T> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> { +impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> { fn specialized_encode(&mut self, lazy: &Lazy<T>) -> Result<(), Self::Error> { self.emit_lazy_distance(*lazy) } } -impl<'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> { +impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> { fn specialized_encode(&mut self, lazy: &Lazy<[T]>) -> Result<(), Self::Error> { self.emit_usize(lazy.meta)?; if lazy.meta == 0 { @@ -114,6 +114,15 @@ impl<'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> { } } +impl<'tcx, T> SpecializedEncoder<Lazy<Table<T>>> for EncodeContext<'tcx> + where T: LazyMeta<Meta = ()>, +{ + fn specialized_encode(&mut self, lazy: &Lazy<Table<T>>) -> Result<(), Self::Error> { + self.emit_usize(lazy.meta)?; + self.emit_lazy_distance(*lazy) + } +} + impl<'tcx> SpecializedEncoder<CrateNum> for EncodeContext<'tcx> { #[inline] fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> { @@ -258,7 +267,7 @@ impl<T: Encodable> EncodeContentsForLazy<T> for T { } } -impl<I, T> EncodeContentsForLazy<[T]> for I +impl<I, T: Encodable> EncodeContentsForLazy<[T]> for I where I: IntoIterator, I::Item: EncodeContentsForLazy<T>, { diff --git a/src/librustc_metadata/schema.rs b/src/librustc_metadata/schema.rs index a1b097225b7..6ae20a9d438 100644 --- a/src/librustc_metadata/schema.rs +++ b/src/librustc_metadata/schema.rs @@ -14,6 +14,7 @@ use rustc_target::spec::{PanicStrategy, TargetTriple}; use rustc_index::vec::IndexVec; use rustc_data_structures::svh::Svh; +use rustc_serialize::Encodable; use syntax::{ast, attr}; use syntax::edition::Edition; use syntax::symbol::Symbol; @@ -53,7 +54,7 @@ crate trait LazyMeta { fn min_size(meta: Self::Meta) -> usize; } -impl<T> LazyMeta for T { +impl<T: Encodable> LazyMeta for T { type Meta = (); fn min_size(_: ()) -> usize { @@ -62,7 +63,7 @@ impl<T> LazyMeta for T { } } -impl<T> LazyMeta for [T] { +impl<T: Encodable> LazyMeta for [T] { type Meta = usize; fn min_size(len: usize) -> usize { @@ -118,13 +119,13 @@ impl<T: ?Sized + LazyMeta> Lazy<T> { } } -impl<T> Lazy<T> { +impl<T: Encodable> Lazy<T> { crate fn from_position(position: NonZeroUsize) -> Lazy<T> { Lazy::from_position_and_meta(position, ()) } } -impl<T> Lazy<[T]> { +impl<T: Encodable> Lazy<[T]> { crate fn empty() -> Lazy<[T]> { Lazy::from_position_and_meta(NonZeroUsize::new(1).unwrap(), 0) } @@ -160,6 +161,7 @@ crate enum LazyState { // manually, instead of relying on the default, to get the correct variance. // Only needed when `T` itself contains a parameter (e.g. `'tcx`). macro_rules! Lazy { + (Table<$T:ty>) => {Lazy<Table<$T>, usize>}; ([$T:ty]) => {Lazy<[$T], usize>}; ($T:ty) => {Lazy<$T, ()>}; } @@ -194,7 +196,7 @@ crate struct CrateRoot<'tcx> { pub exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportLevel)]), pub interpret_alloc_index: Lazy<[u32]>, - pub entries_table: Lazy!([Table<Entry<'tcx>>]), + pub entries_table: Lazy!(Table<Entry<'tcx>>), /// The DefIndex's of any proc macros delcared by /// this crate diff --git a/src/librustc_metadata/table.rs b/src/librustc_metadata/table.rs index 94966a44edd..e18535060e6 100644 --- a/src/librustc_metadata/table.rs +++ b/src/librustc_metadata/table.rs @@ -1,7 +1,7 @@ use crate::schema::*; use rustc::hir::def_id::{DefId, DefIndex}; -use rustc_serialize::opaque::Encoder; +use rustc_serialize::{Encodable, opaque::Encoder}; use std::convert::TryInto; use std::marker::PhantomData; use std::num::NonZeroUsize; @@ -73,12 +73,12 @@ impl FixedSizeEncoding for u32 { /// (e.g. while visiting the definitions of a crate), and on-demand decoding /// of specific indices (e.g. queries for per-definition data). /// Similar to `Vec<Lazy<T>>`, but with zero-copy decoding. -crate struct Table<T> { +crate struct Table<T: LazyMeta<Meta = ()>> { positions: Vec<u8>, _marker: PhantomData<T>, } -impl<T> Table<T> { +impl<T: LazyMeta<Meta = ()>> Table<T> { crate fn new(max_index: usize) -> Self { Table { positions: vec![0; max_index * 4], @@ -105,7 +105,7 @@ impl<T> Table<T> { position.write_to_bytes_at(positions, array_index) } - crate fn encode(&self, buf: &mut Encoder) -> Lazy<[Self]> { + crate fn encode(&self, buf: &mut Encoder) -> Lazy<Self> { let pos = buf.position(); buf.emit_raw_bytes(&self.positions); Lazy::from_position_and_meta( @@ -115,7 +115,15 @@ impl<T> Table<T> { } } -impl<T> Lazy<[Table<T>]> { +impl<T: LazyMeta<Meta = ()>> LazyMeta for Table<T> { + type Meta = usize; + + fn min_size(len: usize) -> usize { + len * 4 + } +} + +impl<T: Encodable> Lazy<Table<T>> { /// Given the metadata, extract out the offset of a particular /// DefIndex (if any). #[inline(never)] |
