Skip to content

Commit 1b88007

Browse files
committed
rustc_metadata: Split fn get_implementations_for_trait into two functions
1 parent 4e8855b commit 1b88007

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -1365,39 +1365,39 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13651365
self.root.traits.decode(self).map(|index| self.local_def_id(index))
13661366
}
13671367

1368-
fn get_implementations_for_trait(
1368+
fn get_trait_impls(&'a self) -> impl Iterator<Item = (DefId, Option<SimplifiedType>)> + 'a {
1369+
self.trait_impls.values().flat_map(move |impls| {
1370+
impls
1371+
.decode(self)
1372+
.map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty))
1373+
})
1374+
}
1375+
1376+
fn get_implementations_of_trait(
13691377
&self,
13701378
tcx: TyCtxt<'tcx>,
1371-
filter: Option<DefId>,
1379+
trait_def_id: DefId,
13721380
) -> &'tcx [(DefId, Option<SimplifiedType>)] {
13731381
if self.root.is_proc_macro_crate() {
13741382
// proc-macro crates export no trait impls.
13751383
return &[];
13761384
}
13771385

1378-
if let Some(def_id) = filter {
1379-
// Do a reverse lookup beforehand to avoid touching the crate_num
1380-
// hash map in the loop below.
1381-
let filter = match self.reverse_translate_def_id(def_id) {
1382-
Some(def_id) => (def_id.krate.as_u32(), def_id.index),
1383-
None => return &[],
1384-
};
1386+
// Do a reverse lookup beforehand to avoid touching the crate_num
1387+
// hash map in the loop below.
1388+
let key = match self.reverse_translate_def_id(trait_def_id) {
1389+
Some(def_id) => (def_id.krate.as_u32(), def_id.index),
1390+
None => return &[],
1391+
};
13851392

1386-
if let Some(impls) = self.trait_impls.get(&filter) {
1387-
tcx.arena.alloc_from_iter(
1388-
impls.decode(self).map(|(idx, simplified_self_ty)| {
1389-
(self.local_def_id(idx), simplified_self_ty)
1390-
}),
1391-
)
1392-
} else {
1393-
&[]
1394-
}
1395-
} else {
1396-
tcx.arena.alloc_from_iter(self.trait_impls.values().flat_map(|impls| {
1393+
if let Some(impls) = self.trait_impls.get(&key) {
1394+
tcx.arena.alloc_from_iter(
13971395
impls
13981396
.decode(self)
1399-
.map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty))
1400-
}))
1397+
.map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty)),
1398+
)
1399+
} else {
1400+
&[]
14011401
}
14021402
}
14031403

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
192192
extra_filename => { cdata.root.extra_filename.clone() }
193193

194194
traits_in_crate => { tcx.arena.alloc_from_iter(cdata.get_traits()) }
195+
all_trait_implementations => { tcx.arena.alloc_from_iter(cdata.get_trait_impls()) }
195196

196-
implementations_of_trait => {
197-
cdata.get_implementations_for_trait(tcx, Some(other))
198-
}
199-
200-
all_trait_implementations => {
201-
cdata.get_implementations_for_trait(tcx, None)
202-
}
197+
implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) }
203198

204199
visibility => { cdata.get_visibility(def_id.index) }
205200
dep_kind => {

0 commit comments

Comments
 (0)