about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustc/hir/def_id.rs16
-rw-r--r--src/librustc/ty/mod.rs4
-rw-r--r--src/libserialize/serialize.rs36
-rw-r--r--src/libsyntax/ast.rs14
-rw-r--r--src/libsyntax_pos/lib.rs14
5 files changed, 57 insertions, 27 deletions
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<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
+        s.emit_u32(self.0)
+    }
+}
+
+impl serialize::UseSpecializedDecodable for CrateNum {
+    fn default_decode<D: Decoder>(d: &mut D) -> Result<CrateNum, D::Error> {
+        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<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
+impl<'tcx> serialize::UseSpecializedEncodable for AdtDef<'tcx> {
+    fn default_encode<S: Encoder>(&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<E> 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<T: ?Sized>: Encoder {
+pub trait SpecializedEncoder<T: ?Sized + UseSpecializedEncodable>: 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<E: Encoder, T: ?Sized> SpecializedEncoder<T> for E {
-    default fn specialized_encode(&mut self, _: &T) -> Result<(), E::Error> {
-        Err(E::Error::not_found::<E, T>("SpecializedEncoder", "specialized_encode"))
+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.
-/// Can also be implemented alongside `UseSpecializedDecodable`
-/// to provide a default `specialized_decode` for decoders
-/// which do not implement `SpecializedDecoder` themselves.
-pub trait SpecializedDecoder<T>: Decoder {
+pub trait SpecializedDecoder<T: UseSpecializedDecodable>: 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<T, Self::Error>;
 }
 
-impl<D: Decoder, T> SpecializedDecoder<T> for D {
+impl<D: Decoder, T: UseSpecializedDecodable> SpecializedDecoder<T> for D {
     default fn specialized_decode(&mut self) -> Result<T, D::Error> {
-        Err(D::Error::not_found::<D, T>("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<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> {
@@ -736,7 +733,12 @@ impl<T: ?Sized + UseSpecializedEncodable> 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<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> {
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<P<Ty>>,
 }
 
-#[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<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
+        s.emit_u32(self.0)
+    }
+}
+
+impl serialize::UseSpecializedDecodable for NodeId {
+    fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> {
+        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<String>,
 }
 
-impl Encodable for Span {
-    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
+impl serialize::UseSpecializedEncodable for Span {
+    fn default_encode<S: Encoder>(&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: Decoder>(d: &mut D) -> Result<Span, D::Error> {
+        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: {:?} }}",