Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 221d1a9

Browse files
committed
serialize: allow specifying the default behavior for specializations.
1 parent b01d489 commit 221d1a9

File tree

5 files changed

+57
-27
lines changed

5 files changed

+57
-27
lines changed

src/librustc/hir/def_id.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
use ty;
1212

1313
use rustc_data_structures::indexed_vec::Idx;
14-
use serialize;
14+
use serialize::{self, Encoder, Decoder};
1515

1616
use std::fmt;
1717
use std::u32;
1818

19-
#[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, Hash, Debug)]
19+
#[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Debug)]
2020
pub struct CrateNum(u32);
2121

2222
impl Idx for CrateNum {
@@ -59,7 +59,17 @@ impl fmt::Display for CrateNum {
5959
}
6060
}
6161

62-
impl serialize::UseSpecializedDecodable for CrateNum {}
62+
impl serialize::UseSpecializedEncodable for CrateNum {
63+
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
64+
s.emit_u32(self.0)
65+
}
66+
}
67+
68+
impl serialize::UseSpecializedDecodable for CrateNum {
69+
fn default_decode<D: Decoder>(d: &mut D) -> Result<CrateNum, D::Error> {
70+
d.read_u32().map(CrateNum)
71+
}
72+
}
6373

6474
/// A DefIndex is an index into the hir-map for a crate, identifying a
6575
/// particular definition. It should really be considered an interned

src/librustc/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,8 +1470,8 @@ impl<'tcx, 'container> Hash for AdtDefData<'tcx, 'container> {
14701470
}
14711471
}
14721472

1473-
impl<'tcx> Encodable for AdtDef<'tcx> {
1474-
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
1473+
impl<'tcx> serialize::UseSpecializedEncodable for AdtDef<'tcx> {
1474+
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
14751475
self.did.encode(s)
14761476
}
14771477
}

