1
Fork 0

serialize: Use assoc types + less old_orphan_check

This commit moves the libserialize crate (and will force the hand of the
rustc-serialize crate) to not require the `old_orphan_check` feature gate as
well as using associated types wherever possible. Concretely, the following
changes were made:

* The error type of `Encoder` and `Decoder` is now an associated type, meaning
  that these traits have no type parameters.

* The `Encoder` and `Decoder` type parameters on the `Encodable` and `Decodable`
  traits have moved to the corresponding method of the trait. This movement
  alleviates the dependency on `old_orphan_check` but implies that
  implementations can no longer be specialized for the type of encoder/decoder
  being implemented.

Due to the trait definitions changing, this is a:

[breaking-change]
This commit is contained in:
Alex Crichton 2015-01-03 22:24:50 -08:00
parent ed22606c83
commit 0cb7a4062a
20 changed files with 5744 additions and 426 deletions

View file

@ -182,18 +182,34 @@ impl Name {
/// A mark represents a unique id associated with a macro expansion
pub type Mrk = u32;
#[cfg(stage0)]
impl<S: Encoder<E>, E> Encodable<S, E> for Ident {
fn encode(&self, s: &mut S) -> Result<(), E> {
s.emit_str(token::get_ident(*self).get())
}
}
#[cfg(not(stage0))]
impl Encodable for Ident {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_str(token::get_ident(*self).get())
}
}
#[cfg(stage0)]
impl<D: Decoder<E>, E> Decodable<D, E> for Ident {
fn decode(d: &mut D) -> Result<Ident, E> {
Ok(str_to_ident(try!(d.read_str())[]))
}
}
#[cfg(not(stage0))]
impl Decodable for Ident {
fn decode<D: Decoder>(d: &mut D) -> Result<Ident, D::Error> {
Ok(str_to_ident(try!(d.read_str())[]))
}
}
/// Function name (not all functions have names)
pub type FnIdent = Option<Ident>;
@ -1686,27 +1702,7 @@ mod test {
// are ASTs encodable?
#[test]
fn check_asts_encodable() {
use std::io;
let e = Crate {
module: Mod {
inner: Span {
lo: BytePos(11),
hi: BytePos(19),
expn_id: NO_EXPANSION,
},
view_items: Vec::new(),
items: Vec::new(),
},
attrs: Vec::new(),
config: Vec::new(),
span: Span {
lo: BytePos(10),
hi: BytePos(20),
expn_id: NO_EXPANSION,
},
exported_macros: Vec::new(),
};
// doesn't matter which encoder we use....
let _f = &e as &serialize::Encodable<json::Encoder, fmt::Error>;
fn assert_encodable<T: serialize::Encodable>() {}
assert_encodable::<Crate>();
}
}