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