Enforce correct use of Deserialize trait

This commit is contained in:
David Tolnay 2017-01-14 11:27:45 -08:00
parent 787ecc6a82
commit 738aa31733
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
5 changed files with 312 additions and 383 deletions

View File

@ -182,14 +182,14 @@ mod bytebuf {
type Value = ByteBuf;
#[inline]
fn visit_unit<E>(&mut self) -> Result<ByteBuf, E>
fn visit_unit<E>(self) -> Result<ByteBuf, E>
where E: de::Error,
{
Ok(ByteBuf::new())
}
#[inline]
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<ByteBuf, V::Error>
fn visit_seq<V>(self, mut visitor: V) -> Result<ByteBuf, V::Error>
where V: de::SeqVisitor,
{
let (len, _) = visitor.size_hint();
@ -203,26 +203,26 @@ mod bytebuf {
}
#[inline]
fn visit_bytes<E>(&mut self, v: &[u8]) -> Result<ByteBuf, E>
fn visit_bytes<E>(self, v: &[u8]) -> Result<ByteBuf, E>
where E: de::Error,
{
Ok(ByteBuf::from(v))
}
#[inline]
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<ByteBuf, E>
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<ByteBuf, E>
where E: de::Error,
{
Ok(ByteBuf::from(v))
}
fn visit_str<E>(&mut self, v: &str) -> Result<ByteBuf, E>
fn visit_str<E>(self, v: &str) -> Result<ByteBuf, E>
where E: de::Error,
{
Ok(ByteBuf::from(v))
}
fn visit_string<E>(&mut self, v: String) -> Result<ByteBuf, E>
fn visit_string<E>(self, v: String) -> Result<ByteBuf, E>
where E: de::Error,
{
Ok(ByteBuf::from(v))
@ -231,7 +231,7 @@ mod bytebuf {
impl de::Deserialize for ByteBuf {
#[inline]
fn deserialize<D>(deserializer: &mut D) -> Result<ByteBuf, D::Error>
fn deserialize<D>(deserializer: D) -> Result<ByteBuf, D::Error>
where D: de::Deserializer
{
deserializer.deserialize_bytes(ByteBufVisitor)

View File

@ -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<E>(&mut self) -> Result<(), E>
fn visit_unit<E>(self) -> Result<(), E>
where E: Error,
{
Ok(())
}
fn visit_seq<V>(&mut self, _: V) -> Result<(), V::Error>
fn visit_seq<V>(self, _: V) -> Result<(), V::Error>
where V: SeqVisitor,
{
Ok(())
@ -96,7 +97,7 @@ impl Visitor for UnitVisitor {
}
impl Deserialize for () {
fn deserialize<D>(deserializer: &mut D) -> Result<(), D::Error>
fn deserialize<D>(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<E>(&mut self, v: bool) -> Result<bool, E>
fn visit_bool<E>(self, v: bool) -> Result<bool, E>
where E: Error,
{
Ok(v)
}
fn visit_str<E>(&mut self, s: &str) -> Result<bool, E>
fn visit_str<E>(self, s: &str) -> Result<bool, E>
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<D>(deserializer: &mut D) -> Result<bool, D::Error>
fn deserialize<D>(deserializer: D) -> Result<bool, D::Error>
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<E>(&mut self, v: $src_ty) -> Result<T, E>
fn $method<E>(self, v: $src_ty) -> Result<T, E>
where E: Error,
{
match FromPrimitive::$from_method(v) {
@ -186,7 +187,7 @@ impl<T> Visitor for PrimitiveVisitor<T>
impl_deserialize_num_method!(f64, visit_f64, from_f64, Type::F64);
#[inline]
fn visit_str<E>(&mut self, s: &str) -> Result<T, E>
fn visit_str<E>(self, s: &str) -> Result<T, E>
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<D>(deserializer: &mut D) -> Result<$ty, D::Error>
fn deserialize<D>(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<E>(&mut self, v: char) -> Result<char, E>
fn visit_char<E>(self, v: char) -> Result<char, E>
where E: Error,
{
Ok(v)
}
#[inline]
fn visit_str<E>(&mut self, v: &str) -> Result<char, E>
fn visit_str<E>(self, v: &str) -> Result<char, E>
where E: Error,
{
let mut iter = v.chars();
@ -254,7 +255,7 @@ impl Visitor for CharVisitor {
impl Deserialize for char {
#[inline]
fn deserialize<D>(deserializer: &mut D) -> Result<char, D::Error>
fn deserialize<D>(deserializer: D) -> Result<char, D::Error>
where D: Deserializer,
{
deserializer.deserialize_char(CharVisitor)
@ -270,25 +271,25 @@ struct StringVisitor;
impl Visitor for StringVisitor {
type Value = String;
fn visit_str<E>(&mut self, v: &str) -> Result<String, E>
fn visit_str<E>(self, v: &str) -> Result<String, E>
where E: Error,
{
Ok(v.to_owned())
}
fn visit_string<E>(&mut self, v: String) -> Result<String, E>
fn visit_string<E>(self, v: String) -> Result<String, E>
where E: Error,
{
Ok(v)
}
fn visit_unit<E>(&mut self) -> Result<String, E>
fn visit_unit<E>(self) -> Result<String, E>
where E: Error,
{
Ok(String::new())
}
fn visit_bytes<E>(&mut self, v: &[u8]) -> Result<String, E>
fn visit_bytes<E>(self, v: &[u8]) -> Result<String, E>
where E: Error,
{
match str::from_utf8(v) {
@ -297,7 +298,7 @@ impl Visitor for StringVisitor {
}
}
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<String, E>
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<String, E>
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<D>(deserializer: &mut D) -> Result<String, D::Error>
fn deserialize<D>(deserializer: D) -> Result<String, D::Error>
where D: Deserializer,
{
deserializer.deserialize_string(StringVisitor)
@ -328,21 +329,21 @@ impl<
type Value = Option<T>;
#[inline]
fn visit_unit<E>(&mut self) -> Result<Option<T>, E>
fn visit_unit<E>(self) -> Result<Option<T>, E>
where E: Error,
{
Ok(None)
}
#[inline]
fn visit_none<E>(&mut self) -> Result<Option<T>, E>
fn visit_none<E>(self) -> Result<Option<T>, E>
where E: Error,
{
Ok(None)
}
#[inline]
fn visit_some<D>(&mut self, deserializer: &mut D) -> Result<Option<T>, D::Error>
fn visit_some<D>(self, deserializer: D) -> Result<Option<T>, D::Error>
where D: Deserializer,
{
Ok(Some(try!(Deserialize::deserialize(deserializer))))
@ -350,7 +351,7 @@ impl<
}
impl<T> Deserialize for Option<T> where T: Deserialize {
fn deserialize<D>(deserializer: &mut D) -> Result<Option<T>, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Option<T>, D::Error>
where D: Deserializer,
{
deserializer.deserialize_option(OptionVisitor { marker: PhantomData })
@ -368,7 +369,7 @@ impl<T> Visitor for PhantomDataVisitor<T> {
type Value = PhantomData<T>;
#[inline]
fn visit_unit<E>(&mut self) -> Result<PhantomData<T>, E>
fn visit_unit<E>(self) -> Result<PhantomData<T>, E>
where E: Error,
{
Ok(PhantomData)
@ -376,7 +377,7 @@ impl<T> Visitor for PhantomDataVisitor<T> {
}
impl<T> Deserialize for PhantomData<T> {
fn deserialize<D>(deserializer: &mut D) -> Result<PhantomData<T>, D::Error>
fn deserialize<D>(deserializer: D) -> Result<PhantomData<T>, 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<E>(&mut self) -> Result<$ty, E>
fn visit_unit<E>(self) -> Result<$ty, E>
where E: Error,
{
Ok($ctor)
}
#[inline]
fn visit_seq<V>(&mut self, mut $visitor: V) -> Result<$ty, V::Error>
fn visit_seq<V>(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<D>(deserializer: &mut D) -> Result<$ty, D::Error>
fn deserialize<D>(deserializer: D) -> Result<$ty, D::Error>
where D: Deserializer,
{
deserializer.deserialize_seq($visitor_ty::new())
@ -531,14 +532,14 @@ impl<T> Visitor for ArrayVisitor<[T; 0]> where T: Deserialize {
type Value = [T; 0];
#[inline]
fn visit_unit<E>(&mut self) -> Result<[T; 0], E>
fn visit_unit<E>(self) -> Result<[T; 0], E>
where E: Error,
{
Ok([])
}
#[inline]
fn visit_seq<V>(&mut self, _: V) -> Result<[T; 0], V::Error>
fn visit_seq<V>(self, _: V) -> Result<[T; 0], V::Error>
where V: SeqVisitor,
{
Ok([])
@ -548,7 +549,7 @@ impl<T> Visitor for ArrayVisitor<[T; 0]> where T: Deserialize {
impl<T> Deserialize for [T; 0]
where T: Deserialize
{
fn deserialize<D>(deserializer: &mut D) -> Result<[T; 0], D::Error>
fn deserialize<D>(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<V>(&mut self, mut visitor: V) -> Result<[T; $len], V::Error>
fn visit_seq<V>(self, mut visitor: V) -> Result<[T; $len], V::Error>
where V: SeqVisitor,
{
$(
@ -579,7 +580,7 @@ macro_rules! array_impls {
impl<T> Deserialize for [T; $len]
where T: Deserialize,
{
fn deserialize<D>(deserializer: &mut D) -> Result<[T; $len], D::Error>
fn deserialize<D>(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<V>(&mut self, mut visitor: V) -> Result<($($name,)+), V::Error>
fn visit_seq<V>(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<D>(deserializer: &mut D) -> Result<($($name,)+), D::Error>
fn deserialize<D>(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<E>(&mut self) -> Result<$ty, E>
fn visit_unit<E>(self) -> Result<$ty, E>
where E: Error,
{
Ok($ctor)
}
#[inline]
fn visit_map<Visitor>(&mut self, mut $visitor: Visitor) -> Result<$ty, Visitor::Error>
fn visit_map<Visitor>(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<D>(deserializer: &mut D) -> Result<$ty, D::Error>
fn deserialize<D>(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<D>(deserializer: &mut D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<D>(deserializer: &mut D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<D>(deserializer: &mut D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<D>(deserializer: &mut D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<D>(deserializer: &mut D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<D>(deserializer: &mut D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<E>(&mut self, v: &str) -> Result<path::PathBuf, E>
fn visit_str<E>(self, v: &str) -> Result<path::PathBuf, E>
where E: Error,
{
Ok(From::from(v))
}
fn visit_string<E>(&mut self, v: String) -> Result<path::PathBuf, E>
fn visit_string<E>(self, v: String) -> Result<path::PathBuf, E>
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<D>(deserializer: &mut D) -> Result<path::PathBuf, D::Error>
fn deserialize<D>(deserializer: D) -> Result<path::PathBuf, D::Error>
where D: Deserializer,
{
deserializer.deserialize_string(PathBufVisitor)
@ -891,7 +892,7 @@ impl Deserialize for path::PathBuf {
#[cfg(any(feature = "std", feature = "alloc"))]
impl<T: Deserialize> Deserialize for Box<T> {
fn deserialize<D>(deserializer: &mut D) -> Result<Box<T>, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Box<T>, D::Error>
where D: Deserializer,
{
let val = try!(Deserialize::deserialize(deserializer));
@ -901,7 +902,7 @@ impl<T: Deserialize> Deserialize for Box<T> {
#[cfg(any(feature = "std", feature = "collections"))]
impl<T: Deserialize> Deserialize for Box<[T]> {
fn deserialize<D>(deserializer: &mut D) -> Result<Box<[T]>, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Box<[T]>, D::Error>
where D: Deserializer,
{
let v: Vec<T> = try!(Deserialize::deserialize(deserializer));
@ -911,7 +912,7 @@ impl<T: Deserialize> Deserialize for Box<[T]> {
#[cfg(any(feature = "std", feature = "collections"))]
impl Deserialize for Box<str> {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer
{
let s = try!(String::deserialize(deserializer));
@ -921,7 +922,7 @@ impl Deserialize for Box<str> {
#[cfg(any(feature = "std", feature = "alloc"))]
impl<T: Deserialize> Deserialize for Arc<T> {
fn deserialize<D>(deserializer: &mut D) -> Result<Arc<T>, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Arc<T>, D::Error>
where D: Deserializer,
{
let val = try!(Deserialize::deserialize(deserializer));
@ -931,7 +932,7 @@ impl<T: Deserialize> Deserialize for Arc<T> {
#[cfg(any(feature = "std", feature = "alloc"))]
impl<T: Deserialize> Deserialize for Rc<T> {
fn deserialize<D>(deserializer: &mut D) -> Result<Rc<T>, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Rc<T>, D::Error>
where D: Deserializer,
{
let val = try!(Deserialize::deserialize(deserializer));
@ -942,7 +943,7 @@ impl<T: Deserialize> Deserialize for Rc<T> {
#[cfg(any(feature = "std", feature = "collections"))]
impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deserialize, {
#[inline]
fn deserialize<D>(deserializer: &mut D) -> Result<Cow<'a, T>, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Cow<'a, T>, 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<D>(deserializer: &mut D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer,
{
enum Field { Secs, Nanos };
impl Deserialize for Field {
fn deserialize<D>(deserializer: &mut D) -> Result<Field, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
where D: Deserializer,
{
struct FieldVisitor;
@ -976,7 +977,7 @@ impl Deserialize for Duration {
impl Visitor for FieldVisitor {
type Value = Field;
fn visit_usize<E>(&mut self, value: usize) -> Result<Field, E>
fn visit_usize<E>(self, value: usize) -> Result<Field, E>
where E: Error,
{
match value {
@ -986,7 +987,7 @@ impl Deserialize for Duration {
}
}
fn visit_str<E>(&mut self, value: &str) -> Result<Field, E>
fn visit_str<E>(self, value: &str) -> Result<Field, E>
where E: Error,
{
match value {
@ -996,7 +997,7 @@ impl Deserialize for Duration {
}
}
fn visit_bytes<E>(&mut self, value: &[u8]) -> Result<Field, E>
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E>
where E: Error,
{
match value {
@ -1019,7 +1020,7 @@ impl Deserialize for Duration {
impl Visitor for DurationVisitor {
type Value = Duration;
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<Duration, V::Error>
fn visit_seq<V>(self, mut visitor: V) -> Result<Duration, V::Error>
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<V>(&mut self, mut visitor: V) -> Result<Duration, V::Error>
fn visit_map<V>(self, mut visitor: V) -> Result<Duration, V::Error>
where V: MapVisitor,
{
let mut secs: Option<u64> = None;
@ -1079,7 +1080,7 @@ impl Deserialize for Duration {
#[cfg(feature = "unstable")]
impl<T> Deserialize for NonZero<T> where T: Deserialize + PartialEq + Zeroable + Zero {
fn deserialize<D>(deserializer: &mut D) -> Result<NonZero<T>, D::Error> where D: Deserializer {
fn deserialize<D>(deserializer: D) -> Result<NonZero<T>, 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<T> Deserialize for NonZero<T> where T: Deserialize + PartialEq + Zeroable +
impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
fn deserialize<D>(deserializer: &mut D) -> Result<Result<T, E>, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Result<T, E>, D::Error>
where D: Deserializer {
enum Field {
Ok,
@ -1103,7 +1104,7 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
impl Deserialize for Field {
#[inline]
fn deserialize<D>(deserializer: &mut D) -> Result<Field, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
where D: Deserializer
{
struct FieldVisitor;
@ -1112,7 +1113,7 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
type Value = Field;
#[cfg(any(feature = "std", feature = "collections"))]
fn visit_usize<E>(&mut self, value: usize) -> Result<Field, E> where E: Error {
fn visit_usize<E>(self, value: usize) -> Result<Field, E> where E: Error {
#[cfg(feature = "collections")]
use collections::string::ToString;
match value {
@ -1123,7 +1124,7 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
}
#[cfg(all(not(feature = "std"), not(feature = "collections")))]
fn visit_usize<E>(&mut self, value: usize) -> Result<Field, E> where E: Error {
fn visit_usize<E>(self, value: usize) -> Result<Field, E> where E: Error {
match value {
0 => Ok(Field::Ok),
1 => Ok(Field::Err),
@ -1131,7 +1132,7 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
}
}
fn visit_str<E>(&mut self, value: &str) -> Result<Field, E> where E: Error {
fn visit_str<E>(self, value: &str) -> Result<Field, E> where E: Error {
match value {
"Ok" => Ok(Field::Ok),
"Err" => Ok(Field::Err),
@ -1139,7 +1140,7 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
}
}
fn visit_bytes<E>(&mut self, value: &[u8]) -> Result<Field, E> where E: Error {
fn visit_bytes<E>(self, value: &[u8]) -> Result<Field, E> where E: Error {
match value {
b"Ok" => Ok(Field::Ok),
b"Err" => Ok(Field::Err),
@ -1165,18 +1166,12 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
{
type Value = Result<T, E>;
fn visit_enum<V>(&mut self, mut visitor: V) -> Result<Result<T, E>, V::Error>
where V: VariantVisitor
fn visit_enum<V>(self, visitor: V) -> Result<Result<T, E>, 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<D>(deserializer: &mut D) -> Result<IgnoredAny, D::Error>
fn deserialize<D>(deserializer: D) -> Result<IgnoredAny, D::Error>
where D: Deserializer,
{
struct IgnoredAnyVisitor;
@ -1204,58 +1199,58 @@ impl Deserialize for IgnoredAny {
type Value = IgnoredAny;
#[inline]
fn visit_bool<E>(&mut self, _: bool) -> Result<IgnoredAny, E> {
fn visit_bool<E>(self, _: bool) -> Result<IgnoredAny, E> {
Ok(IgnoredAny)
}
#[inline]
fn visit_i64<E>(&mut self, _: i64) -> Result<IgnoredAny, E> {
fn visit_i64<E>(self, _: i64) -> Result<IgnoredAny, E> {
Ok(IgnoredAny)
}
#[inline]
fn visit_u64<E>(&mut self, _: u64) -> Result<IgnoredAny, E> {
fn visit_u64<E>(self, _: u64) -> Result<IgnoredAny, E> {
Ok(IgnoredAny)
}
#[inline]
fn visit_f64<E>(&mut self, _: f64) -> Result<IgnoredAny, E> {
fn visit_f64<E>(self, _: f64) -> Result<IgnoredAny, E> {
Ok(IgnoredAny)
}
#[inline]
fn visit_str<E>(&mut self, _: &str) -> Result<IgnoredAny, E>
fn visit_str<E>(self, _: &str) -> Result<IgnoredAny, E>
where E: Error,
{
Ok(IgnoredAny)
}
#[inline]
fn visit_none<E>(&mut self) -> Result<IgnoredAny, E> {
fn visit_none<E>(self) -> Result<IgnoredAny, E> {
Ok(IgnoredAny)
}
#[inline]
fn visit_some<D>(&mut self, _: &mut D) -> Result<IgnoredAny, D::Error>
fn visit_some<D>(self, _: D) -> Result<IgnoredAny, D::Error>
where D: Deserializer,
{
Ok(IgnoredAny)
}
#[inline]
fn visit_newtype_struct<D>(&mut self, _: &mut D) -> Result<IgnoredAny, D::Error>
fn visit_newtype_struct<D>(self, _: D) -> Result<IgnoredAny, D::Error>
where D: Deserializer,
{
Ok(IgnoredAny)
}
#[inline]
fn visit_unit<E>(&mut self) -> Result<IgnoredAny, E> {
fn visit_unit<E>(self) -> Result<IgnoredAny, E> {
Ok(IgnoredAny)
}
#[inline]
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<IgnoredAny, V::Error>
fn visit_seq<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
where V: SeqVisitor,
{
while let Some(_) = try!(visitor.visit::<IgnoredAny>()) {
@ -1265,7 +1260,7 @@ impl Deserialize for IgnoredAny {
}
#[inline]
fn visit_map<V>(&mut self, mut visitor: V) -> Result<IgnoredAny, V::Error>
fn visit_map<V>(self, mut visitor: V) -> Result<IgnoredAny, V::Error>
where V: MapVisitor,
{
while let Some((_, _)) = try!(visitor.visit::<IgnoredAny, IgnoredAny>()) {
@ -1275,7 +1270,7 @@ impl Deserialize for IgnoredAny {
}
#[inline]
fn visit_bytes<E>(&mut self, _: &[u8]) -> Result<IgnoredAny, E>
fn visit_bytes<E>(self, _: &[u8]) -> Result<IgnoredAny, E>
where E: Error,
{
Ok(IgnoredAny)

View File

@ -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<D>(deserializer: &mut D) -> Result<Self, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting a `bool` value.
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: 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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_usize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `u64` value.
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: 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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_isize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `i64` value.
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: 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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting a `f64` value.
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: Visitor;
/// This method hints that the `Deserialize` type is expecting a `char` value.
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: Visitor;
/// This method hints that the `Deserialize` type is expecting a `&str` value.
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: Visitor;
/// This method hints that the `Deserialize` type is expecting a `String` value.
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: Visitor;
/// This method hints that the `Deserialize` type is expecting an `unit` value.
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: 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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self,
fn deserialize_seq_fixed_size<V>(self,
len: usize,
visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
@ -346,17 +346,17 @@ pub trait Deserializer {
/// This method hints that the `Deserialize` type is expecting a `Vec<u8>`. This allows
/// deserializers that provide a custom byte vector serialization to properly deserialize the
/// type.
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: 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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self,
fn deserialize_unit_struct<V>(self,
name: &'static str,
visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self,
fn deserialize_newtype_struct<V>(self,
name: &'static str,
visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self,
fn deserialize_tuple_struct<V>(self,
name: &'static str,
len: usize,
visitor: V) -> Result<V::Value, Self::Error>
@ -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<V>(&mut self,
fn deserialize_struct<V>(self,
name: &'static str,
fields: &'static [&'static str],
visitor: V) -> Result<V::Value, Self::Error>
@ -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<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: 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<V>(&mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self,
fn deserialize_enum<V>(self,
name: &'static str,
variants: &'static [&'static str],
visitor: V) -> Result<V::Value, Self::Error>
@ -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<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: 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<E>(&mut self, v: bool) -> Result<Self::Value, E>
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
where E: Error,
{
let _ = v;
@ -427,35 +427,35 @@ pub trait Visitor {
}
/// `visit_isize` deserializes a `isize` into a `Value`.
fn visit_isize<E>(&mut self, v: isize) -> Result<Self::Value, E>
fn visit_isize<E>(self, v: isize) -> Result<Self::Value, E>
where E: Error,
{
self.visit_i64(v as i64)
}
/// `visit_i8` deserializes a `i8` into a `Value`.
fn visit_i8<E>(&mut self, v: i8) -> Result<Self::Value, E>
fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
where E: Error,
{
self.visit_i64(v as i64)
}
/// `visit_i16` deserializes a `i16` into a `Value`.
fn visit_i16<E>(&mut self, v: i16) -> Result<Self::Value, E>
fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
where E: Error,
{
self.visit_i64(v as i64)
}
/// `visit_i32` deserializes a `i32` into a `Value`.
fn visit_i32<E>(&mut self, v: i32) -> Result<Self::Value, E>
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
where E: Error,
{
self.visit_i64(v as i64)
}
/// `visit_i64` deserializes a `i64` into a `Value`.
fn visit_i64<E>(&mut self, v: i64) -> Result<Self::Value, E>
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where E: Error,
{
let _ = v;
@ -463,35 +463,35 @@ pub trait Visitor {
}
/// `visit_usize` deserializes a `usize` into a `Value`.
fn visit_usize<E>(&mut self, v: usize) -> Result<Self::Value, E>
fn visit_usize<E>(self, v: usize) -> Result<Self::Value, E>
where E: Error,
{
self.visit_u64(v as u64)
}
/// `visit_u8` deserializes a `u8` into a `Value`.
fn visit_u8<E>(&mut self, v: u8) -> Result<Self::Value, E>
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
where E: Error,
{
self.visit_u64(v as u64)
}
/// `visit_u16` deserializes a `u16` into a `Value`.
fn visit_u16<E>(&mut self, v: u16) -> Result<Self::Value, E>
fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
where E: Error,
{
self.visit_u64(v as u64)
}
/// `visit_u32` deserializes a `u32` into a `Value`.
fn visit_u32<E>(&mut self, v: u32) -> Result<Self::Value, E>
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
where E: Error,
{
self.visit_u64(v as u64)
}
/// `visit_u64` deserializes a `u64` into a `Value`.
fn visit_u64<E>(&mut self, v: u64) -> Result<Self::Value, E>
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where E: Error,
{
let _ = v;
@ -499,14 +499,14 @@ pub trait Visitor {
}
/// `visit_f32` deserializes a `f32` into a `Value`.
fn visit_f32<E>(&mut self, v: f32) -> Result<Self::Value, E>
fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
where E: Error,
{
self.visit_f64(v as f64)
}
/// `visit_f64` deserializes a `f64` into a `Value`.
fn visit_f64<E>(&mut self, v: f64) -> Result<Self::Value, E>
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where E: Error,
{
let _ = v;
@ -515,14 +515,14 @@ pub trait Visitor {
/// `visit_char` deserializes a `char` into a `Value`.
#[inline]
fn visit_char<E>(&mut self, v: char) -> Result<Self::Value, E>
fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
where E: Error,
{
self.visit_str(::utils::encode_utf8(v).as_str())
}
/// `visit_str` deserializes a `&str` into a `Value`.
fn visit_str<E>(&mut self, v: &str) -> Result<Self::Value, E>
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
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<E>(&mut self, v: String) -> Result<Self::Value, E>
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where E: Error,
{
self.visit_str(&v)
}
/// `visit_unit` deserializes a `()` into a `Value`.
fn visit_unit<E>(&mut self) -> Result<Self::Value, E>
fn visit_unit<E>(self) -> Result<Self::Value, E>
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<E>(&mut self, name: &'static str) -> Result<Self::Value, E>
fn visit_unit_struct<E>(self, name: &'static str) -> Result<Self::Value, E>
where E: Error,
{
let _ = name;
@ -557,14 +557,14 @@ pub trait Visitor {
}
/// `visit_none` deserializes a none value into a `Value`.
fn visit_none<E>(&mut self) -> Result<Self::Value, E>
fn visit_none<E>(self) -> Result<Self::Value, E>
where E: Error,
{
Err(Error::invalid_type(Type::Option))
}
/// `visit_some` deserializes a value into a `Value`.
fn visit_some<D>(&mut self, deserializer: &mut D) -> Result<Self::Value, D::Error>
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
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<D>(&mut self, deserializer: &mut D) -> Result<Self::Value, D::Error>
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer,
{
let _ = deserializer;
@ -580,7 +580,7 @@ pub trait Visitor {
}
/// `visit_seq` deserializes a `SeqVisitor` into a `Value`.
fn visit_seq<V>(&mut self, visitor: V) -> Result<Self::Value, V::Error>
fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: SeqVisitor,
{
let _ = visitor;
@ -588,23 +588,23 @@ pub trait Visitor {
}
/// `visit_map` deserializes a `MapVisitor` into a `Value`.
fn visit_map<V>(&mut self, visitor: V) -> Result<Self::Value, V::Error>
fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: MapVisitor,
{
let _ = visitor;
Err(Error::invalid_type(Type::Map))
}
/// `visit_enum` deserializes a `VariantVisitor` into a `Value`.
fn visit_enum<V>(&mut self, visitor: V) -> Result<Self::Value, V::Error>
where V: VariantVisitor,
/// `visit_enum` deserializes a `EnumVisitor` into a `Value`.
fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
where V: EnumVisitor,
{
let _ = visitor;
Err(Error::invalid_type(Type::Enum))
}
/// `visit_bytes` deserializes a `&[u8]` into a `Value`.
fn visit_bytes<E>(&mut self, v: &[u8]) -> Result<Self::Value, E>
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where E: Error,
{
let _ = v;
@ -613,7 +613,7 @@ pub trait Visitor {
/// `visit_byte_buf` deserializes a `Vec<u8>` into a `Value`.
#[cfg(any(feature = "std", feature = "collections"))]
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<Self::Value, E>
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
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<Error=Self::Error>;
/// `visit_variant` is called to identify which variant to deserialize.
fn visit_variant<V>(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<V>(&mut self) -> Result<V, Self::Error>
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<T>(&mut self) -> Result<T, Self::Error>
fn visit_newtype<T>(self) -> Result<T, Self::Error>
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<V>(&mut self,
fn visit_tuple<V>(self,
len: usize,
visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self,
fn visit_struct<V>(self,
fields: &'static [&'static str],
visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor;
}
impl<'a, T> VariantVisitor for &'a mut T where T: VariantVisitor {
type Error = T::Error;
fn visit_variant<V>(&mut self) -> Result<V, T::Error>
where V: Deserialize
{
(**self).visit_variant()
}
fn visit_unit(&mut self) -> Result<(), T::Error> {
(**self).visit_unit()
}
fn visit_newtype<D>(&mut self) -> Result<D, T::Error>
where D: Deserialize,
{
(**self).visit_newtype()
}
fn visit_tuple<V>(&mut self,
len: usize,
visitor: V) -> Result<V::Value, T::Error>
where V: Visitor,
{
(**self).visit_tuple(len, visitor)
}
fn visit_struct<V>(&mut self,
fields: &'static [&'static str],
visitor: V) -> Result<V::Value, T::Error>
where V: Visitor,
{
(**self).visit_struct(fields, visitor)
}
}

View File

@ -184,13 +184,13 @@ impl<E> de::Deserializer for UnitDeserializer<E>
tuple_struct struct struct_field tuple enum ignored_any
}
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
{
visitor.visit_unit()
}
fn deserialize_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
{
visitor.visit_none()
@ -202,7 +202,7 @@ impl<E> de::Deserializer for UnitDeserializer<E>
macro_rules! primitive_deserializer {
($ty:ty, $name:ident, $method:ident) => {
/// A helper deserializer that deserializes a number.
pub struct $name<E>(Option<$ty>, PhantomData<E>);
pub struct $name<E>($ty, PhantomData<E>);
impl<E> ValueDeserializer<E> for $ty
where E: de::Error,
@ -210,7 +210,7 @@ macro_rules! primitive_deserializer {
type Deserializer = $name<E>;
fn into_deserializer(self) -> $name<E> {
$name(Some(self), PhantomData)
$name(self, PhantomData)
}
}
@ -226,13 +226,10 @@ macro_rules! primitive_deserializer {
ignored_any
}
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<E>);
pub struct StrDeserializer<'a, E>(&'a str, PhantomData<E>);
impl<'a, E> ValueDeserializer<E> for &'a str
where E: de::Error,
@ -264,7 +261,7 @@ impl<'a, E> ValueDeserializer<E> 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<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self,
fn deserialize_enum<V>(self,
_name: &str,
_variants: &'static [&'static str],
mut visitor: V) -> Result<V::Value, Self::Error>
visitor: V) -> Result<V::Value, Self::Error>
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<E>;
fn visit_variant<T>(&mut self) -> Result<T, Self::Error>
fn visit_variant<T>(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<T>(&mut self) -> Result<T, Self::Error>
where T: super::Deserialize,
{
Err(super::Error::invalid_type(super::Type::NewtypeVariant))
}
fn visit_tuple<V>(&mut self,
_len: usize,
_visitor: V) -> Result<V::Value, Self::Error>
where V: super::Visitor
{
Err(super::Error::invalid_type(super::Type::TupleVariant))
}
fn visit_struct<V>(&mut self,
_fields: &'static [&'static str],
_visitor: V) -> Result<V::Value, Self::Error>
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<E>(Option<String>, PhantomData<E>);
pub struct StringDeserializer<E>(String, PhantomData<E>);
#[cfg(any(feature = "std", feature = "collections"))]
impl<E> ValueDeserializer<E> for String
@ -349,7 +318,7 @@ impl<E> ValueDeserializer<E> for String
type Deserializer = StringDeserializer<E>;
fn into_deserializer(self) -> StringDeserializer<E> {
StringDeserializer(Some(self), PhantomData)
StringDeserializer(self, PhantomData)
}
}
@ -359,19 +328,16 @@ impl<E> de::Deserializer for StringDeserializer<E>
{
type Error = E;
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self,
fn deserialize_enum<V>(self,
_name: &str,
_variants: &'static [&'static str],
mut visitor: V) -> Result<V::Value, Self::Error>
visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
{
visitor.visit_enum(self)
@ -385,41 +351,16 @@ impl<E> de::Deserializer for StringDeserializer<E>
}
#[cfg(any(feature = "std", feature = "collections"))]
impl<'a, E> de::VariantVisitor for StringDeserializer<E>
impl<'a, E> de::EnumVisitor for StringDeserializer<E>
where E: de::Error,
{
type Error = E;
type Variant = private::UnitOnly<E>;
fn visit_variant<T>(&mut self) -> Result<T, Self::Error>
fn visit_variant<T>(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<T>(&mut self) -> Result<T, Self::Error>
where T: super::Deserialize,
{
Err(super::Error::invalid_type(super::Type::NewtypeVariant))
}
fn visit_tuple<V>(&mut self,
_len: usize,
_visitor: V) -> Result<V::Value, Self::Error>
where V: super::Visitor
{
Err(super::Error::invalid_type(super::Type::TupleVariant))
}
fn visit_struct<V>(&mut self,
_fields: &'static [&'static str],
_visitor: V) -> Result<V::Value, Self::Error>
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<E>
/// A helper deserializer that deserializes a `String`.
#[cfg(any(feature = "std", feature = "collections"))]
pub struct CowStrDeserializer<'a, E>(Option<Cow<'a, str>>, PhantomData<E>);
pub struct CowStrDeserializer<'a, E>(Cow<'a, str>, PhantomData<E>);
#[cfg(any(feature = "std", feature = "collections"))]
impl<'a, E> ValueDeserializer<E> for Cow<'a, str>
@ -436,7 +377,7 @@ impl<'a, E> ValueDeserializer<E> 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<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self,
fn deserialize_enum<V>(self,
_name: &str,
_variants: &'static [&'static str],
mut visitor: V) -> Result<V::Value, Self::Error>
visitor: V) -> Result<V::Value, Self::Error>
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<E>;
fn visit_variant<T>(&mut self) -> Result<T, Self::Error>
fn visit_variant<T>(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<T>(&mut self) -> Result<T, Self::Error>
where T: super::Deserialize,
{
Err(super::Error::invalid_type(super::Type::NewtypeVariant))
}
fn visit_tuple<V>(&mut self,
_len: usize,
_visitor: V) -> Result<V::Value, Self::Error>
where V: super::Visitor
{
Err(super::Error::invalid_type(super::Type::TupleVariant))
}
fn visit_struct<V>(&mut self,
_fields: &'static [&'static str],
_visitor: V) -> Result<V::Value, Self::Error>
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<I, T, E> de::Deserializer for SeqDeserializer<I, E>
{
type Error = E;
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
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<I, T, E> de::SeqVisitor for SeqDeserializer<I, E>
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<V_, E> de::Deserializer for SeqVisitorDeserializer<V_, E>
{
type Error = E;
fn deserialize<V: de::Visitor>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error> {
visitor.visit_seq(&mut self.visitor)
fn deserialize<V: de::Visitor>(self, visitor: V) -> Result<V::Value, Self::Error> {
visitor.visit_seq(self.visitor)
}
forward_to_deserialize! {
@ -729,29 +643,29 @@ impl<I, K, V, E> de::Deserializer for MapDeserializer<I, K, V, E>
{
type Error = E;
fn deserialize<V_>(&mut self, mut visitor: V_) -> Result<V_::Value, Self::Error>
fn deserialize<V_>(mut self, visitor: V_) -> Result<V_::Value, Self::Error>
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<V_>(&mut self, mut visitor: V_) -> Result<V_::Value, Self::Error>
fn deserialize_seq<V_>(mut self, visitor: V_) -> Result<V_::Value, Self::Error>
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<V_>(&mut self, len: usize, mut visitor: V_) -> Result<V_::Value, Self::Error>
fn deserialize_seq_fixed_size<V_>(mut self, len: usize, visitor: V_) -> Result<V_::Value, Self::Error>
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<I, K, V, E> de::MapVisitor for MapDeserializer<I, K, V, E>
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<I, K, V, E> de::MapVisitor for MapDeserializer<I, K, V, E>
{
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<I, K, V, E> de::MapVisitor for MapDeserializer<I, K, V, E>
}
}
fn visit<TK, TV>(&mut self) -> Result<Option<(TK, TV)>, 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<usize>) {
self.len.map_or_else(
|| self.iter.size_hint(),
@ -819,9 +745,9 @@ impl<I, K, V, E> de::SeqVisitor for MapDeserializer<I, K, V, E>
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<I, K, V, E> de::SeqVisitor for MapDeserializer<I, K, V, E>
// Used in the `impl SeqVisitor for MapDeserializer` to visit the map as a
// sequence of pairs.
struct PairDeserializer<A, B, E>(Option<(A, B)>, PhantomData<E>);
struct PairDeserializer<A, B, E>(A, B, PhantomData<E>);
impl<A, B, E> de::Deserializer for PairDeserializer<A, B, E>
where A: ValueDeserializer<E>,
@ -849,30 +775,25 @@ impl<A, B, E> de::Deserializer for PairDeserializer<A, B, E>
struct_field tuple enum ignored_any
}
fn deserialize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
{
self.deserialize_seq(visitor)
}
fn deserialize_seq<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<V>(&mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_seq_fixed_size<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
{
if len == 2 {
@ -896,11 +817,9 @@ impl<A, B, E> de::SeqVisitor for PairVisitor<A, B, E>
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<V_, E> de::Deserializer for MapVisitorDeserializer<V_, E>
{
type Error = E;
fn deserialize<V: de::Visitor>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error> {
visitor.visit_map(&mut self.visitor)
fn deserialize<V: de::Visitor>(self, visitor: V) -> Result<V::Value, Self::Error> {
visitor.visit_map(self.visitor)
}
forward_to_deserialize! {
@ -994,25 +913,22 @@ impl<'a, E> ValueDeserializer<E> 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<E>);
pub struct BytesDeserializer<'a, E>(&'a [u8], PhantomData<E>);
impl<'a, E> de::Deserializer for BytesDeserializer<'a, E>
where E: de::Error
{
type Error = E;
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<E> ValueDeserializer<E> for bytes::ByteBuf
type Deserializer = ByteBufDeserializer<E>;
fn into_deserializer(self) -> Self::Deserializer {
ByteBufDeserializer(Some(self.into()), PhantomData)
ByteBufDeserializer(self.into(), PhantomData)
}
}
/// A helper deserializer that deserializes a `Vec<u8>`.
#[cfg(any(feature = "std", feature = "collections"))]
pub struct ByteBufDeserializer<E>(Option<Vec<u8>>, PhantomData<E>);
pub struct ByteBufDeserializer<E>(Vec<u8>, PhantomData<E>);
#[cfg(any(feature = "std", feature = "collections"))]
impl<E> de::Deserializer for ByteBufDeserializer<E>
@ -1045,13 +961,10 @@ impl<E> de::Deserializer for ByteBufDeserializer<E>
{
type Error = E;
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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<E> de::Deserializer for ByteBufDeserializer<E>
tuple_struct struct struct_field tuple enum ignored_any
}
}
///////////////////////////////////////////////////////////////////////////////
#[cfg(any(feature = "std", feature = "collections"))]
mod private {
use super::*;
pub struct UnitOnly<E>(PhantomData<E>);
pub fn unit_only<T, E>(t: T) -> (T, UnitOnly<E>) {
(t, UnitOnly(PhantomData))
}
impl<E> de::VariantVisitor for UnitOnly<E>
where E: de::Error
{
type Error = E;
fn visit_unit(self) -> Result<(), Self::Error> {
Ok(())
}
fn visit_newtype<T>(self) -> Result<T, Self::Error>
where T: de::Deserialize,
{
Err(de::Error::invalid_type(de::Type::NewtypeVariant))
}
fn visit_tuple<V>(self,
_len: usize,
_visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor
{
Err(de::Error::invalid_type(de::Type::TupleVariant))
}
fn visit_struct<V>(self,
_fields: &'static [&'static str],
_visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor
{
Err(de::Error::invalid_type(de::Type::StructVariant))
}
}
}

View File

@ -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<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
/// fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
/// where V: Visitor
/// {
/// /* ... */