about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <edy.burt@gmail.com>2019-11-14 02:44:24 +0200
committerEduard-Mihai Burtescu <edy.burt@gmail.com>2019-11-27 15:15:57 +0200
commitee42979eeb4b13bfb71116f330c735bc48fb38bc (patch)
treeb93dfa4a40dea1b054cbe345108a1ad3e98d954e
parent67421bdfdf278f3737595c27a77d90bacd4215f3 (diff)
downloadrust-ee42979eeb4b13bfb71116f330c735bc48fb38bc.tar.gz
rust-ee42979eeb4b13bfb71116f330c735bc48fb38bc.zip
rustc_metadata: use a separate TableBuilder type to build a Table.
-rw-r--r--src/librustc_metadata/rmeta/encoder.rs46
-rw-r--r--src/librustc_metadata/rmeta/table.rs26
2 files changed, 39 insertions, 33 deletions
diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs
index 5feb54bb8fa..e47047ae83d 100644
--- a/src/librustc_metadata/rmeta/encoder.rs
+++ b/src/librustc_metadata/rmeta/encoder.rs
@@ -1,5 +1,5 @@
 use crate::rmeta::*;
-use crate::rmeta::table::{FixedSizeEncoding, Table};
+use crate::rmeta::table::{FixedSizeEncoding, TableBuilder};
 
 use rustc::middle::cstore::{LinkagePreference, NativeLibrary,
                             EncodedMetadata, ForeignModule};
@@ -47,7 +47,7 @@ struct EncodeContext<'tcx> {
     opaque: opaque::Encoder,
     tcx: TyCtxt<'tcx>,
 
-    per_def: PerDefTables<'tcx>,
+    per_def: PerDefTableBuilders<'tcx>,
 
     lazy_state: LazyState,
     type_shorthands: FxHashMap<Ty<'tcx>, usize>,
@@ -61,27 +61,27 @@ struct EncodeContext<'tcx> {
 }
 
 #[derive(Default)]
-struct PerDefTables<'tcx> {
-    kind: Table<DefIndex, Lazy<EntryKind<'tcx>>>,
-    visibility: Table<DefIndex, Lazy<ty::Visibility>>,
-    span: Table<DefIndex, Lazy<Span>>,
-    attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
-    children: Table<DefIndex, Lazy<[DefIndex]>>,
-    stability: Table<DefIndex, Lazy<attr::Stability>>,
-    deprecation: Table<DefIndex, Lazy<attr::Deprecation>>,
-
-    ty: Table<DefIndex, Lazy<Ty<'tcx>>>,
-    fn_sig: Table<DefIndex, Lazy<ty::PolyFnSig<'tcx>>>,
-    impl_trait_ref: Table<DefIndex, Lazy<ty::TraitRef<'tcx>>>,
-    inherent_impls: Table<DefIndex, Lazy<[DefIndex]>>,
-    variances: Table<DefIndex, Lazy<[ty::Variance]>>,
-    generics: Table<DefIndex, Lazy<ty::Generics>>,
-    explicit_predicates: Table<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
-    inferred_outlives: Table<DefIndex, Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
-    super_predicates: Table<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
-
-    mir: Table<DefIndex, Lazy<mir::Body<'tcx>>>,
-    promoted_mir: Table<DefIndex, Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
+struct PerDefTableBuilders<'tcx> {
+    kind: TableBuilder<DefIndex, Lazy<EntryKind<'tcx>>>,
+    visibility: TableBuilder<DefIndex, Lazy<ty::Visibility>>,
+    span: TableBuilder<DefIndex, Lazy<Span>>,
+    attributes: TableBuilder<DefIndex, Lazy<[ast::Attribute]>>,
+    children: TableBuilder<DefIndex, Lazy<[DefIndex]>>,
+    stability: TableBuilder<DefIndex, Lazy<attr::Stability>>,
+    deprecation: TableBuilder<DefIndex, Lazy<attr::Deprecation>>,
+
+    ty: TableBuilder<DefIndex, Lazy<Ty<'tcx>>>,
+    fn_sig: TableBuilder<DefIndex, Lazy<ty::PolyFnSig<'tcx>>>,
+    impl_trait_ref: TableBuilder<DefIndex, Lazy<ty::TraitRef<'tcx>>>,
+    inherent_impls: TableBuilder<DefIndex, Lazy<[DefIndex]>>,
+    variances: TableBuilder<DefIndex, Lazy<[ty::Variance]>>,
+    generics: TableBuilder<DefIndex, Lazy<ty::Generics>>,
+    explicit_predicates: TableBuilder<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
+    inferred_outlives: TableBuilder<DefIndex, Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
+    super_predicates: TableBuilder<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
+
+    mir: TableBuilder<DefIndex, Lazy<mir::Body<'tcx>>>,
+    promoted_mir: TableBuilder<DefIndex, Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
 }
 
 macro_rules! encoder_methods {
diff --git a/src/librustc_metadata/rmeta/table.rs b/src/librustc_metadata/rmeta/table.rs
index 833d84bfb5d..10122fbba1f 100644
--- a/src/librustc_metadata/rmeta/table.rs
+++ b/src/librustc_metadata/rmeta/table.rs
@@ -117,33 +117,39 @@ impl<T: Encodable> FixedSizeEncoding for Option<Lazy<[T]>> {
     }
 }
 
