Use forward_deserializer macro for define StrDeserializer for IdentifierDeserializer

This commit is contained in:
Mingun
2020-10-23 14:06:04 +05:00
parent 094f63b86a
commit 9e1f573f88
4 changed files with 16 additions and 36 deletions
+1
View File
@@ -116,6 +116,7 @@ use lib::*;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#[macro_use]
pub mod value; pub mod value;
mod from_primitive; mod from_primitive;
+12 -8
View File
@@ -32,6 +32,8 @@ use ser;
/// For structs that contain a PhantomData. We do not want the trait /// For structs that contain a PhantomData. We do not want the trait
/// bound `E: Clone` inferred by derive(Clone). /// bound `E: Clone` inferred by derive(Clone).
#[doc(hidden)]
#[macro_export]
macro_rules! impl_copy_clone { macro_rules! impl_copy_clone {
($ty:ident $(<$lifetime:tt>)*) => { ($ty:ident $(<$lifetime:tt>)*) => {
impl<$($lifetime,)* E> Copy for $ty<$($lifetime,)* E> {} impl<$($lifetime,)* E> Copy for $ty<$($lifetime,)* E> {}
@@ -45,6 +47,8 @@ macro_rules! impl_copy_clone {
} }
/// Creates a deserializer any method of which forwards to the specified visitor method /// Creates a deserializer any method of which forwards to the specified visitor method
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! forward_deserializer { macro_rules! forward_deserializer {
// Non-borrowed references // Non-borrowed references
( (
@@ -72,15 +76,15 @@ macro_rules! forward_deserializer {
impl_copy_clone!($deserializer $(<$lifetime>)*); impl_copy_clone!($deserializer $(<$lifetime>)*);
impl<'de, $($lifetime,)* E> de::Deserializer<'de> for $deserializer<$($lifetime,)* E> impl<'de, $($lifetime,)* E> $crate::de::Deserializer<'de> for $deserializer<$($lifetime,)* E>
where where
E: de::Error, E: $crate::de::Error,
{ {
type Error = E; type Error = E;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_any<V>(self, visitor: V) -> $crate::export::Result<V::Value, Self::Error>
where where
V: de::Visitor<'de>, V: $crate::de::Visitor<'de>,
{ {
visitor.$visit(self.value) visitor.$visit(self.value)
} }
@@ -116,15 +120,15 @@ macro_rules! forward_deserializer {
impl_copy_clone!($deserializer<'de>); impl_copy_clone!($deserializer<'de>);
impl<'de, E> de::Deserializer<'de> for $deserializer<'de, E> impl<'de, E> $crate::de::Deserializer<'de> for $deserializer<'de, E>
where where
E: de::Error, E: $crate::de::Error,
{ {
type Error = E; type Error = E;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_any<V>(self, visitor: V) -> $crate::export::Result<V::Value, Self::Error>
where where
V: de::Visitor<'de>, V: $crate::de::Visitor<'de>,
{ {
visitor.$visit(self.value) visitor.$visit(self.value)
} }
+1
View File
@@ -256,6 +256,7 @@ mod macros;
#[macro_use] #[macro_use]
mod integer128; mod integer128;
#[macro_use]
pub mod de; pub mod de;
pub mod ser; pub mod ser;
+2 -28
View File
@@ -2554,10 +2554,7 @@ where
} }
} }
pub struct StrDeserializer<'a, E> { forward_deserializer!(ref StrDeserializer<'a>(&'a str) => visit_str);
value: &'a str,
marker: PhantomData<E>,
}
impl<'a, E> IdentifierDeserializer<'a, E> for &'a str impl<'a, E> IdentifierDeserializer<'a, E> for &'a str
where where
@@ -2566,30 +2563,7 @@ where
type Deserializer = StrDeserializer<'a, E>; type Deserializer = StrDeserializer<'a, E>;
fn from(self) -> Self::Deserializer { fn from(self) -> Self::Deserializer {
StrDeserializer { StrDeserializer::new(self)
value: self,
marker: PhantomData,
}
}
}
impl<'de, 'a, E> Deserializer<'de> for StrDeserializer<'a, E>
where
E: Error,
{
type Error = E;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_str(self.value)
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
} }
} }