diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 7e040490..06ae2e45 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -1658,6 +1658,17 @@ pub trait VariantVisitor<'de>: Sized { /////////////////////////////////////////////////////////////////////////////// +/// This trait converts primitive types into a deserializer. +pub trait IntoDeserializer<'de, E: Error = value::Error> { + /// The actual deserializer type. + type Deserializer: Deserializer<'de, Error = E>; + + /// Convert this value into a deserializer. + fn into_deserializer(self) -> Self::Deserializer; +} + +/////////////////////////////////////////////////////////////////////////////// + /// Used in error messages. /// /// - expected `a` diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index 5a261334..83e50a56 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -2,7 +2,7 @@ use lib::*; -use de::{self, Expected, SeqVisitor}; +use de::{self, IntoDeserializer, Expected, SeqVisitor}; /////////////////////////////////////////////////////////////////////////////// @@ -50,18 +50,7 @@ impl error::Error for Error { /////////////////////////////////////////////////////////////////////////////// -/// This trait converts primitive types into a deserializer. -pub trait ValueDeserializer<'de, E: de::Error = Error> { - /// The actual deserializer type. - type Deserializer: de::Deserializer<'de, Error = E>; - - /// Convert this value into a deserializer. - fn into_deserializer(self) -> Self::Deserializer; -} - -/////////////////////////////////////////////////////////////////////////////// - -impl<'de, E> ValueDeserializer<'de, E> for () +impl<'de, E> IntoDeserializer<'de, E> for () where E: de::Error { type Deserializer = UnitDeserializer; @@ -110,7 +99,7 @@ macro_rules! primitive_deserializer { marker: PhantomData } - impl<'de, E> ValueDeserializer<'de, E> for $ty + impl<'de, E> IntoDeserializer<'de, E> for $ty where E: de::Error, { type Deserializer = $name; @@ -163,7 +152,7 @@ pub struct U32Deserializer { marker: PhantomData, } -impl<'de, E> ValueDeserializer<'de, E> for u32 +impl<'de, E> IntoDeserializer<'de, E> for u32 where E: de::Error { type Deserializer = U32Deserializer; @@ -225,7 +214,7 @@ pub struct StrDeserializer<'a, E> { marker: PhantomData, } -impl<'de, 'a, E> ValueDeserializer<'de, E> for &'a str +impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a str where E: de::Error { type Deserializer = StrDeserializer<'a, E>; @@ -289,7 +278,7 @@ pub struct StringDeserializer { } #[cfg(any(feature = "std", feature = "collections"))] -impl<'de, E> ValueDeserializer<'de, E> for String +impl<'de, E> IntoDeserializer<'de, E> for String where E: de::Error { type Deserializer = StringDeserializer; @@ -355,7 +344,7 @@ pub struct CowStrDeserializer<'a, E> { } #[cfg(any(feature = "std", feature = "collections"))] -impl<'de, 'a, E> ValueDeserializer<'de, E> for Cow<'a, str> +impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str> where E: de::Error { type Deserializer = CowStrDeserializer<'a, E>; @@ -455,7 +444,7 @@ impl SeqDeserializer impl<'de, I, T, E> de::Deserializer<'de> for SeqDeserializer where I: Iterator, - T: ValueDeserializer<'de, E>, + T: IntoDeserializer<'de, E>, E: de::Error { type Error = E; @@ -477,7 +466,7 @@ impl<'de, I, T, E> de::Deserializer<'de> for SeqDeserializer impl<'de, I, T, E> de::SeqVisitor<'de> for SeqDeserializer where I: Iterator, - T: ValueDeserializer<'de, E>, + T: IntoDeserializer<'de, E>, E: de::Error { type Error = E; @@ -514,8 +503,8 @@ impl Expected for ExpectedInSeq { /////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "collections"))] -impl<'de, T, E> ValueDeserializer<'de, E> for Vec - where T: ValueDeserializer<'de, E>, +impl<'de, T, E> IntoDeserializer<'de, E> for Vec + where T: IntoDeserializer<'de, E>, E: de::Error { type Deserializer = SeqDeserializer< as IntoIterator>::IntoIter, E>; @@ -526,8 +515,8 @@ impl<'de, T, E> ValueDeserializer<'de, E> for Vec } #[cfg(any(feature = "std", feature = "collections"))] -impl<'de, T, E> ValueDeserializer<'de, E> for BTreeSet - where T: ValueDeserializer<'de, E> + Eq + Ord, +impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet + where T: IntoDeserializer<'de, E> + Eq + Ord, E: de::Error { type Deserializer = SeqDeserializer< as IntoIterator>::IntoIter, E>; @@ -538,8 +527,8 @@ impl<'de, T, E> ValueDeserializer<'de, E> for BTreeSet } #[cfg(feature = "std")] -impl<'de, T, E> ValueDeserializer<'de, E> for HashSet - where T: ValueDeserializer<'de, E> + Eq + Hash, +impl<'de, T, E> IntoDeserializer<'de, E> for HashSet + where T: IntoDeserializer<'de, E> + Eq + Hash, E: de::Error { type Deserializer = SeqDeserializer< as IntoIterator>::IntoIter, E>; @@ -589,8 +578,8 @@ impl<'de, V_> de::Deserializer<'de> for SeqVisitorDeserializer pub struct MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, - ::First: ValueDeserializer<'de, E>, - ::Second: ValueDeserializer<'de, E>, + ::First: IntoDeserializer<'de, E>, + ::Second: IntoDeserializer<'de, E>, E: de::Error { iter: iter::Fuse, @@ -603,8 +592,8 @@ pub struct MapDeserializer<'de, I, E> impl<'de, I, E> MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, - ::First: ValueDeserializer<'de, E>, - ::Second: ValueDeserializer<'de, E>, + ::First: IntoDeserializer<'de, E>, + ::Second: IntoDeserializer<'de, E>, E: de::Error { /// Construct a new `MapDeserializer`. @@ -650,8 +639,8 @@ impl<'de, I, E> MapDeserializer<'de, I, E> impl<'de, I, E> de::Deserializer<'de> for MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, - ::First: ValueDeserializer<'de, E>, - ::Second: ValueDeserializer<'de, E>, + ::First: IntoDeserializer<'de, E>, + ::Second: IntoDeserializer<'de, E>, E: de::Error { type Error = E; @@ -691,8 +680,8 @@ impl<'de, I, E> de::Deserializer<'de> for MapDeserializer<'de, I, E> impl<'de, I, E> de::MapVisitor<'de> for MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, - ::First: ValueDeserializer<'de, E>, - ::Second: ValueDeserializer<'de, E>, + ::First: IntoDeserializer<'de, E>, + ::Second: IntoDeserializer<'de, E>, E: de::Error { type Error = E; @@ -744,8 +733,8 @@ impl<'de, I, E> de::MapVisitor<'de> for MapDeserializer<'de, I, E> impl<'de, I, E> de::SeqVisitor<'de> for MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, - ::First: ValueDeserializer<'de, E>, - ::Second: ValueDeserializer<'de, E>, + ::First: IntoDeserializer<'de, E>, + ::Second: IntoDeserializer<'de, E>, E: de::Error { type Error = E; @@ -772,8 +761,8 @@ impl<'de, I, E> de::SeqVisitor<'de> for MapDeserializer<'de, I, E> struct PairDeserializer(A, B, PhantomData); impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer - where A: ValueDeserializer<'de, E>, - B: ValueDeserializer<'de, E>, + where A: IntoDeserializer<'de, E>, + B: IntoDeserializer<'de, E>, E: de::Error { type Error = E; @@ -821,8 +810,8 @@ impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer struct PairVisitor(Option, Option, PhantomData); impl<'de, A, B, E> de::SeqVisitor<'de> for PairVisitor - where A: ValueDeserializer<'de, E>, - B: ValueDeserializer<'de, E>, + where A: IntoDeserializer<'de, E>, + B: IntoDeserializer<'de, E>, E: de::Error { type Error = E; @@ -866,9 +855,9 @@ impl Expected for ExpectedInMap { /////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "collections"))] -impl<'de, K, V, E> ValueDeserializer<'de, E> for BTreeMap - where K: ValueDeserializer<'de, E> + Eq + Ord, - V: ValueDeserializer<'de, E>, +impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap + where K: IntoDeserializer<'de, E> + Eq + Ord, + V: IntoDeserializer<'de, E>, E: de::Error { type Deserializer = MapDeserializer<'de, as IntoIterator>::IntoIter, E>; @@ -879,9 +868,9 @@ impl<'de, K, V, E> ValueDeserializer<'de, E> for BTreeMap } #[cfg(feature = "std")] -impl<'de, K, V, E> ValueDeserializer<'de, E> for HashMap - where K: ValueDeserializer<'de, E> + Eq + Hash, - V: ValueDeserializer<'de, E>, +impl<'de, K, V, E> IntoDeserializer<'de, E> for HashMap + where K: IntoDeserializer<'de, E> + Eq + Hash, + V: IntoDeserializer<'de, E>, E: de::Error { type Deserializer = MapDeserializer<'de, as IntoIterator>::IntoIter, E>; diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 394c72cb..9d56071d 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1410,7 +1410,7 @@ mod content { } } - impl<'de, E> de::value::ValueDeserializer<'de, E> for ContentDeserializer + impl<'de, E> de::IntoDeserializer<'de, E> for ContentDeserializer where E: de::Error { type Deserializer = Self; @@ -1420,7 +1420,7 @@ mod content { } } - impl<'de, 'a, E> de::value::ValueDeserializer<'de, E> for ContentRefDeserializer<'a, E> + impl<'de, 'a, E> de::IntoDeserializer<'de, E> for ContentRefDeserializer<'a, E> where E: de::Error { type Deserializer = Self; diff --git a/serde_test/src/de.rs b/serde_test/src/de.rs index 243c8eb6..75996fc5 100644 --- a/serde_test/src/de.rs +++ b/serde_test/src/de.rs @@ -1,6 +1,6 @@ -use serde::de::{self, Deserialize, DeserializeSeed, EnumVisitor, MapVisitor, SeqVisitor, - VariantVisitor, Visitor}; -use serde::de::value::{ValueDeserializer, MapVisitorDeserializer, SeqVisitorDeserializer}; +use serde::de::{self, Deserialize, DeserializeSeed, EnumVisitor, IntoDeserializer, + MapVisitor, SeqVisitor, VariantVisitor, Visitor}; +use serde::de::value::{MapVisitorDeserializer, SeqVisitorDeserializer}; use error::Error; use token::Token; diff --git a/test_suite/tests/test_value.rs b/test_suite/tests/test_value.rs index fc444163..08b1c3c3 100644 --- a/test_suite/tests/test_value.rs +++ b/test_suite/tests/test_value.rs @@ -3,7 +3,7 @@ extern crate serde_derive; extern crate serde; use serde::Deserialize; -use serde::de::value::{self, ValueDeserializer}; +use serde::de::{value, IntoDeserializer}; #[test] fn test_u32_to_enum() { @@ -13,7 +13,7 @@ fn test_u32_to_enum() { B, } - let deserializer = ValueDeserializer::::into_deserializer(1u32); + let deserializer = IntoDeserializer::::into_deserializer(1u32); let e: E = E::deserialize(deserializer).unwrap(); assert_eq!(E::B, e); }