diff options
| author | Matthew Jasper <mjjasper1@gmail.com> | 2020-06-11 15:49:57 +0100 |
|---|---|---|
| committer | Matthew Jasper <mjjasper1@gmail.com> | 2020-08-14 17:34:30 +0100 |
| commit | cbcef3effcf64bd0d89ea2dc8973e18d7fcf94c3 (patch) | |
| tree | cf11d5e77bdfe87b0a19d5f93ff251b256770325 /src/librustc_serialize | |
| parent | 55b9adfafa11b2ced5c0477c949fd875b19b3877 (diff) | |
Rework `rustc_serialize`
- Move the type parameter from `encode` and `decode` methods to the trait. - Remove `UseSpecialized(En|De)codable` traits. - Remove blanket impls for references. - Add `RefDecodable` trait to allow deserializing to arena-allocated references safely. - Remove ability to (de)serialize HIR. - Create proc-macros `(Ty)?(En|De)codable` to help implement these new traits.
Diffstat (limited to 'src/librustc_serialize')
| -rw-r--r-- | src/librustc_serialize/collection_impls.rs | 134 | ||||
| -rw-r--r-- | src/librustc_serialize/json.rs | 14 | ||||
| -rw-r--r-- | src/librustc_serialize/lib.rs | 4 | ||||
| -rw-r--r-- | src/librustc_serialize/serialize.rs | 386 |
4 files changed, 226 insertions, 312 deletions
diff --git a/src/librustc_serialize/collection_impls.rs b/src/librustc_serialize/collection_impls.rs index 49b8094abd0..3d274cb0150 100644 --- a/src/librustc_serialize/collection_impls.rs +++ b/src/librustc_serialize/collection_impls.rs @@ -9,8 +9,8 @@ use std::sync::Arc; use smallvec::{Array, SmallVec}; -impl<A: Array<Item: Encodable>> Encodable for SmallVec<A> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, A: Array<Item: Encodable<S>>> Encodable<S> for SmallVec<A> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; @@ -20,8 +20,8 @@ impl<A: Array<Item: Encodable>> Encodable for SmallVec<A> { } } -impl<A: Array<Item: Decodable>> Decodable for SmallVec<A> { - fn decode<D: Decoder>(d: &mut D) -> Result<SmallVec<A>, D::Error> { +impl<D: Decoder, A: Array<Item: Decodable<D>>> Decodable<D> for SmallVec<A> { + fn decode(d: &mut D) -> Result<SmallVec<A>, D::Error> { d.read_seq(|d, len| { let mut vec = SmallVec::with_capacity(len); // FIXME(#48994) - could just be collected into a Result<SmallVec, D::Error> @@ -33,8 +33,8 @@ impl<A: Array<Item: Decodable>> Decodable for SmallVec<A> { } } -impl<T: Encodable> Encodable for LinkedList<T> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T: Encodable<S>> Encodable<S> for LinkedList<T> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; @@ -44,8 +44,8 @@ impl<T: Encodable> Encodable for LinkedList<T> { } } -impl<T: Decodable> Decodable for LinkedList<T> { - fn decode<D: Decoder>(d: &mut D) -> Result<LinkedList<T>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for LinkedList<T> { + fn decode(d: &mut D) -> Result<LinkedList<T>, D::Error> { d.read_seq(|d, len| { let mut list = LinkedList::new(); for i in 0..len { @@ -56,8 +56,8 @@ impl<T: Decodable> Decodable for LinkedList<T> { } } -impl<T: Encodable> Encodable for VecDeque<T> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T: Encodable<S>> Encodable<S> for VecDeque<T> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; @@ -67,8 +67,8 @@ impl<T: Encodable> Encodable for VecDeque<T> { } } -impl<T: Decodable> Decodable for VecDeque<T> { - fn decode<D: Decoder>(d: &mut D) -> Result<VecDeque<T>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for VecDeque<T> { + fn decode(d: &mut D) -> Result<VecDeque<T>, D::Error> { d.read_seq(|d, len| { let mut deque: VecDeque<T> = VecDeque::with_capacity(len); for i in 0..len { @@ -79,12 +79,12 @@ impl<T: Decodable> Decodable for VecDeque<T> { } } -impl<K, V> Encodable for BTreeMap<K, V> +impl<S: Encoder, K, V> Encodable<S> for BTreeMap<K, V> where - K: Encodable + PartialEq + Ord, - V: Encodable, + K: Encodable<S> + PartialEq + Ord, + V: Encodable<S>, { - fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> { + fn encode(&self, e: &mut S) -> Result<(), S::Error> { e.emit_map(self.len(), |e| { for (i, (key, val)) in self.iter().enumerate() { e.emit_map_elt_key(i, |e| key.encode(e))?; @@ -95,12 +95,12 @@ where } } -impl<K, V> Decodable for BTreeMap<K, V> +impl<D: Decoder, K, V> Decodable<D> for BTreeMap<K, V> where - K: Decodable + PartialEq + Ord, - V: Decodable, + K: Decodable<D> + PartialEq + Ord, + V: Decodable<D>, { - fn decode<D: Decoder>(d: &mut D) -> Result<BTreeMap<K, V>, D::Error> { + fn decode(d: &mut D) -> Result<BTreeMap<K, V>, D::Error> { d.read_map(|d, len| { let mut map = BTreeMap::new(); for i in 0..len { @@ -113,11 +113,11 @@ where } } -impl<T> Encodable for BTreeSet<T> +impl<S: Encoder, T> Encodable<S> for BTreeSet<T> where - T: Encodable + PartialEq + Ord, + T: Encodable<S> + PartialEq + Ord, { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; @@ -127,11 +127,11 @@ where } } -impl<T> Decodable for BTreeSet<T> +impl<D: Decoder, T> Decodable<D> for BTreeSet<T> where - T: Decodable + PartialEq + Ord, + T: Decodable<D> + PartialEq + Ord, { - fn decode<D: Decoder>(d: &mut D) -> Result<BTreeSet<T>, D::Error> { + fn decode(d: &mut D) -> Result<BTreeSet<T>, D::Error> { d.read_seq(|d, len| { let mut set = BTreeSet::new(); for i in 0..len { @@ -142,13 +142,13 @@ where } } -impl<K, V, S> Encodable for HashMap<K, V, S> +impl<E: Encoder, K, V, S> Encodable<E> for HashMap<K, V, S> where - K: Encodable + Eq, - V: Encodable, + K: Encodable<E> + Eq, + V: Encodable<E>, S: BuildHasher, { - fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> { + fn encode(&self, e: &mut E) -> Result<(), E::Error> { e.emit_map(self.len(), |e| { for (i, (key, val)) in self.iter().enumerate() { e.emit_map_elt_key(i, |e| key.encode(e))?; @@ -159,13 +159,13 @@ where } } -impl<K, V, S> Decodable for HashMap<K, V, S> +impl<D: Decoder, K, V, S> Decodable<D> for HashMap<K, V, S> where - K: Decodable + Hash + Eq, - V: Decodable, + K: Decodable<D> + Hash + Eq, + V: Decodable<D>, S: BuildHasher + Default, { - fn decode<D: Decoder>(d: &mut D) -> Result<HashMap<K, V, S>, D::Error> { + fn decode(d: &mut D) -> Result<HashMap<K, V, S>, D::Error> { d.read_map(|d, len| { let state = Default::default(); let mut map = HashMap::with_capacity_and_hasher(len, state); @@ -179,12 +179,12 @@ where } } -impl<T, S> Encodable for HashSet<T, S> +impl<E: Encoder, T, S> Encodable<E> for HashSet<T, S> where - T: Encodable + Eq, + T: Encodable<E> + Eq, S: BuildHasher, { - fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> { + fn encode(&self, s: &mut E) -> Result<(), E::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; @@ -194,12 +194,22 @@ where } } -impl<T, S> Decodable for HashSet<T, S> +impl<E: Encoder, T, S> Encodable<E> for &HashSet<T, S> where - T: Decodable + Hash + Eq, + T: Encodable<E> + Eq, + S: BuildHasher, +{ + fn encode(&self, s: &mut E) -> Result<(), E::Error> { + (**self).encode(s) + } +} + +impl<D: Decoder, T, S> Decodable<D> for HashSet<T, S> +where + T: Decodable<D> + Hash + Eq, S: BuildHasher + Default, { - fn decode<D: Decoder>(d: &mut D) -> Result<HashSet<T, S>, D::Error> { + fn decode(d: &mut D) -> Result<HashSet<T, S>, D::Error> { d.read_seq(|d, len| { let state = Default::default(); let mut set = HashSet::with_capacity_and_hasher(len, state); @@ -211,13 +221,13 @@ where } } -impl<K, V, S> Encodable for indexmap::IndexMap<K, V, S> +impl<E: Encoder, K, V, S> Encodable<E> for indexmap::IndexMap<K, V, S> where - K: Encodable + Hash + Eq, - V: Encodable, + K: Encodable<E> + Hash + Eq, + V: Encodable<E>, S: BuildHasher, { - fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> { + fn encode(&self, e: &mut E) -> Result<(), E::Error> { e.emit_map(self.len(), |e| { for (i, (key, val)) in self.iter().enumerate() { e.emit_map_elt_key(i, |e| key.encode(e))?; @@ -228,13 +238,13 @@ where } } -impl<K, V, S> Decodable for indexmap::IndexMap<K, V, S> +impl<D: Decoder, K, V, S> Decodable<D> for indexmap::IndexMap<K, V, S> where - K: Decodable + Hash + Eq, - V: Decodable, + K: Decodable<D> + Hash + Eq, + V: Decodable<D>, S: BuildHasher + Default, { - fn decode<D: Decoder>(d: &mut D) -> Result<indexmap::IndexMap<K, V, S>, D::Error> { + fn decode(d: &mut D) -> Result<indexmap::IndexMap<K, V, S>, D::Error> { d.read_map(|d, len| { let state = Default::default(); let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state); @@ -248,12 +258,12 @@ where } } -impl<T, S> Encodable for indexmap::IndexSet<T, S> +impl<E: Encoder, T, S> Encodable<E> for indexmap::IndexSet<T, S> where - T: Encodable + Hash + Eq, + T: Encodable<E> + Hash + Eq, S: BuildHasher, { - fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> { + fn encode(&self, s: &mut E) -> Result<(), E::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; @@ -263,12 +273,12 @@ where } } -impl<T, S> Decodable for indexmap::IndexSet<T, S> +impl<D: Decoder, T, S> Decodable<D> for indexmap::IndexSet<T, S> where - T: Decodable + Hash + Eq, + T: Decodable<D> + Hash + Eq, S: BuildHasher + Default, { - fn decode<D: Decoder>(d: &mut D) -> Result<indexmap::IndexSet<T, S>, D::Error> { + fn decode(d: &mut D) -> Result<indexmap::IndexSet<T, S>, D::Error> { d.read_seq(|d, len| { let state = Default::default(); let mut set = indexmap::IndexSet::with_capacity_and_hasher(len, state); @@ -280,8 +290,8 @@ where } } -impl<T: Encodable> Encodable for Rc<[T]> { - fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> { +impl<E: Encoder, T: Encodable<E>> Encodable<E> for Rc<[T]> { + fn encode(&self, s: &mut E) -> Result<(), E::Error> { s.emit_seq(self.len(), |s| { for (index, e) in self.iter().enumerate() { s.emit_seq_elt(index, |s| e.encode(s))?; @@ -291,8 +301,8 @@ impl<T: Encodable> Encodable for Rc<[T]> { } } -impl<T: Decodable> Decodable for Rc<[T]> { - fn decode<D: Decoder>(d: &mut D) -> Result<Rc<[T]>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<[T]> { + fn decode(d: &mut D) -> Result<Rc<[T]>, D::Error> { d.read_seq(|d, len| { let mut vec = Vec::with_capacity(len); for index in 0..len { @@ -303,8 +313,8 @@ impl<T: Decodable> Decodable for Rc<[T]> { } } -impl<T: Encodable> Encodable for Arc<[T]> { - fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> { +impl<E: Encoder, T: Encodable<E>> Encodable<E> for Arc<[T]> { + fn encode(&self, s: &mut E) -> Result<(), E::Error> { s.emit_seq(self.len(), |s| { for (index, e) in self.iter().enumerate() { s.emit_seq_elt(index, |s| e.encode(s))?; @@ -314,8 +324,8 @@ impl<T: Encodable> Encodable for Arc<[T]> { } } -impl<T: Decodable> Decodable for Arc<[T]> { - fn decode<D: Decoder>(d: &mut D) -> Result<Arc<[T]>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<[T]> { + fn decode(d: &mut D) -> Result<Arc<[T]>, D::Error> { d.read_seq(|d, len| { let mut vec = Vec::with_capacity(len); for index in 0..len { diff --git a/src/librustc_serialize/json.rs b/src/librustc_serialize/json.rs index ab7f6975325..dafeb053d83 100644 --- a/src/librustc_serialize/json.rs +++ b/src/librustc_serialize/json.rs @@ -292,7 +292,7 @@ pub fn error_str(error: ErrorCode) -> &'static str { } /// Shortcut function to decode a JSON `&str` into an object -pub fn decode<T: crate::Decodable>(s: &str) -> DecodeResult<T> { +pub fn decode<T: crate::Decodable<Decoder>>(s: &str) -> DecodeResult<T> { let json = match from_str(s) { Ok(x) => x, Err(e) => return Err(ParseError(e)), @@ -303,7 +303,9 @@ pub fn decode<T: crate::Decodable>(s: &str) -> DecodeResult<T> { } /// Shortcut function to encode a `T` into a JSON `String` -pub fn encode<T: crate::Encodable>(object: &T) -> Result<string::String, EncoderError> { +pub fn encode<T: for<'r> crate::Encodable<Encoder<'r>>>( + object: &T, +) -> Result<string::String, EncoderError> { let mut s = String::new(); { let mut encoder = Encoder::new(&mut s); @@ -1144,8 +1146,8 @@ impl<'a> crate::Encoder for PrettyEncoder<'a> { } } -impl Encodable for Json { - fn encode<E: crate::Encoder>(&self, e: &mut E) -> Result<(), E::Error> { +impl<E: crate::Encoder> Encodable<E> for Json { + fn encode(&self, e: &mut E) -> Result<(), E::Error> { match *self { Json::I64(v) => v.encode(e), Json::U64(v) => v.encode(e), @@ -2727,7 +2729,7 @@ impl<'a> fmt::Display for PrettyJson<'a> { } } -impl<'a, T: Encodable> fmt::Display for AsJson<'a, T> { +impl<'a, T: for<'r> Encodable<Encoder<'r>>> fmt::Display for AsJson<'a, T> { /// Encodes a json value into a string fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut shim = FormatShim { inner: f }; @@ -2747,7 +2749,7 @@ impl<'a, T> AsPrettyJson<'a, T> { } } -impl<'a, T: Encodable> fmt::Display for AsPrettyJson<'a, T> { +impl<'a, T: for<'x> Encodable<PrettyEncoder<'x>>> fmt::Display for AsPrettyJson<'a, T> { /// Encodes a json value into a string fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut shim = FormatShim { inner: f }; diff --git a/src/librustc_serialize/lib.rs b/src/librustc_serialize/lib.rs index 3dc3e783820..265b3b95e95 100644 --- a/src/librustc_serialize/lib.rs +++ b/src/librustc_serialize/lib.rs @@ -10,7 +10,6 @@ Core encoding and decoding interfaces. test(attr(allow(unused_variables), deny(warnings))) )] #![feature(box_syntax)] -#![feature(min_specialization)] #![feature(never_type)] #![feature(nll)] #![feature(associated_type_bounds)] @@ -19,9 +18,6 @@ Core encoding and decoding interfaces. pub use self::serialize::{Decodable, Decoder, Encodable, Encoder}; -pub use self::serialize::{SpecializationError, SpecializedDecoder, SpecializedEncoder}; -pub use self::serialize::{UseSpecializedDecodable, UseSpecializedEncodable}; - mod collection_impls; mod serialize; diff --git a/src/librustc_serialize/serialize.rs b/src/librustc_serialize/serialize.rs index 29c5737ad89..d279954bf91 100644 --- a/src/librustc_serialize/serialize.rs +++ b/src/librustc_serialize/serialize.rs @@ -4,7 +4,6 @@ Core encoding and decoding interfaces. */ -use std::any; use std::borrow::Cow; use std::cell::{Cell, RefCell}; use std::marker::PhantomData; @@ -380,282 +379,288 @@ pub trait Decoder { fn error(&mut self, err: &str) -> Self::Error; } -pub trait Encodable { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>; +pub trait Encodable<S: Encoder> { + fn encode(&self, s: &mut S) -> Result<(), S::Error>; } -pub trait Decodable: Sized { - fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error>; +pub trait Decodable<D: Decoder>: Sized { + fn decode(d: &mut D) -> Result<Self, D::Error>; } -impl Encodable for usize { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for usize { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_usize(*self) } } -impl Decodable for usize { - fn decode<D: Decoder>(d: &mut D) -> Result<usize, D::Error> { +impl<D: Decoder> Decodable<D> for usize { + fn decode(d: &mut D) -> Result<usize, D::Error> { d.read_usize() } } -impl Encodable for u8 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for u8 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_u8(*self) } } -impl Decodable for u8 { - fn decode<D: Decoder>(d: &mut D) -> Result<u8, D::Error> { +impl<D: Decoder> Decodable<D> for u8 { + fn decode(d: &mut D) -> Result<u8, D::Error> { d.read_u8() } } -impl Encodable for u16 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for u16 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_u16(*self) } } -impl Decodable for u16 { - fn decode<D: Decoder>(d: &mut D) -> Result<u16, D::Error> { +impl<D: Decoder> Decodable<D> for u16 { + fn decode(d: &mut D) -> Result<u16, D::Error> { d.read_u16() } } -impl Encodable for u32 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for u32 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_u32(*self) } } -impl Decodable for u32 { - fn decode<D: Decoder>(d: &mut D) -> Result<u32, D::Error> { +impl<D: Decoder> Decodable<D> for u32 { + fn decode(d: &mut D) -> Result<u32, D::Error> { d.read_u32() } } -impl Encodable for ::std::num::NonZeroU32 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for ::std::num::NonZeroU32 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_u32(self.get()) } } -impl Decodable for ::std::num::NonZeroU32 { - fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> { +impl<D: Decoder> Decodable<D> for ::std::num::NonZeroU32 { + fn decode(d: &mut D) -> Result<Self, D::Error> { d.read_u32().map(|d| ::std::num::NonZeroU32::new(d).unwrap()) } } -impl Encodable for u64 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for u64 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_u64(*self) } } -impl Decodable for u64 { - fn decode<D: Decoder>(d: &mut D) -> Result<u64, D::Error> { +impl<D: Decoder> Decodable<D> for u64 { + fn decode(d: &mut D) -> Result<u64, D::Error> { d.read_u64() } } -impl Encodable for u128 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for u128 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_u128(*self) } } -impl Decodable for u128 { - fn decode<D: Decoder>(d: &mut D) -> Result<u128, D::Error> { +impl<D: Decoder> Decodable<D> for u128 { + fn decode(d: &mut D) -> Result<u128, D::Error> { d.read_u128() } } -impl Encodable for isize { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for isize { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_isize(*self) } } -impl Decodable for isize { - fn decode<D: Decoder>(d: &mut D) -> Result<isize, D::Error> { +impl<D: Decoder> Decodable<D> for isize { + fn decode(d: &mut D) -> Result<isize, D::Error> { d.read_isize() } } -impl Encodable for i8 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for i8 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_i8(*self) } } -impl Decodable for i8 { - fn decode<D: Decoder>(d: &mut D) -> Result<i8, D::Error> { +impl<D: Decoder> Decodable<D> for i8 { + fn decode(d: &mut D) -> Result<i8, D::Error> { d.read_i8() } } -impl Encodable for i16 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for i16 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_i16(*self) } } -impl Decodable for i16 { - fn decode<D: Decoder>(d: &mut D) -> Result<i16, D::Error> { +impl<D: Decoder> Decodable<D> for i16 { + fn decode(d: &mut D) -> Result<i16, D::Error> { d.read_i16() } } -impl Encodable for i32 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for i32 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_i32(*self) } } -impl Decodable for i32 { - fn decode<D: Decoder>(d: &mut D) -> Result<i32, D::Error> { +impl<D: Decoder> Decodable<D> for i32 { + fn decode(d: &mut D) -> Result<i32, D::Error> { d.read_i32() } } -impl Encodable for i64 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for i64 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_i64(*self) } } -impl Decodable for i64 { - fn decode<D: Decoder>(d: &mut D) -> Result<i64, D::Error> { +impl<D: Decoder> Decodable<D> for i64 { + fn decode(d: &mut D) -> Result<i64, D::Error> { d.read_i64() } } -impl Encodable for i128 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for i128 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_i128(*self) } } -impl Decodable for i128 { - fn decode<D: Decoder>(d: &mut D) -> Result<i128, D::Error> { +impl<D: Decoder> Decodable<D> for i128 { + fn decode(d: &mut D) -> Result<i128, D::Error> { d.read_i128() } } -impl Encodable for str { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for str { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_str(self) } } -impl Encodable for String { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for &str { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { + s.emit_str(self) + } +} + +impl<S: Encoder> Encodable<S> for String { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_str(&self[..]) } } -impl Decodable for String { - fn decode<D: Decoder>(d: &mut D) -> Result<String, D::Error> { +impl<D: Decoder> Decodable<D> for String { + fn decode(d: &mut D) -> Result<String, D::Error> { Ok(d.read_str()?.into_owned()) } } -impl Encodable for f32 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for f32 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_f32(*self) } } -impl Decodable for f32 { - fn decode<D: Decoder>(d: &mut D) -> Result<f32, D::Error> { +impl<D: Decoder> Decodable<D> for f32 { + fn decode(d: &mut D) -> Result<f32, D::Error> { d.read_f32() } } -impl Encodable for f64 { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for f64 { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_f64(*self) } } -impl Decodable for f64 { - fn decode<D: Decoder>(d: &mut D) -> Result<f64, D::Error> { +impl<D: Decoder> Decodable<D> for f64 { + fn decode(d: &mut D) -> Result<f64, D::Error> { d.read_f64() } } -impl Encodable for bool { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for bool { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_bool(*self) } } -impl Decodable for bool { - fn decode<D: Decoder>(d: &mut D) -> Result<bool, D::Error> { +impl<D: Decoder> Decodable<D> for bool { + fn decode(d: &mut D) -> Result<bool, D::Error> { d.read_bool() } } -impl Encodable for char { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for char { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_char(*self) } } -impl Decodable for char { - fn decode<D: Decoder>(d: &mut D) -> Result<char, D::Error> { +impl<D: Decoder> Decodable<D> for char { + fn decode(d: &mut D) -> Result<char, D::Error> { d.read_char() } } -impl Encodable for () { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for () { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_unit() } } -impl Decodable for () { - fn decode<D: Decoder>(d: &mut D) -> Result<(), D::Error> { +impl<D: Decoder> Decodable<D> for () { + fn decode(d: &mut D) -> Result<(), D::Error> { d.read_nil() } } -impl<T> Encodable for PhantomData<T> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T> Encodable<S> for PhantomData<T> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_unit() } } -impl<T> Decodable for PhantomData<T> { - fn decode<D: Decoder>(d: &mut D) -> Result<PhantomData<T>, D::Error> { +impl<D: Decoder, T> Decodable<D> for PhantomData<T> { + fn decode(d: &mut D) -> Result<PhantomData<T>, D::Error> { d.read_nil()?; Ok(PhantomData) } } -impl<T: Decodable> Decodable for Box<[T]> { - fn decode<D: Decoder>(d: &mut D) -> Result<Box<[T]>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> { + fn decode(d: &mut D) -> Result<Box<[T]>, D::Error> { let v: Vec<T> = Decodable::decode(d)?; Ok(v.into_boxed_slice()) } } -impl<T: Encodable> Encodable for Rc<T> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T: Encodable<S>> Encodable<S> for Rc<T> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (**self).encode(s) } } -impl<T: Decodable> Decodable for Rc<T> { - fn decode<D: Decoder>(d: &mut D) -> Result<Rc<T>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> { + fn decode(d: &mut D) -> Result<Rc<T>, D::Error> { Ok(Rc::new(Decodable::decode(d)?)) } } -impl<T: Encodable> Encodable for [T] { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))? @@ -665,8 +670,8 @@ impl<T: Encodable> Encodable for [T] { } } -impl<T: Encodable> Encodable for Vec<T> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))? @@ -676,8 +681,8 @@ impl<T: Encodable> Encodable for Vec<T> { } } -impl<T: Decodable> Decodable for Vec<T> { - fn decode<D: Decoder>(d: &mut D) -> Result<Vec<T>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> { + fn decode(d: &mut D) -> Result<Vec<T>, D::Error> { d.read_seq(|d, len| { let mut v = Vec::with_capacity(len); for i in 0..len { @@ -688,8 +693,8 @@ impl<T: Decodable> Decodable for Vec<T> { } } -impl Encodable for [u8; 20] { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for [u8; 20] { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))? @@ -699,8 +704,8 @@ impl Encodable for [u8; 20] { } } -impl Decodable for [u8; 20] { - fn decode<D: Decoder>(d: &mut D) -> Result<[u8; 20], D::Error> { +impl<D: Decoder> Decodable<D> for [u8; 20] { + fn decode(d: &mut D) -> Result<[u8; 20], D::Error> { d.read_seq(|d, len| { assert!(len == 20); let mut v = [0u8; 20]; @@ -712,11 +717,11 @@ impl Decodable for [u8; 20] { } } -impl<'a, T: Encodable> Encodable for Cow<'a, [T]> +impl<'a, S: Encoder, T: Encodable<S>> Encodable<S> for Cow<'a, [T]> where [T]: ToOwned<Owned = Vec<T>>, { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))? @@ -726,11 +731,11 @@ where } } -impl<T: Decodable + ToOwned> Decodable for Cow<'static, [T]> +impl<D: Decoder, T: Decodable<D> + ToOwned> Decodable<D> for Cow<'static, [T]> where [T]: ToOwned<Owned = Vec<T>>, { - fn decode<D: Decoder>(d: &mut D) -> Result<Cow<'static, [T]>, D::Error> { + fn decode(d: &mut D) -> Result<Cow<'static, [T]>, D::Error> { d.read_seq(|d, len| { let mut v = Vec::with_capacity(len); for i in 0..len { @@ -741,8 +746,8 @@ where } } -impl<T: Encodable> Encodable for Option<T> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_option(|s| match *self { None => s.emit_option_none(), Some(ref v) => s.emit_option_some(|s| v.encode(s)), @@ -750,14 +755,14 @@ impl<T: Encodable> Encodable for Option<T> { } } -impl<T: Decodable> Decodable for Option<T> { - fn decode<D: Decoder>(d: &mut D) -> Result<Option<T>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for Option<T> { + fn decode(d: &mut D) -> Result<Option<T>, D::Error> { d.read_option(|d, b| if b { Ok(Some(Decodable::decode(d)?)) } else { Ok(None) }) } } -impl<T1: Encodable, T2: Encodable> Encodable for Result<T1, T2> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T1: Encodable<S>, T2: Encodable<S>> Encodable<S> for Result<T1, T2> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_enum("Result", |s| match *self { Ok(ref v) => { s.emit_enum_variant("Ok", 0, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s))) @@ -769,8 +774,8 @@ impl<T1: Encodable, T2: Encodable> Encodable for Result<T1, T2> { } } -impl<T1: Decodable, T2: Decodable> Decodable for Result<T1, T2> { - fn decode<D: Decoder>(d: &mut D) -> Result<Result<T1, T2>, D::Error> { +impl<D: Decoder, T1: Decodable<D>, T2: Decodable<D>> Decodable<D> for Result<T1, T2> { + fn decode(d: &mut D) -> Result<Result<T1, T2>, D::Error> { d.read_enum("Result", |d| { d.read_enum_variant(&["Ok", "Err"], |d, disr| match disr { 0 => Ok(Ok(d.read_enum_variant_arg(0, |d| T1::decode(d))?)), @@ -806,9 +811,9 @@ macro_rules! count { macro_rules! tuple { () => (); ( $($name:ident,)+ ) => ( - impl<$($name:Decodable),+> Decodable for ($($name,)+) { + impl<D: Decoder, $($name: Decodable<D>),+> Decodable<D> for ($($name,)+) { #[allow(non_snake_case)] - fn decode<D: Decoder>(d: &mut D) -> Result<($($name,)+), D::Error> { + fn decode(d: &mut D) -> Result<($($name,)+), D::Error> { let len: usize = count!($($name)+); d.read_tuple(len, |d| { let mut i = 0; @@ -819,9 +824,9 @@ macro_rules! tuple { }) } } - impl<$($name:Encodable),+> Encodable for ($($name,)+) { + impl<S: Encoder, $($name: Encodable<S>),+> Encodable<S> for ($($name,)+) { #[allow(non_snake_case)] - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { let ($(ref $name,)+) = *self; let mut n = 0; $(let $name = $name; n += 1;)+ @@ -838,33 +843,33 @@ macro_rules! tuple { tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, } -impl Encodable for path::Path { - fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for path::Path { + fn encode(&self, e: &mut S) -> Result<(), S::Error> { self.to_str().unwrap().encode(e) } } -impl Encodable for path::PathBuf { - fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> { +impl<S: Encoder> Encodable<S> for path::PathBuf { + fn encode(&self, e: &mut S) -> Result<(), S::Error> { path::Path::encode(self, e) } } -impl Decodable for path::PathBuf { - fn decode<D: Decoder>(d: &mut D) -> Result<path::PathBuf, D::Error> { +impl<D: Decoder> Decodable<D> for path::PathBuf { + fn decode(d: &mut D) -> Result<path::PathBuf, D::Error> { let bytes: String = Decodable::decode(d)?; Ok(path::PathBuf::from(bytes)) } } -impl<T: Encodable + Copy> Encodable for Cell<T> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T: Encodable<S> + Copy> Encodable<S> for Cell<T> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { self.get().encode(s) } } -impl<T: Decodable + Copy> Decodable for Cell<T> { - fn decode<D: Decoder>(d: &mut D) -> Result<Cell<T>, D::Error> { +impl<D: Decoder, T: Decodable<D> + Copy> Decodable<D> for Cell<T> { + fn decode(d: &mut D) -> Result<Cell<T>, D::Error> { Ok(Cell::new(Decodable::decode(d)?)) } } @@ -874,136 +879,37 @@ impl<T: Decodable + Copy> Decodable for Cell<T> { // `encoder.error("attempting to Encode borrowed RefCell")` // from `encode` when `try_borrow` returns `None`. -impl<T: Encodable> Encodable for RefCell<T> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T: Encodable<S>> Encodable<S> for RefCell<T> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { self.borrow().encode(s) } } -impl<T: Decodable> Decodable for RefCell<T> { - fn decode<D: Decoder>(d: &mut D) -> Result<RefCell<T>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for RefCell<T> { + fn decode(d: &mut D) -> Result<RefCell<T>, D::Error> { Ok(RefCell::new(Decodable::decode(d)?)) } } -impl<T: Encodable> Encodable for Arc<T> { - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T: Encodable<S>> Encodable<S> for Arc<T> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (**self).encode(s) } } -impl<T: Decodable> Decodable for Arc<T> { - fn decode<D: Decoder>(d: &mut D) -> Result<Arc<T>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> { + fn decode(d: &mut D) -> Result<Arc<T>, D::Error> { Ok(Arc::new(Decodable::decode(d)?)) } } -// ___________________________________________________________________________ -// Specialization-based interface for multi-dispatch Encodable/Decodable. - -/// Implement this trait on your `{Encodable,Decodable}::Error` types -/// to override the default panic behavior for missing specializations. -pub trait SpecializationError { - /// Creates an error for a missing method specialization. - /// Defaults to panicking with type, trait & method names. - /// `S` is the encoder/decoder state type, - /// `T` is the type being encoded/decoded, and - /// the arguments are the names of the trait - /// and method that should've been overridden. - fn not_found<S, T: ?Sized>(trait_name: &'static str, method_name: &'static str) -> Self; -} - -impl<E> SpecializationError for E { - default fn not_found<S, T: ?Sized>(trait_name: &'static str, method_name: &'static str) -> E { - panic!( - "missing specialization: `<{} as {}<{}>>::{}` not overridden", - any::type_name::<S>(), - trait_name, - any::type_name::<T>(), - method_name - ); - } -} - -/// Implement this trait on encoders, with `T` being the type -/// you want to encode (employing `UseSpecializedEncodable`), -/// using a strategy specific to the encoder. -pub trait SpecializedEncoder<T: ?Sized + UseSpecializedEncodable>: Encoder { - /// Encode the value in a manner specific to this encoder state. - fn specialized_encode(&mut self, value: &T) -> Result<(), Self::Error>; -} - -impl<E: Encoder, T: ?Sized + UseSpecializedEncodable> SpecializedEncoder<T> for E { - default fn specialized_encode(&mut self, value: &T) -> Result<(), E::Error> { - value.default_encode(self) - } -} - -/// Implement this trait on decoders, with `T` being the type -/// you want to decode (employing `UseSpecializedDecodable`), -/// using a strategy specific to the decoder. -pub trait SpecializedDecoder<T: UseSpecializedDecodable>: Decoder { - /// Decode a value in a manner specific to this decoder state. - fn specialized_decode(&mut self) -> Result<T, Self::Error>; -} - -impl<D: Decoder, T: UseSpecializedDecodable> SpecializedDecoder<T> for D { - default fn specialized_decode(&mut self) -> Result<T, D::Error> { - T::default_decode(self) - } -} - -/// Implement this trait on your type to get an `Encodable` -/// implementation which goes through `SpecializedEncoder`. -pub trait UseSpecializedEncodable { - /// Defaults to returning an error (see `SpecializationError`). - fn default_encode<E: Encoder>(&self, _: &mut E) -> Result<(), E::Error> { - Err(E::Error::not_found::<E, Self>("SpecializedEncoder", "specialized_encode")) - } -} - -impl<T: ?Sized + UseSpecializedEncodable> Encodable for T { - default fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> { - E::specialized_encode(e, self) - } -} - -/// Implement this trait on your type to get an `Decodable` -/// implementation which goes through `SpecializedDecoder`. -pub trait UseSpecializedDecodable: Sized { - /// Defaults to returning an error (see `SpecializationError`). - fn default_decode<D: Decoder>(_: &mut D) -> Result<Self, D::Error> { - Err(D::Error::not_found::<D, Self>("SpecializedDecoder", "specialized_decode")) - } -} - -impl<T: UseSpecializedDecodable> Decodable for T { - default fn decode<D: Decoder>(d: &mut D) -> Result<T, D::Error> { - D::specialized_decode(d) - } -} - -// Can't avoid specialization for &T and Box<T> impls, -// as proxy impls on them are blankets that conflict -// with the Encodable and Decodable impls above, -// which only have `default` on their methods -// for this exact reason. -// May be fixable in a simpler fashion via the -// more complex lattice model for specialization. -impl<'a, T: ?Sized + Encodable> UseSpecializedEncodable for &'a T { - fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { - (**self).encode(s) - } -} -impl<T: ?Sized + Encodable> UseSpecializedEncodable for Box<T> { - fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { +impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (**self).encode(s) } } -impl<T: Decodable> UseSpecializedDecodable for Box<T> { - fn default_decode<D: Decoder>(d: &mut D) -> Result<Box<T>, D::Error> { +impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> { + fn decode(d: &mut D) -> Result<Box<T>, D::Error> { Ok(box Decodable::decode(d)?) } } -impl<'a, T: Decodable> UseSpecializedDecodable for &'a T {} -impl<'a, T: Decodable> UseSpecializedDecodable for &'a [T] {} |
