hkalbasi_rustc_ap_rustc_serialize/
serialize.rs

1//! Support code for encoding and decoding types.
2
3use std::alloc::Allocator;
4use std::borrow::Cow;
5use std::cell::{Cell, RefCell};
6use std::marker::PhantomData;
7use std::path;
8use std::rc::Rc;
9use 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
16const 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>.)
33pub trait Encoder {
34    fn emit_usize(&mut self, v: usize);
35    fn emit_u128(&mut self, v: u128);
36    fn emit_u64(&mut self, v: u64);
37    fn emit_u32(&mut self, v: u32);
38    fn emit_u16(&mut self, v: u16);
39    fn emit_u8(&mut self, v: u8);
40
41    fn emit_isize(&mut self, v: isize);
42    fn emit_i128(&mut self, v: i128);
43    fn emit_i64(&mut self, v: i64);
44    fn emit_i32(&mut self, v: i32);
45    fn emit_i16(&mut self, v: i16);
46
47    #[inline]
48    fn emit_i8(&mut self, v: i8) {
49        self.emit_u8(v as u8);
50    }
51
52    #[inline]
53    fn emit_bool(&mut self, v: bool) {
54        self.emit_u8(if v { 1 } else { 0 });
55    }
56
57    #[inline]
58    fn emit_char(&mut self, v: char) {
59        self.emit_u32(v as u32);
60    }
61
62    #[inline]
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
69    fn emit_raw_bytes(&mut self, s: &[u8]);
70
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>.)
91pub trait Decoder {
92    fn read_usize(&mut self) -> usize;
93    fn read_u128(&mut self) -> u128;
94    fn read_u64(&mut self) -> u64;
95    fn read_u32(&mut self) -> u32;
96    fn read_u16(&mut self) -> u16;
97    fn read_u8(&mut self) -> u8;
98
99    fn read_isize(&mut self) -> isize;
100    fn read_i128(&mut self) -> i128;
101    fn read_i64(&mut self) -> i64;
102    fn read_i32(&mut self) -> i32;
103    fn read_i16(&mut self) -> i16;
104
105    #[inline]
106    fn read_i8(&mut self) -> i8 {
107        self.read_u8() as i8
108    }
109
110    #[inline]
111    fn read_bool(&mut self) -> bool {
112        let value = self.read_u8();
113        value != 0
114    }
115
116    #[inline]
117    fn read_char(&mut self) -> char {
118        let bits = self.read_u32();
119        std::char::from_u32(bits).unwrap()
120    }
121
122    #[inline]
123    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
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
136    fn peek_byte(&self) -> u8;
137    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`.
151pub trait Encodable<S: Encoder> {
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`.
166pub trait Decodable<D: Decoder>: Sized {
167    fn decode(d: &mut D) -> Self;
168}
169
170macro_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
188direct_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
207impl<S: Encoder, T: ?Sized> Encodable<S> for &T
208where
209    T: Encodable<S>,
210{
211    fn encode(&self, s: &mut S) {
212        (**self).encode(s)
213    }
214}
215
216impl<S: Encoder> Encodable<S> for ! {
217    fn encode(&self, _s: &mut S) {
218        unreachable!();
219    }
220}
221
222impl<D: Decoder> Decodable<D> for ! {
223    fn decode(_d: &mut D) -> ! {
224        unreachable!()
225    }
226}
227
228impl<S: Encoder> Encodable<S> for ::std::num::NonZeroU32 {
229    fn encode(&self, s: &mut S) {
230        s.emit_u32(self.get());
231    }
232}
233
234impl<D: Decoder> Decodable<D> for ::std::num::NonZeroU32 {
235    fn decode(d: &mut D) -> Self {
236        ::std::num::NonZeroU32::new(d.read_u32()).unwrap()
237    }
238}
239
240impl<S: Encoder> Encodable<S> for str {
241    fn encode(&self, s: &mut S) {
242        s.emit_str(self);
243    }
244}
245
246impl<S: Encoder> Encodable<S> for String {
247    fn encode(&self, s: &mut S) {
248        s.emit_str(&self[..]);
249    }
250}
251
252impl<D: Decoder> Decodable<D> for String {
253    fn decode(d: &mut D) -> String {
254        d.read_str().to_owned()
255    }
256}
257
258impl<S: Encoder> Encodable<S> for () {
259    fn encode(&self, _s: &mut S) {}
260}
261
262impl<D: Decoder> Decodable<D> for () {
263    fn decode(_: &mut D) -> () {}
264}
265
266impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
267    fn encode(&self, _s: &mut S) {}
268}
269
270impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
271    fn decode(_: &mut D) -> PhantomData<T> {
272        PhantomData
273    }
274}
275
276impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for 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
283impl<S: Encoder, T: Encodable<S>> Encodable<S> for Rc<T> {
284    fn encode(&self, s: &mut S) {
285        (**self).encode(s);
286    }
287}
288
289impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> {
290    fn decode(d: &mut D) -> Rc<T> {
291        Rc::new(Decodable::decode(d))
292    }
293}
294
295impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
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
304impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
305    fn encode(&self, s: &mut S) {
306        let slice: &[T] = self;
307        slice.encode(s);
308    }
309}
310
311impl<D: Decoder, T: Decodable<D>, A: Allocator + Default> Decodable<D> for 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
329impl<S: Encoder, T: Encodable<S>, const N: usize> Encodable<S> for [T; N] {
330    fn encode(&self, s: &mut S) {
331        let slice: &[T] = self;
332        slice.encode(s);
333    }
334}
335
336impl<D: Decoder, const N: usize> Decodable<D> for [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
348impl<'a, S: Encoder, T: Encodable<S>> Encodable<S> for Cow<'a, [T]>
349where
350    [T]: ToOwned<Owned = Vec<T>>,
351{
352    fn encode(&self, s: &mut S) {
353        let slice: &[T] = self;
354        slice.encode(s);
355    }
356}
357
358impl<D: Decoder, T: Decodable<D> + ToOwned> Decodable<D> for Cow<'static, [T]>
359where
360    [T]: ToOwned<Owned = Vec<T>>,
361{
362    fn decode(d: &mut D) -> Cow<'static, [T]> {
363        let v: Vec<T> = Decodable::decode(d);
364        Cow::Owned(v)
365    }
366}
367
368impl<'a, S: Encoder> Encodable<S> for Cow<'a, str> {
369    fn encode(&self, s: &mut S) {
370        let val: &str = self;
371        val.encode(s)
372    }
373}
374
375impl<'a, D: Decoder> Decodable<D> for Cow<'a, str> {
376    fn decode(d: &mut D) -> Cow<'static, str> {
377        let v: String = Decodable::decode(d);
378        Cow::Owned(v)
379    }
380}
381
382impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> {
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
391impl<D: Decoder, T: Decodable<D>> Decodable<D> for 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
401impl<S: Encoder, T1: Encodable<S>, T2: Encodable<S>> Encodable<S> for Result<T1, T2> {
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
410impl<D: Decoder, T1: Decodable<D>, T2: Decodable<D>> Decodable<D> for 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
420macro_rules! peel {
421    ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
422}
423
424macro_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
443tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
444
445impl<S: Encoder> Encodable<S> for path::Path {
446    fn encode(&self, e: &mut S) {
447        self.to_str().unwrap().encode(e);
448    }
449}
450
451impl<S: Encoder> Encodable<S> for path::PathBuf {
452    fn encode(&self, e: &mut S) {
453        path::Path::encode(self, e);
454    }
455}
456
457impl<D: Decoder> Decodable<D> for path::PathBuf {
458    fn decode(d: &mut D) -> path::PathBuf {
459        let bytes: String = Decodable::decode(d);
460        path::PathBuf::from(bytes)
461    }
462}
463
464impl<S: Encoder, T: Encodable<S> + Copy> Encodable<S> for Cell<T> {
465    fn encode(&self, s: &mut S) {
466        self.get().encode(s);
467    }
468}
469
470impl<D: Decoder, T: Decodable<D> + Copy> Decodable<D> for Cell<T> {
471    fn decode(d: &mut D) -> Cell<T> {
472        Cell::new(Decodable::decode(d))
473    }
474}
475
476impl<S: Encoder, T: Encodable<S>> Encodable<S> for RefCell<T> {
477    fn encode(&self, s: &mut S) {
478        self.borrow().encode(s);
479    }
480}
481
482impl<D: Decoder, T: Decodable<D>> Decodable<D> for RefCell<T> {
483    fn decode(d: &mut D) -> RefCell<T> {
484        RefCell::new(Decodable::decode(d))
485    }
486}
487
488impl<S: Encoder, T: Encodable<S>> Encodable<S> for Arc<T> {
489    fn encode(&self, s: &mut S) {
490        (**self).encode(s);
491    }
492}
493
494impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
495    fn decode(d: &mut D) -> Arc<T> {
496        Arc::new(Decodable::decode(d))
497    }
498}
499
500impl<S: Encoder, T: ?Sized + Encodable<S>, A: Allocator + Default> Encodable<S> for Box<T, A> {
501    fn encode(&self, s: &mut S) {
502        (**self).encode(s)
503    }
504}
505
506impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for 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}