//! Support code for encoding and decoding types. use smallvec::{Array, SmallVec}; use std::borrow::Cow; use std::cell::{Cell, RefCell}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; use std::hash::{BuildHasher, Hash}; use std::marker::PhantomData; use std::path; use std::rc::Rc; use std::sync::Arc; use thin_vec::ThinVec; /// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string. /// This way we can skip validation and still be relatively sure that deserialization /// did not desynchronize. /// /// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout const STR_SENTINEL: u8 = 0xC1; /// A note about error handling. /// /// Encoders may be fallible, but in practice failure is rare and there are so /// many nested calls that typical Rust error handling (via `Result` and `?`) /// is pervasive and has non-trivial cost. Instead, impls of this trait must /// implement a delayed error handling strategy. If a failure occurs, they /// should record this internally, and all subsequent encoding operations can /// be processed or ignored, whichever is appropriate. Then they should provide /// a `finish` method that finishes up encoding. If the encoder is fallible, /// `finish` should return a `Result` that indicates success or failure. /// /// This current does not support `f32` nor `f64`, as they're not needed in any /// serialized data structures. That could be changed, but consider whether it /// really makes sense to store floating-point values at all. /// (If you need it, revert .) pub trait Encoder { fn emit_usize(&mut self, v: usize); fn emit_u128(&mut self, v: u128); fn emit_u64(&mut self, v: u64); fn emit_u32(&mut self, v: u32); fn emit_u16(&mut self, v: u16); fn emit_u8(&mut self, v: u8); fn emit_isize(&mut self, v: isize); fn emit_i128(&mut self, v: i128); fn emit_i64(&mut self, v: i64); fn emit_i32(&mut self, v: i32); fn emit_i16(&mut self, v: i16); #[inline] fn emit_i8(&mut self, v: i8) { self.emit_u8(v as u8); } #[inline] fn emit_bool(&mut self, v: bool) { self.emit_u8(if v { 1 } else { 0 }); } #[inline] fn emit_char(&mut self, v: char) { self.emit_u32(v as u32); } #[inline] fn emit_str(&mut self, v: &str) { self.emit_usize(v.len()); self.emit_raw_bytes(v.as_bytes()); self.emit_u8(STR_SENTINEL); } fn emit_raw_bytes(&mut self, s: &[u8]); fn emit_enum_variant(&mut self, v_id: usize, f: F) where F: FnOnce(&mut Self), { self.emit_usize(v_id); f(self); } } // Note: all the methods in this trait are infallible, which may be surprising. // They used to be fallible (i.e. return a `Result`) but many of the impls just // panicked when something went wrong, and for the cases that didn't the // top-level invocation would also just panic on failure. Switching to // infallibility made things faster and lots of code a little simpler and more // concise. /// /// This current does not support `f32` nor `f64`, as they're not needed in any /// serialized data structures. That could be changed, but consider whether it /// really makes sense to store floating-point values at all. /// (If you need it, revert .) pub trait Decoder { fn read_usize(&mut self) -> usize; fn read_u128(&mut self) -> u128; fn read_u64(&mut self) -> u64; fn read_u32(&mut self) -> u32; fn read_u16(&mut self) -> u16; fn read_u8(&mut self) -> u8; fn read_isize(&mut self) -> isize; fn read_i128(&mut self) -> i128; fn read_i64(&mut self) -> i64; fn read_i32(&mut self) -> i32; fn read_i16(&mut self) -> i16; #[inline] fn read_i8(&mut self) -> i8 { self.read_u8() as i8 } #[inline] fn read_bool(&mut self) -> bool { let value = self.read_u8(); value != 0 } #[inline] fn read_char(&mut self) -> char { let bits = self.read_u32(); std::char::from_u32(bits).unwrap() } #[inline] fn read_str(&mut self) -> &str { let len = self.read_usize(); let bytes = self.read_raw_bytes(len + 1); assert!(bytes[len] == STR_SENTINEL); unsafe { std::str::from_utf8_unchecked(&bytes[..len]) } } fn read_raw_bytes(&mut self, len: usize) -> &[u8]; // Although there is an `emit_enum_variant` method in `Encoder`, the code // patterns in decoding are different enough to encoding that there is no // need for a corresponding `read_enum_variant` method here. fn peek_byte(&self) -> u8; fn position(&self) -> usize; } /// Trait for types that can be serialized /// /// This can be implemented using the `Encodable`, `TyEncodable` and /// `MetadataEncodable` macros. /// /// * `Encodable` should be used in crates that don't depend on /// `rustc_middle`. /// * `MetadataEncodable` is used in `rustc_metadata` for types that contain /// `rustc_metadata::rmeta::Lazy`. /// * `TyEncodable` should be used for types that are only serialized in crate /// metadata or the incremental cache. This is most types in `rustc_middle`. pub trait Encodable { fn encode(&self, s: &mut S); } /// Trait for types that can be deserialized /// /// This can be implemented using the `Decodable`, `TyDecodable` and /// `MetadataDecodable` macros. /// /// * `Decodable` should be used in crates that don't depend on /// `rustc_middle`. /// * `MetadataDecodable` is used in `rustc_metadata` for types that contain /// `rustc_metadata::rmeta::Lazy`. /// * `TyDecodable` should be used for types that are only serialized in crate /// metadata or the incremental cache. This is most types in `rustc_middle`. pub trait Decodable: Sized { fn decode(d: &mut D) -> Self; } macro_rules! direct_serialize_impls { ($($ty:ident $emit_method:ident $read_method:ident),*) => { $( impl Encodable for $ty { fn encode(&self, s: &mut S) { s.$emit_method(*self); } } impl Decodable for $ty { fn decode(d: &mut D) -> $ty { d.$read_method() } } )* } } direct_serialize_impls! { usize emit_usize read_usize, u8 emit_u8 read_u8, u16 emit_u16 read_u16, u32 emit_u32 read_u32, u64 emit_u64 read_u64, u128 emit_u128 read_u128, isize emit_isize read_isize, i8 emit_i8 read_i8, i16 emit_i16 read_i16, i32 emit_i32 read_i32, i64 emit_i64 read_i64, i128 emit_i128 read_i128, bool emit_bool read_bool, char emit_char read_char } impl Encodable for &T where T: Encodable, { fn encode(&self, s: &mut S) { (**self).encode(s) } } impl Encodable for ! { fn encode(&self, _s: &mut S) { unreachable!(); } } impl Decodable for ! { fn decode(_d: &mut D) -> ! { unreachable!() } } impl Encodable for ::std::num::NonZeroU32 { fn encode(&self, s: &mut S) { s.emit_u32(self.get()); } } impl Decodable for ::std::num::NonZeroU32 { fn decode(d: &mut D) -> Self { ::std::num::NonZeroU32::new(d.read_u32()).unwrap() } } impl Encodable for str { fn encode(&self, s: &mut S) { s.emit_str(self); } } impl Encodable for String { fn encode(&self, s: &mut S) { s.emit_str(&self[..]); } } impl Decodable for String { fn decode(d: &mut D) -> String { d.read_str().to_owned() } } impl Encodable for () { fn encode(&self, _s: &mut S) {} } impl Decodable for () { fn decode(_: &mut D) -> () {} } impl Encodable for PhantomData { fn encode(&self, _s: &mut S) {} } impl Decodable for PhantomData { fn decode(_: &mut D) -> PhantomData { PhantomData } } impl> Decodable for Box<[T]> { fn decode(d: &mut D) -> Box<[T]> { let v: Vec = Decodable::decode(d); v.into_boxed_slice() } } impl> Encodable for Rc { fn encode(&self, s: &mut S) { (**self).encode(s); } } impl> Decodable for Rc { fn decode(d: &mut D) -> Rc { Rc::new(Decodable::decode(d)) } } impl> Encodable for [T] { default fn encode(&self, s: &mut S) { s.emit_usize(self.len()); for e in self.iter() { e.encode(s); } } } impl> Encodable for Vec { fn encode(&self, s: &mut S) { self.as_slice().encode(s); } } impl> Decodable for Vec { default fn decode(d: &mut D) -> Vec { let len = d.read_usize(); (0..len).map(|_| Decodable::decode(d)).collect() } } impl, const N: usize> Encodable for [T; N] { fn encode(&self, s: &mut S) { self.as_slice().encode(s); } } impl Decodable for [u8; N] { fn decode(d: &mut D) -> [u8; N] { let len = d.read_usize(); assert!(len == N); let mut v = [0u8; N]; for i in 0..len { v[i] = Decodable::decode(d); } v } } impl<'a, S: Encoder, T: Encodable> Encodable for Cow<'a, [T]> where [T]: ToOwned>, { fn encode(&self, s: &mut S) { let slice: &[T] = self; slice.encode(s); } } impl + ToOwned> Decodable for Cow<'static, [T]> where [T]: ToOwned>, { fn decode(d: &mut D) -> Cow<'static, [T]> { let v: Vec = Decodable::decode(d); Cow::Owned(v) } } impl<'a, S: Encoder> Encodable for Cow<'a, str> { fn encode(&self, s: &mut S) { let val: &str = self; val.encode(s) } } impl<'a, D: Decoder> Decodable for Cow<'a, str> { fn decode(d: &mut D) -> Cow<'static, str> { let v: String = Decodable::decode(d); Cow::Owned(v) } } impl> Encodable for Option { fn encode(&self, s: &mut S) { match *self { None => s.emit_enum_variant(0, |_| {}), Some(ref v) => s.emit_enum_variant(1, |s| v.encode(s)), } } } impl> Decodable for Option { fn decode(d: &mut D) -> Option { match d.read_usize() { 0 => None, 1 => Some(Decodable::decode(d)), _ => panic!("Encountered invalid discriminant while decoding `Option`."), } } } impl, T2: Encodable> Encodable for Result { fn encode(&self, s: &mut S) { match *self { Ok(ref v) => s.emit_enum_variant(0, |s| v.encode(s)), Err(ref v) => s.emit_enum_variant(1, |s| v.encode(s)), } } } impl, T2: Decodable> Decodable for Result { fn decode(d: &mut D) -> Result { match d.read_usize() { 0 => Ok(T1::decode(d)), 1 => Err(T2::decode(d)), _ => panic!("Encountered invalid discriminant while decoding `Result`."), } } } macro_rules! peel { ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* }) } macro_rules! tuple { () => (); ( $($name:ident,)+ ) => ( impl),+> Decodable for ($($name,)+) { fn decode(d: &mut D) -> ($($name,)+) { ($({ let element: $name = Decodable::decode(d); element },)+) } } impl),+> Encodable for ($($name,)+) { #[allow(non_snake_case)] fn encode(&self, s: &mut S) { let ($(ref $name,)+) = *self; $($name.encode(s);)+ } } peel! { $($name,)+ } ) } tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, } impl Encodable for path::Path { fn encode(&self, e: &mut S) { self.to_str().unwrap().encode(e); } } impl Encodable for path::PathBuf { fn encode(&self, e: &mut S) { path::Path::encode(self, e); } } impl Decodable for path::PathBuf { fn decode(d: &mut D) -> path::PathBuf { let bytes: String = Decodable::decode(d); path::PathBuf::from(bytes) } } impl + Copy> Encodable for Cell { fn encode(&self, s: &mut S) { self.get().encode(s); } } impl + Copy> Decodable for Cell { fn decode(d: &mut D) -> Cell { Cell::new(Decodable::decode(d)) } } impl> Encodable for RefCell { fn encode(&self, s: &mut S) { self.borrow().encode(s); } } impl> Decodable for RefCell { fn decode(d: &mut D) -> RefCell { RefCell::new(Decodable::decode(d)) } } impl> Encodable for Arc { fn encode(&self, s: &mut S) { (**self).encode(s); } } impl> Decodable for Arc { fn decode(d: &mut D) -> Arc { Arc::new(Decodable::decode(d)) } } impl> Encodable for Box { fn encode(&self, s: &mut S) { (**self).encode(s) } } impl> Decodable for Box { fn decode(d: &mut D) -> Box { Box::new(Decodable::decode(d)) } } impl>> Encodable for SmallVec { fn encode(&self, s: &mut S) { self.as_slice().encode(s); } } impl>> Decodable for SmallVec { fn decode(d: &mut D) -> SmallVec { let len = d.read_usize(); (0..len).map(|_| Decodable::decode(d)).collect() } } impl> Encodable for ThinVec { fn encode(&self, s: &mut S) { self.as_slice().encode(s); } } impl> Decodable for ThinVec { fn decode(d: &mut D) -> ThinVec { let len = d.read_usize(); (0..len).map(|_| Decodable::decode(d)).collect() } } impl> Encodable for VecDeque { fn encode(&self, s: &mut S) { s.emit_usize(self.len()); for e in self.iter() { e.encode(s); } } } impl> Decodable for VecDeque { fn decode(d: &mut D) -> VecDeque { let len = d.read_usize(); (0..len).map(|_| Decodable::decode(d)).collect() } } impl Encodable for BTreeMap where K: Encodable + PartialEq + Ord, V: Encodable, { fn encode(&self, e: &mut S) { e.emit_usize(self.len()); for (key, val) in self.iter() { key.encode(e); val.encode(e); } } } impl Decodable for BTreeMap where K: Decodable + PartialEq + Ord, V: Decodable, { fn decode(d: &mut D) -> BTreeMap { let len = d.read_usize(); (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect() } } impl Encodable for BTreeSet where T: Encodable + PartialEq + Ord, { fn encode(&self, s: &mut S) { s.emit_usize(self.len()); for e in self.iter() { e.encode(s); } } } impl Decodable for BTreeSet where T: Decodable + PartialEq + Ord, { fn decode(d: &mut D) -> BTreeSet { let len = d.read_usize(); (0..len).map(|_| Decodable::decode(d)).collect() } } impl Encodable for HashMap where K: Encodable + Eq, V: Encodable, S: BuildHasher, { fn encode(&self, e: &mut E) { e.emit_usize(self.len()); for (key, val) in self.iter() { key.encode(e); val.encode(e); } } } impl Decodable for HashMap where K: Decodable + Hash + Eq, V: Decodable, S: BuildHasher + Default, { fn decode(d: &mut D) -> HashMap { let len = d.read_usize(); (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect() } } impl Encodable for HashSet where T: Encodable + Eq, S: BuildHasher, { fn encode(&self, s: &mut E) { s.emit_usize(self.len()); for e in self.iter() { e.encode(s); } } } impl Decodable for HashSet where T: Decodable + Hash + Eq, S: BuildHasher + Default, { fn decode(d: &mut D) -> HashSet { let len = d.read_usize(); (0..len).map(|_| Decodable::decode(d)).collect() } } impl Encodable for indexmap::IndexMap where K: Encodable + Hash + Eq, V: Encodable, S: BuildHasher, { fn encode(&self, e: &mut E) { e.emit_usize(self.len()); for (key, val) in self.iter() { key.encode(e); val.encode(e); } } } impl Decodable for indexmap::IndexMap where K: Decodable + Hash + Eq, V: Decodable, S: BuildHasher + Default, { fn decode(d: &mut D) -> indexmap::IndexMap { let len = d.read_usize(); (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect() } } impl Encodable for indexmap::IndexSet where T: Encodable + Hash + Eq, S: BuildHasher, { fn encode(&self, s: &mut E) { s.emit_usize(self.len()); for e in self.iter() { e.encode(s); } } } impl Decodable for indexmap::IndexSet where T: Decodable + Hash + Eq, S: BuildHasher + Default, { fn decode(d: &mut D) -> indexmap::IndexSet { let len = d.read_usize(); (0..len).map(|_| Decodable::decode(d)).collect() } } impl> Encodable for Rc<[T]> { fn encode(&self, s: &mut E) { let slice: &[T] = self; slice.encode(s); } } impl> Decodable for Rc<[T]> { fn decode(d: &mut D) -> Rc<[T]> { let vec: Vec = Decodable::decode(d); vec.into() } } impl> Encodable for Arc<[T]> { fn encode(&self, s: &mut E) { let slice: &[T] = self; slice.encode(s); } } impl> Decodable for Arc<[T]> { fn decode(d: &mut D) -> Arc<[T]> { let vec: Vec = Decodable::decode(d); vec.into() } }