• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Support code for encoding and decoding types.
2 
3 use std::alloc::Allocator;
4 use std::borrow::Cow;
5 use std::cell::{Cell, RefCell};
6 use std::marker::PhantomData;
7 use std::path;
8 use std::rc::Rc;
9 use std::sync::Arc;
10 
11 /// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
12 /// This way we can skip validation and still be relatively sure that deserialization
13 /// did not desynchronize.
14 ///
15 /// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout
16 const STR_SENTINEL: u8 = 0xC1;
17 
18 /// A note about error handling.
19 ///
20 /// Encoders may be fallible, but in practice failure is rare and there are so
21 /// many nested calls that typical Rust error handling (via `Result` and `?`)
22 /// is pervasive and has non-trivial cost. Instead, impls of this trait must
23 /// implement a delayed error handling strategy. If a failure occurs, they
24 /// should record this internally, and all subsequent encoding operations can
25 /// be processed or ignored, whichever is appropriate. Then they should provide
26 /// a `finish` method that finishes up encoding. If the encoder is fallible,
27 /// `finish` should return a `Result` that indicates success or failure.
28 ///
29 /// This current does not support `f32` nor `f64`, as they're not needed in any
30 /// serialized data structures. That could be changed, but consider whether it
31 /// really makes sense to store floating-point values at all.
32 /// (If you need it, revert <https://github.com/rust-lang/rust/pull/109984>.)
33 pub trait Encoder {
emit_usize(&mut self, v: usize)34     fn emit_usize(&mut self, v: usize);
emit_u128(&mut self, v: u128)35     fn emit_u128(&mut self, v: u128);
emit_u64(&mut self, v: u64)36     fn emit_u64(&mut self, v: u64);
emit_u32(&mut self, v: u32)37     fn emit_u32(&mut self, v: u32);
emit_u16(&mut self, v: u16)38     fn emit_u16(&mut self, v: u16);
emit_u8(&mut self, v: u8)39     fn emit_u8(&mut self, v: u8);
40 
emit_isize(&mut self, v: isize)41     fn emit_isize(&mut self, v: isize);
emit_i128(&mut self, v: i128)42     fn emit_i128(&mut self, v: i128);
emit_i64(&mut self, v: i64)43     fn emit_i64(&mut self, v: i64);
emit_i32(&mut self, v: i32)44     fn emit_i32(&mut self, v: i32);
emit_i16(&mut self, v: i16)45     fn emit_i16(&mut self, v: i16);
46 
47     #[inline]
emit_i8(&mut self, v: i8)48     fn emit_i8(&mut self, v: i8) {
49         self.emit_u8(v as u8);
50     }
51 
52     #[inline]
emit_bool(&mut self, v: bool)53     fn emit_bool(&mut self, v: bool) {
54         self.emit_u8(if v { 1 } else { 0 });
55     }
56 
57     #[inline]
emit_char(&mut self, v: char)58     fn emit_char(&mut self, v: char) {
59         self.emit_u32(v as u32);
60     }
61 
62     #[inline]
emit_str(&mut self, v: &str)63     fn emit_str(&mut self, v: &str) {
64         self.emit_usize(v.len());
65         self.emit_raw_bytes(v.as_bytes());
66         self.emit_u8(STR_SENTINEL);
67     }
68 
emit_raw_bytes(&mut self, s: &[u8])69     fn emit_raw_bytes(&mut self, s: &[u8]);
70 
emit_enum_variant<F>(&mut self, v_id: usize, f: F) where F: FnOnce(&mut Self),71     fn emit_enum_variant<F>(&mut self, v_id: usize, f: F)
72     where
73         F: FnOnce(&mut Self),
74     {
75         self.emit_usize(v_id);
76         f(self);
77     }
78 }
79 
80 // Note: all the methods in this trait are infallible, which may be surprising.
81 // They used to be fallible (i.e. return a `Result`) but many of the impls just
82 // panicked when something went wrong, and for the cases that didn't the
83 // top-level invocation would also just panic on failure. Switching to
84 // infallibility made things faster and lots of code a little simpler and more
85 // concise.
86 ///
87 /// This current does not support `f32` nor `f64`, as they're not needed in any
88 /// serialized data structures. That could be changed, but consider whether it
89 /// really makes sense to store floating-point values at all.
90 /// (If you need it, revert <https://github.com/rust-lang/rust/pull/109984>.)
91 pub trait Decoder {
read_usize(&mut self) -> usize92     fn read_usize(&mut self) -> usize;
read_u128(&mut self) -> u12893     fn read_u128(&mut self) -> u128;
read_u64(&mut self) -> u6494     fn read_u64(&mut self) -> u64;
read_u32(&mut self) -> u3295     fn read_u32(&mut self) -> u32;
read_u16(&mut self) -> u1696     fn read_u16(&mut self) -> u16;
read_u8(&mut self) -> u897     fn read_u8(&mut self) -> u8;
98 
read_isize(&mut self) -> isize99     fn read_isize(&mut self) -> isize;
read_i128(&mut self) -> i128100     fn read_i128(&mut self) -> i128;
read_i64(&mut self) -> i64101     fn read_i64(&mut self) -> i64;
read_i32(&mut self) -> i32102     fn read_i32(&mut self) -> i32;
read_i16(&mut self) -> i16103     fn read_i16(&mut self) -> i16;
104 
105     #[inline]
read_i8(&mut self) -> i8106     fn read_i8(&mut self) -> i8 {
107         self.read_u8() as i8
108     }
109 
110     #[inline]
read_bool(&mut self) -> bool111     fn read_bool(&mut self) -> bool {
112         let value = self.read_u8();
113         value != 0
114     }
115 
116     #[inline]
read_char(&mut self) -> char117     fn read_char(&mut self) -> char {
118         let bits = self.read_u32();
119         std::char::from_u32(bits).unwrap()
120     }
121 
122     #[inline]
read_str(&mut self) -> &str123     fn read_str(&mut self) -> &str {
124         let len = self.read_usize();
125         let bytes = self.read_raw_bytes(len + 1);
126         assert!(bytes[len] == STR_SENTINEL);
127         unsafe { std::str::from_utf8_unchecked(&bytes[..len]) }
128     }
129 
read_raw_bytes(&mut self, len: usize) -> &[u8]130     fn read_raw_bytes(&mut self, len: usize) -> &[u8];
131 
132     // Although there is an `emit_enum_variant` method in `Encoder`, the code
133     // patterns in decoding are different enough to encoding that there is no
134     // need for a corresponding `read_enum_variant` method here.
135 
peek_byte(&self) -> u8136     fn peek_byte(&self) -> u8;
position(&self) -> usize137     fn position(&self) -> usize;
138 }
139 
140 /// Trait for types that can be serialized
141 ///
142 /// This can be implemented using the `Encodable`, `TyEncodable` and
143 /// `MetadataEncodable` macros.
144 ///
145 /// * `Encodable` should be used in crates that don't depend on
146 ///   `rustc_middle`.
147 /// * `MetadataEncodable` is used in `rustc_metadata` for types that contain
148 ///   `rustc_metadata::rmeta::Lazy`.
149 /// * `TyEncodable` should be used for types that are only serialized in crate
150 ///   metadata or the incremental cache. This is most types in `rustc_middle`.
151 pub trait Encodable<S: Encoder> {
encode(&self, s: &mut S)152     fn encode(&self, s: &mut S);
153 }
154 
155 /// Trait for types that can be deserialized
156 ///
157 /// This can be implemented using the `Decodable`, `TyDecodable` and
158 /// `MetadataDecodable` macros.
159 ///
160 /// * `Decodable` should be used in crates that don't depend on
161 ///   `rustc_middle`.
162 /// * `MetadataDecodable` is used in `rustc_metadata` for types that contain
163 ///   `rustc_metadata::rmeta::Lazy`.
164 /// * `TyDecodable` should be used for types that are only serialized in crate
165 ///   metadata or the incremental cache. This is most types in `rustc_middle`.
166 pub trait Decodable<D: Decoder>: Sized {
decode(d: &mut D) -> Self167     fn decode(d: &mut D) -> Self;
168 }
169 
170 macro_rules! direct_serialize_impls {
171     ($($ty:ident $emit_method:ident $read_method:ident),*) => {
172         $(
173             impl<S: Encoder> Encodable<S> for $ty {
174                 fn encode(&self, s: &mut S) {
175                     s.$emit_method(*self);
176                 }
177             }
178 
179             impl<D: Decoder> Decodable<D> for $ty {
180                 fn decode(d: &mut D) -> $ty {
181                     d.$read_method()
182                 }
183             }
184         )*
185     }
186 }
187 
188 direct_serialize_impls! {
189     usize emit_usize read_usize,
190     u8 emit_u8 read_u8,
191     u16 emit_u16 read_u16,
192     u32 emit_u32 read_u32,
193     u64 emit_u64 read_u64,
194     u128 emit_u128 read_u128,
195 
196     isize emit_isize read_isize,
197     i8 emit_i8 read_i8,
198     i16 emit_i16 read_i16,
199     i32 emit_i32 read_i32,
200     i64 emit_i64 read_i64,
201     i128 emit_i128 read_i128,
202 
203     bool emit_bool read_bool,
204     char emit_char read_char
205 }
206 
207 impl<S: Encoder, T: ?Sized> Encodable<S> for &T
208 where
209     T: Encodable<S>,
210 {
encode(&self, s: &mut S)211     fn encode(&self, s: &mut S) {
212         (**self).encode(s)
213     }
214 }
215 
216 impl<S: Encoder> Encodable<S> for ! {
encode(&self, _s: &mut S)217     fn encode(&self, _s: &mut S) {
218         unreachable!();
219     }
220 }
221 
222 impl<D: Decoder> Decodable<D> for ! {
decode(_d: &mut D) -> !223     fn decode(_d: &mut D) -> ! {
224         unreachable!()
225     }
226 }
227 
228 impl<S: Encoder> Encodable<S> for ::std::num::NonZeroU32 {
encode(&self, s: &mut S)229     fn encode(&self, s: &mut S) {
230         s.emit_u32(self.get());
231     }
232 }
233 
234 impl<D: Decoder> Decodable<D> for ::std::num::NonZeroU32 {
decode(d: &mut D) -> Self235     fn decode(d: &mut D) -> Self {
236         ::std::num::NonZeroU32::new(d.read_u32()).unwrap()
237     }
238 }
239 
240 impl<S: Encoder> Encodable<S> for str {
encode(&self, s: &mut S)241     fn encode(&self, s: &mut S) {
242         s.emit_str(self);
243     }
244 }
245 
246 impl<S: Encoder> Encodable<S> for String {
encode(&self, s: &mut S)247     fn encode(&self, s: &mut S) {
248         s.emit_str(&self[..]);
249     }
250 }
251 
252 impl<D: Decoder> Decodable<D> for String {
decode(d: &mut D) -> String253     fn decode(d: &mut D) -> String {
254         d.read_str().to_owned()
255     }
256 }
257 
258 impl<S: Encoder> Encodable<S> for () {
encode(&self, _s: &mut S)259     fn encode(&self, _s: &mut S) {}
260 }
261 
262 impl<D: Decoder> Decodable<D> for () {
decode(_: &mut D) -> ()263     fn decode(_: &mut D) -> () {}
264 }
265 
266 impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
encode(&self, _s: &mut S)267     fn encode(&self, _s: &mut S) {}
268 }
269 
270 impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
decode(_: &mut D) -> PhantomData<T>271     fn decode(_: &mut D) -> PhantomData<T> {
272         PhantomData
273     }
274 }
275 
276 impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<[T], A> {
decode(d: &mut D) -> Box<[T], A>277     fn decode(d: &mut D) -> Box<[T], A> {
278         let v: Vec<T, A> = Decodable::decode(d);
279         v.into_boxed_slice()
280     }
281 }
282 
283 impl<S: Encoder, T: Encodable<S>> Encodable<S> for Rc<T> {
encode(&self, s: &mut S)284     fn encode(&self, s: &mut S) {
285         (**self).encode(s);
286     }
287 }
288 
289 impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> {
decode(d: &mut D) -> Rc<T>290     fn decode(d: &mut D) -> Rc<T> {
291         Rc::new(Decodable::decode(d))
292     }
293 }
294 
295 impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
encode(&self, s: &mut S)296     default fn encode(&self, s: &mut S) {
297         s.emit_usize(self.len());
298         for e in self.iter() {
299             e.encode(s);
300         }
301     }
302 }
303 
304 impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
encode(&self, s: &mut S)305     fn encode(&self, s: &mut S) {
306         let slice: &[T] = self;
307         slice.encode(s);
308     }
309 }
310 
311 impl<D: Decoder, T: Decodable<D>, A: Allocator + Default> Decodable<D> for Vec<T, A> {
decode(d: &mut D) -> Vec<T, A>312     default fn decode(d: &mut D) -> Vec<T, A> {
313         let len = d.read_usize();
314         let allocator = A::default();
315         // SAFETY: we set the capacity in advance, only write elements, and
316         // only set the length at the end once the writing has succeeded.
317         let mut vec = Vec::with_capacity_in(len, allocator);
318         unsafe {
319             let ptr: *mut T = vec.as_mut_ptr();
320             for i in 0..len {
321                 std::ptr::write(ptr.add(i), Decodable::decode(d));
322             }
323             vec.set_len(len);
324         }
325         vec
326     }
327 }
328 
329 impl<S: Encoder, T: Encodable<S>, const N: usize> Encodable<S> for [T; N] {
encode(&self, s: &mut S)330     fn encode(&self, s: &mut S) {
331         let slice: &[T] = self;
332         slice.encode(s);
333     }
334 }
335 
336 impl<D: Decoder, const N: usize> Decodable<D> for [u8; N] {
decode(d: &mut D) -> [u8; N]337     fn decode(d: &mut D) -> [u8; N] {
338         let len = d.read_usize();
339         assert!(len == N);
340         let mut v = [0u8; N];
341         for i in 0..len {
342             v[i] = Decodable::decode(d);
343         }
344         v
345     }
346 }
347 
348 impl<'a, S: Encoder, T: Encodable<S>> Encodable<S> for Cow<'a, [T]>
349 where
350     [T]: ToOwned<Owned = Vec<T>>,
351 {
encode(&self, s: &mut S)352     fn encode(&self, s: &mut S) {
353         let slice: &[T] = self;
354         slice.encode(s);
355     }
356 }
357 
358 impl<D: Decoder, T: Decodable<D> + ToOwned> Decodable<D> for Cow<'static, [T]>
359 where
360     [T]: ToOwned<Owned = Vec<T>>,
361 {
decode(d: &mut D) -> Cow<'static, [T]>362     fn decode(d: &mut D) -> Cow<'static, [T]> {
363         let v: Vec<T> = Decodable::decode(d);
364         Cow::Owned(v)
365     }
366 }
367 
368 impl<'a, S: Encoder> Encodable<S> for Cow<'a, str> {
encode(&self, s: &mut S)369     fn encode(&self, s: &mut S) {
370         let val: &str = self;
371         val.encode(s)
372     }
373 }
374 
375 impl<'a, D: Decoder> Decodable<D> for Cow<'a, str> {
decode(d: &mut D) -> Cow<'static, str>376     fn decode(d: &mut D) -> Cow<'static, str> {
377         let v: String = Decodable::decode(d);
378         Cow::Owned(v)
379     }
380 }
381 
382 impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> {
encode(&self, s: &mut S)383     fn encode(&self, s: &mut S) {
384         match *self {
385             None => s.emit_enum_variant(0, |_| {}),
386             Some(ref v) => s.emit_enum_variant(1, |s| v.encode(s)),
387         }
388     }
389 }
390 
391 impl<D: Decoder, T: Decodable<D>> Decodable<D> for Option<T> {
decode(d: &mut D) -> Option<T>392     fn decode(d: &mut D) -> Option<T> {
393         match d.read_usize() {
394             0 => None,
395             1 => Some(Decodable::decode(d)),
396             _ => panic!("Encountered invalid discriminant while decoding `Option`."),
397         }
398     }
399 }
400 
401 impl<S: Encoder, T1: Encodable<S>, T2: Encodable<S>> Encodable<S> for Result<T1, T2> {
encode(&self, s: &mut S)402     fn encode(&self, s: &mut S) {
403         match *self {
404             Ok(ref v) => s.emit_enum_variant(0, |s| v.encode(s)),
405             Err(ref v) => s.emit_enum_variant(1, |s| v.encode(s)),
406         }
407     }
408 }
409 
410 impl<D: Decoder, T1: Decodable<D>, T2: Decodable<D>> Decodable<D> for Result<T1, T2> {
decode(d: &mut D) -> Result<T1, T2>411     fn decode(d: &mut D) -> Result<T1, T2> {
412         match d.read_usize() {
413             0 => Ok(T1::decode(d)),
414             1 => Err(T2::decode(d)),
415             _ => panic!("Encountered invalid discriminant while decoding `Result`."),
416         }
417     }
418 }
419 
420 macro_rules! peel {
421     ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
422 }
423 
424 macro_rules! tuple {
425     () => ();
426     ( $($name:ident,)+ ) => (
427         impl<D: Decoder, $($name: Decodable<D>),+> Decodable<D> for ($($name,)+) {
428             fn decode(d: &mut D) -> ($($name,)+) {
429                 ($({ let element: $name = Decodable::decode(d); element },)+)
430             }
431         }
432         impl<S: Encoder, $($name: Encodable<S>),+> Encodable<S> for ($($name,)+) {
433             #[allow(non_snake_case)]
434             fn encode(&self, s: &mut S) {
435                 let ($(ref $name,)+) = *self;
436                 $($name.encode(s);)+
437             }
438         }
439         peel! { $($name,)+ }
440     )
441 }
442 
443 tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
444 
445 impl<S: Encoder> Encodable<S> for path::Path {
encode(&self, e: &mut S)446     fn encode(&self, e: &mut S) {
447         self.to_str().unwrap().encode(e);
448     }
449 }
450 
451 impl<S: Encoder> Encodable<S> for path::PathBuf {
encode(&self, e: &mut S)452     fn encode(&self, e: &mut S) {
453         path::Path::encode(self, e);
454     }
455 }
456 
457 impl<D: Decoder> Decodable<D> for path::PathBuf {
decode(d: &mut D) -> path::PathBuf458     fn decode(d: &mut D) -> path::PathBuf {
459         let bytes: String = Decodable::decode(d);
460         path::PathBuf::from(bytes)
461     }
462 }
463 
464 impl<S: Encoder, T: Encodable<S> + Copy> Encodable<S> for Cell<T> {
encode(&self, s: &mut S)465     fn encode(&self, s: &mut S) {
466         self.get().encode(s);
467     }
468 }
469 
470 impl<D: Decoder, T: Decodable<D> + Copy> Decodable<D> for Cell<T> {
decode(d: &mut D) -> Cell<T>471     fn decode(d: &mut D) -> Cell<T> {
472         Cell::new(Decodable::decode(d))
473     }
474 }
475 
476 impl<S: Encoder, T: Encodable<S>> Encodable<S> for RefCell<T> {
encode(&self, s: &mut S)477     fn encode(&self, s: &mut S) {
478         self.borrow().encode(s);
479     }
480 }
481 
482 impl<D: Decoder, T: Decodable<D>> Decodable<D> for RefCell<T> {
decode(d: &mut D) -> RefCell<T>483     fn decode(d: &mut D) -> RefCell<T> {
484         RefCell::new(Decodable::decode(d))
485     }
486 }
487 
488 impl<S: Encoder, T: Encodable<S>> Encodable<S> for Arc<T> {
encode(&self, s: &mut S)489     fn encode(&self, s: &mut S) {
490         (**self).encode(s);
491     }
492 }
493 
494 impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
decode(d: &mut D) -> Arc<T>495     fn decode(d: &mut D) -> Arc<T> {
496         Arc::new(Decodable::decode(d))
497     }
498 }
499 
500 impl<S: Encoder, T: ?Sized + Encodable<S>, A: Allocator + Default> Encodable<S> for Box<T, A> {
encode(&self, s: &mut S)501     fn encode(&self, s: &mut S) {
502         (**self).encode(s)
503     }
504 }
505 
506 impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<T, A> {
decode(d: &mut D) -> Box<T, A>507     fn decode(d: &mut D) -> Box<T, A> {
508         let allocator = A::default();
509         Box::new_in(Decodable::decode(d), allocator)
510     }
511 }
512