diff --git a/Cargo.toml b/Cargo.toml index 1c95e486..a4f0df33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,16 +1,13 @@ [package] name = "serde" -version = "0.2.1" +version = "0.3.0" authors = ["Erick Tryzelaar "] license = "MIT/Apache-2.0" description = "A serialization/deserialization framework" repository = "https://github.com/erickt/rust-serde" -[lib] -name = "serde" - [dependencies] rustc-serialize = "*" -[dev-dependencies] -serde_macros = "0.2.1" +[dev-dependencies.serde_macros] +path = "serde_macros/" diff --git a/benches/bench_log.rs b/benches/bench_log.rs index 9d39c8e2..f7e5e543 100644 --- a/benches/bench_log.rs +++ b/benches/bench_log.rs @@ -58,7 +58,7 @@ impl ser::Serialize for HttpProtocol { #[inline] fn visit< V: ser::Visitor, - >(&self, visitor: &mut V) -> Result { + >(&self, visitor: &mut V) -> Result<(), V::Error> { visitor.visit_u8(*self as u8) } } @@ -106,7 +106,7 @@ impl ser::Serialize for HttpMethod { #[inline] fn visit< V: ser::Visitor, - >(&self, visitor: &mut V) -> Result { + >(&self, visitor: &mut V) -> Result<(), V::Error> { visitor.visit_u8(*self as u8) } } @@ -147,7 +147,7 @@ impl ser::Serialize for CacheStatus { #[inline] fn visit< V: ser::Visitor, - >(&self, visitor: &mut V) -> Result { + >(&self, visitor: &mut V) -> Result<(), V::Error> { visitor.visit_u8(*self as u8) } } @@ -197,7 +197,7 @@ impl ser::Serialize for OriginProtocol { #[inline] fn visit< V: ser::Visitor, - >(&self, visitor: &mut V) -> Result { + >(&self, visitor: &mut V) -> Result<(), V::Error> { visitor.visit_u8(*self as u8) } } @@ -239,7 +239,7 @@ impl ser::Serialize for ZonePlan { #[inline] fn visit< V: ser::Visitor, - >(&self, visitor: &mut V) -> Result { + >(&self, visitor: &mut V) -> Result<(), V::Error> { visitor.visit_u8(*self as u8) } } @@ -532,7 +532,7 @@ impl ser::Serialize for Country { #[inline] fn visit< V: ser::Visitor, - >(&self, visitor: &mut V) -> Result { + >(&self, visitor: &mut V) -> Result<(), V::Error> { visitor.visit_u8(*self as u8) } } diff --git a/serde_macros/Cargo.toml b/serde_macros/Cargo.toml index 2bbb2a6e..d96a5267 100644 --- a/serde_macros/Cargo.toml +++ b/serde_macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "serde_macros" -version = "0.2.1" +version = "0.3.0" authors = ["Erick Tryzelaar "] license = "MIT/Apache-2.0" description = "Macros to auto-generate implementations for the serde framework" diff --git a/serde_macros/src/lib.rs b/serde_macros/src/lib.rs index 4b32f212..7ee325ab 100644 --- a/serde_macros/src/lib.rs +++ b/serde_macros/src/lib.rs @@ -97,10 +97,7 @@ fn expand_derive_serialize( vec!("std", "result", "Result"), None, vec![ - Box::new(Ty::Literal(Path::new_(vec!["__V", "Value"], - None, - vec![], - false))), + Box::new(Ty::Tuple(vec![])), Box::new(Ty::Literal(Path::new_(vec!["__V", "Error"], None, vec![], @@ -258,7 +255,7 @@ fn serialize_tuple_struct( impl $visitor_impl_generics ::serde::ser::SeqVisitor for Visitor $visitor_generics { #[inline] - fn visit(&mut self, visitor: &mut V) -> Result, V::Error> + fn visit(&mut self, visitor: &mut V) -> Result, V::Error> where V: ::serde::ser::Visitor, { match self.state { @@ -342,7 +339,7 @@ fn serialize_struct( impl $visitor_impl_generics ::serde::ser::MapVisitor for Visitor $visitor_generics { #[inline] - fn visit(&mut self, visitor: &mut V) -> Result, V::Error> + fn visit(&mut self, visitor: &mut V) -> Result, V::Error> where V: ::serde::ser::Visitor, { match self.state { @@ -540,7 +537,7 @@ fn serialize_variant( let methods = vec![ ast::MethodImplItem( quote_method!(cx, - fn visit(&mut self, visitor: &mut V) -> Result, V::Error> + fn visit(&mut self, visitor: &mut V) -> Result, V::Error> where V: ::serde::ser::Visitor, { match self.state { diff --git a/src/json/ser.rs b/src/json/ser.rs index 092f1a1f..2fdfa76e 100644 --- a/src/json/ser.rs +++ b/src/json/ser.rs @@ -52,7 +52,6 @@ impl Serializer { } impl ser::Serializer for Serializer { - type Value = (); type Error = io::Error; #[inline] @@ -117,7 +116,6 @@ impl<'a, W> Visitor<'a, W> where W: io::Write, { } impl<'a, W> ser::Visitor for Visitor<'a, W> where W: io::Write, { - type Value = (); type Error = io::Error; #[inline] diff --git a/src/json/value.rs b/src/json/value.rs index b7698d96..c25b98fd 100644 --- a/src/json/value.rs +++ b/src/json/value.rs @@ -21,7 +21,7 @@ pub enum Value { impl ser::Serialize for Value { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: ser::Visitor, { match *self { @@ -163,7 +163,6 @@ impl Serializer { } impl ser::Serializer for Serializer { - type Value = (); type Error = (); #[inline] @@ -176,7 +175,6 @@ impl ser::Serializer for Serializer { } impl ser::Visitor for Serializer { - type Value = (); type Error = (); #[inline] diff --git a/src/ser.rs b/src/ser.rs index 2efb786a..6625d30e 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -9,117 +9,115 @@ use std::sync::Arc; /////////////////////////////////////////////////////////////////////////////// pub trait Serialize { - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor; } /////////////////////////////////////////////////////////////////////////////// pub trait Serializer { - type Value; type Error; - fn visit(&mut self, value: &T) -> Result + fn visit(&mut self, value: &T) -> Result<(), Self::Error> where T: Serialize; } /////////////////////////////////////////////////////////////////////////////// pub trait Visitor { - type Value; type Error; - fn visit_bool(&mut self, v: bool) -> Result; + fn visit_bool(&mut self, v: bool) -> Result<(), Self::Error>; #[inline] - fn visit_isize(&mut self, v: isize) -> Result { + fn visit_isize(&mut self, v: isize) -> Result<(), Self::Error> { self.visit_i64(v as i64) } #[inline] - fn visit_i8(&mut self, v: i8) -> Result { + fn visit_i8(&mut self, v: i8) -> Result<(), Self::Error> { self.visit_i64(v as i64) } #[inline] - fn visit_i16(&mut self, v: i16) -> Result { + fn visit_i16(&mut self, v: i16) -> Result<(), Self::Error> { self.visit_i64(v as i64) } #[inline] - fn visit_i32(&mut self, v: i32) -> Result { + fn visit_i32(&mut self, v: i32) -> Result<(), Self::Error> { self.visit_i64(v as i64) } #[inline] - fn visit_i64(&mut self, v: i64) -> Result; + fn visit_i64(&mut self, v: i64) -> Result<(), Self::Error>; #[inline] - fn visit_usize(&mut self, v: usize) -> Result { + fn visit_usize(&mut self, v: usize) -> Result<(), Self::Error> { self.visit_u64(v as u64) } #[inline] - fn visit_u8(&mut self, v: u8) -> Result { + fn visit_u8(&mut self, v: u8) -> Result<(), Self::Error> { self.visit_u64(v as u64) } #[inline] - fn visit_u16(&mut self, v: u16) -> Result { + fn visit_u16(&mut self, v: u16) -> Result<(), Self::Error> { self.visit_u64(v as u64) } #[inline] - fn visit_u32(&mut self, v: u32) -> Result { + fn visit_u32(&mut self, v: u32) -> Result<(), Self::Error> { self.visit_u64(v as u64) } #[inline] - fn visit_u64(&mut self, v: u64) -> Result; + fn visit_u64(&mut self, v: u64) -> Result<(), Self::Error>; #[inline] - fn visit_f32(&mut self, v: f32) -> Result { + fn visit_f32(&mut self, v: f32) -> Result<(), Self::Error> { self.visit_f64(v as f64) } - fn visit_f64(&mut self, v: f64) -> Result; + fn visit_f64(&mut self, v: f64) -> Result<(), Self::Error>; #[inline] - fn visit_char(&mut self, v: char) -> Result { + fn visit_char(&mut self, v: char) -> Result<(), Self::Error> { // The unwraps in here should be safe. let mut s = &mut [0; 4]; let len = v.encode_utf8(s).unwrap(); self.visit_str(str::from_utf8(&s[..len]).unwrap()) } - fn visit_str(&mut self, value: &str) -> Result; + fn visit_str(&mut self, value: &str) -> Result<(), Self::Error>; - fn visit_unit(&mut self) -> Result; + fn visit_unit(&mut self) -> Result<(), Self::Error>; #[inline] - fn visit_named_unit(&mut self, _name: &str) -> Result { + fn visit_named_unit(&mut self, _name: &str) -> Result<(), Self::Error> { self.visit_unit() } #[inline] fn visit_enum_unit(&mut self, _name: &str, - _variant: &str) -> Result { + _variant: &str) -> Result<(), Self::Error> { self.visit_unit() } - fn visit_none(&mut self) -> Result; + fn visit_none(&mut self) -> Result<(), Self::Error>; - fn visit_some(&mut self, value: V) -> Result + fn visit_some(&mut self, value: V) -> Result<(), Self::Error> where V: Serialize; - fn visit_seq(&mut self, visitor: V) -> Result + fn visit_seq(&mut self, visitor: V) -> Result<(), Self::Error> where V: SeqVisitor; #[inline] fn visit_named_seq(&mut self, _name: &'static str, - visitor: V) -> Result + visitor: V) -> Result<(), Self::Error> where V: SeqVisitor, { self.visit_seq(visitor) @@ -129,7 +127,7 @@ pub trait Visitor { fn visit_enum_seq(&mut self, _name: &'static str, _variant: &'static str, - visitor: V) -> Result + visitor: V) -> Result<(), Self::Error> where V: SeqVisitor, { self.visit_seq(visitor) @@ -137,16 +135,16 @@ pub trait Visitor { fn visit_seq_elt(&mut self, first: bool, - value: T) -> Result + value: T) -> Result<(), Self::Error> where T: Serialize; - fn visit_map(&mut self, visitor: V) -> Result + fn visit_map(&mut self, visitor: V) -> Result<(), Self::Error> where V: MapVisitor; #[inline] fn visit_named_map(&mut self, _name: &'static str, - visitor: V) -> Result + visitor: V) -> Result<(), Self::Error> where V: MapVisitor, { self.visit_map(visitor) @@ -156,7 +154,7 @@ pub trait Visitor { fn visit_enum_map(&mut self, _name: &'static str, _variant: &'static str, - visitor: V) -> Result + visitor: V) -> Result<(), Self::Error> where V: MapVisitor, { self.visit_map(visitor) @@ -165,13 +163,13 @@ pub trait Visitor { fn visit_map_elt(&mut self, first: bool, key: K, - value: V) -> Result + value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize; } pub trait SeqVisitor { - fn visit(&mut self, visitor: &mut V) -> Result, V::Error> + fn visit(&mut self, visitor: &mut V) -> Result, V::Error> where V: Visitor; #[inline] @@ -181,7 +179,7 @@ pub trait SeqVisitor { } pub trait MapVisitor { - fn visit(&mut self, visitor: &mut V) -> Result, V::Error> + fn visit(&mut self, visitor: &mut V) -> Result, V::Error> where V: Visitor; #[inline] @@ -196,7 +194,7 @@ macro_rules! impl_visit { ($ty:ty, $method:ident) => { impl Serialize for $ty { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { visitor.$method(*self) @@ -224,7 +222,7 @@ impl_visit!(char, visit_char); impl<'a> Serialize for &'a str { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { visitor.visit_str(*self) @@ -233,7 +231,7 @@ impl<'a> Serialize for &'a str { impl Serialize for String { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { (&self[..]).visit(visitor) @@ -244,7 +242,7 @@ impl Serialize for String { impl Serialize for Option where T: Serialize { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { match *self { @@ -278,7 +276,7 @@ impl SeqVisitor for SeqIteratorVisitor Iter: Iterator, { #[inline] - fn visit(&mut self, visitor: &mut V) -> Result, V::Error> + fn visit(&mut self, visitor: &mut V) -> Result, V::Error> where V: Visitor, { let first = self.first; @@ -305,7 +303,7 @@ impl<'a, T> Serialize for &'a [T] where T: Serialize, { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { visitor.visit_seq(SeqIteratorVisitor::new(self.iter())) @@ -314,7 +312,7 @@ impl<'a, T> Serialize for &'a [T] impl Serialize for Vec where T: Serialize { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { (&self[..]).visit(visitor) @@ -323,7 +321,7 @@ impl Serialize for Vec where T: Serialize { impl Serialize for BTreeSet where T: Serialize { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { visitor.visit_seq(SeqIteratorVisitor::new(self.iter())) @@ -335,7 +333,7 @@ impl Serialize for HashSet S: HashState, { #[inline] - fn visit(&self, visitor: &mut V) -> Result { + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> { visitor.visit_seq(SeqIteratorVisitor::new(self.iter())) } } @@ -344,7 +342,7 @@ impl Serialize for HashSet impl Serialize for () { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { visitor.visit_unit() @@ -384,7 +382,7 @@ macro_rules! tuple_impls { impl<'a, $($T),+> SeqVisitor for $TupleVisitor<'a, $($T),+> where $($T: Serialize),+ { - fn visit(&mut self, visitor: &mut V) -> Result, V::Error> + fn visit(&mut self, visitor: &mut V) -> Result, V::Error> where V: Visitor, { let first = self.first; @@ -412,7 +410,7 @@ macro_rules! tuple_impls { where $($T: Serialize),+ { #[inline] - fn visit(&self, visitor: &mut V) -> Result { + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> { visitor.visit_seq($TupleVisitor::new(self)) } } @@ -550,7 +548,7 @@ impl MapVisitor for MapIteratorVisitor I: Iterator, { #[inline] - fn visit(&mut self, visitor: &mut V_) -> Result, V_::Error> + fn visit(&mut self, visitor: &mut V_) -> Result, V_::Error> where V_: Visitor, { let first = self.first; @@ -578,7 +576,7 @@ impl Serialize for BTreeMap V: Serialize, { #[inline] - fn visit(&self, visitor: &mut V_) -> Result { + fn visit(&self, visitor: &mut V_) -> Result<(), V_::Error> { visitor.visit_map(MapIteratorVisitor::new(self.iter())) } } @@ -589,7 +587,7 @@ impl Serialize for HashMap S: HashState, { #[inline] - fn visit(&self, visitor: &mut V_) -> Result { + fn visit(&self, visitor: &mut V_) -> Result<(), V_::Error> { visitor.visit_map(MapIteratorVisitor::new(self.iter())) } } @@ -598,7 +596,7 @@ impl Serialize for HashMap impl<'a, T> Serialize for &'a T where T: Serialize { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { (**self).visit(visitor) @@ -607,7 +605,7 @@ impl<'a, T> Serialize for &'a T where T: Serialize { impl<'a, T> Serialize for &'a mut T where T: Serialize { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { (**self).visit(visitor) @@ -616,7 +614,7 @@ impl<'a, T> Serialize for &'a mut T where T: Serialize { impl Serialize for Box where T: Serialize { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { (**self).visit(visitor) @@ -625,7 +623,7 @@ impl Serialize for Box where T: Serialize { impl Serialize for Rc where T: Serialize, { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { (**self).visit(visitor) @@ -634,7 +632,7 @@ impl Serialize for Rc where T: Serialize, { impl Serialize for Arc where T: Serialize, { #[inline] - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { (**self).visit(visitor) @@ -644,7 +642,7 @@ impl Serialize for Arc where T: Serialize, { /////////////////////////////////////////////////////////////////////////////// impl Serialize for path::Path { - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { self.to_str().unwrap().visit(visitor) @@ -652,7 +650,7 @@ impl Serialize for path::Path { } impl Serialize for path::PathBuf { - fn visit(&self, visitor: &mut V) -> Result + fn visit(&self, visitor: &mut V) -> Result<(), V::Error> where V: Visitor, { self.to_str().unwrap().visit(visitor) diff --git a/tests/test_ser.rs b/tests/test_ser.rs index 6a36a113..5b1db017 100644 --- a/tests/test_ser.rs +++ b/tests/test_ser.rs @@ -79,7 +79,6 @@ impl<'a> AssertSerializer<'a> { } impl<'a> Serializer for AssertSerializer<'a> { - type Value = (); type Error = (); fn visit(&mut self, value: &T) -> Result<(), ()> { @@ -88,7 +87,6 @@ impl<'a> Serializer for AssertSerializer<'a> { } impl<'a> Visitor for AssertSerializer<'a> { - type Value = (); type Error = (); fn visit_unit(&mut self) -> Result<(), ()> {