ra_ap_rustc_serialize/
serialize.rs

1//! Support code for encoding and decoding types.
2
3use std::borrow::Cow;
4use std::cell::{Cell, RefCell};
5use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
6use std::hash::{BuildHasher, Hash};
7use std::marker::PhantomData;
8use std::num::NonZero;
9use std::path;
10use std::rc::Rc;
11use std::sync::Arc;
12
13use smallvec::{Array, SmallVec};
14use thin_vec::ThinVec;
15
16/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
17/// This way we can skip validation and still be relatively sure that deserialization
18/// did not desynchronize.
19///
20/// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout
21const STR_SENTINEL: u8 = 0xC1;
22
23/// A note about error handling.
24///
25/// Encoders may be fallible, but in practice failure is rare and there are so
26/// many nested calls that typical Rust error handling (via `Result` and `?`)
27/// is pervasive and has non-trivial cost. Instead, impls of this trait must
28/// implement a delayed error handling strategy. If a failure occurs, they
29/// should record this internally, and all subsequent encoding operations can
30/// be processed or ignored, whichever is appropriate. Then they should provide
31/// a `finish` method that finishes up encoding. If the encoder is fallible,
32/// `finish` should return a `Result` that indicates success or failure.
33///
34/// This current does not support `f32` nor `f64`, as they're not needed in any
35/// serialized data structures. That could be changed, but consider whether it
36/// really makes sense to store floating-point values at all.
37/// (If you need it, revert <https://github.com/rust-lang/rust/pull/109984>.)
38pub trait Encoder {
39    fn emit_usize(&mut self, v: usize);
40    fn emit_u128(&mut self, v: u128);
41    fn emit_u64(&mut self, v: u64);
42    fn emit_u32(&mut self, v: u32);
43    fn emit_u16(&mut self, v: u16);
44    fn emit_u8(&mut self, v: u8);
45
46    fn emit_isize(&mut self, v: isize);
47    fn emit_i128(&mut self, v: i128);
48    fn emit_i64(&mut self, v: i64);
49    fn emit_i32(&mut self, v: i32);
50    fn emit_i16(&mut self, v: i16);
51
52    #[inline]
53    fn emit_i8(&mut self, v: i8) {
54        self.emit_u8(v as u8);
55    }
56
57    #[inline]
58    fn emit_bool(&mut self, v: bool) {
59        self.emit_u8(if v { 1 } else { 0 });
60    }
61
62    #[inline]
63    fn emit_char(&mut self, v: char) {
64        self.emit_u32(v as u32);
65    }
66
67    #[inline]
68    fn emit_str(&mut self, v: &str) {
69        self.emit_usize(v.len());
70        self.emit_raw_bytes(v.as_bytes());
71        self.emit_u8(STR_SENTINEL);
72    }
73
74    fn emit_raw_bytes(&mut self, s: &[u8]);
75}
76
77// Note: all the methods in this trait are infallible, which may be surprising.
78// They used to be fallible (i.e. return a `Result`) but many of the impls just
79// panicked when something went wrong, and for the cases that didn't the
80// top-level invocation would also just panic on failure. Switching to
81// infallibility made things faster and lots of code a little simpler and more
82// concise.
83///
84/// This current does not support `f32` nor `f64`, as they're not needed in any
85/// serialized data structures. That could be changed, but consider whether it
86/// really makes sense to store floating-point values at all.
87/// (If you need it, revert <https://github.com/rust-lang/rust/pull/109984>.)
88pub trait Decoder {
89    fn read_usize(&mut self) -> usize;
90    fn read_u128(&mut self) -> u128;
91    fn read_u64(&mut self) -> u64;
92    fn read_u32(&mut self) -> u32;
93    fn read_u16(&mut self) -> u16;
94    fn read_u8(&mut self) -> u8;
95
96    fn read_isize(&mut self) -> isize;
97    fn read_i128(&mut self) -> i128;
98    fn read_i64(&mut self) -> i64;
99    fn read_i32(&mut self) -> i32;
100    fn read_i16(&mut self) -> i16;
101
102    #[inline]
103    fn read_i8(&mut self) -> i8 {
104        self.read_u8() as i8
105    }
106
107    #[inline]
108    fn read_bool(&mut self) -> bool {
109        let value = self.read_u8();
110        value != 0
111    }
112
113    #[inline]
114    fn read_char(&mut self) -> char {
115        let bits = self.read_u32();
116        std::char::from_u32(bits).unwrap()
117    }
118
119    #[inline]
120    fn read_str(&mut self) -> &str {
121        let len = self.read_usize();
122        let bytes = self.read_raw_bytes(len + 1);
123        assert!(bytes[len] == STR_SENTINEL);
124        unsafe { std::str::from_utf8_unchecked(&bytes[..len]) }
125    }
126
127    fn read_raw_bytes(&mut self, len: usize) -> &[u8];
128
129    fn peek_byte(&self) -> u8;
130    fn position(&self) -> usize;
131}
132
133/// Trait for types that can be serialized
134///
135/// This can be implemented using the `Encodable`, `TyEncodable` and
136/// `MetadataEncodable` macros.
137///
138/// * `Encodable` should be used in crates that don't depend on
139///   `rustc_middle`.
140/// * `MetadataEncodable` is used in `rustc_metadata` for types that contain
141///   `rustc_metadata::rmeta::Lazy`.
142/// * `TyEncodable` should be used for types that are only serialized in crate
143///   metadata or the incremental cache. This is most types in `rustc_middle`.
144pub trait Encodable<S: Encoder> {
145    fn encode(&self, s: &mut S);
146}
147
148/// Trait for types that can be deserialized
149///
150/// This can be implemented using the `Decodable`, `TyDecodable` and
151/// `MetadataDecodable` macros.
152///
153/// * `Decodable` should be used in crates that don't depend on
154///   `rustc_middle`.
155/// * `MetadataDecodable` is used in `rustc_metadata` for types that contain
156///   `rustc_metadata::rmeta::Lazy`.
157/// * `TyDecodable` should be used for types that are only serialized in crate
158///   metadata or the incremental cache. This is most types in `rustc_middle`.
159pub trait Decodable<D: Decoder>: Sized {
160    fn decode(d: &mut D) -> Self;
161}
162
163macro_rules! direct_serialize_impls {
164    ($($ty:ident $emit_method:ident $read_method:ident),*) => {
165        $(
166            impl<S: Encoder> Encodable<S> for $ty {
167                fn encode(&self, s: &mut S) {
168                    s.$emit_method(*self);
169                }
170            }
171
172            impl<D: Decoder> Decodable<D> for $ty {
173                fn decode(d: &mut D) -> $ty {
174                    d.$read_method()
175                }
176            }
177        )*
178    }
179}
180
181direct_serialize_impls! {
182    usize emit_usize read_usize,
183    u8 emit_u8 read_u8,
184    u16 emit_u16 read_u16,
185    u32 emit_u32 read_u32,
186    u64 emit_u64 read_u64,
187    u128 emit_u128 read_u128,
188
189    isize emit_isize read_isize,
190    i8 emit_i8 read_i8,
191    i16 emit_i16 read_i16,
192    i32 emit_i32 read_i32,
193    i64 emit_i64 read_i64,
194    i128 emit_i128 read_i128,
195
196    bool emit_bool read_bool,
197    char emit_char read_char
198}
199
200impl<S: Encoder, T: ?Sized> Encodable<S> for &T
201where
202    T: Encodable<S>,
203{
204    fn encode(&self, s: &mut S) {
205        (**self).encode(s)
206    }
207}
208
209impl<S: Encoder> Encodable<S> for ! {
210    fn encode(&self, _s: &mut S) {
211        unreachable!();
212    }
213}
214
215impl<D: Decoder> Decodable<D> for ! {
216    fn decode(_d: &mut D) -> ! {
217        unreachable!()
218    }
219}
220
221impl<S: Encoder> Encodable<S> for NonZero<u32> {
222    fn encode(&self, s: &mut S) {
223        s.emit_u32(self.get());
224    }
225}
226
227impl<D: Decoder> Decodable<D> for NonZero<u32> {
228    fn decode(d: &mut D) -> Self {
229        NonZero::new(d.read_u32()).unwrap()
230    }
231}
232
233impl<S: Encoder> Encodable<S> for str {
234    fn encode(&self, s: &mut S) {
235        s.emit_str(self);
236    }
237}
238
239impl<S: Encoder> Encodable<S> for String {
240    fn encode(&self, s: &mut S) {
241        s.emit_str(&self[..]);
242    }
243}
244
245impl<D: Decoder> Decodable<D> for String {
246    fn decode(d: &mut D) -> String {
247        d.read_str().to_owned()
248    }
249}
250
251impl<S: Encoder> Encodable<S> for () {
252    fn encode(&self, _s: &mut S) {}
253}
254
255impl<D: Decoder> Decodable<D> for () {
256    fn decode(_: &mut D) {}
257}
258
259impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
260    fn encode(&self, _s: &mut S) {}
261}
262
263impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
264    fn decode(_: &mut D) -> PhantomData<T> {
265        PhantomData
266    }
267}
268
269impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> {
270    fn decode(d: &mut D) -> Box<[T]> {
271        let v: Vec<T> = Decodable::decode(d);
272        v.into_boxed_slice()
273    }
274}
275
276impl<S: Encoder, T: Encodable<S>> Encodable<S> for Rc<T> {
277    fn encode(&self, s: &mut S) {
278        (**self).encode(s);
279    }
280}
281
282impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> {
283    fn decode(d: &mut D) -> Rc<T> {
284        Rc::new(Decodable::decode(d))
285    }
286}
287
288impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
289    default fn encode(&self, s: &mut S) {
290        s.emit_usize(self.len());
291        for e in self {
292            e.encode(s);
293        }
294    }
295}
296
297impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
298    fn encode(&self, s: &mut S) {
299        self.as_slice().encode(s);
300    }
301}
302
303impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
304    default fn decode(d: &mut D) -> Vec<T> {
305        let len = d.read_usize();
306        (0..len).map(|_| Decodable::decode(d)).collect()
307    }
308}
309
310impl<S: Encoder, T: Encodable<S>, const N: usize> Encodable<S> for [T; N] {
311    fn encode(&self, s: &mut S) {
312        self.as_slice().encode(s);
313    }
314}
315
316impl<D: Decoder, const N: usize> Decodable<D> for [u8; N] {
317    fn decode(d: &mut D) -> [u8; N] {
318        let len = d.read_usize();
319        assert!(len == N);
320        let mut v = [0u8; N];
321        for i in 0..len {
322            v[i] = Decodable::decode(d);
323        }
324        v
325    }
326}
327
328impl<S: Encoder, T: Encodable<S>> Encodable<S> for Cow<'_, [T]>
329where
330    [T]: ToOwned<Owned = Vec<T>>,
331{
332    fn encode(&self, s: &mut S) {
333        let slice: &[T] = self;
334        slice.encode(s);
335    }
336}
337
338impl<D: Decoder, T: Decodable<D> + ToOwned> Decodable<D> for Cow<'static, [T]>
339where
340    [T]: ToOwned<Owned = Vec<T>>,
341{
342    fn decode(d: &mut D) -> Cow<'static, [T]> {
343        let v: Vec<T> = Decodable::decode(d);
344        Cow::Owned(v)
345    }
346}
347
348impl<S: Encoder> Encodable<S> for Cow<'_, str> {
349    fn encode(&self, s: &mut S) {
350        let val: &str = self;
351        val.encode(s)
352    }
353}
354
355impl<D: Decoder> Decodable<D> for Cow<'_, str> {
356    fn decode(d: &mut D) -> Cow<'static, str> {
357        let v: String = Decodable::decode(d);
358        Cow::Owned(v)
359    }
360}
361
362impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> {
363    fn encode(&self, s: &mut S) {
364        match *self {
365            None => s.emit_u8(0),
366            Some(ref v) => {
367                s.emit_u8(1);
368                v.encode(s);
369            }
370        }
371    }
372}
373
374impl<D: Decoder, T: Decodable<D>> Decodable<D> for Option<T> {
375    fn decode(d: &mut D) -> Option<T> {
376        match d.read_u8() {
377            0 => None,
378            1 => Some(Decodable::decode(d)),
379            _ => panic!("Encountered invalid discriminant while decoding `Option`."),
380        }
381    }
382}
383
384impl<S: Encoder, T1: Encodable<S>, T2: Encodable<S>> Encodable<S> for Result<T1, T2> {
385    fn encode(&self, s: &mut S) {
386        match *self {
387            Ok(ref v) => {
388                s.emit_u8(0);
389                v.encode(s);
390            }
391            Err(ref v) => {
392                s.emit_u8(1);
393                v.encode(s);
394            }
395        }
396    }
397}
398
399impl<D: Decoder, T1: Decodable<D>, T2: Decodable<D>> Decodable<D> for Result<T1, T2> {
400    fn decode(d: &mut D) -> Result<T1, T2> {
401        match d.read_u8() {
402            0 => Ok(T1::decode(d)),
403            1 => Err(T2::decode(d)),
404            _ => panic!("Encountered invalid discriminant while decoding `Result`."),
405        }
406    }
407}
408
409macro_rules! peel {
410    ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
411}
412
413macro_rules! tuple {
414    () => ();
415    ( $($name:ident,)+ ) => (
416        impl<D: Decoder, $($name: Decodable<D>),+> Decodable<D> for ($($name,)+) {
417            fn decode(d: &mut D) -> ($($name,)+) {
418                ($({ let element: $name = Decodable::decode(d); element },)+)
419            }
420        }
421        impl<S: Encoder, $($name: Encodable<S>),+> Encodable<S> for ($($name,)+) {
422            #[allow(non_snake_case)]
423            fn encode(&self, s: &mut S) {
424                let ($(ref $name,)+) = *self;
425                $($name.encode(s);)+
426            }
427        }
428        peel! { $($name,)+ }
429    )
430}
431
432tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
433
434impl<S: Encoder> Encodable<S> for path::Path {
435    fn encode(&self, e: &mut S) {
436        self.to_str().unwrap().encode(e);
437    }
438}
439
440impl<S: Encoder> Encodable<S> for path::PathBuf {
441    fn encode(&self, e: &mut S) {
442        path::Path::encode(self, e);
443    }
444}
445
446impl<D: Decoder> Decodable<D> for path::PathBuf {
447    fn decode(d: &mut D) -> path::PathBuf {
448        let bytes: String = Decodable::decode(d);
449        path::PathBuf::from(bytes)
450    }
451}
452
453impl<S: Encoder, T: Encodable<S> + Copy> Encodable<S> for Cell<T> {
454    fn encode(&self, s: &mut S) {
455        self.get().encode(s);
456    }
457}
458
459impl<D: Decoder, T: Decodable<D> + Copy> Decodable<D> for Cell<T> {
460    fn decode(d: &mut D) -> Cell<T> {
461        Cell::new(Decodable::decode(d))
462    }
463}
464
465impl<S: Encoder, T: Encodable<S>> Encodable<S> for RefCell<T> {
466    fn encode(&self, s: &mut S) {
467        self.borrow().encode(s);
468    }
469}
470
471impl<D: Decoder, T: Decodable<D>> Decodable<D> for RefCell<T> {
472    fn decode(d: &mut D) -> RefCell<T> {
473        RefCell::new(Decodable::decode(d))
474    }
475}
476
477impl<S: Encoder, T: Encodable<S>> Encodable<S> for Arc<T> {
478    fn encode(&self, s: &mut S) {
479        (**self).encode(s);
480    }
481}
482
483impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
484    fn decode(d: &mut D) -> Arc<T> {
485        Arc::new(Decodable::decode(d))
486    }
487}
488
489impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> {
490    fn encode(&self, s: &mut S) {
491        (**self).encode(s)
492    }
493}
494
495impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
496    fn decode(d: &mut D) -> Box<T> {
497        Box::new(Decodable::decode(d))
498    }
499}
500
501impl<S: Encoder, A: Array<Item: Encodable<S>>> Encodable<S> for SmallVec<A> {
502    fn encode(&self, s: &mut S) {
503        self.as_slice().encode(s);
504    }
505}
506
507impl<D: Decoder, A: Array<Item: Decodable<D>>> Decodable<D> for SmallVec<A> {
508    fn decode(d: &mut D) -> SmallVec<A> {
509        let len = d.read_usize();
510        (0..len).map(|_| Decodable::decode(d)).collect()
511    }
512}
513
514impl<S: Encoder, T: Encodable<S>> Encodable<S> for ThinVec<T> {
515    fn encode(&self, s: &mut S) {
516        self.as_slice().encode(s);
517    }
518}
519
520impl<D: Decoder, T: Decodable<D>> Decodable<D> for ThinVec<T> {
521    fn decode(d: &mut D) -> ThinVec<T> {
522        let len = d.read_usize();
523        (0..len).map(|_| Decodable::decode(d)).collect()
524    }
525}
526
527impl<S: Encoder, T: Encodable<S>> Encodable<S> for VecDeque<T> {
528    fn encode(&self, s: &mut S) {
529        s.emit_usize(self.len());
530        for e in self {
531            e.encode(s);
532        }
533    }
534}
535
536impl<D: Decoder, T: Decodable<D>> Decodable<D> for VecDeque<T> {
537    fn decode(d: &mut D) -> VecDeque<T> {
538        let len = d.read_usize();
539        (0..len).map(|_| Decodable::decode(d)).collect()
540    }
541}
542
543impl<S: Encoder, K, V> Encodable<S> for BTreeMap<K, V>
544where
545    K: Encodable<S> + PartialEq + Ord,
546    V: Encodable<S>,
547{
548    fn encode(&self, e: &mut S) {
549        e.emit_usize(self.len());
550        for (key, val) in self {
551            key.encode(e);
552            val.encode(e);
553        }
554    }
555}
556
557impl<D: Decoder, K, V> Decodable<D> for BTreeMap<K, V>
558where
559    K: Decodable<D> + PartialEq + Ord,
560    V: Decodable<D>,
561{
562    fn decode(d: &mut D) -> BTreeMap<K, V> {
563        let len = d.read_usize();
564        (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
565    }
566}
567
568impl<S: Encoder, T> Encodable<S> for BTreeSet<T>
569where
570    T: Encodable<S> + PartialEq + Ord,
571{
572    fn encode(&self, s: &mut S) {
573        s.emit_usize(self.len());
574        for e in self {
575            e.encode(s);
576        }
577    }
578}
579
580impl<D: Decoder, T> Decodable<D> for BTreeSet<T>
581where
582    T: Decodable<D> + PartialEq + Ord,
583{
584    fn decode(d: &mut D) -> BTreeSet<T> {
585        let len = d.read_usize();
586        (0..len).map(|_| Decodable::decode(d)).collect()
587    }
588}
589
590impl<E: Encoder, K, V, S> Encodable<E> for HashMap<K, V, S>
591where
592    K: Encodable<E> + Eq,
593    V: Encodable<E>,
594    S: BuildHasher,
595{
596    fn encode(&self, e: &mut E) {
597        e.emit_usize(self.len());
598        for (key, val) in self {
599            key.encode(e);
600            val.encode(e);
601        }
602    }
603}
604
605impl<D: Decoder, K, V, S> Decodable<D> for HashMap<K, V, S>
606where
607    K: Decodable<D> + Hash + Eq,
608    V: Decodable<D>,
609    S: BuildHasher + Default,
610{
611    fn decode(d: &mut D) -> HashMap<K, V, S> {
612        let len = d.read_usize();
613        (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
614    }
615}
616
617impl<E: Encoder, T, S> Encodable<E> for HashSet<T, S>
618where
619    T: Encodable<E> + Eq,
620    S: BuildHasher,
621{
622    fn encode(&self, s: &mut E) {
623        s.emit_usize(self.len());
624        for e in self {
625            e.encode(s);
626        }
627    }
628}
629
630impl<D: Decoder, T, S> Decodable<D> for HashSet<T, S>
631where
632    T: Decodable<D> + Hash + Eq,
633    S: BuildHasher + Default,
634{
635    fn decode(d: &mut D) -> HashSet<T, S> {
636        let len = d.read_usize();
637        (0..len).map(|_| Decodable::decode(d)).collect()
638    }
639}
640
641impl<E: Encoder, K, V, S> Encodable<E> for indexmap::IndexMap<K, V, S>
642where
643    K: Encodable<E> + Hash + Eq,
644    V: Encodable<E>,
645    S: BuildHasher,
646{
647    fn encode(&self, e: &mut E) {
648        e.emit_usize(self.len());
649        for (key, val) in self {
650            key.encode(e);
651            val.encode(e);
652        }
653    }
654}
655
656impl<D: Decoder, K, V, S> Decodable<D> for indexmap::IndexMap<K, V, S>
657where
658    K: Decodable<D> + Hash + Eq,
659    V: Decodable<D>,
660    S: BuildHasher + Default,
661{
662    fn decode(d: &mut D) -> indexmap::IndexMap<K, V, S> {
663        let len = d.read_usize();
664        (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
665    }
666}
667
668impl<E: Encoder, T, S> Encodable<E> for indexmap::IndexSet<T, S>
669where
670    T: Encodable<E> + Hash + Eq,
671    S: BuildHasher,
672{
673    fn encode(&self, s: &mut E) {
674        s.emit_usize(self.len());
675        for e in self {
676            e.encode(s);
677        }
678    }
679}
680
681impl<D: Decoder, T, S> Decodable<D> for indexmap::IndexSet<T, S>
682where
683    T: Decodable<D> + Hash + Eq,
684    S: BuildHasher + Default,
685{
686    fn decode(d: &mut D) -> indexmap::IndexSet<T, S> {
687        let len = d.read_usize();
688        (0..len).map(|_| Decodable::decode(d)).collect()
689    }
690}
691
692impl<E: Encoder, T: Encodable<E>> Encodable<E> for Rc<[T]> {
693    fn encode(&self, s: &mut E) {
694        let slice: &[T] = self;
695        slice.encode(s);
696    }
697}
698
699impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<[T]> {
700    fn decode(d: &mut D) -> Rc<[T]> {
701        let vec: Vec<T> = Decodable::decode(d);
702        vec.into()
703    }
704}
705
706impl<E: Encoder, T: Encodable<E>> Encodable<E> for Arc<[T]> {
707    fn encode(&self, s: &mut E) {
708        let slice: &[T] = self;
709        slice.encode(s);
710    }
711}
712
713impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<[T]> {
714    fn decode(d: &mut D) -> Arc<[T]> {
715        let vec: Vec<T> = Decodable::decode(d);
716        vec.into()
717    }
718}