From 0cb7a4062a3c69bb0c54f0c9136889a1006e4f62 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 3 Jan 2015 22:24:50 -0800 Subject: 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] --- src/libsyntax/ast.rs | 40 +++++++++++++++------------------ src/libsyntax/codemap.rs | 17 ++++++++++++++ src/libsyntax/ext/deriving/decodable.rs | 30 +++++++++++++------------ src/libsyntax/ext/deriving/encodable.rs | 32 +++++++++++++------------- src/libsyntax/owned_slice.rs | 19 ++++++++++++++++ src/libsyntax/parse/token.rs | 17 ++++++++++++++ src/libsyntax/ptr.rs | 16 +++++++++++++ 7 files changed, 119 insertions(+), 52 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 03b9eaf76b9..c9d27e304ff 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -182,18 +182,34 @@ impl Name { /// A mark represents a unique id associated with a macro expansion pub type Mrk = u32; +#[cfg(stage0)] impl, E> Encodable 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(&self, s: &mut S) -> Result<(), S::Error> { + s.emit_str(token::get_ident(*self).get()) + } +} + +#[cfg(stage0)] impl, E> Decodable for Ident { fn decode(d: &mut D) -> Result { Ok(str_to_ident(try!(d.read_str())[])) } } +#[cfg(not(stage0))] +impl Decodable for Ident { + fn decode(d: &mut D) -> Result { + Ok(str_to_ident(try!(d.read_str())[])) + } +} + /// Function name (not all functions have names) pub type FnIdent = Option; @@ -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; + fn assert_encodable() {} + assert_encodable::(); } } diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 2c7bbcb6faf..a49f2614cd7 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -120,6 +120,7 @@ impl PartialEq for Span { impl Eq for Span {} +#[cfg(stage0)] impl, E> Encodable for Span { /* Note #1972 -- spans are encoded but not decoded */ fn encode(&self, s: &mut S) -> Result<(), E> { @@ -127,12 +128,28 @@ impl, E> Encodable for Span { } } +#[cfg(not(stage0))] +impl Encodable for Span { + /* Note #1972 -- spans are encoded but not decoded */ + fn encode(&self, s: &mut S) -> Result<(), S::Error> { + s.emit_nil() + } +} + +#[cfg(stage0)] impl, E> Decodable for Span { fn decode(_d: &mut D) -> Result { Ok(DUMMY_SP) } } +#[cfg(not(stage0))] +impl Decodable for Span { + fn decode(_d: &mut D) -> Result { + Ok(DUMMY_SP) + } +} + pub fn spanned(lo: BytePos, hi: BytePos, t: T) -> Spanned { respan(mk_sp(lo, hi), t) } diff --git a/src/libsyntax/ext/deriving/decodable.rs b/src/libsyntax/ext/deriving/decodable.rs index 882136cb862..c0631b8350b 100644 --- a/src/libsyntax/ext/deriving/decodable.rs +++ b/src/libsyntax/ext/deriving/decodable.rs @@ -52,27 +52,29 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new_(vec!(krate, "Decodable"), None, - vec!(box Literal(Path::new_local("__D")), - box Literal(Path::new_local("__E"))), true), + path: Path::new_(vec!(krate, "Decodable"), None, vec!(), true), additional_bounds: Vec::new(), - generics: LifetimeBounds { - lifetimes: Vec::new(), - bounds: vec!(("__D", vec!(Path::new_( - vec!(krate, "Decoder"), None, - vec!(box Literal(Path::new_local("__E"))), true))), - ("__E", vec!())) - }, + generics: LifetimeBounds::empty(), methods: vec!( MethodDef { name: "decode", - generics: LifetimeBounds::empty(), + generics: LifetimeBounds { + lifetimes: Vec::new(), + bounds: vec!(("__D", vec!(Path::new_( + vec!(krate, "Decoder"), None, + vec!(), true)))) + }, explicit_self: None, args: vec!(Ptr(box Literal(Path::new_local("__D")), Borrowed(None, MutMutable))), - ret_ty: Literal(Path::new_(vec!("std", "result", "Result"), None, - vec!(box Self, - box Literal(Path::new_local("__E"))), true)), + ret_ty: Literal(Path::new_( + vec!("std", "result", "Result"), + None, + vec!(box Self, box Literal(Path::new_( + vec!["__D", "Error"], None, vec![], false + ))), + true + )), attributes: Vec::new(), combine_substructure: combine_substructure(|a, b, c| { decodable_substructure(a, b, c, krate) diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index b2c929123d5..4323d2979cc 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -128,29 +128,29 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new_(vec!(krate, "Encodable"), None, - vec!(box Literal(Path::new_local("__S")), - box Literal(Path::new_local("__E"))), true), + path: Path::new_(vec!(krate, "Encodable"), None, vec!(), true), additional_bounds: Vec::new(), - generics: LifetimeBounds { - lifetimes: Vec::new(), - bounds: vec!(("__S", vec!(Path::new_( - vec!(krate, "Encoder"), None, - vec!(box Literal(Path::new_local("__E"))), true))), - ("__E", vec!())) - }, + generics: LifetimeBounds::empty(), methods: vec!( MethodDef { name: "encode", - generics: LifetimeBounds::empty(), + generics: LifetimeBounds { + lifetimes: Vec::new(), + bounds: vec!(("__S", vec!(Path::new_( + vec!(krate, "Encoder"), None, + vec!(), true)))) + }, explicit_self: borrowed_explicit_self(), args: vec!(Ptr(box Literal(Path::new_local("__S")), Borrowed(None, MutMutable))), - ret_ty: Literal(Path::new_(vec!("std", "result", "Result"), - None, - vec!(box Tuple(Vec::new()), - box Literal(Path::new_local("__E"))), - true)), + ret_ty: Literal(Path::new_( + vec!("std", "result", "Result"), + None, + vec!(box Tuple(Vec::new()), box Literal(Path::new_( + vec!["__S", "Error"], None, vec![], false + ))), + true + )), attributes: Vec::new(), combine_substructure: combine_substructure(|a, b, c| { encodable_substructure(a, b, c) diff --git a/src/libsyntax/owned_slice.rs b/src/libsyntax/owned_slice.rs index b87e2c6abbc..2a27431a086 100644 --- a/src/libsyntax/owned_slice.rs +++ b/src/libsyntax/owned_slice.rs @@ -82,12 +82,21 @@ impl FromIterator for OwnedSlice { } } +#[cfg(stage0)] impl, T: Encodable, E> Encodable for OwnedSlice { fn encode(&self, s: &mut S) -> Result<(), E> { self.as_slice().encode(s) } } +#[cfg(not(stage0))] +impl Encodable for OwnedSlice { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { + self.as_slice().encode(s) + } +} + +#[cfg(stage0)] impl, T: Decodable, E> Decodable for OwnedSlice { fn decode(d: &mut D) -> Result, E> { Ok(OwnedSlice::from_vec(match Decodable::decode(d) { @@ -96,3 +105,13 @@ impl, T: Decodable, E> Decodable for OwnedSlice { })) } } + +#[cfg(not(stage0))] +impl Decodable for OwnedSlice { + fn decode(d: &mut D) -> Result, D::Error> { + Ok(OwnedSlice::from_vec(match Decodable::decode(d) { + Ok(t) => t, + Err(e) => return Err(e) + })) + } +} diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 4bfcc94a083..b7e89b32b70 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -653,6 +653,7 @@ impl<'a> PartialEq for &'a str { } } +#[cfg(stage0)] impl, E> Decodable for InternedString { fn decode(d: &mut D) -> Result { Ok(get_name(get_ident_interner().intern( @@ -660,12 +661,28 @@ impl, E> Decodable for InternedString { } } +#[cfg(not(stage0))] +impl Decodable for InternedString { + fn decode(d: &mut D) -> Result { + Ok(get_name(get_ident_interner().intern( + try!(d.read_str())[]))) + } +} + +#[cfg(stage0)] impl, E> Encodable for InternedString { fn encode(&self, s: &mut S) -> Result<(), E> { s.emit_str(self.string[]) } } +#[cfg(not(stage0))] +impl Encodable for InternedString { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { + s.emit_str(self.string[]) + } +} + /// Returns the string contents of a name, using the task-local interner. #[inline] pub fn get_name(name: ast::Name) -> InternedString { diff --git a/src/libsyntax/ptr.rs b/src/libsyntax/ptr.rs index 6eee1d903ea..13eda7bb88f 100644 --- a/src/libsyntax/ptr.rs +++ b/src/libsyntax/ptr.rs @@ -111,14 +111,30 @@ impl> Hash for P { } } +#[cfg(stage0)] impl, T: 'static + Decodable> Decodable for P { fn decode(d: &mut D) -> Result, E> { Decodable::decode(d).map(P) } } +#[cfg(not(stage0))] +impl Decodable for P { + fn decode(d: &mut D) -> Result, D::Error> { + Decodable::decode(d).map(P) + } +} + +#[cfg(stage0)] impl, T: Encodable> Encodable for P { fn encode(&self, s: &mut S) -> Result<(), E> { (**self).encode(s) } } + +#[cfg(not(stage0))] +impl Encodable for P { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { + (**self).encode(s) + } +} -- cgit 1.4.1-3-g733a5