diff --git a/serde/src/bytes.rs b/serde/src/bytes.rs index 5fa995fb..84c30866 100644 --- a/serde/src/bytes.rs +++ b/serde/src/bytes.rs @@ -182,14 +182,14 @@ mod bytebuf { type Value = ByteBuf; #[inline] - fn visit_unit(&mut self) -> Result + fn visit_unit(self) -> Result where E: de::Error, { Ok(ByteBuf::new()) } #[inline] - fn visit_seq(&mut self, mut visitor: V) -> Result + fn visit_seq(self, mut visitor: V) -> Result where V: de::SeqVisitor, { let (len, _) = visitor.size_hint(); @@ -203,26 +203,26 @@ mod bytebuf { } #[inline] - fn visit_bytes(&mut self, v: &[u8]) -> Result + fn visit_bytes(self, v: &[u8]) -> Result where E: de::Error, { Ok(ByteBuf::from(v)) } #[inline] - fn visit_byte_buf(&mut self, v: Vec) -> Result + fn visit_byte_buf(self, v: Vec) -> Result where E: de::Error, { Ok(ByteBuf::from(v)) } - fn visit_str(&mut self, v: &str) -> Result + fn visit_str(self, v: &str) -> Result where E: de::Error, { Ok(ByteBuf::from(v)) } - fn visit_string(&mut self, v: String) -> Result + fn visit_string(self, v: String) -> Result where E: de::Error, { Ok(ByteBuf::from(v)) @@ -231,7 +231,7 @@ mod bytebuf { impl de::Deserialize for ByteBuf { #[inline] - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: de::Deserializer { deserializer.deserialize_bytes(ByteBufVisitor) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 5f70466f..ea17afd2 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -65,6 +65,7 @@ use core::num::Zero; use de::{ Deserialize, Deserializer, + EnumVisitor, Error, MapVisitor, SeqVisitor, @@ -82,13 +83,13 @@ pub struct UnitVisitor; impl Visitor for UnitVisitor { type Value = (); - fn visit_unit(&mut self) -> Result<(), E> + fn visit_unit(self) -> Result<(), E> where E: Error, { Ok(()) } - fn visit_seq(&mut self, _: V) -> Result<(), V::Error> + fn visit_seq(self, _: V) -> Result<(), V::Error> where V: SeqVisitor, { Ok(()) @@ -96,7 +97,7 @@ impl Visitor for UnitVisitor { } impl Deserialize for () { - fn deserialize(deserializer: &mut D) -> Result<(), D::Error> + fn deserialize(deserializer: D) -> Result<(), D::Error> where D: Deserializer, { deserializer.deserialize_unit(UnitVisitor) @@ -111,13 +112,13 @@ pub struct BoolVisitor; impl Visitor for BoolVisitor { type Value = bool; - fn visit_bool(&mut self, v: bool) -> Result + fn visit_bool(self, v: bool) -> Result where E: Error, { Ok(v) } - fn visit_str(&mut self, s: &str) -> Result + fn visit_str(self, s: &str) -> Result where E: Error, { match s.trim_matches(::utils::Pattern_White_Space) { @@ -129,7 +130,7 @@ impl Visitor for BoolVisitor { } impl Deserialize for bool { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { deserializer.deserialize_bool(BoolVisitor) @@ -141,7 +142,7 @@ impl Deserialize for bool { macro_rules! impl_deserialize_num_method { ($src_ty:ty, $method:ident, $from_method:ident, $ty:expr) => { #[inline] - fn $method(&mut self, v: $src_ty) -> Result + fn $method(self, v: $src_ty) -> Result where E: Error, { match FromPrimitive::$from_method(v) { @@ -186,7 +187,7 @@ impl Visitor for PrimitiveVisitor impl_deserialize_num_method!(f64, visit_f64, from_f64, Type::F64); #[inline] - fn visit_str(&mut self, s: &str) -> Result + fn visit_str(self, s: &str) -> Result where E: Error, { str::FromStr::from_str(s.trim_matches(::utils::Pattern_White_Space)).or_else(|_| { @@ -199,7 +200,7 @@ macro_rules! impl_deserialize_num { ($ty:ty, $method:ident) => { impl Deserialize for $ty { #[inline] - fn deserialize(deserializer: &mut D) -> Result<$ty, D::Error> + fn deserialize(deserializer: D) -> Result<$ty, D::Error> where D: Deserializer, { deserializer.$method(PrimitiveVisitor::new()) @@ -229,14 +230,14 @@ impl Visitor for CharVisitor { type Value = char; #[inline] - fn visit_char(&mut self, v: char) -> Result + fn visit_char(self, v: char) -> Result where E: Error, { Ok(v) } #[inline] - fn visit_str(&mut self, v: &str) -> Result + fn visit_str(self, v: &str) -> Result where E: Error, { let mut iter = v.chars(); @@ -254,7 +255,7 @@ impl Visitor for CharVisitor { impl Deserialize for char { #[inline] - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { deserializer.deserialize_char(CharVisitor) @@ -270,25 +271,25 @@ struct StringVisitor; impl Visitor for StringVisitor { type Value = String; - fn visit_str(&mut self, v: &str) -> Result + fn visit_str(self, v: &str) -> Result where E: Error, { Ok(v.to_owned()) } - fn visit_string(&mut self, v: String) -> Result + fn visit_string(self, v: String) -> Result where E: Error, { Ok(v) } - fn visit_unit(&mut self) -> Result + fn visit_unit(self) -> Result where E: Error, { Ok(String::new()) } - fn visit_bytes(&mut self, v: &[u8]) -> Result + fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { match str::from_utf8(v) { @@ -297,7 +298,7 @@ impl Visitor for StringVisitor { } } - fn visit_byte_buf(&mut self, v: Vec) -> Result + fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { match String::from_utf8(v) { @@ -309,7 +310,7 @@ impl Visitor for StringVisitor { #[cfg(any(feature = "std", feature = "collections"))] impl Deserialize for String { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { deserializer.deserialize_string(StringVisitor) @@ -328,21 +329,21 @@ impl< type Value = Option; #[inline] - fn visit_unit(&mut self) -> Result, E> + fn visit_unit(self) -> Result, E> where E: Error, { Ok(None) } #[inline] - fn visit_none(&mut self) -> Result, E> + fn visit_none(self) -> Result, E> where E: Error, { Ok(None) } #[inline] - fn visit_some(&mut self, deserializer: &mut D) -> Result, D::Error> + fn visit_some(self, deserializer: D) -> Result, D::Error> where D: Deserializer, { Ok(Some(try!(Deserialize::deserialize(deserializer)))) @@ -350,7 +351,7 @@ impl< } impl Deserialize for Option where T: Deserialize { - fn deserialize(deserializer: &mut D) -> Result, D::Error> + fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer, { deserializer.deserialize_option(OptionVisitor { marker: PhantomData }) @@ -368,7 +369,7 @@ impl Visitor for PhantomDataVisitor { type Value = PhantomData; #[inline] - fn visit_unit(&mut self) -> Result, E> + fn visit_unit(self) -> Result, E> where E: Error, { Ok(PhantomData) @@ -376,7 +377,7 @@ impl Visitor for PhantomDataVisitor { } impl Deserialize for PhantomData { - fn deserialize(deserializer: &mut D) -> Result, D::Error> + fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer, { let visitor = PhantomDataVisitor { marker: PhantomData }; @@ -417,14 +418,14 @@ macro_rules! seq_impl { type Value = $ty; #[inline] - fn visit_unit(&mut self) -> Result<$ty, E> + fn visit_unit(self) -> Result<$ty, E> where E: Error, { Ok($ctor) } #[inline] - fn visit_seq(&mut self, mut $visitor: V) -> Result<$ty, V::Error> + fn visit_seq(self, mut $visitor: V) -> Result<$ty, V::Error> where V: SeqVisitor, { let mut values = $with_capacity; @@ -440,7 +441,7 @@ macro_rules! seq_impl { impl<$($typaram),*> Deserialize for $ty where $($typaram: $bound1 $(+ $bound2)*),* { - fn deserialize(deserializer: &mut D) -> Result<$ty, D::Error> + fn deserialize(deserializer: D) -> Result<$ty, D::Error> where D: Deserializer, { deserializer.deserialize_seq($visitor_ty::new()) @@ -531,14 +532,14 @@ impl Visitor for ArrayVisitor<[T; 0]> where T: Deserialize { type Value = [T; 0]; #[inline] - fn visit_unit(&mut self) -> Result<[T; 0], E> + fn visit_unit(self) -> Result<[T; 0], E> where E: Error, { Ok([]) } #[inline] - fn visit_seq(&mut self, _: V) -> Result<[T; 0], V::Error> + fn visit_seq(self, _: V) -> Result<[T; 0], V::Error> where V: SeqVisitor, { Ok([]) @@ -548,7 +549,7 @@ impl Visitor for ArrayVisitor<[T; 0]> where T: Deserialize { impl Deserialize for [T; 0] where T: Deserialize { - fn deserialize(deserializer: &mut D) -> Result<[T; 0], D::Error> + fn deserialize(deserializer: D) -> Result<[T; 0], D::Error> where D: Deserializer, { deserializer.deserialize_seq_fixed_size(0, ArrayVisitor::<[T; 0]>::new()) @@ -562,7 +563,7 @@ macro_rules! array_impls { type Value = [T; $len]; #[inline] - fn visit_seq(&mut self, mut visitor: V) -> Result<[T; $len], V::Error> + fn visit_seq(self, mut visitor: V) -> Result<[T; $len], V::Error> where V: SeqVisitor, { $( @@ -579,7 +580,7 @@ macro_rules! array_impls { impl Deserialize for [T; $len] where T: Deserialize, { - fn deserialize(deserializer: &mut D) -> Result<[T; $len], D::Error> + fn deserialize(deserializer: D) -> Result<[T; $len], D::Error> where D: Deserializer, { deserializer.deserialize_seq_fixed_size($len, ArrayVisitor::<[T; $len]>::new()) @@ -646,7 +647,7 @@ macro_rules! tuple_impls { #[inline] #[allow(non_snake_case)] - fn visit_seq(&mut self, mut visitor: V) -> Result<($($name,)+), V::Error> + fn visit_seq(self, mut visitor: V) -> Result<($($name,)+), V::Error> where V: SeqVisitor, { $( @@ -662,7 +663,7 @@ macro_rules! tuple_impls { impl<$($name: Deserialize),+> Deserialize for ($($name,)+) { #[inline] - fn deserialize(deserializer: &mut D) -> Result<($($name,)+), D::Error> + fn deserialize(deserializer: D) -> Result<($($name,)+), D::Error> where D: Deserializer, { deserializer.deserialize_tuple($len, $visitor::new()) @@ -723,14 +724,14 @@ macro_rules! map_impl { type Value = $ty; #[inline] - fn visit_unit(&mut self) -> Result<$ty, E> + fn visit_unit(self) -> Result<$ty, E> where E: Error, { Ok($ctor) } #[inline] - fn visit_map(&mut self, mut $visitor: Visitor) -> Result<$ty, Visitor::Error> + fn visit_map(self, mut $visitor: Visitor) -> Result<$ty, Visitor::Error> where Visitor: MapVisitor, { let mut values = $with_capacity; @@ -746,7 +747,7 @@ macro_rules! map_impl { impl<$($typaram),*> Deserialize for $ty where $($typaram: $bound1 $(+ $bound2)*),* { - fn deserialize(deserializer: &mut D) -> Result<$ty, D::Error> + fn deserialize(deserializer: D) -> Result<$ty, D::Error> where D: Deserializer, { deserializer.deserialize_map($visitor_ty::new()) @@ -778,7 +779,7 @@ map_impl!( #[cfg(feature = "std")] impl Deserialize for net::IpAddr { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { let s = try!(String::deserialize(deserializer)); @@ -791,7 +792,7 @@ impl Deserialize for net::IpAddr { #[cfg(feature = "std")] impl Deserialize for net::Ipv4Addr { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { let s = try!(String::deserialize(deserializer)); @@ -804,7 +805,7 @@ impl Deserialize for net::Ipv4Addr { #[cfg(feature = "std")] impl Deserialize for net::Ipv6Addr { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { let s = try!(String::deserialize(deserializer)); @@ -819,7 +820,7 @@ impl Deserialize for net::Ipv6Addr { #[cfg(feature = "std")] impl Deserialize for net::SocketAddr { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { let s = try!(String::deserialize(deserializer)); @@ -832,7 +833,7 @@ impl Deserialize for net::SocketAddr { #[cfg(feature = "std")] impl Deserialize for net::SocketAddrV4 { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { let s = try!(String::deserialize(deserializer)); @@ -845,7 +846,7 @@ impl Deserialize for net::SocketAddrV4 { #[cfg(feature = "std")] impl Deserialize for net::SocketAddrV6 { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { let s = try!(String::deserialize(deserializer)); @@ -865,13 +866,13 @@ struct PathBufVisitor; impl Visitor for PathBufVisitor { type Value = path::PathBuf; - fn visit_str(&mut self, v: &str) -> Result + fn visit_str(self, v: &str) -> Result where E: Error, { Ok(From::from(v)) } - fn visit_string(&mut self, v: String) -> Result + fn visit_string(self, v: String) -> Result where E: Error, { self.visit_str(&v) @@ -880,7 +881,7 @@ impl Visitor for PathBufVisitor { #[cfg(feature = "std")] impl Deserialize for path::PathBuf { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { deserializer.deserialize_string(PathBufVisitor) @@ -891,7 +892,7 @@ impl Deserialize for path::PathBuf { #[cfg(any(feature = "std", feature = "alloc"))] impl Deserialize for Box { - fn deserialize(deserializer: &mut D) -> Result, D::Error> + fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer, { let val = try!(Deserialize::deserialize(deserializer)); @@ -901,7 +902,7 @@ impl Deserialize for Box { #[cfg(any(feature = "std", feature = "collections"))] impl Deserialize for Box<[T]> { - fn deserialize(deserializer: &mut D) -> Result, D::Error> + fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer, { let v: Vec = try!(Deserialize::deserialize(deserializer)); @@ -911,7 +912,7 @@ impl Deserialize for Box<[T]> { #[cfg(any(feature = "std", feature = "collections"))] impl Deserialize for Box { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer { let s = try!(String::deserialize(deserializer)); @@ -921,7 +922,7 @@ impl Deserialize for Box { #[cfg(any(feature = "std", feature = "alloc"))] impl Deserialize for Arc { - fn deserialize(deserializer: &mut D) -> Result, D::Error> + fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer, { let val = try!(Deserialize::deserialize(deserializer)); @@ -931,7 +932,7 @@ impl Deserialize for Arc { #[cfg(any(feature = "std", feature = "alloc"))] impl Deserialize for Rc { - fn deserialize(deserializer: &mut D) -> Result, D::Error> + fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer, { let val = try!(Deserialize::deserialize(deserializer)); @@ -942,7 +943,7 @@ impl Deserialize for Rc { #[cfg(any(feature = "std", feature = "collections"))] impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deserialize, { #[inline] - fn deserialize(deserializer: &mut D) -> Result, D::Error> + fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer, { let val = try!(Deserialize::deserialize(deserializer)); @@ -962,13 +963,13 @@ impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deser // } #[cfg(feature = "std")] impl Deserialize for Duration { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { enum Field { Secs, Nanos }; impl Deserialize for Field { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { struct FieldVisitor; @@ -976,7 +977,7 @@ impl Deserialize for Duration { impl Visitor for FieldVisitor { type Value = Field; - fn visit_usize(&mut self, value: usize) -> Result + fn visit_usize(self, value: usize) -> Result where E: Error, { match value { @@ -986,7 +987,7 @@ impl Deserialize for Duration { } } - fn visit_str(&mut self, value: &str) -> Result + fn visit_str(self, value: &str) -> Result where E: Error, { match value { @@ -996,7 +997,7 @@ impl Deserialize for Duration { } } - fn visit_bytes(&mut self, value: &[u8]) -> Result + fn visit_bytes(self, value: &[u8]) -> Result where E: Error, { match value { @@ -1019,7 +1020,7 @@ impl Deserialize for Duration { impl Visitor for DurationVisitor { type Value = Duration; - fn visit_seq(&mut self, mut visitor: V) -> Result + fn visit_seq(self, mut visitor: V) -> Result where V: SeqVisitor, { let secs: u64 = match try!(visitor.visit()) { @@ -1037,7 +1038,7 @@ impl Deserialize for Duration { Ok(Duration::new(secs, nanos)) } - fn visit_map(&mut self, mut visitor: V) -> Result + fn visit_map(self, mut visitor: V) -> Result where V: MapVisitor, { let mut secs: Option = None; @@ -1079,7 +1080,7 @@ impl Deserialize for Duration { #[cfg(feature = "unstable")] impl Deserialize for NonZero where T: Deserialize + PartialEq + Zeroable + Zero { - fn deserialize(deserializer: &mut D) -> Result, D::Error> where D: Deserializer { + fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer { let value = try!(Deserialize::deserialize(deserializer)); if value == Zero::zero() { return Err(Error::invalid_value("expected a non-zero value")) @@ -1094,7 +1095,7 @@ impl Deserialize for NonZero where T: Deserialize + PartialEq + Zeroable + impl Deserialize for Result where T: Deserialize, E: Deserialize { - fn deserialize(deserializer: &mut D) -> Result, D::Error> + fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer { enum Field { Ok, @@ -1103,7 +1104,7 @@ impl Deserialize for Result where T: Deserialize, E: Deserialize { impl Deserialize for Field { #[inline] - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer { struct FieldVisitor; @@ -1112,7 +1113,7 @@ impl Deserialize for Result where T: Deserialize, E: Deserialize { type Value = Field; #[cfg(any(feature = "std", feature = "collections"))] - fn visit_usize(&mut self, value: usize) -> Result where E: Error { + fn visit_usize(self, value: usize) -> Result where E: Error { #[cfg(feature = "collections")] use collections::string::ToString; match value { @@ -1123,7 +1124,7 @@ impl Deserialize for Result where T: Deserialize, E: Deserialize { } #[cfg(all(not(feature = "std"), not(feature = "collections")))] - fn visit_usize(&mut self, value: usize) -> Result where E: Error { + fn visit_usize(self, value: usize) -> Result where E: Error { match value { 0 => Ok(Field::Ok), 1 => Ok(Field::Err), @@ -1131,7 +1132,7 @@ impl Deserialize for Result where T: Deserialize, E: Deserialize { } } - fn visit_str(&mut self, value: &str) -> Result where E: Error { + fn visit_str(self, value: &str) -> Result where E: Error { match value { "Ok" => Ok(Field::Ok), "Err" => Ok(Field::Err), @@ -1139,7 +1140,7 @@ impl Deserialize for Result where T: Deserialize, E: Deserialize { } } - fn visit_bytes(&mut self, value: &[u8]) -> Result where E: Error { + fn visit_bytes(self, value: &[u8]) -> Result where E: Error { match value { b"Ok" => Ok(Field::Ok), b"Err" => Ok(Field::Err), @@ -1165,18 +1166,12 @@ impl Deserialize for Result where T: Deserialize, E: Deserialize { { type Value = Result; - fn visit_enum(&mut self, mut visitor: V) -> Result, V::Error> - where V: VariantVisitor + fn visit_enum(self, visitor: V) -> Result, V::Error> + where V: EnumVisitor { match try!(visitor.visit_variant()) { - Field::Ok => { - let value = try!(visitor.visit_newtype()); - Ok(Ok(value)) - } - Field::Err => { - let value = try!(visitor.visit_newtype()); - Ok(Err(value)) - } + (Field::Ok, variant) => variant.visit_newtype().map(Ok), + (Field::Err, variant) => variant.visit_newtype().map(Err), } } } @@ -1195,7 +1190,7 @@ pub struct IgnoredAny; impl Deserialize for IgnoredAny { #[inline] - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer, { struct IgnoredAnyVisitor; @@ -1204,58 +1199,58 @@ impl Deserialize for IgnoredAny { type Value = IgnoredAny; #[inline] - fn visit_bool(&mut self, _: bool) -> Result { + fn visit_bool(self, _: bool) -> Result { Ok(IgnoredAny) } #[inline] - fn visit_i64(&mut self, _: i64) -> Result { + fn visit_i64(self, _: i64) -> Result { Ok(IgnoredAny) } #[inline] - fn visit_u64(&mut self, _: u64) -> Result { + fn visit_u64(self, _: u64) -> Result { Ok(IgnoredAny) } #[inline] - fn visit_f64(&mut self, _: f64) -> Result { + fn visit_f64(self, _: f64) -> Result { Ok(IgnoredAny) } #[inline] - fn visit_str(&mut self, _: &str) -> Result + fn visit_str(self, _: &str) -> Result where E: Error, { Ok(IgnoredAny) } #[inline] - fn visit_none(&mut self) -> Result { + fn visit_none(self) -> Result { Ok(IgnoredAny) } #[inline] - fn visit_some(&mut self, _: &mut D) -> Result + fn visit_some(self, _: D) -> Result where D: Deserializer, { Ok(IgnoredAny) } #[inline] - fn visit_newtype_struct(&mut self, _: &mut D) -> Result + fn visit_newtype_struct(self, _: D) -> Result where D: Deserializer, { Ok(IgnoredAny) } #[inline] - fn visit_unit(&mut self) -> Result { + fn visit_unit(self) -> Result { Ok(IgnoredAny) } #[inline] - fn visit_seq(&mut self, mut visitor: V) -> Result + fn visit_seq(self, mut visitor: V) -> Result where V: SeqVisitor, { while let Some(_) = try!(visitor.visit::()) { @@ -1265,7 +1260,7 @@ impl Deserialize for IgnoredAny { } #[inline] - fn visit_map(&mut self, mut visitor: V) -> Result + fn visit_map(self, mut visitor: V) -> Result where V: MapVisitor, { while let Some((_, _)) = try!(visitor.visit::()) { @@ -1275,7 +1270,7 @@ impl Deserialize for IgnoredAny { } #[inline] - fn visit_bytes(&mut self, _: &[u8]) -> Result + fn visit_bytes(self, _: &[u8]) -> Result where E: Error, { Ok(IgnoredAny) diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index bd3b2a7c..af488dd4 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -222,7 +222,7 @@ impl fmt::Display for Type { /// `Deserialize` represents a type that can be deserialized. pub trait Deserialize: Sized { /// Deserialize this value given this `Deserializer`. - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: Deserializer; } @@ -245,100 +245,100 @@ pub trait Deserializer { type Error: Error; /// This method walks a visitor through a value as it is being deserialized. - fn deserialize(&mut self, visitor: V) -> Result + fn deserialize(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a `bool` value. - fn deserialize_bool(&mut self, visitor: V) -> Result + fn deserialize_bool(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `usize` value. /// A reasonable default is to forward to `deserialize_u64`. - fn deserialize_usize(&mut self, visitor: V) -> Result + fn deserialize_usize(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `u8` value. /// A reasonable default is to forward to `deserialize_u64`. - fn deserialize_u8(&mut self, visitor: V) -> Result + fn deserialize_u8(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `u16` value. /// A reasonable default is to forward to `deserialize_u64`. - fn deserialize_u16(&mut self, visitor: V) -> Result + fn deserialize_u16(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `u32` value. /// A reasonable default is to forward to `deserialize_u64`. - fn deserialize_u32(&mut self, visitor: V) -> Result + fn deserialize_u32(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `u64` value. - fn deserialize_u64(&mut self, visitor: V) -> Result + fn deserialize_u64(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `isize` value. /// A reasonable default is to forward to `deserialize_i64`. - fn deserialize_isize(&mut self, visitor: V) -> Result + fn deserialize_isize(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `i8` value. /// A reasonable default is to forward to `deserialize_i64`. - fn deserialize_i8(&mut self, visitor: V) -> Result + fn deserialize_i8(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `i16` value. /// A reasonable default is to forward to `deserialize_i64`. - fn deserialize_i16(&mut self, visitor: V) -> Result + fn deserialize_i16(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `i32` value. /// A reasonable default is to forward to `deserialize_i64`. - fn deserialize_i32(&mut self, visitor: V) -> Result + fn deserialize_i32(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `i64` value. - fn deserialize_i64(&mut self, visitor: V) -> Result + fn deserialize_i64(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a `f32` value. /// A reasonable default is to forward to `deserialize_f64`. - fn deserialize_f32(&mut self, visitor: V) -> Result + fn deserialize_f32(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a `f64` value. - fn deserialize_f64(&mut self, visitor: V) -> Result + fn deserialize_f64(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a `char` value. - fn deserialize_char(&mut self, visitor: V) -> Result + fn deserialize_char(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a `&str` value. - fn deserialize_str(&mut self, visitor: V) -> Result + fn deserialize_str(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a `String` value. - fn deserialize_string(&mut self, visitor: V) -> Result + fn deserialize_string(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `unit` value. - fn deserialize_unit(&mut self, visitor: V) -> Result + fn deserialize_unit(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an `Option` value. This allows /// deserializers that encode an optional value as a nullable value to convert the null value /// into a `None`, and a regular value as `Some(value)`. - fn deserialize_option(&mut self, visitor: V) -> Result + fn deserialize_option(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a sequence value. This allows /// deserializers to parse sequences that aren't tagged as sequences. - fn deserialize_seq(&mut self, visitor: V) -> Result + fn deserialize_seq(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a fixed size array. This allows /// deserializers to parse arrays that aren't tagged as arrays. - fn deserialize_seq_fixed_size(&mut self, + fn deserialize_seq_fixed_size(self, len: usize, visitor: V) -> Result where V: Visitor; @@ -346,17 +346,17 @@ pub trait Deserializer { /// This method hints that the `Deserialize` type is expecting a `Vec`. This allows /// deserializers that provide a custom byte vector serialization to properly deserialize the /// type. - fn deserialize_bytes(&mut self, visitor: V) -> Result + fn deserialize_bytes(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a map of values. This allows /// deserializers to parse sequences that aren't tagged as maps. - fn deserialize_map(&mut self, visitor: V) -> Result + fn deserialize_map(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a unit struct. This allows /// deserializers to a unit struct that aren't tagged as a unit struct. - fn deserialize_unit_struct(&mut self, + fn deserialize_unit_struct(self, name: &'static str, visitor: V) -> Result where V: Visitor; @@ -364,14 +364,14 @@ pub trait Deserializer { /// This method hints that the `Deserialize` type is expecting a newtype struct. This allows /// deserializers to a newtype struct that aren't tagged as a newtype struct. /// A reasonable default is to simply deserialize the expected value directly. - fn deserialize_newtype_struct(&mut self, + fn deserialize_newtype_struct(self, name: &'static str, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a tuple struct. This allows /// deserializers to parse sequences that aren't tagged as sequences. - fn deserialize_tuple_struct(&mut self, + fn deserialize_tuple_struct(self, name: &'static str, len: usize, visitor: V) -> Result @@ -379,7 +379,7 @@ pub trait Deserializer { /// This method hints that the `Deserialize` type is expecting a struct. This allows /// deserializers to parse sequences that aren't tagged as maps. - fn deserialize_struct(&mut self, + fn deserialize_struct(self, name: &'static str, fields: &'static [&'static str], visitor: V) -> Result @@ -388,18 +388,18 @@ pub trait Deserializer { /// This method hints that the `Deserialize` type is expecting some sort of struct field /// name. This allows deserializers to choose between &str, usize, or &[u8] to properly /// deserialize a struct field. - fn deserialize_struct_field(&mut self, visitor: V) -> Result + fn deserialize_struct_field(self, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting a tuple value. This allows /// deserializers that provide a custom tuple serialization to properly deserialize the type. - fn deserialize_tuple(&mut self, len: usize, visitor: V) -> Result + fn deserialize_tuple(self, len: usize, visitor: V) -> Result where V: Visitor; /// This method hints that the `Deserialize` type is expecting an enum value. This allows /// deserializers that provide a custom enumeration serialization to properly deserialize the /// type. - fn deserialize_enum(&mut self, + fn deserialize_enum(self, name: &'static str, variants: &'static [&'static str], visitor: V) -> Result @@ -407,19 +407,19 @@ pub trait Deserializer { /// This method hints that the `Deserialize` type needs to deserialize a value whose type /// doesn't matter because it is ignored. - fn deserialize_ignored_any(&mut self, visitor: V) -> Result + fn deserialize_ignored_any(self, visitor: V) -> Result where V: Visitor; } /////////////////////////////////////////////////////////////////////////////// /// This trait represents a visitor that walks through a deserializer. -pub trait Visitor { +pub trait Visitor: Sized { /// The value produced by this visitor. type Value; /// `visit_bool` deserializes a `bool` into a `Value`. - fn visit_bool(&mut self, v: bool) -> Result + fn visit_bool(self, v: bool) -> Result where E: Error, { let _ = v; @@ -427,35 +427,35 @@ pub trait Visitor { } /// `visit_isize` deserializes a `isize` into a `Value`. - fn visit_isize(&mut self, v: isize) -> Result + fn visit_isize(self, v: isize) -> Result where E: Error, { self.visit_i64(v as i64) } /// `visit_i8` deserializes a `i8` into a `Value`. - fn visit_i8(&mut self, v: i8) -> Result + fn visit_i8(self, v: i8) -> Result where E: Error, { self.visit_i64(v as i64) } /// `visit_i16` deserializes a `i16` into a `Value`. - fn visit_i16(&mut self, v: i16) -> Result + fn visit_i16(self, v: i16) -> Result where E: Error, { self.visit_i64(v as i64) } /// `visit_i32` deserializes a `i32` into a `Value`. - fn visit_i32(&mut self, v: i32) -> Result + fn visit_i32(self, v: i32) -> Result where E: Error, { self.visit_i64(v as i64) } /// `visit_i64` deserializes a `i64` into a `Value`. - fn visit_i64(&mut self, v: i64) -> Result + fn visit_i64(self, v: i64) -> Result where E: Error, { let _ = v; @@ -463,35 +463,35 @@ pub trait Visitor { } /// `visit_usize` deserializes a `usize` into a `Value`. - fn visit_usize(&mut self, v: usize) -> Result + fn visit_usize(self, v: usize) -> Result where E: Error, { self.visit_u64(v as u64) } /// `visit_u8` deserializes a `u8` into a `Value`. - fn visit_u8(&mut self, v: u8) -> Result + fn visit_u8(self, v: u8) -> Result where E: Error, { self.visit_u64(v as u64) } /// `visit_u16` deserializes a `u16` into a `Value`. - fn visit_u16(&mut self, v: u16) -> Result + fn visit_u16(self, v: u16) -> Result where E: Error, { self.visit_u64(v as u64) } /// `visit_u32` deserializes a `u32` into a `Value`. - fn visit_u32(&mut self, v: u32) -> Result + fn visit_u32(self, v: u32) -> Result where E: Error, { self.visit_u64(v as u64) } /// `visit_u64` deserializes a `u64` into a `Value`. - fn visit_u64(&mut self, v: u64) -> Result + fn visit_u64(self, v: u64) -> Result where E: Error, { let _ = v; @@ -499,14 +499,14 @@ pub trait Visitor { } /// `visit_f32` deserializes a `f32` into a `Value`. - fn visit_f32(&mut self, v: f32) -> Result + fn visit_f32(self, v: f32) -> Result where E: Error, { self.visit_f64(v as f64) } /// `visit_f64` deserializes a `f64` into a `Value`. - fn visit_f64(&mut self, v: f64) -> Result + fn visit_f64(self, v: f64) -> Result where E: Error, { let _ = v; @@ -515,14 +515,14 @@ pub trait Visitor { /// `visit_char` deserializes a `char` into a `Value`. #[inline] - fn visit_char(&mut self, v: char) -> Result + fn visit_char(self, v: char) -> Result where E: Error, { self.visit_str(::utils::encode_utf8(v).as_str()) } /// `visit_str` deserializes a `&str` into a `Value`. - fn visit_str(&mut self, v: &str) -> Result + fn visit_str(self, v: &str) -> Result where E: Error, { let _ = v; @@ -534,14 +534,14 @@ pub trait Visitor { /// to the `visit_str` method. #[inline] #[cfg(any(feature = "std", feature = "collections"))] - fn visit_string(&mut self, v: String) -> Result + fn visit_string(self, v: String) -> Result where E: Error, { self.visit_str(&v) } /// `visit_unit` deserializes a `()` into a `Value`. - fn visit_unit(&mut self) -> Result + fn visit_unit(self) -> Result where E: Error, { Err(Error::invalid_type(Type::Unit)) @@ -549,7 +549,7 @@ pub trait Visitor { /// `visit_unit_struct` deserializes a unit struct into a `Value`. #[inline] - fn visit_unit_struct(&mut self, name: &'static str) -> Result + fn visit_unit_struct(self, name: &'static str) -> Result where E: Error, { let _ = name; @@ -557,14 +557,14 @@ pub trait Visitor { } /// `visit_none` deserializes a none value into a `Value`. - fn visit_none(&mut self) -> Result + fn visit_none(self) -> Result where E: Error, { Err(Error::invalid_type(Type::Option)) } /// `visit_some` deserializes a value into a `Value`. - fn visit_some(&mut self, deserializer: &mut D) -> Result + fn visit_some(self, deserializer: D) -> Result where D: Deserializer, { let _ = deserializer; @@ -572,7 +572,7 @@ pub trait Visitor { } /// `visit_newtype_struct` deserializes a value into a `Value`. - fn visit_newtype_struct(&mut self, deserializer: &mut D) -> Result + fn visit_newtype_struct(self, deserializer: D) -> Result where D: Deserializer, { let _ = deserializer; @@ -580,7 +580,7 @@ pub trait Visitor { } /// `visit_seq` deserializes a `SeqVisitor` into a `Value`. - fn visit_seq(&mut self, visitor: V) -> Result + fn visit_seq(self, visitor: V) -> Result where V: SeqVisitor, { let _ = visitor; @@ -588,23 +588,23 @@ pub trait Visitor { } /// `visit_map` deserializes a `MapVisitor` into a `Value`. - fn visit_map(&mut self, visitor: V) -> Result + fn visit_map(self, visitor: V) -> Result where V: MapVisitor, { let _ = visitor; Err(Error::invalid_type(Type::Map)) } - /// `visit_enum` deserializes a `VariantVisitor` into a `Value`. - fn visit_enum(&mut self, visitor: V) -> Result - where V: VariantVisitor, + /// `visit_enum` deserializes a `EnumVisitor` into a `Value`. + fn visit_enum(self, visitor: V) -> Result + where V: EnumVisitor, { let _ = visitor; Err(Error::invalid_type(Type::Enum)) } /// `visit_bytes` deserializes a `&[u8]` into a `Value`. - fn visit_bytes(&mut self, v: &[u8]) -> Result + fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { let _ = v; @@ -613,7 +613,7 @@ pub trait Visitor { /// `visit_byte_buf` deserializes a `Vec` into a `Value`. #[cfg(any(feature = "std", feature = "collections"))] - fn visit_byte_buf(&mut self, v: Vec) -> Result + fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { self.visit_bytes(&v) @@ -746,28 +746,40 @@ impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor { /////////////////////////////////////////////////////////////////////////////// -/// `VariantVisitor` is a visitor that is created by the `Deserializer` and passed to the -/// `Deserialize` in order to deserialize a specific enum variant. +/// `EnumVisitor` is a visitor that is created by the `Deserializer` and passed +/// to the `Deserialize` in order to identify which variant of an enum to +/// deserialize. +pub trait EnumVisitor { + /// The error type that can be returned if some error occurs during deserialization. + type Error: Error; + /// The `Visitor` that will be used to deserialize the content of the enum + /// variant. + type Variant: VariantVisitor; + + /// `visit_variant` is called to identify which variant to deserialize. + fn visit_variant(self) -> Result<(V, Self::Variant), Self::Error> + where V: Deserialize; +} + +/// `VariantVisitor` is a visitor that is created by the `Deserializer` and +/// passed to the `Deserialize` to deserialize the content of a particular enum +/// variant. pub trait VariantVisitor { /// The error type that can be returned if some error occurs during deserialization. type Error: Error; - /// `visit_variant` is called to identify which variant to deserialize. - fn visit_variant(&mut self) -> Result - where V: Deserialize; - /// `visit_unit` is called when deserializing a variant with no values. - fn visit_unit(&mut self) -> Result<(), Self::Error>; + fn visit_unit(self) -> Result<(), Self::Error>; /// `visit_newtype` is called when deserializing a variant with a single value. /// A good default is often to use the `visit_tuple` method to deserialize a `(value,)`. - fn visit_newtype(&mut self) -> Result + fn visit_newtype(self) -> Result where T: Deserialize; /// `visit_tuple` is called when deserializing a tuple-like variant. /// If no tuple variants are expected, yield a /// `Err(serde::de::Error::invalid_type(serde::de::Type::TupleVariant))` - fn visit_tuple(&mut self, + fn visit_tuple(self, len: usize, visitor: V) -> Result where V: Visitor; @@ -775,44 +787,8 @@ pub trait VariantVisitor { /// `visit_struct` is called when deserializing a struct-like variant. /// If no struct variants are expected, yield a /// `Err(serde::de::Error::invalid_type(serde::de::Type::StructVariant))` - fn visit_struct(&mut self, + fn visit_struct(self, fields: &'static [&'static str], visitor: V) -> Result where V: Visitor; } - -impl<'a, T> VariantVisitor for &'a mut T where T: VariantVisitor { - type Error = T::Error; - - fn visit_variant(&mut self) -> Result - where V: Deserialize - { - (**self).visit_variant() - } - - fn visit_unit(&mut self) -> Result<(), T::Error> { - (**self).visit_unit() - } - - fn visit_newtype(&mut self) -> Result - where D: Deserialize, - { - (**self).visit_newtype() - } - - fn visit_tuple(&mut self, - len: usize, - visitor: V) -> Result - where V: Visitor, - { - (**self).visit_tuple(len, visitor) - } - - fn visit_struct(&mut self, - fields: &'static [&'static str], - visitor: V) -> Result - where V: Visitor, - { - (**self).visit_struct(fields, visitor) - } -} diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index c97e70b7..52342cec 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -184,13 +184,13 @@ impl de::Deserializer for UnitDeserializer tuple_struct struct struct_field tuple enum ignored_any } - fn deserialize(&mut self, mut visitor: V) -> Result + fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { visitor.visit_unit() } - fn deserialize_option(&mut self, mut visitor: V) -> Result + fn deserialize_option(self, visitor: V) -> Result where V: de::Visitor, { visitor.visit_none() @@ -202,7 +202,7 @@ impl de::Deserializer for UnitDeserializer macro_rules! primitive_deserializer { ($ty:ty, $name:ident, $method:ident) => { /// A helper deserializer that deserializes a number. - pub struct $name(Option<$ty>, PhantomData); + pub struct $name($ty, PhantomData); impl ValueDeserializer for $ty where E: de::Error, @@ -210,7 +210,7 @@ macro_rules! primitive_deserializer { type Deserializer = $name; fn into_deserializer(self) -> $name { - $name(Some(self), PhantomData) + $name(self, PhantomData) } } @@ -226,13 +226,10 @@ macro_rules! primitive_deserializer { ignored_any } - fn deserialize(&mut self, mut visitor: V) -> Result + fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - match self.0.take() { - Some(v) => visitor.$method(v), - None => Err(de::Error::end_of_stream()), - } + visitor.$method(self.0) } } } @@ -256,7 +253,7 @@ primitive_deserializer!(char, CharDeserializer, visit_char); /////////////////////////////////////////////////////////////////////////////// /// A helper deserializer that deserializes a `&str`. -pub struct StrDeserializer<'a, E>(Option<&'a str>, PhantomData); +pub struct StrDeserializer<'a, E>(&'a str, PhantomData); impl<'a, E> ValueDeserializer for &'a str where E: de::Error, @@ -264,7 +261,7 @@ impl<'a, E> ValueDeserializer for &'a str type Deserializer = StrDeserializer<'a, E>; fn into_deserializer(self) -> StrDeserializer<'a, E> { - StrDeserializer(Some(self), PhantomData) + StrDeserializer(self, PhantomData) } } @@ -273,19 +270,16 @@ impl<'a, E> de::Deserializer for StrDeserializer<'a, E> { type Error = E; - fn deserialize(&mut self, mut visitor: V) -> Result + fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - match self.0.take() { - Some(v) => visitor.visit_str(v), - None => Err(de::Error::end_of_stream()), - } + visitor.visit_str(self.0) } - fn deserialize_enum(&mut self, + fn deserialize_enum(self, _name: &str, _variants: &'static [&'static str], - mut visitor: V) -> Result + visitor: V) -> Result where V: de::Visitor, { visitor.visit_enum(self) @@ -298,41 +292,16 @@ impl<'a, E> de::Deserializer for StrDeserializer<'a, E> } } -impl<'a, E> de::VariantVisitor for StrDeserializer<'a, E> +impl<'a, E> de::EnumVisitor for StrDeserializer<'a, E> where E: de::Error, { type Error = E; + type Variant = private::UnitOnly; - fn visit_variant(&mut self) -> Result + fn visit_variant(self) -> Result<(T, Self::Variant), Self::Error> where T: de::Deserialize, { - de::Deserialize::deserialize(self) - } - - fn visit_unit(&mut self) -> Result<(), Self::Error> { - Ok(()) - } - - fn visit_newtype(&mut self) -> Result - where T: super::Deserialize, - { - Err(super::Error::invalid_type(super::Type::NewtypeVariant)) - } - - fn visit_tuple(&mut self, - _len: usize, - _visitor: V) -> Result - where V: super::Visitor - { - Err(super::Error::invalid_type(super::Type::TupleVariant)) - } - - fn visit_struct(&mut self, - _fields: &'static [&'static str], - _visitor: V) -> Result - where V: super::Visitor - { - Err(super::Error::invalid_type(super::Type::StructVariant)) + de::Deserialize::deserialize(self).map(private::unit_only) } } @@ -340,7 +309,7 @@ impl<'a, E> de::VariantVisitor for StrDeserializer<'a, E> /// A helper deserializer that deserializes a `String`. #[cfg(any(feature = "std", feature = "collections"))] -pub struct StringDeserializer(Option, PhantomData); +pub struct StringDeserializer(String, PhantomData); #[cfg(any(feature = "std", feature = "collections"))] impl ValueDeserializer for String @@ -349,7 +318,7 @@ impl ValueDeserializer for String type Deserializer = StringDeserializer; fn into_deserializer(self) -> StringDeserializer { - StringDeserializer(Some(self), PhantomData) + StringDeserializer(self, PhantomData) } } @@ -359,19 +328,16 @@ impl de::Deserializer for StringDeserializer { type Error = E; - fn deserialize(&mut self, mut visitor: V) -> Result + fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - match self.0.take() { - Some(string) => visitor.visit_string(string), - None => Err(de::Error::end_of_stream()), - } + visitor.visit_string(self.0) } - fn deserialize_enum(&mut self, + fn deserialize_enum(self, _name: &str, _variants: &'static [&'static str], - mut visitor: V) -> Result + visitor: V) -> Result where V: de::Visitor, { visitor.visit_enum(self) @@ -385,41 +351,16 @@ impl de::Deserializer for StringDeserializer } #[cfg(any(feature = "std", feature = "collections"))] -impl<'a, E> de::VariantVisitor for StringDeserializer +impl<'a, E> de::EnumVisitor for StringDeserializer where E: de::Error, { type Error = E; + type Variant = private::UnitOnly; - fn visit_variant(&mut self) -> Result + fn visit_variant(self) -> Result<(T, Self::Variant), Self::Error> where T: de::Deserialize, { - de::Deserialize::deserialize(self) - } - - fn visit_unit(&mut self) -> Result<(), Self::Error> { - Ok(()) - } - - fn visit_newtype(&mut self) -> Result - where T: super::Deserialize, - { - Err(super::Error::invalid_type(super::Type::NewtypeVariant)) - } - - fn visit_tuple(&mut self, - _len: usize, - _visitor: V) -> Result - where V: super::Visitor - { - Err(super::Error::invalid_type(super::Type::TupleVariant)) - } - - fn visit_struct(&mut self, - _fields: &'static [&'static str], - _visitor: V) -> Result - where V: super::Visitor - { - Err(super::Error::invalid_type(super::Type::StructVariant)) + de::Deserialize::deserialize(self).map(private::unit_only) } } @@ -427,7 +368,7 @@ impl<'a, E> de::VariantVisitor for StringDeserializer /// A helper deserializer that deserializes a `String`. #[cfg(any(feature = "std", feature = "collections"))] -pub struct CowStrDeserializer<'a, E>(Option>, PhantomData); +pub struct CowStrDeserializer<'a, E>(Cow<'a, str>, PhantomData); #[cfg(any(feature = "std", feature = "collections"))] impl<'a, E> ValueDeserializer for Cow<'a, str> @@ -436,7 +377,7 @@ impl<'a, E> ValueDeserializer for Cow<'a, str> type Deserializer = CowStrDeserializer<'a, E>; fn into_deserializer(self) -> CowStrDeserializer<'a, E> { - CowStrDeserializer(Some(self), PhantomData) + CowStrDeserializer(self, PhantomData) } } @@ -446,20 +387,19 @@ impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E> { type Error = E; - fn deserialize(&mut self, mut visitor: V) -> Result + fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - match self.0.take() { - Some(Cow::Borrowed(string)) => visitor.visit_str(string), - Some(Cow::Owned(string)) => visitor.visit_string(string), - None => Err(de::Error::end_of_stream()), + match self.0 { + Cow::Borrowed(string) => visitor.visit_str(string), + Cow::Owned(string) => visitor.visit_string(string), } } - fn deserialize_enum(&mut self, + fn deserialize_enum(self, _name: &str, _variants: &'static [&'static str], - mut visitor: V) -> Result + visitor: V) -> Result where V: de::Visitor, { visitor.visit_enum(self) @@ -473,41 +413,16 @@ impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E> } #[cfg(any(feature = "std", feature = "collections"))] -impl<'a, E> de::VariantVisitor for CowStrDeserializer<'a, E> +impl<'a, E> de::EnumVisitor for CowStrDeserializer<'a, E> where E: de::Error, { type Error = E; + type Variant = private::UnitOnly; - fn visit_variant(&mut self) -> Result + fn visit_variant(self) -> Result<(T, Self::Variant), Self::Error> where T: de::Deserialize, { - de::Deserialize::deserialize(self) - } - - fn visit_unit(&mut self) -> Result<(), Self::Error> { - Ok(()) - } - - fn visit_newtype(&mut self) -> Result - where T: super::Deserialize, - { - Err(super::Error::invalid_type(super::Type::NewtypeVariant)) - } - - fn visit_tuple(&mut self, - _len: usize, - _visitor: V) -> Result - where V: super::Visitor - { - Err(super::Error::invalid_type(super::Type::TupleVariant)) - } - - fn visit_struct(&mut self, - _fields: &'static [&'static str], - _visitor: V) -> Result - where V: super::Visitor - { - Err(super::Error::invalid_type(super::Type::StructVariant)) + de::Deserialize::deserialize(self).map(private::unit_only) } } @@ -540,10 +455,10 @@ impl de::Deserializer for SeqDeserializer { type Error = E; - fn deserialize(&mut self, mut visitor: V) -> Result + fn deserialize(mut self, visitor: V) -> Result where V: de::Visitor, { - let v = try!(visitor.visit_seq(&mut *self)); + let v = try!(visitor.visit_seq(&mut self)); if self.len == 0 { Ok(v) } else { @@ -571,8 +486,7 @@ impl de::SeqVisitor for SeqDeserializer match self.iter.next() { Some(value) => { self.len -= 1; - let mut de = value.into_deserializer(); - Ok(Some(try!(de::Deserialize::deserialize(&mut de)))) + de::Deserialize::deserialize(value.into_deserializer()).map(Some) } None => Ok(None), } @@ -651,8 +565,8 @@ impl de::Deserializer for SeqVisitorDeserializer { type Error = E; - fn deserialize(&mut self, mut visitor: V) -> Result { - visitor.visit_seq(&mut self.visitor) + fn deserialize(self, visitor: V) -> Result { + visitor.visit_seq(self.visitor) } forward_to_deserialize! { @@ -729,29 +643,29 @@ impl de::Deserializer for MapDeserializer { type Error = E; - fn deserialize(&mut self, mut visitor: V_) -> Result + fn deserialize(mut self, visitor: V_) -> Result where V_: de::Visitor, { - let value = try!(visitor.visit_map(&mut *self)); + let value = try!(visitor.visit_map(&mut self)); try!(self.end()); Ok(value) } - fn deserialize_seq(&mut self, mut visitor: V_) -> Result + fn deserialize_seq(mut self, visitor: V_) -> Result where V_: de::Visitor, { - let value = try!(visitor.visit_seq(&mut *self)); + let value = try!(visitor.visit_seq(&mut self)); try!(self.end()); Ok(value) } - fn deserialize_seq_fixed_size(&mut self, len: usize, mut visitor: V_) -> Result + fn deserialize_seq_fixed_size(mut self, len: usize, visitor: V_) -> Result where V_: de::Visitor, { match self.len { Some(map_len) if map_len != len => Err(de::Error::invalid_length(len)), _ => { - let value = try!(visitor.visit_seq(&mut *self)); + let value = try!(visitor.visit_seq(&mut self)); try!(self.end()); Ok(value) } @@ -779,8 +693,7 @@ impl de::MapVisitor for MapDeserializer match self.next() { Some((key, value)) => { self.value = Some(value); - let mut de = key.into_deserializer(); - de::Deserialize::deserialize(&mut de).map(Some) + de::Deserialize::deserialize(key.into_deserializer()).map(Some) } None => Ok(None), } @@ -791,8 +704,7 @@ impl de::MapVisitor for MapDeserializer { match self.value.take() { Some(value) => { - let mut de = value.into_deserializer(); - de::Deserialize::deserialize(&mut de) + de::Deserialize::deserialize(value.into_deserializer()) } None => { Err(de::Error::end_of_stream()) @@ -800,6 +712,20 @@ impl de::MapVisitor for MapDeserializer } } + fn visit(&mut self) -> Result, Self::Error> + where TK: de::Deserialize, + TV: de::Deserialize + { + match self.next() { + Some((key, value)) => { + let key = try!(de::Deserialize::deserialize(key.into_deserializer())); + let value = try!(de::Deserialize::deserialize(value.into_deserializer())); + Ok(Some((key, value))) + } + None => Ok(None) + } + } + fn size_hint(&self) -> (usize, Option) { self.len.map_or_else( || self.iter.size_hint(), @@ -819,9 +745,9 @@ impl de::SeqVisitor for MapDeserializer where T: de::Deserialize, { match self.next() { - Some(kv) => { - let mut de = PairDeserializer(Some(kv), PhantomData); - de::Deserialize::deserialize(&mut de).map(Some) + Some((k, v)) => { + let de = PairDeserializer(k, v, PhantomData); + de::Deserialize::deserialize(de).map(Some) } None => Ok(None), } @@ -834,7 +760,7 @@ impl de::SeqVisitor for MapDeserializer // Used in the `impl SeqVisitor for MapDeserializer` to visit the map as a // sequence of pairs. -struct PairDeserializer(Option<(A, B)>, PhantomData); +struct PairDeserializer(A, B, PhantomData); impl de::Deserializer for PairDeserializer where A: ValueDeserializer, @@ -849,30 +775,25 @@ impl de::Deserializer for PairDeserializer struct_field tuple enum ignored_any } - fn deserialize(&mut self, visitor: V) -> Result + fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { self.deserialize_seq(visitor) } - fn deserialize_seq(&mut self, mut visitor: V) -> Result + fn deserialize_seq(self, visitor: V) -> Result where V: de::Visitor, { - match self.0.take() { - Some((k, v)) => { - let mut pair_visitor = PairVisitor(Some(k), Some(v), PhantomData); - let pair = try!(visitor.visit_seq(&mut pair_visitor)); - if pair_visitor.1.is_none() { - Ok(pair) - } else { - Err(de::Error::invalid_length(pair_visitor.size_hint().0)) - } - } - None => Err(de::Error::end_of_stream()), + let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData); + let pair = try!(visitor.visit_seq(&mut pair_visitor)); + if pair_visitor.1.is_none() { + Ok(pair) + } else { + Err(de::Error::invalid_length(pair_visitor.size_hint().0)) } } - fn deserialize_seq_fixed_size(&mut self, len: usize, visitor: V) -> Result + fn deserialize_seq_fixed_size(self, len: usize, visitor: V) -> Result where V: de::Visitor, { if len == 2 { @@ -896,11 +817,9 @@ impl de::SeqVisitor for PairVisitor where T: de::Deserialize, { if let Some(k) = self.0.take() { - let mut de = k.into_deserializer(); - de::Deserialize::deserialize(&mut de).map(Some) + de::Deserialize::deserialize(k.into_deserializer()).map(Some) } else if let Some(v) = self.1.take() { - let mut de = v.into_deserializer(); - de::Deserialize::deserialize(&mut de).map(Some) + de::Deserialize::deserialize(v.into_deserializer()).map(Some) } else { Ok(None) } @@ -975,8 +894,8 @@ impl de::Deserializer for MapVisitorDeserializer { type Error = E; - fn deserialize(&mut self, mut visitor: V) -> Result { - visitor.visit_map(&mut self.visitor) + fn deserialize(self, visitor: V) -> Result { + visitor.visit_map(self.visitor) } forward_to_deserialize! { @@ -994,25 +913,22 @@ impl<'a, E> ValueDeserializer for bytes::Bytes<'a> type Deserializer = BytesDeserializer<'a, E>; fn into_deserializer(self) -> BytesDeserializer<'a, E> { - BytesDeserializer(Some(self.into()), PhantomData) + BytesDeserializer(self.into(), PhantomData) } } /// A helper deserializer that deserializes a `&[u8]`. -pub struct BytesDeserializer<'a, E> (Option<&'a [u8]>, PhantomData); +pub struct BytesDeserializer<'a, E>(&'a [u8], PhantomData); impl<'a, E> de::Deserializer for BytesDeserializer<'a, E> where E: de::Error { type Error = E; - fn deserialize(&mut self, mut visitor: V) -> Result + fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - match self.0.take() { - Some(bytes) => visitor.visit_bytes(bytes), - None => Err(de::Error::end_of_stream()), - } + visitor.visit_bytes(self.0) } forward_to_deserialize! { @@ -1031,13 +947,13 @@ impl ValueDeserializer for bytes::ByteBuf type Deserializer = ByteBufDeserializer; fn into_deserializer(self) -> Self::Deserializer { - ByteBufDeserializer(Some(self.into()), PhantomData) + ByteBufDeserializer(self.into(), PhantomData) } } /// A helper deserializer that deserializes a `Vec`. #[cfg(any(feature = "std", feature = "collections"))] -pub struct ByteBufDeserializer(Option>, PhantomData); +pub struct ByteBufDeserializer(Vec, PhantomData); #[cfg(any(feature = "std", feature = "collections"))] impl de::Deserializer for ByteBufDeserializer @@ -1045,13 +961,10 @@ impl de::Deserializer for ByteBufDeserializer { type Error = E; - fn deserialize(&mut self, mut visitor: V) -> Result + fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - match self.0.take() { - Some(bytes) => visitor.visit_byte_buf(bytes), - None => Err(de::Error::end_of_stream()), - } + visitor.visit_byte_buf(self.0) } forward_to_deserialize! { @@ -1060,3 +973,48 @@ impl de::Deserializer for ByteBufDeserializer tuple_struct struct struct_field tuple enum ignored_any } } + +/////////////////////////////////////////////////////////////////////////////// + +mod private { + use de; + use core::marker::PhantomData; + + pub struct UnitOnly(PhantomData); + + pub fn unit_only(t: T) -> (T, UnitOnly) { + (t, UnitOnly(PhantomData)) + } + + impl de::VariantVisitor for UnitOnly + where E: de::Error + { + type Error = E; + + fn visit_unit(self) -> Result<(), Self::Error> { + Ok(()) + } + + fn visit_newtype(self) -> Result + where T: de::Deserialize, + { + Err(de::Error::invalid_type(de::Type::NewtypeVariant)) + } + + fn visit_tuple(self, + _len: usize, + _visitor: V) -> Result + where V: de::Visitor + { + Err(de::Error::invalid_type(de::Type::TupleVariant)) + } + + fn visit_struct(self, + _fields: &'static [&'static str], + _visitor: V) -> Result + where V: de::Visitor + { + Err(de::Error::invalid_type(de::Type::StructVariant)) + } + } +} diff --git a/serde/src/macros.rs b/serde/src/macros.rs index d8673ca4..2580980f 100644 --- a/serde/src/macros.rs +++ b/serde/src/macros.rs @@ -4,7 +4,7 @@ macro_rules! forward_to_deserialize_method { ($func:ident($($arg:ty),*)) => { #[inline] - fn $func<__V>(&mut self, $(_: $arg,)* visitor: __V) -> ::std::result::Result<__V::Value, Self::Error> + fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> ::std::result::Result<__V::Value, Self::Error> where __V: $crate::de::Visitor { self.deserialize(visitor) @@ -18,7 +18,7 @@ macro_rules! forward_to_deserialize_method { macro_rules! forward_to_deserialize_method { ($func:ident($($arg:ty),*)) => { #[inline] - fn $func<__V>(&mut self, $(_: $arg,)* visitor: __V) -> ::core::result::Result<__V::Value, Self::Error> + fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> ::core::result::Result<__V::Value, Self::Error> where __V: $crate::de::Visitor { self.deserialize(visitor) @@ -128,7 +128,7 @@ macro_rules! forward_to_deserialize_helper { /// /// ```rust,ignore /// impl Deserializer for MyDeserializer { -/// fn deserialize(&mut self, visitor: V) -> Result +/// fn deserialize(self, visitor: V) -> Result /// where V: Visitor /// { /// /* ... */ diff --git a/serde_codegen/src/de.rs b/serde_codegen/src/de.rs index 808bdabc..32f186ee 100644 --- a/serde_codegen/src/de.rs +++ b/serde_codegen/src/de.rs @@ -36,7 +36,7 @@ pub fn expand_derive_deserialize(item: &syn::MacroInput) -> Result(deserializer: &mut __D) -> ::std::result::Result<#ty, __D::Error> + fn deserialize<__D>(deserializer: __D) -> ::std::result::Result<#ty, __D::Error> where __D: _serde::Deserializer #body } @@ -207,14 +207,14 @@ fn deserialize_unit_struct( type Value = #type_ident; #[inline] - fn visit_unit<__E>(&mut self) -> ::std::result::Result<#type_ident, __E> + fn visit_unit<__E>(self) -> ::std::result::Result<#type_ident, __E> where __E: _serde::de::Error, { Ok(#type_ident) } #[inline] - fn visit_seq<__V>(&mut self, _: __V) -> ::std::result::Result<#type_ident, __V::Error> + fn visit_seq<__V>(self, _: __V) -> ::std::result::Result<#type_ident, __V::Error> where __V: _serde::de::SeqVisitor, { Ok(#type_ident) @@ -265,7 +265,7 @@ fn deserialize_tuple( ); let dispatch = if is_enum { - quote!(visitor.visit_tuple(#nfields, #visitor_expr)) + quote!(_serde::de::VariantVisitor::visit_tuple(visitor, #nfields, #visitor_expr)) } else if nfields == 1 { let type_name = item_attrs.name().deserialize_name(); quote!(deserializer.deserialize_newtype_struct(#type_name, #visitor_expr)) @@ -290,7 +290,7 @@ fn deserialize_tuple( #visit_newtype_struct #[inline] - fn visit_seq<__V>(&mut self, #visitor_var: __V) -> ::std::result::Result<#ty, __V::Error> + fn visit_seq<__V>(self, #visitor_var: __V) -> ::std::result::Result<#ty, __V::Error> where __V: _serde::de::SeqVisitor { #visit_seq @@ -389,7 +389,7 @@ fn deserialize_newtype_struct( }; quote! { #[inline] - fn visit_newtype_struct<__E>(&mut self, __e: &mut __E) -> ::std::result::Result + fn visit_newtype_struct<__E>(self, __e: __E) -> ::std::result::Result where __E: _serde::Deserializer, { Ok(#type_path(#value)) @@ -433,7 +433,7 @@ fn deserialize_struct( let is_enum = variant_ident.is_some(); let dispatch = if is_enum { quote! { - visitor.visit_struct(FIELDS, #visitor_expr) + _serde::de::VariantVisitor::visit_struct(visitor, FIELDS, #visitor_expr) } } else { let type_name = item_attrs.name().deserialize_name(); @@ -458,14 +458,14 @@ fn deserialize_struct( type Value = #ty; #[inline] - fn visit_seq<__V>(&mut self, #visitor_var: __V) -> ::std::result::Result<#ty, __V::Error> + fn visit_seq<__V>(self, #visitor_var: __V) -> ::std::result::Result<#ty, __V::Error> where __V: _serde::de::SeqVisitor { #visit_seq } #[inline] - fn visit_map<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<#ty, __V::Error> + fn visit_map<__V>(self, mut visitor: __V) -> ::std::result::Result<#ty, __V::Error> where __V: _serde::de::MapVisitor { #visit_map @@ -523,7 +523,7 @@ fn deserialize_item_enum( ); quote! { - __Field::#variant_name => #block + (__Field::#variant_name, visitor) => #block } }); @@ -535,7 +535,7 @@ fn deserialize_item_enum( // FIXME: Once we drop support for Rust 1.15: // let Err(err) = visitor.visit_variant::<__Field>(); // Err(err) - visitor.visit_variant::<__Field>().map(|impossible| match impossible {}) + visitor.visit_variant::<__Field>().map(|(impossible, _)| match impossible {}) } } else { quote! { @@ -555,8 +555,8 @@ fn deserialize_item_enum( impl #impl_generics _serde::de::Visitor for #visitor_ty #where_clause { type Value = #ty; - fn visit_enum<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<#ty, __V::Error> - where __V: _serde::de::VariantVisitor, + fn visit_enum<__V>(self, visitor: __V) -> ::std::result::Result<#ty, __V::Error> + where __V: _serde::de::EnumVisitor, { #match_variant } @@ -580,7 +580,7 @@ fn deserialize_variant( match variant.style { Style::Unit => { quote!({ - try!(visitor.visit_unit()); + try!(_serde::de::VariantVisitor::visit_unit(visitor)); Ok(#type_ident::#variant_ident) }) } @@ -624,7 +624,9 @@ fn deserialize_newtype_variant( let visit = match field.attrs.deserialize_with() { None => { let field_ty = &field.ty; - quote!(try!(visitor.visit_newtype::<#field_ty>())) + quote! { + try!(_serde::de::VariantVisitor::visit_newtype::<#field_ty>(visitor)) + } } Some(path) => { let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with( @@ -632,7 +634,7 @@ fn deserialize_newtype_variant( quote!({ #wrapper #wrapper_impl - try!(visitor.visit_newtype::<#wrapper_ty>()).value + try!(_serde::de::VariantVisitor::visit_newtype::<#wrapper_ty>(visitor)).value }) } }; @@ -678,7 +680,7 @@ fn deserialize_field_visitor( impl _serde::Deserialize for __Field { #[inline] - fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<__Field, __D::Error> + fn deserialize<__D>(deserializer: __D) -> ::std::result::Result<__Field, __D::Error> where __D: _serde::Deserializer, { struct __FieldVisitor; @@ -686,7 +688,7 @@ fn deserialize_field_visitor( impl _serde::de::Visitor for __FieldVisitor { type Value = __Field; - fn visit_str<__E>(&mut self, value: &str) -> ::std::result::Result<__Field, __E> + fn visit_str<__E>(self, value: &str) -> ::std::result::Result<__Field, __E> where __E: _serde::de::Error { match value { @@ -896,7 +898,7 @@ fn wrap_deserialize_with( }, quote! { impl #impl_generics _serde::Deserialize for #wrapper_ty #where_clause { - fn deserialize<__D>(__d: &mut __D) -> ::std::result::Result + fn deserialize<__D>(__d: __D) -> ::std::result::Result where __D: _serde::Deserializer { let value = try!(#deserialize_with(__d)); diff --git a/serde_test/src/de.rs b/serde_test/src/de.rs index 559e9972..80ba9e80 100644 --- a/serde_test/src/de.rs +++ b/serde_test/src/de.rs @@ -3,11 +3,13 @@ use std::iter; use serde::de::{ self, Deserialize, + EnumVisitor, MapVisitor, SeqVisitor, VariantVisitor, Visitor, }; +use serde::de::value::ValueDeserializer; use error::Error; use token::Token; @@ -44,7 +46,7 @@ impl Deserializer } } - fn visit_seq(&mut self, len: Option, mut visitor: V) -> Result + fn visit_seq(&mut self, len: Option, visitor: V) -> Result where V: Visitor, { let value = try!(visitor.visit_seq(DeserializerSeqVisitor { @@ -55,7 +57,7 @@ impl Deserializer Ok(value) } - fn visit_array(&mut self, len: usize, mut visitor: V) -> Result + fn visit_array(&mut self, len: usize, visitor: V) -> Result where V: Visitor, { let value = try!(visitor.visit_seq(DeserializerArrayVisitor { @@ -66,7 +68,7 @@ impl Deserializer Ok(value) } - fn visit_tuple(&mut self, len: usize, mut visitor: V) -> Result + fn visit_tuple(&mut self, len: usize, visitor: V) -> Result where V: Visitor, { let value = try!(visitor.visit_seq(DeserializerTupleVisitor { @@ -77,7 +79,7 @@ impl Deserializer Ok(value) } - fn visit_tuple_struct(&mut self, len: usize, mut visitor: V) -> Result + fn visit_tuple_struct(&mut self, len: usize, visitor: V) -> Result where V: Visitor, { let value = try!(visitor.visit_seq(DeserializerTupleStructVisitor { @@ -88,7 +90,7 @@ impl Deserializer Ok(value) } - fn visit_variant_seq(&mut self, len: Option, mut visitor: V) -> Result + fn visit_variant_seq(&mut self, len: Option, visitor: V) -> Result where V: Visitor, { let value = try!(visitor.visit_seq(DeserializerVariantSeqVisitor { @@ -99,7 +101,7 @@ impl Deserializer Ok(value) } - fn visit_map(&mut self, len: Option, mut visitor: V) -> Result + fn visit_map(&mut self, len: Option, visitor: V) -> Result where V: Visitor, { let value = try!(visitor.visit_map(DeserializerMapVisitor { @@ -110,7 +112,7 @@ impl Deserializer Ok(value) } - fn visit_struct(&mut self, fields: &'static [&'static str], mut visitor: V) -> Result + fn visit_struct(&mut self, fields: &'static [&'static str], visitor: V) -> Result where V: Visitor, { let value = try!(visitor.visit_map(DeserializerStructVisitor { @@ -121,7 +123,7 @@ impl Deserializer Ok(value) } - fn visit_variant_map(&mut self, len: Option, mut visitor: V) -> Result + fn visit_variant_map(&mut self, len: Option, visitor: V) -> Result where V: Visitor, { let value = try!(visitor.visit_map(DeserializerVariantMapVisitor { @@ -133,101 +135,101 @@ impl Deserializer } } -impl de::Deserializer for Deserializer +impl<'a, I> de::Deserializer for &'a mut Deserializer where I: Iterator>, { type Error = Error; - fn deserialize_seq<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_seq<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_struct_field<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_struct_field<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_map<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_map<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_unit<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_unit<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_bytes<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_bytes<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_ignored_any<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_ignored_any<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_string<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_string<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_str<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_str<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_char<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_char<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_i64<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_i64<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_i32<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_i32<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_i16<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_i16<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_i8<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_i8<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_u64<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_u64<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_u32<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_u32<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_u16<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_u16<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_u8<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_u8<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_f32<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_f32<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_f64<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_f64<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_bool<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_bool<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_usize<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_usize<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize_isize<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error> + fn deserialize_isize<__V>(self, visitor: __V) -> Result<__V::Value, Self::Error> where __V: de::Visitor { self.deserialize(visitor) } - fn deserialize(&mut self, mut visitor: V) -> Result + fn deserialize(self, visitor: V) -> Result where V: Visitor, { match self.tokens.next() { @@ -271,7 +273,7 @@ impl de::Deserializer for Deserializer /// Hook into `Option` deserializing so we can treat `Unit` as a /// `None`, or a regular value as `Some(value)`. - fn deserialize_option(&mut self, mut visitor: V) -> Result + fn deserialize_option(self, visitor: V) -> Result where V: Visitor, { match self.tokens.peek() { @@ -292,17 +294,17 @@ impl de::Deserializer for Deserializer } } - fn deserialize_enum(&mut self, + fn deserialize_enum(self, name: &str, _variants: &'static [&'static str], - mut visitor: V) -> Result + visitor: V) -> Result where V: Visitor, { match self.tokens.peek() { Some(&Token::EnumStart(n)) if name == n => { self.tokens.next(); - visitor.visit_enum(DeserializerVariantVisitor { + visitor.visit_enum(DeserializerEnumVisitor { de: self, }) } @@ -310,7 +312,7 @@ impl de::Deserializer for Deserializer | Some(&Token::EnumNewType(n, _)) | Some(&Token::EnumSeqStart(n, _, _)) | Some(&Token::EnumMapStart(n, _, _)) if name == n => { - visitor.visit_enum(DeserializerVariantVisitor { + visitor.visit_enum(DeserializerEnumVisitor { de: self, }) } @@ -322,7 +324,7 @@ impl de::Deserializer for Deserializer } } - fn deserialize_unit_struct(&mut self, name: &str, mut visitor: V) -> Result + fn deserialize_unit_struct(self, name: &str, visitor: V) -> Result where V: Visitor, { match self.tokens.peek() { @@ -339,9 +341,9 @@ impl de::Deserializer for Deserializer } } - fn deserialize_newtype_struct(&mut self, + fn deserialize_newtype_struct(self, name: &str, - mut visitor: V) -> Result + visitor: V) -> Result where V: Visitor, { match self.tokens.peek() { @@ -358,7 +360,7 @@ impl de::Deserializer for Deserializer } } - fn deserialize_seq_fixed_size(&mut self, + fn deserialize_seq_fixed_size(self, len: usize, visitor: V) -> Result where V: Visitor, @@ -373,9 +375,9 @@ impl de::Deserializer for Deserializer } } - fn deserialize_tuple(&mut self, + fn deserialize_tuple(self, len: usize, - mut visitor: V) -> Result + visitor: V) -> Result where V: Visitor, { match self.tokens.peek() { @@ -408,10 +410,10 @@ impl de::Deserializer for Deserializer } } - fn deserialize_tuple_struct(&mut self, + fn deserialize_tuple_struct(self, name: &str, len: usize, - mut visitor: V) -> Result + visitor: V) -> Result where V: Visitor, { match self.tokens.peek() { @@ -452,7 +454,7 @@ impl de::Deserializer for Deserializer } } - fn deserialize_struct(&mut self, + fn deserialize_struct(self, name: &str, fields: &'static [&'static str], visitor: V) -> Result @@ -496,7 +498,7 @@ impl<'a, I> SeqVisitor for DeserializerSeqVisitor<'a, I> Some(&Token::SeqSep) => { self.de.tokens.next(); self.len = self.len.map(|len| len - 1); - Ok(Some(try!(Deserialize::deserialize(self.de)))) + Deserialize::deserialize(&mut *self.de).map(Some) } Some(&Token::SeqEnd) => Ok(None), Some(_) => { @@ -532,7 +534,7 @@ impl<'a, I> SeqVisitor for DeserializerArrayVisitor<'a, I> Some(&Token::SeqSep) => { self.de.tokens.next(); self.len -= 1; - Ok(Some(try!(Deserialize::deserialize(self.de)))) + Deserialize::deserialize(&mut *self.de).map(Some) } Some(&Token::SeqEnd) => Ok(None), Some(_) => { @@ -567,7 +569,7 @@ impl<'a, I> SeqVisitor for DeserializerTupleVisitor<'a, I> Some(&Token::TupleSep) => { self.de.tokens.next(); self.len -= 1; - Ok(Some(try!(Deserialize::deserialize(self.de)))) + Deserialize::deserialize(&mut *self.de).map(Some) } Some(&Token::TupleEnd) => Ok(None), Some(_) => { @@ -602,7 +604,7 @@ impl<'a, I> SeqVisitor for DeserializerTupleStructVisitor<'a, I> Some(&Token::TupleStructSep) => { self.de.tokens.next(); self.len -= 1; - Ok(Some(try!(Deserialize::deserialize(self.de)))) + Deserialize::deserialize(&mut *self.de).map(Some) } Some(&Token::TupleStructEnd) => Ok(None), Some(_) => { @@ -637,7 +639,7 @@ impl<'a, I> SeqVisitor for DeserializerVariantSeqVisitor<'a, I> Some(&Token::EnumSeqSep) => { self.de.tokens.next(); self.len = self.len.map(|len| len - 1); - Ok(Some(try!(Deserialize::deserialize(self.de)))) + Deserialize::deserialize(&mut *self.de).map(Some) } Some(&Token::EnumSeqEnd) => Ok(None), Some(_) => { @@ -673,7 +675,7 @@ impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I> Some(&Token::MapSep) => { self.de.tokens.next(); self.len = self.len.map(|len| if len > 0 { len - 1} else { 0 }); - Ok(Some(try!(Deserialize::deserialize(self.de)))) + Deserialize::deserialize(&mut *self.de).map(Some) } Some(&Token::MapEnd) => Ok(None), Some(_) => { @@ -687,7 +689,7 @@ impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I> fn visit_value(&mut self) -> Result where V: Deserialize, { - Ok(try!(Deserialize::deserialize(self.de))) + Deserialize::deserialize(&mut *self.de) } fn size_hint(&self) -> (usize, Option) { @@ -715,7 +717,7 @@ impl<'a, I> MapVisitor for DeserializerStructVisitor<'a, I> Some(&Token::StructSep) => { self.de.tokens.next(); self.len = self.len.saturating_sub(1); - Ok(Some(try!(Deserialize::deserialize(self.de)))) + Deserialize::deserialize(&mut *self.de).map(Some) } Some(&Token::StructEnd) => Ok(None), Some(_) => { @@ -729,7 +731,7 @@ impl<'a, I> MapVisitor for DeserializerStructVisitor<'a, I> fn visit_value(&mut self) -> Result where V: Deserialize, { - Ok(try!(Deserialize::deserialize(self.de))) + Deserialize::deserialize(&mut *self.de) } fn size_hint(&self) -> (usize, Option) { @@ -739,16 +741,17 @@ impl<'a, I> MapVisitor for DeserializerStructVisitor<'a, I> ////////////////////////////////////////////////////////////////////////// -struct DeserializerVariantVisitor<'a, I: 'a> where I: Iterator> { +struct DeserializerEnumVisitor<'a, I: 'a> where I: Iterator> { de: &'a mut Deserializer, } -impl<'a, I> VariantVisitor for DeserializerVariantVisitor<'a, I> +impl<'a, I> EnumVisitor for DeserializerEnumVisitor<'a, I> where I: Iterator>, { type Error = Error; + type Variant = Self; - fn visit_variant(&mut self) -> Result + fn visit_variant(self) -> Result<(V, Self), Error> where V: Deserialize, { match self.de.tokens.peek() { @@ -756,18 +759,25 @@ impl<'a, I> VariantVisitor for DeserializerVariantVisitor<'a, I> | Some(&Token::EnumNewType(_, v)) | Some(&Token::EnumSeqStart(_, v, _)) | Some(&Token::EnumMapStart(_, v, _)) => { - let mut de = de::value::ValueDeserializer::::into_deserializer(v); - let value = try!(Deserialize::deserialize(&mut de)); - Ok(value) + let de = v.into_deserializer(); + let value = try!(Deserialize::deserialize(de)); + Ok((value, self)) } Some(_) => { - Deserialize::deserialize(self.de) + let value = try!(Deserialize::deserialize(&mut *self.de)); + Ok((value, self)) } None => Err(Error::EndOfStream), } } +} - fn visit_unit(&mut self) -> Result<(), Error> { +impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I> + where I: Iterator> +{ + type Error = Error; + + fn visit_unit(self) -> Result<(), Error> { match self.de.tokens.peek() { Some(&Token::EnumUnit(_, _)) => { self.de.tokens.next(); @@ -780,7 +790,7 @@ impl<'a, I> VariantVisitor for DeserializerVariantVisitor<'a, I> } } - fn visit_newtype(&mut self) -> Result + fn visit_newtype(self) -> Result where T: Deserialize, { match self.de.tokens.peek() { @@ -795,7 +805,7 @@ impl<'a, I> VariantVisitor for DeserializerVariantVisitor<'a, I> } } - fn visit_tuple(&mut self, + fn visit_tuple(self, len: usize, visitor: V) -> Result where V: Visitor, @@ -826,7 +836,7 @@ impl<'a, I> VariantVisitor for DeserializerVariantVisitor<'a, I> } } - fn visit_struct(&mut self, + fn visit_struct(self, fields: &'static [&'static str], visitor: V) -> Result where V: Visitor, @@ -877,7 +887,7 @@ impl<'a, I> MapVisitor for DeserializerVariantMapVisitor<'a, I> Some(&Token::EnumMapSep) => { self.de.tokens.next(); self.len = self.len.map(|len| if len > 0 { len - 1} else { 0 }); - Ok(Some(try!(Deserialize::deserialize(self.de)))) + Deserialize::deserialize(&mut *self.de).map(Some) } Some(&Token::EnumMapEnd) => Ok(None), Some(_) => { @@ -891,7 +901,7 @@ impl<'a, I> MapVisitor for DeserializerVariantMapVisitor<'a, I> fn visit_value(&mut self) -> Result where V: Deserialize, { - Ok(try!(Deserialize::deserialize(self.de))) + Deserialize::deserialize(&mut *self.de) } fn size_hint(&self) -> (usize, Option) { diff --git a/testing/tests/test_annotations.rs b/testing/tests/test_annotations.rs index ba77f8f9..b2971fea 100644 --- a/testing/tests/test_annotations.rs +++ b/testing/tests/test_annotations.rs @@ -25,7 +25,7 @@ trait SerializeWith: Sized { } trait DeserializeWith: Sized { - fn deserialize_with(de: &mut D) -> Result + fn deserialize_with(de: D) -> Result where D: Deserializer; } @@ -50,7 +50,7 @@ impl SerializeWith for i32 { } impl DeserializeWith for i32 { - fn deserialize_with(de: &mut D) -> Result + fn deserialize_with(de: D) -> Result where D: Deserializer { if try!(Deserialize::deserialize(de)) { @@ -239,7 +239,7 @@ impl Default for NotDeserializeStruct { } impl DeserializeWith for NotDeserializeStruct { - fn deserialize_with(_: &mut D) -> Result + fn deserialize_with(_: D) -> Result where D: Deserializer { panic!() diff --git a/testing/tests/test_gen.rs b/testing/tests/test_gen.rs index 24338312..3945a924 100644 --- a/testing/tests/test_gen.rs +++ b/testing/tests/test_gen.rs @@ -299,7 +299,7 @@ trait SerializeWith { } trait DeserializeWith: Sized { - fn deserialize_with(_: &mut D) -> StdResult; + fn deserialize_with(_: D) -> StdResult; } // Implements neither Serialize nor Deserialize @@ -309,7 +309,7 @@ fn ser_x(_: &X, _: &mut S) -> StdResult<(), S::Error> { unimplemented!() } -fn de_x(_: &mut D) -> StdResult { +fn de_x(_: D) -> StdResult { unimplemented!() } @@ -320,7 +320,7 @@ impl SerializeWith for X { } impl DeserializeWith for X { - fn deserialize_with(_: &mut D) -> StdResult { + fn deserialize_with(_: D) -> StdResult { unimplemented!() } }