• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::Interner;
2 
3 use rustc_data_structures::fx::FxHashMap;
4 use rustc_serialize::{Decoder, Encoder};
5 
6 /// The shorthand encoding uses an enum's variant index `usize`
7 /// and is offset by this value so it never matches a real variant.
8 /// This offset is also chosen so that the first byte is never < 0x80.
9 pub const SHORTHAND_OFFSET: usize = 0x80;
10 
11 /// Trait for decoding to a reference.
12 ///
13 /// This is a separate trait from `Decodable` so that we can implement it for
14 /// upstream types, such as `FxHashSet`.
15 ///
16 /// The `TyDecodable` derive macro will use this trait for fields that are
17 /// references (and don't use a type alias to hide that).
18 ///
19 /// `Decodable` can still be implemented in cases where `Decodable` is required
20 /// by a trait bound.
21 pub trait RefDecodable<'tcx, D: TyDecoder> {
decode(d: &mut D) -> &'tcx Self22     fn decode(d: &mut D) -> &'tcx Self;
23 }
24 
25 pub trait TyEncoder: Encoder {
26     type I: Interner;
27     const CLEAR_CROSS_CRATE: bool;
28 
position(&self) -> usize29     fn position(&self) -> usize;
30 
type_shorthands(&mut self) -> &mut FxHashMap<<Self::I as Interner>::Ty, usize>31     fn type_shorthands(&mut self) -> &mut FxHashMap<<Self::I as Interner>::Ty, usize>;
32 
predicate_shorthands( &mut self, ) -> &mut FxHashMap<<Self::I as Interner>::PredicateKind, usize>33     fn predicate_shorthands(
34         &mut self,
35     ) -> &mut FxHashMap<<Self::I as Interner>::PredicateKind, usize>;
36 
encode_alloc_id(&mut self, alloc_id: &<Self::I as Interner>::AllocId)37     fn encode_alloc_id(&mut self, alloc_id: &<Self::I as Interner>::AllocId);
38 }
39 
40 pub trait TyDecoder: Decoder {
41     type I: Interner;
42     const CLEAR_CROSS_CRATE: bool;
43 
interner(&self) -> Self::I44     fn interner(&self) -> Self::I;
45 
cached_ty_for_shorthand<F>( &mut self, shorthand: usize, or_insert_with: F, ) -> <Self::I as Interner>::Ty where F: FnOnce(&mut Self) -> <Self::I as Interner>::Ty46     fn cached_ty_for_shorthand<F>(
47         &mut self,
48         shorthand: usize,
49         or_insert_with: F,
50     ) -> <Self::I as Interner>::Ty
51     where
52         F: FnOnce(&mut Self) -> <Self::I as Interner>::Ty;
53 
with_position<F, R>(&mut self, pos: usize, f: F) -> R where F: FnOnce(&mut Self) -> R54     fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
55     where
56         F: FnOnce(&mut Self) -> R;
57 
positioned_at_shorthand(&self) -> bool58     fn positioned_at_shorthand(&self) -> bool {
59         (self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0
60     }
61 
decode_alloc_id(&mut self) -> <Self::I as Interner>::AllocId62     fn decode_alloc_id(&mut self) -> <Self::I as Interner>::AllocId;
63 }
64