diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs index f36fcfd5187..399243551d6 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -11,12 +11,12 @@ use ty; use rustc_data_structures::indexed_vec::Idx; -use serialize; +use serialize::{self, Encoder, Decoder}; use std::fmt; use std::u32; -#[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, Hash, Debug)] +#[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Debug)] pub struct CrateNum(u32); impl Idx for CrateNum { @@ -59,7 +59,17 @@ impl fmt::Display for CrateNum { } } -impl serialize::UseSpecializedDecodable for CrateNum {} +impl serialize::UseSpecializedEncodable for CrateNum { + fn default_encode(&self, s: &mut S) -> Result<(), S::Error> { + s.emit_u32(self.0) + } +} + +impl serialize::UseSpecializedDecodable for CrateNum { + fn default_decode(d: &mut D) -> Result { + d.read_u32().map(CrateNum) + } +} /// A DefIndex is an index into the hir-map for a crate, identifying a /// particular definition. It should really be considered an interned diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index d7076ddf044..9eb87fa2ed4 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -1470,8 +1470,8 @@ impl<'tcx, 'container> Hash for AdtDefData<'tcx, 'container> { } } -impl<'tcx> Encodable for AdtDef<'tcx> { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { +impl<'tcx> serialize::UseSpecializedEncodable for AdtDef<'tcx> { + fn default_encode(&self, s: &mut S) -> Result<(), S::Error> { self.did.encode(s) } } diff --git a/src/libserialize/serialize.rs b/src/libserialize/serialize.rs index 88f6c12e980..6650a981884 100644 --- a/src/libserialize/serialize.rs +++ b/src/libserialize/serialize.rs @@ -691,42 +691,39 @@ impl SpecializationError for E { /// Implement this trait on encoders, with `T` being the type /// you want to encode (employing `UseSpecializedEncodable`), /// using a strategy specific to the encoder. -/// Can also be implemented alongside `UseSpecializedEncodable` -/// to provide a default `specialized_encode` for encoders -/// which do not implement `SpecializedEncoder` themselves. -pub trait SpecializedEncoder: Encoder { +pub trait SpecializedEncoder: Encoder { /// Encode the value in a manner specific to this encoder state. - /// Defaults to returning an error (see `SpecializationError`). fn specialized_encode(&mut self, value: &T) -> Result<(), Self::Error>; } -impl SpecializedEncoder for E { - default fn specialized_encode(&mut self, _: &T) -> Result<(), E::Error> { - Err(E::Error::not_found::("SpecializedEncoder", "specialized_encode")) +impl SpecializedEncoder 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. -/// Can also be implemented alongside `UseSpecializedDecodable` -/// to provide a default `specialized_decode` for decoders -/// which do not implement `SpecializedDecoder` themselves. -pub trait SpecializedDecoder: Decoder { +pub trait SpecializedDecoder: Decoder { /// Decode a value in a manner specific to this decoder state. - /// Defaults to returning an error (see `SpecializationError`). fn specialized_decode(&mut self) -> Result; } -impl SpecializedDecoder for D { +impl SpecializedDecoder for D { default fn specialized_decode(&mut self) -> Result { - Err(D::Error::not_found::("SpecializedDecoder", "specialized_decode")) + T::default_decode(self) } } /// Implement this trait on your type to get an `Encodable` /// implementation which goes through `SpecializedEncoder`. -pub trait UseSpecializedEncodable {} +pub trait UseSpecializedEncodable { + /// Defaults to returning an error (see `SpecializationError`). + fn default_encode(&self, _: &mut E) -> Result<(), E::Error> { + Err(E::Error::not_found::("SpecializedEncoder", "specialized_encode")) + } +} impl Encodable for T { default fn encode(&self, e: &mut E) -> Result<(), E::Error> { @@ -736,7 +733,12 @@ impl Encodable for T { /// Implement this trait on your type to get an `Decodable` /// implementation which goes through `SpecializedDecoder`. -pub trait UseSpecializedDecodable: Sized {} +pub trait UseSpecializedDecodable: Sized { + /// Defaults to returning an error (see `SpecializationError`). + fn default_decode(_: &mut D) -> Result { + Err(D::Error::not_found::("SpecializedDecoder", "specialized_decode")) + } +} impl Decodable for T { default fn decode(d: &mut D) -> Result { diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 70e614f9c9a..c18b36161df 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -300,7 +300,7 @@ pub struct ParenthesizedParameterData { pub output: Option>, } -#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, RustcEncodable, Hash, Debug)] +#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)] pub struct NodeId(u32); impl NodeId { @@ -328,7 +328,17 @@ impl fmt::Display for NodeId { } } -impl serialize::UseSpecializedDecodable for NodeId {} +impl serialize::UseSpecializedEncodable for NodeId { + fn default_encode(&self, s: &mut S) -> Result<(), S::Error> { + s.emit_u32(self.0) + } +} + +impl serialize::UseSpecializedDecodable for NodeId { + fn default_decode(d: &mut D) -> Result { + d.read_u32().map(NodeId) + } +} /// Node id used to represent the root of the crate. pub const CRATE_NODE_ID: NodeId = NodeId(0); diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index fb59f411106..8c8b4173fe5 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -138,8 +138,8 @@ pub struct SpanLabel { pub label: Option, } -impl Encodable for Span { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { +impl serialize::UseSpecializedEncodable for Span { + fn default_encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_struct("Span", 2, |s| { s.emit_struct_field("lo", 0, |s| { self.lo.encode(s) @@ -152,7 +152,15 @@ impl Encodable for Span { } } -impl serialize::UseSpecializedDecodable for Span {} +impl serialize::UseSpecializedDecodable for Span { + fn default_decode(d: &mut D) -> Result { + d.read_struct("Span", 2, |d| { + let lo = d.read_struct_field("lo", 0, Decodable::decode)?; + let hi = d.read_struct_field("hi", 1, Decodable::decode)?; + Ok(mk_sp(lo, hi)) + }) + } +} fn default_span_debug(span: Span, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Span {{ lo: {:?}, hi: {:?}, expn_id: {:?} }}",