-/// Random-access table (i.e. offeringconstant-time `get`/`set`), similar to
+/// Random-access table (i.e. offering constant-time `get`/`set`), similar to
 /// `Vec<Option<T>>`, but without requiring encoding or decoding all the values
 /// eagerly and in-order.
 /// A total of `(max_idx + 1) * <Option<T> as FixedSizeEncoding>::BYTE_LEN` bytes
-/// are used for a table, where `max_idx` is the largest index passed to `set`.
-// FIXME(eddyb) replace `Vec` with `[_]` here, such that `Box<Table<T>>` would be used
-// when building it, and `Lazy<Table<T>>` or `&Table<T>` when reading it.
-// (not sure if that is possible given that the `Vec` is being resized now)
+/// are used for a table, where `max_idx` is the largest index passed to
+/// `TableBuilder::set`.
 pub(super) struct Table<I: Idx, T> where Option<T>: FixedSizeEncoding {
+    _marker: PhantomData<(fn(&I), T)>,
+    // NOTE(eddyb) this makes `Table` not implement `Sized`, but no
+    // value of `Table` is ever created (it's always behind `Lazy`).
+    _bytes: [u8],
+}
+
+/// Helper for constructing a table's serialization (also see `Table`).
+pub(super) struct TableBuilder<I: Idx, T> where Option<T>: FixedSizeEncoding {
     // FIXME(eddyb) use `IndexVec<I, [u8; <Option<T>>::BYTE_LEN]>` instead of
     // `Vec<u8>`, once that starts working (i.e. lazy normalization).
-    // Then again, that has the downside of not allowing `Table::encode` to
+    // Then again, that has the downside of not allowing `TableBuilder::encode` to
     // obtain a `&[u8]` entirely in safe code, for writing the bytes out.
     bytes: Vec<u8>,
     _marker: PhantomData<(fn(&I), T)>,
 }
 
-impl<I: Idx, T> Default for Table<I, T> where Option<T>: FixedSizeEncoding {
+impl<I: Idx, T> Default for TableBuilder<I, T> where Option<T>: FixedSizeEncoding {
     fn default() -> Self {
-        Table {
+        TableBuilder {
             bytes: vec![],
             _marker: PhantomData,
         }
     }
 }
 
-impl<I: Idx, T> Table<I, T> where Option<T>: FixedSizeEncoding {
+impl<I: Idx, T> TableBuilder<I, T> where Option<T>: FixedSizeEncoding {
     pub(super) fn set(&mut self, i: I, value: T) {
         // FIXME(eddyb) investigate more compact encodings for sparse tables.
         // On the PR @michaelwoerister mentioned:
@@ -159,7 +165,7 @@ impl<I: Idx, T> Table<I, T> where Option<T>: FixedSizeEncoding {
         Some(value).write_to_bytes_at(&mut self.bytes, i);
     }
 
-    pub(super) fn encode(&self, buf: &mut Encoder) -> Lazy<Self> {
+    pub(super) fn encode(&self, buf: &mut Encoder) -> Lazy<Table<I, T>> {
         let pos = buf.position();
         buf.emit_raw_bytes(&self.bytes);
         Lazy::from_position_and_meta(