Skip to content

Commit 6b76932

Browse files
committed
introduce Deref/DerefMut to model subtype rel
The idea is that ItemContentBuilder is a base-type of IndexBuilder.
1 parent b2c7922 commit 6b76932

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

src/librustc_metadata/encoder.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use rustc::hir::intravisit::Visitor;
5454
use rustc::hir::intravisit;
5555
use rustc::hir::map::DefKey;
5656

57-
use super::index_builder::{IndexBuilder, XRef};
57+
use super::index_builder::{IndexBuilder, ItemContentBuilder, XRef};
5858

5959
pub struct EncodeContext<'a, 'tcx: 'a> {
6060
pub diag: &'a Handler,
@@ -132,7 +132,7 @@ fn encode_item_variances(rbml_w: &mut Encoder,
132132
rbml_w.end_tag();
133133
}
134134

135-
impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
135+
impl<'a, 'tcx> ItemContentBuilder<'a, 'tcx> {
136136
fn encode_bounds_and_type_for_item(&mut self,
137137
rbml_w: &mut Encoder,
138138
id: NodeId) {
@@ -164,7 +164,7 @@ fn write_closure_type<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
164164
rbml_w.mark_stable_position();
165165
}
166166

167-
impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
167+
impl<'a, 'tcx> ItemContentBuilder<'a, 'tcx> {
168168
fn encode_type(&mut self,
169169
rbml_w: &mut Encoder,
170170
typ: Ty<'tcx>) {
@@ -200,7 +200,9 @@ impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
200200
rbml_w.end_tag();
201201
}
202202
}
203+
}
203204

205+
impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
204206
fn encode_enum_variant_info(&mut self,
205207
rbml_w: &mut Encoder,
206208
did: DefId,
@@ -302,7 +304,7 @@ fn encode_reexports(ecx: &EncodeContext,
302304
}
303305
}
304306

305-
impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
307+
impl<'a, 'tcx> ItemContentBuilder<'a, 'tcx> {
306308
fn encode_info_for_mod(&mut self,
307309
rbml_w: &mut Encoder,
308310
md: &hir::Mod,
@@ -487,7 +489,9 @@ impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
487489

488490
rbml_w.end_tag();
489491
}
492+
}
490493

494+
impl<'a, 'tcx> ItemContentBuilder<'a, 'tcx> {
491495
fn encode_generics(&mut self,
492496
rbml_w: &mut Encoder,
493497
generics: &ty::Generics<'tcx>,
@@ -532,7 +536,9 @@ impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
532536
_ => encode_family(rbml_w, METHOD_FAMILY)
533537
}
534538
}
539+
}
535540

541+
impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
536542
fn encode_info_for_associated_const(&mut self,
537543
rbml_w: &mut Encoder,
538544
associated_const: &ty::AssociatedConst,
@@ -680,7 +686,9 @@ impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
680686
}
681687
rbml_w.end_tag();
682688
}
689+
}
683690

691+
impl<'a, 'tcx> ItemContentBuilder<'a, 'tcx> {
684692
fn encode_repr_attrs(&mut self,
685693
rbml_w: &mut Encoder,
686694
attrs: &[ast::Attribute]) {
@@ -1237,9 +1245,7 @@ impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
12371245
}
12381246
}
12391247
}
1240-
}
12411248

1242-
impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
12431249
fn encode_info_for_foreign_item(&mut self,
12441250
rbml_w: &mut Encoder,
12451251
nitem: &hir::ForeignItem) {

src/librustc_metadata/index_builder.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,20 @@ use rustc::dep_graph::{DepNode, DepTask};
1515
use rustc::hir::def_id::DefId;
1616
use rustc::ty;
1717
use rustc_data_structures::fnv::FnvHashMap;
18+
use std::ops::{Deref, DerefMut};
1819

20+
/// Builder that can encode new items, adding them into the index.
21+
/// Item encoding cannot be nested.
1922
pub struct IndexBuilder<'a, 'tcx: 'a> {
20-
ecx: &'a EncodeContext<'a, 'tcx>,
2123
items: IndexData,
24+
builder: ItemContentBuilder<'a, 'tcx>,
25+
}
26+
27+
/// Builder that can encode the content of items, but can't start a
28+
/// new item itself. Most code is attached to here.
29+
pub struct ItemContentBuilder<'a, 'tcx: 'a> {
2230
xrefs: FnvHashMap<XRef<'tcx>, u32>, // sequentially-assigned
31+
ecx: &'a EncodeContext<'a, 'tcx>,
2332
}
2433

2534
/// "interned" entries referenced by id
@@ -29,16 +38,14 @@ pub enum XRef<'tcx> { Predicate(ty::Predicate<'tcx>) }
2938
impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
3039
pub fn new(ecx: &'a EncodeContext<'a, 'tcx>) -> Self {
3140
IndexBuilder {
32-
ecx: ecx,
3341
items: IndexData::new(ecx.tcx.map.num_local_def_ids()),
34-
xrefs: FnvHashMap()
42+
builder: ItemContentBuilder {
43+
ecx: ecx,
44+
xrefs: FnvHashMap(),
45+
},
3546
}
3647
}
3748

38-
pub fn ecx(&self) -> &'a EncodeContext<'a, 'tcx> {
39-
self.ecx
40-
}
41-
4249
/// Records that `id` is being emitted at the current offset.
4350
/// This data is later used to construct the item index in the
4451
/// metadata so we can quickly find the data for a given item.
@@ -51,13 +58,32 @@ impl<'a, 'tcx> IndexBuilder<'a, 'tcx> {
5158
self.ecx.tcx.dep_graph.in_task(DepNode::MetaData(id))
5259
}
5360

54-
pub fn add_xref(&mut self, xref: XRef<'tcx>) -> u32 {
55-
let old_len = self.xrefs.len() as u32;
56-
*self.xrefs.entry(xref).or_insert(old_len)
61+
pub fn into_fields(self) -> (IndexData, FnvHashMap<XRef<'tcx>, u32>) {
62+
(self.items, self.builder.xrefs)
5763
}
64+
}
5865

59-
pub fn into_fields(self) -> (IndexData, FnvHashMap<XRef<'tcx>, u32>) {
60-
(self.items, self.xrefs)
66+
impl<'a, 'tcx> Deref for IndexBuilder<'a, 'tcx> {
67+
type Target = ItemContentBuilder<'a, 'tcx>;
68+
69+
fn deref(&self) -> &Self::Target {
70+
&self.builder
71+
}
72+
}
73+
74+
impl<'a, 'tcx> DerefMut for IndexBuilder<'a, 'tcx> {
75+
fn deref_mut(&mut self) -> &mut Self::Target {
76+
&mut self.builder
6177
}
6278
}
6379

80+
impl<'a, 'tcx> ItemContentBuilder<'a, 'tcx> {
81+
pub fn ecx(&self) -> &'a EncodeContext<'a, 'tcx> {
82+
self.ecx
83+
}
84+
85+
pub fn add_xref(&mut self, xref: XRef<'tcx>) -> u32 {
86+
let old_len = self.xrefs.len() as u32;
87+
*self.xrefs.entry(xref).or_insert(old_len)
88+
}
89+
}

0 commit comments

Comments
 (0)