serialize: allow specifying the default behavior for specializations.

This commit is contained in:
Eduard Burtescu 2016-09-17 23:31:03 +03:00
parent b01d4891e2
commit 221d1a97e5
5 changed files with 57 additions and 27 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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> {

View File

@ -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);

View File

@ -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: {:?} }}",