src/libserialize/serialize.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -691,42 +691,39 @@ impl<E> SpecializationError for E {
691691
/// Implement this trait on encoders, with `T` being the type
692692
/// you want to encode (employing `UseSpecializedEncodable`),
693693
/// using a strategy specific to the encoder.
694-
/// Can also be implemented alongside `UseSpecializedEncodable`
695-
/// to provide a default `specialized_encode` for encoders
696-
/// which do not implement `SpecializedEncoder` themselves.
697-
pub trait SpecializedEncoder<T: ?Sized>: Encoder {
694+
pub trait SpecializedEncoder<T: ?Sized + UseSpecializedEncodable>: Encoder {
698695
/// Encode the value in a manner specific to this encoder state.
699-
/// Defaults to returning an error (see `SpecializationError`).
700696
fn specialized_encode(&mut self, value: &T) -> Result<(), Self::Error>;
701697
}
702698

703-
impl<E: Encoder, T: ?Sized> SpecializedEncoder<T> for E {
704-
default fn specialized_encode(&mut self, _: &T) -> Result<(), E::Error> {
705-
Err(E::Error::not_found::<E, T>("SpecializedEncoder", "specialized_encode"))
699+
impl<E: Encoder, T: ?Sized + UseSpecializedEncodable> SpecializedEncoder<T> for E {
700+
default fn specialized_encode(&mut self, value: &T) -> Result<(), E::Error> {
701+
value.default_encode(self)
706702
}
707703
}
708704

709705
/// Implement this trait on decoders, with `T` being the type
710706
/// you want to decode (employing `UseSpecializedDecodable`),
711707
/// using a strategy specific to the decoder.
712-
/// Can also be implemented alongside `UseSpecializedDecodable`
713-
/// to provide a default `specialized_decode` for decoders
714-
/// which do not implement `SpecializedDecoder` themselves.
715-
pub trait SpecializedDecoder<T>: Decoder {
708+
pub trait SpecializedDecoder<T: UseSpecializedDecodable>: Decoder {
716709
/// Decode a value in a manner specific to this decoder state.
717-
/// Defaults to returning an error (see `SpecializationError`).
718710
fn specialized_decode(&mut self) -> Result<T, Self::Error>;
719711
}
720712

721-
impl<D: Decoder, T> SpecializedDecoder<T> for D {
713+
impl<D: Decoder, T: UseSpecializedDecodable> SpecializedDecoder<T> for D {
722714
default fn specialized_decode(&mut self) -> Result<T, D::Error> {
723-
Err(D::Error::not_found::<D, T>("SpecializedDecoder", "specialized_decode"))
715+
T::default_decode(self)
724716
}
725717
}
726718

727719
/// Implement this trait on your type to get an `Encodable`
728720
/// implementation which goes through `SpecializedEncoder`.
729-
pub trait UseSpecializedEncodable {}
721+
pub trait UseSpecializedEncodable {
722+
/// Defaults to returning an error (see `SpecializationError`).
723+
fn default_encode<E: Encoder>(&self, _: &mut E) -> Result<(), E::Error> {
724+
Err(E::Error::not_found::<E, Self>("SpecializedEncoder", "specialized_encode"))
725+
}
726+
}
730727

731728
impl<T: ?Sized + UseSpecializedEncodable> Encodable for T {
732729
default fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
@@ -736,7 +733,12 @@ impl<T: ?Sized + UseSpecializedEncodable> Encodable for T {
736733

737734
/// Implement this trait on your type to get an `Decodable`
738735
/// implementation which goes through `SpecializedDecoder`.
739-
pub trait UseSpecializedDecodable: Sized {}
736+
pub trait UseSpecializedDecodable: Sized {
737+
/// Defaults to returning an error (see `SpecializationError`).
738+
fn default_decode<D: Decoder>(_: &mut D) -> Result<Self, D::Error> {
739+
Err(D::Error::not_found::<D, Self>("SpecializedDecoder", "specialized_decode"))
740+
}
741+
}
740742

741743
impl<T: UseSpecializedDecodable> Decodable for T {
742744
default fn decode<D: Decoder>(d: &mut D) -> Result<T, D::Error> {

src/libsyntax/ast.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ pub struct ParenthesizedParameterData {
300300
pub output: Option<P<Ty>>,
301301
}
302302

303-
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, RustcEncodable, Hash, Debug)]
303+
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
304304
pub struct NodeId(u32);
305305

306306
impl NodeId {
@@ -328,7 +328,17 @@ impl fmt::Display for NodeId {
328328
}
329329
}
330330

331-
impl serialize::UseSpecializedDecodable for NodeId {}
331+
impl serialize::UseSpecializedEncodable for NodeId {
332+
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
333+
s.emit_u32(self.0)
334+
}
335+
}
336+
337+
impl serialize::UseSpecializedDecodable for NodeId {
338+
fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> {
339+
d.read_u32().map(NodeId)
340+
}
341+
}
332342

333343
/// Node id used to represent the root of the crate.
334344
pub const CRATE_NODE_ID: NodeId = NodeId(0);

src/libsyntax_pos/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ pub struct SpanLabel {
138138
pub label: Option<String>,
139139
}
140140

141-
impl Encodable for Span {
142-
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
141+
impl serialize::UseSpecializedEncodable for Span {
142+
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
143143
s.emit_struct("Span", 2, |s| {
144144
s.emit_struct_field("lo", 0, |s| {
145145
self.lo.encode(s)
@@ -152,7 +152,15 @@ impl Encodable for Span {
152152
}
153153
}
154154

155-
impl serialize::UseSpecializedDecodable for Span {}
155+
impl serialize::UseSpecializedDecodable for Span {
156+
fn default_decode<D: Decoder>(d: &mut D) -> Result<Span, D::Error> {
157+
d.read_struct("Span", 2, |d| {
158+
let lo = d.read_struct_field("lo", 0, Decodable::decode)?;
159+
let hi = d.read_struct_field("hi", 1, Decodable::decode)?;
160+
Ok(mk_sp(lo, hi))
161+
})
162+
}
163+
}
156164

157165
fn default_span_debug(span: Span, f: &mut fmt::Formatter) -> fmt::Result {
158166
write!(f, "Span {{ lo: {:?}, hi: {:?}, expn_id: {:?} }}",

0 commit comments

Comments
 (0)