rustc_metadata/rmeta/
def_path_hash_map.rs
1use rustc_data_structures::owned_slice::OwnedSlice;
2use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap};
3use rustc_middle::parameterized_over_tcx;
4use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
5use rustc_span::def_id::{DefIndex, DefPathHash};
6
7use crate::rmeta::{DecodeContext, EncodeContext};
8
9pub(crate) enum DefPathHashMapRef<'tcx> {
10 OwnedFromMetadata(odht::HashTable<HashMapConfig, OwnedSlice>),
11 BorrowedFromTcx(&'tcx DefPathHashMap),
12}
13
14parameterized_over_tcx! {
15 DefPathHashMapRef,
16}
17
18impl DefPathHashMapRef<'_> {
19 #[inline]
20 pub(crate) fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
21 match *self {
22 DefPathHashMapRef::OwnedFromMetadata(ref map) => {
23 map.get(&def_path_hash.local_hash()).unwrap()
24 }
25 DefPathHashMapRef::BorrowedFromTcx(_) => {
26 panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization")
27 }
28 }
29 }
30}
31
32impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for DefPathHashMapRef<'tcx> {
33 fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
34 match *self {
35 DefPathHashMapRef::BorrowedFromTcx(def_path_hash_map) => {
36 let bytes = def_path_hash_map.raw_bytes();
37 e.emit_usize(bytes.len());
38 e.emit_raw_bytes(bytes);
39 }
40 DefPathHashMapRef::OwnedFromMetadata(_) => {
41 panic!("DefPathHashMap::OwnedFromMetadata variant only exists for deserialization")
42 }
43 }
44 }
45}
46
47impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefPathHashMapRef<'static> {
48 fn decode(d: &mut DecodeContext<'a, 'tcx>) -> DefPathHashMapRef<'static> {
49 let len = d.read_usize();
50 let pos = d.position();
51 let o = d.blob().bytes().clone().slice(|blob| &blob[pos..pos + len]);
52
53 let _ = d.read_raw_bytes(len);
57
58 let inner = odht::HashTable::from_raw_bytes(o).unwrap_or_else(|e| {
59 panic!("decode error: {e}");
60 });
61 DefPathHashMapRef::OwnedFromMetadata(inner)
62 }
63}