1 //! This module contains some shared code for encoding and decoding various
2 //! things from the `ty` module, and in particular implements support for
3 //! "shorthands" which allow to have pointers back into the already encoded
4 //! stream instead of re-encoding the same thing twice.
5 //!
6 //! The functionality in here is shared between persisting to crate metadata and
7 //! persisting to incr. comp. caches.
8
9 use crate::arena::ArenaAllocatable;
10 use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos};
11 use crate::mir::{
12 self,
13 interpret::{AllocId, ConstAllocation},
14 };
15 use crate::traits;
16 use crate::ty::subst::SubstsRef;
17 use crate::ty::{self, AdtDef, Ty};
18 use rustc_data_structures::fx::FxHashMap;
19 use rustc_middle::ty::TyCtxt;
20 use rustc_serialize::{Decodable, Encodable};
21 use rustc_span::Span;
22 use rustc_target::abi::FieldIdx;
23 pub use rustc_type_ir::{TyDecoder, TyEncoder};
24 use std::hash::Hash;
25 use std::intrinsics;
26 use std::marker::DiscriminantKind;
27
28 /// The shorthand encoding uses an enum's variant index `usize`
29 /// and is offset by this value so it never matches a real variant.
30 /// This offset is also chosen so that the first byte is never < 0x80.
31 pub const SHORTHAND_OFFSET: usize = 0x80;
32
33 pub trait EncodableWithShorthand<E: TyEncoder>: Copy + Eq + Hash {
34 type Variant: Encodable<E>;
variant(&self) -> &Self::Variant35 fn variant(&self) -> &Self::Variant;
36 }
37
38 #[allow(rustc::usage_of_ty_tykind)]
39 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> EncodableWithShorthand<E> for Ty<'tcx> {
40 type Variant = ty::TyKind<'tcx>;
41
42 #[inline]
variant(&self) -> &Self::Variant43 fn variant(&self) -> &Self::Variant {
44 self.kind()
45 }
46 }
47
48 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> EncodableWithShorthand<E> for ty::PredicateKind<'tcx> {
49 type Variant = ty::PredicateKind<'tcx>;
50
51 #[inline]
variant(&self) -> &Self::Variant52 fn variant(&self) -> &Self::Variant {
53 self
54 }
55 }
56
57 /// Trait for decoding to a reference.
58 ///
59 /// This is a separate trait from `Decodable` so that we can implement it for
60 /// upstream types, such as `FxHashSet`.
61 ///
62 /// The `TyDecodable` derive macro will use this trait for fields that are
63 /// references (and don't use a type alias to hide that).
64 ///
65 /// `Decodable` can still be implemented in cases where `Decodable` is required
66 /// by a trait bound.
67 pub trait RefDecodable<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> {
decode(d: &mut D) -> &'tcx Self68 fn decode(d: &mut D) -> &'tcx Self;
69 }
70
71 /// Encode the given value or a previously cached shorthand.
encode_with_shorthand<'tcx, E, T, M>(encoder: &mut E, value: &T, cache: M) where E: TyEncoder<I = TyCtxt<'tcx>>, M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>, T: EncodableWithShorthand<E>, T::Variant: DiscriminantKind<Discriminant = isize>,72 pub fn encode_with_shorthand<'tcx, E, T, M>(encoder: &mut E, value: &T, cache: M)
73 where
74 E: TyEncoder<I = TyCtxt<'tcx>>,
75 M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
76 T: EncodableWithShorthand<E>,
77 // The discriminant and shorthand must have the same size.
78 T::Variant: DiscriminantKind<Discriminant = isize>,
79 {
80 let existing_shorthand = cache(encoder).get(value).copied();
81 if let Some(shorthand) = existing_shorthand {
82 encoder.emit_usize(shorthand);
83 return;
84 }
85
86 let variant = value.variant();
87
88 let start = encoder.position();
89 variant.encode(encoder);
90 let len = encoder.position() - start;
91
92 // The shorthand encoding uses the same usize as the
93 // discriminant, with an offset so they can't conflict.
94 let discriminant = intrinsics::discriminant_value(variant);
95 assert!(SHORTHAND_OFFSET > discriminant as usize);
96
97 let shorthand = start + SHORTHAND_OFFSET;
98
99 // Get the number of bits that leb128 could fit
100 // in the same space as the fully encoded type.
101 let leb128_bits = len * 7;
102
103 // Check that the shorthand is a not longer than the
104 // full encoding itself, i.e., it's an obvious win.
105 if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
106 cache(encoder).insert(*value, shorthand);
107 }
108 }
109
110 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for Ty<'tcx> {
encode(&self, e: &mut E)111 fn encode(&self, e: &mut E) {
112 encode_with_shorthand(e, self, TyEncoder::type_shorthands);
113 }
114 }
115
116 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E>
117 for ty::Binder<'tcx, ty::PredicateKind<'tcx>>
118 {
encode(&self, e: &mut E)119 fn encode(&self, e: &mut E) {
120 self.bound_vars().encode(e);
121 encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands);
122 }
123 }
124
125 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Predicate<'tcx> {
encode(&self, e: &mut E)126 fn encode(&self, e: &mut E) {
127 self.kind().encode(e);
128 }
129 }
130
131 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Clause<'tcx> {
encode(&self, e: &mut E)132 fn encode(&self, e: &mut E) {
133 self.as_predicate().encode(e);
134 }
135 }
136
137 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Region<'tcx> {
encode(&self, e: &mut E)138 fn encode(&self, e: &mut E) {
139 self.kind().encode(e);
140 }
141 }
142
143 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Const<'tcx> {
encode(&self, e: &mut E)144 fn encode(&self, e: &mut E) {
145 self.0.0.encode(e);
146 }
147 }
148
149 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ConstAllocation<'tcx> {
encode(&self, e: &mut E)150 fn encode(&self, e: &mut E) {
151 self.inner().encode(e)
152 }
153 }
154
155 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for AdtDef<'tcx> {
encode(&self, e: &mut E)156 fn encode(&self, e: &mut E) {
157 self.0.0.encode(e)
158 }
159 }
160
161 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for AllocId {
encode(&self, e: &mut E)162 fn encode(&self, e: &mut E) {
163 e.encode_alloc_id(self)
164 }
165 }
166
167 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::ParamEnv<'tcx> {
encode(&self, e: &mut E)168 fn encode(&self, e: &mut E) {
169 self.caller_bounds().encode(e);
170 self.reveal().encode(e);
171 self.constness().encode(e);
172 }
173 }
174
175 #[inline]
decode_arena_allocable< 'tcx, D: TyDecoder<I = TyCtxt<'tcx>>, T: ArenaAllocatable<'tcx> + Decodable<D>, >( decoder: &mut D, ) -> &'tcx T where D: TyDecoder,176 fn decode_arena_allocable<
177 'tcx,
178 D: TyDecoder<I = TyCtxt<'tcx>>,
179 T: ArenaAllocatable<'tcx> + Decodable<D>,
180 >(
181 decoder: &mut D,
182 ) -> &'tcx T
183 where
184 D: TyDecoder,
185 {
186 decoder.interner().arena.alloc(Decodable::decode(decoder))
187 }
188
189 #[inline]
decode_arena_allocable_slice< 'tcx, D: TyDecoder<I = TyCtxt<'tcx>>, T: ArenaAllocatable<'tcx> + Decodable<D>, >( decoder: &mut D, ) -> &'tcx [T] where D: TyDecoder,190 fn decode_arena_allocable_slice<
191 'tcx,
192 D: TyDecoder<I = TyCtxt<'tcx>>,
193 T: ArenaAllocatable<'tcx> + Decodable<D>,
194 >(
195 decoder: &mut D,
196 ) -> &'tcx [T]
197 where
198 D: TyDecoder,
199 {
200 decoder.interner().arena.alloc_from_iter(<Vec<T> as Decodable<D>>::decode(decoder))
201 }
202
203 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for Ty<'tcx> {
204 #[allow(rustc::usage_of_ty_tykind)]
decode(decoder: &mut D) -> Ty<'tcx>205 fn decode(decoder: &mut D) -> Ty<'tcx> {
206 // Handle shorthands first, if we have a usize > 0x80.
207 if decoder.positioned_at_shorthand() {
208 let pos = decoder.read_usize();
209 assert!(pos >= SHORTHAND_OFFSET);
210 let shorthand = pos - SHORTHAND_OFFSET;
211
212 decoder.cached_ty_for_shorthand(shorthand, |decoder| {
213 decoder.with_position(shorthand, Ty::decode)
214 })
215 } else {
216 let tcx = decoder.interner();
217 tcx.mk_ty_from_kind(rustc_type_ir::TyKind::decode(decoder))
218 }
219 }
220 }
221
222 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D>
223 for ty::Binder<'tcx, ty::PredicateKind<'tcx>>
224 {
decode(decoder: &mut D) -> ty::Binder<'tcx, ty::PredicateKind<'tcx>>225 fn decode(decoder: &mut D) -> ty::Binder<'tcx, ty::PredicateKind<'tcx>> {
226 let bound_vars = Decodable::decode(decoder);
227 // Handle shorthands first, if we have a usize > 0x80.
228 ty::Binder::bind_with_vars(
229 if decoder.positioned_at_shorthand() {
230 let pos = decoder.read_usize();
231 assert!(pos >= SHORTHAND_OFFSET);
232 let shorthand = pos - SHORTHAND_OFFSET;
233
234 decoder.with_position(shorthand, ty::PredicateKind::decode)
235 } else {
236 ty::PredicateKind::decode(decoder)
237 },
238 bound_vars,
239 )
240 }
241 }
242
243 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Predicate<'tcx> {
decode(decoder: &mut D) -> ty::Predicate<'tcx>244 fn decode(decoder: &mut D) -> ty::Predicate<'tcx> {
245 let predicate_kind = Decodable::decode(decoder);
246 decoder.interner().mk_predicate(predicate_kind)
247 }
248 }
249
250 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Clause<'tcx> {
decode(decoder: &mut D) -> ty::Clause<'tcx>251 fn decode(decoder: &mut D) -> ty::Clause<'tcx> {
252 let pred: ty::Predicate<'tcx> = Decodable::decode(decoder);
253 pred.expect_clause()
254 }
255 }
256
257 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for SubstsRef<'tcx> {
decode(decoder: &mut D) -> Self258 fn decode(decoder: &mut D) -> Self {
259 let len = decoder.read_usize();
260 let tcx = decoder.interner();
261 tcx.mk_substs_from_iter(
262 (0..len).map::<ty::subst::GenericArg<'tcx>, _>(|_| Decodable::decode(decoder)),
263 )
264 }
265 }
266
267 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for mir::Place<'tcx> {
decode(decoder: &mut D) -> Self268 fn decode(decoder: &mut D) -> Self {
269 let local: mir::Local = Decodable::decode(decoder);
270 let len = decoder.read_usize();
271 let projection = decoder.interner().mk_place_elems_from_iter(
272 (0..len).map::<mir::PlaceElem<'tcx>, _>(|_| Decodable::decode(decoder)),
273 );
274 mir::Place { local, projection }
275 }
276 }
277
278 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Region<'tcx> {
decode(decoder: &mut D) -> Self279 fn decode(decoder: &mut D) -> Self {
280 ty::Region::new_from_kind(decoder.interner(), Decodable::decode(decoder))
281 }
282 }
283
284 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for CanonicalVarInfos<'tcx> {
decode(decoder: &mut D) -> Self285 fn decode(decoder: &mut D) -> Self {
286 let len = decoder.read_usize();
287 decoder.interner().mk_canonical_var_infos_from_iter(
288 (0..len).map::<CanonicalVarInfo<'tcx>, _>(|_| Decodable::decode(decoder)),
289 )
290 }
291 }
292
293 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for AllocId {
decode(decoder: &mut D) -> Self294 fn decode(decoder: &mut D) -> Self {
295 decoder.decode_alloc_id()
296 }
297 }
298
299 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::SymbolName<'tcx> {
decode(decoder: &mut D) -> Self300 fn decode(decoder: &mut D) -> Self {
301 ty::SymbolName::new(decoder.interner(), &decoder.read_str())
302 }
303 }
304
305 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::ParamEnv<'tcx> {
decode(d: &mut D) -> Self306 fn decode(d: &mut D) -> Self {
307 let caller_bounds = Decodable::decode(d);
308 let reveal = Decodable::decode(d);
309 let constness = Decodable::decode(d);
310 ty::ParamEnv::new(caller_bounds, reveal, constness)
311 }
312 }
313
314 macro_rules! impl_decodable_via_ref {
315 ($($t:ty,)+) => {
316 $(impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for $t {
317 fn decode(decoder: &mut D) -> Self {
318 RefDecodable::decode(decoder)
319 }
320 })*
321 }
322 }
323
324 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for ty::List<Ty<'tcx>> {
decode(decoder: &mut D) -> &'tcx Self325 fn decode(decoder: &mut D) -> &'tcx Self {
326 let len = decoder.read_usize();
327 decoder
328 .interner()
329 .mk_type_list_from_iter((0..len).map::<Ty<'tcx>, _>(|_| Decodable::decode(decoder)))
330 }
331 }
332
333 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
334 for ty::List<ty::PolyExistentialPredicate<'tcx>>
335 {
decode(decoder: &mut D) -> &'tcx Self336 fn decode(decoder: &mut D) -> &'tcx Self {
337 let len = decoder.read_usize();
338 decoder.interner().mk_poly_existential_predicates_from_iter(
339 (0..len).map::<ty::Binder<'tcx, _>, _>(|_| Decodable::decode(decoder)),
340 )
341 }
342 }
343
344 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Const<'tcx> {
decode(decoder: &mut D) -> Self345 fn decode(decoder: &mut D) -> Self {
346 let consts: ty::ConstData<'tcx> = Decodable::decode(decoder);
347 decoder.interner().mk_ct_from_kind(consts.kind, consts.ty)
348 }
349 }
350
351 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] {
decode(decoder: &mut D) -> &'tcx Self352 fn decode(decoder: &mut D) -> &'tcx Self {
353 decoder.interner().arena.alloc_from_iter(
354 (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
355 )
356 }
357 }
358
359 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ConstAllocation<'tcx> {
decode(decoder: &mut D) -> Self360 fn decode(decoder: &mut D) -> Self {
361 decoder.interner().mk_const_alloc(Decodable::decode(decoder))
362 }
363 }
364
365 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for AdtDef<'tcx> {
decode(decoder: &mut D) -> Self366 fn decode(decoder: &mut D) -> Self {
367 decoder.interner().mk_adt_def_from_data(Decodable::decode(decoder))
368 }
369 }
370
371 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for [(ty::Clause<'tcx>, Span)] {
decode(decoder: &mut D) -> &'tcx Self372 fn decode(decoder: &mut D) -> &'tcx Self {
373 decoder.interner().arena.alloc_from_iter(
374 (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
375 )
376 }
377 }
378
379 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
380 for ty::List<ty::BoundVariableKind>
381 {
decode(decoder: &mut D) -> &'tcx Self382 fn decode(decoder: &mut D) -> &'tcx Self {
383 let len = decoder.read_usize();
384 decoder.interner().mk_bound_variable_kinds_from_iter(
385 (0..len).map::<ty::BoundVariableKind, _>(|_| Decodable::decode(decoder)),
386 )
387 }
388 }
389
390 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for ty::List<ty::Const<'tcx>> {
decode(decoder: &mut D) -> &'tcx Self391 fn decode(decoder: &mut D) -> &'tcx Self {
392 let len = decoder.read_usize();
393 decoder.interner().mk_const_list_from_iter(
394 (0..len).map::<ty::Const<'tcx>, _>(|_| Decodable::decode(decoder)),
395 )
396 }
397 }
398
399 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for ty::List<ty::Clause<'tcx>> {
decode(decoder: &mut D) -> &'tcx Self400 fn decode(decoder: &mut D) -> &'tcx Self {
401 let len = decoder.read_usize();
402 decoder.interner().mk_clauses_from_iter(
403 (0..len).map::<ty::Clause<'tcx>, _>(|_| Decodable::decode(decoder)),
404 )
405 }
406 }
407
408 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for ty::List<FieldIdx> {
decode(decoder: &mut D) -> &'tcx Self409 fn decode(decoder: &mut D) -> &'tcx Self {
410 let len = decoder.read_usize();
411 decoder
412 .interner()
413 .mk_fields_from_iter((0..len).map::<FieldIdx, _>(|_| Decodable::decode(decoder)))
414 }
415 }
416
417 impl_decodable_via_ref! {
418 &'tcx ty::TypeckResults<'tcx>,
419 &'tcx ty::List<Ty<'tcx>>,
420 &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
421 &'tcx traits::ImplSource<'tcx, ()>,
422 &'tcx mir::Body<'tcx>,
423 &'tcx mir::UnsafetyCheckResult,
424 &'tcx mir::BorrowCheckResult<'tcx>,
425 &'tcx mir::coverage::CodeRegion,
426 &'tcx ty::List<ty::BoundVariableKind>,
427 &'tcx ty::List<ty::Clause<'tcx>>,
428 &'tcx ty::List<FieldIdx>,
429 }
430
431 #[macro_export]
432 macro_rules! __impl_decoder_methods {
433 ($($name:ident -> $ty:ty;)*) => {
434 $(
435 #[inline]
436 fn $name(&mut self) -> $ty {
437 self.opaque.$name()
438 }
439 )*
440 }
441 }
442
443 macro_rules! impl_arena_allocatable_decoder {
444 ([]$args:tt) => {};
445 ([decode $(, $attrs:ident)*]
446 [$name:ident: $ty:ty]) => {
447 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for $ty {
448 #[inline]
449 fn decode(decoder: &mut D) -> &'tcx Self {
450 decode_arena_allocable(decoder)
451 }
452 }
453
454 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for [$ty] {
455 #[inline]
456 fn decode(decoder: &mut D) -> &'tcx Self {
457 decode_arena_allocable_slice(decoder)
458 }
459 }
460 };
461 }
462
463 macro_rules! impl_arena_allocatable_decoders {
464 ([$($a:tt $name:ident: $ty:ty,)*]) => {
465 $(
466 impl_arena_allocatable_decoder!($a [$name: $ty]);
467 )*
468 }
469 }
470
471 rustc_hir::arena_types!(impl_arena_allocatable_decoders);
472 arena_types!(impl_arena_allocatable_decoders);
473
474 macro_rules! impl_arena_copy_decoder {
475 (<$tcx:tt> $($ty:ty,)*) => {
476 $(impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for $ty {
477 #[inline]
478 fn decode(decoder: &mut D) -> &'tcx Self {
479 decoder.interner().arena.alloc(Decodable::decode(decoder))
480 }
481 }
482
483 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for [$ty] {
484 #[inline]
485 fn decode(decoder: &mut D) -> &'tcx Self {
486 decoder.interner().arena.alloc_from_iter(<Vec<_> as Decodable<D>>::decode(decoder))
487 }
488 })*
489 };
490 }
491
492 impl_arena_copy_decoder! {<'tcx>
493 Span,
494 rustc_span::symbol::Ident,
495 ty::Variance,
496 rustc_span::def_id::DefId,
497 rustc_span::def_id::LocalDefId,
498 (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo),
499 ty::DeducedParamAttrs,
500 }
501
502 #[macro_export]
503 macro_rules! implement_ty_decoder {
504 ($DecoderName:ident <$($typaram:tt),*>) => {
505 mod __ty_decoder_impl {
506 use rustc_serialize::Decoder;
507
508 use super::$DecoderName;
509
510 impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
511 $crate::__impl_decoder_methods! {
512 read_usize -> usize;
513 read_u128 -> u128;
514 read_u64 -> u64;
515 read_u32 -> u32;
516 read_u16 -> u16;
517 read_u8 -> u8;
518
519 read_isize -> isize;
520 read_i128 -> i128;
521 read_i64 -> i64;
522 read_i32 -> i32;
523 read_i16 -> i16;
524 }
525
526 #[inline]
527 fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
528 self.opaque.read_raw_bytes(len)
529 }
530
531 #[inline]
532 fn peek_byte(&self) -> u8 {
533 self.opaque.peek_byte()
534 }
535
536 #[inline]
537 fn position(&self) -> usize {
538 self.opaque.position()
539 }
540 }
541 }
542 }
543 }
544
545 macro_rules! impl_binder_encode_decode {
546 ($($t:ty),+ $(,)?) => {
547 $(
548 impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Binder<'tcx, $t> {
549 fn encode(&self, e: &mut E) {
550 self.bound_vars().encode(e);
551 self.as_ref().skip_binder().encode(e);
552 }
553 }
554 impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Binder<'tcx, $t> {
555 fn decode(decoder: &mut D) -> Self {
556 let bound_vars = Decodable::decode(decoder);
557 ty::Binder::bind_with_vars(Decodable::decode(decoder), bound_vars)
558 }
559 }
560 )*
561 }
562 }
563
564 impl_binder_encode_decode! {
565 &'tcx ty::List<Ty<'tcx>>,
566 ty::FnSig<'tcx>,
567 ty::Predicate<'tcx>,
568 ty::TraitPredicate<'tcx>,
569 ty::ExistentialPredicate<'tcx>,
570 ty::TraitRef<'tcx>,
571 Vec<ty::GeneratorInteriorTypeCause<'tcx>>,
572 ty::ExistentialTraitRef<'tcx>,
573 }
574