]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / compiler / rustc_metadata / src / rmeta / def_path_hash_map.rs
CommitLineData
c295e0f8
XL
1use crate::rmeta::DecodeContext;
2use crate::rmeta::EncodeContext;
353b0b11 3use rustc_data_structures::owned_slice::OwnedSlice;
c295e0f8 4use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap};
923072b8
FG
5use rustc_middle::parameterized_over_tcx;
6use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
c295e0f8
XL
7use rustc_span::def_id::{DefIndex, DefPathHash};
8
923072b8 9pub(crate) enum DefPathHashMapRef<'tcx> {
353b0b11 10 OwnedFromMetadata(odht::HashTable<HashMapConfig, OwnedSlice>),
c295e0f8
XL
11 BorrowedFromTcx(&'tcx DefPathHashMap),
12}
13
923072b8
FG
14parameterized_over_tcx! {
15 DefPathHashMapRef,
16}
17
a2a8927a 18impl DefPathHashMapRef<'_> {
c295e0f8
XL
19 #[inline]
20 pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
21 match *self {
22 DefPathHashMapRef::OwnedFromMetadata(ref map) => map.get(def_path_hash).unwrap(),
23 DefPathHashMapRef::BorrowedFromTcx(_) => {
24 panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization")
25 }
26 }
27 }
28}
29
30impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for DefPathHashMapRef<'tcx> {
923072b8 31 fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
c295e0f8
XL
32 match *self {
33 DefPathHashMapRef::BorrowedFromTcx(def_path_hash_map) => {
34 let bytes = def_path_hash_map.raw_bytes();
923072b8
FG
35 e.emit_usize(bytes.len());
36 e.emit_raw_bytes(bytes);
c295e0f8
XL
37 }
38 DefPathHashMapRef::OwnedFromMetadata(_) => {
39 panic!("DefPathHashMap::OwnedFromMetadata variant only exists for deserialization")
40 }
41 }
42 }
43}
44
45impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefPathHashMapRef<'static> {
5099ac24 46 fn decode(d: &mut DecodeContext<'a, 'tcx>) -> DefPathHashMapRef<'static> {
5099ac24 47 let len = d.read_usize();
c295e0f8 48 let pos = d.position();
49aad941 49 let o = d.blob().clone().0.slice(|blob| &blob[pos..pos + len]);
c295e0f8 50
353b0b11
FG
51 // Although we already have the data we need via the `OwnedSlice`, we still need
52 // to advance the `DecodeContext`'s position so it's in a valid state after
53 // the method. We use `read_raw_bytes()` for that.
c295e0f8
XL
54 let _ = d.read_raw_bytes(len);
55
5099ac24 56 let inner = odht::HashTable::from_raw_bytes(o).unwrap_or_else(|e| {
9c376795 57 panic!("decode error: {e}");
5099ac24
FG
58 });
59 DefPathHashMapRef::OwnedFromMetadata(inner)
c295e0f8
XL
60 }
61}