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