Skip to content

Commit

Permalink
Use forward_deserializer macro for define StrDeserializer for Identif…
Browse files Browse the repository at this point in the history
…ierDeserializer
  • Loading branch information
Mingun committed Oct 23, 2020
1 parent 094f63b commit 9e1f573
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 36 deletions.
1 change: 1 addition & 0 deletions serde/src/de/mod.rs
Expand Up @@ -116,6 +116,7 @@ use lib::*;

////////////////////////////////////////////////////////////////////////////////

#[macro_use]
pub mod value;

mod from_primitive;
Expand Down
20 changes: 12 additions & 8 deletions serde/src/de/value.rs
Expand Up @@ -32,6 +32,8 @@ use ser;

/// For structs that contain a PhantomData. We do not want the trait
/// bound `E: Clone` inferred by derive(Clone).
#[doc(hidden)]
#[macro_export]
macro_rules! impl_copy_clone {
($ty:ident $(<$lifetime:tt>)*) => {
impl<$($lifetime,)* E> Copy for $ty<$($lifetime,)* E> {}
Expand All @@ -45,6 +47,8 @@ macro_rules! impl_copy_clone {
}

/// Creates a deserializer any method of which forwards to the specified visitor method
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! forward_deserializer {
// Non-borrowed references
(
Expand Down Expand Up @@ -72,15 +76,15 @@ macro_rules! forward_deserializer {

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
E: de::Error,
E: $crate::de::Error,
{
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
V: de::Visitor<'de>,
V: $crate::de::Visitor<'de>,
{
visitor.$visit(self.value)
}
Expand Down Expand Up @@ -116,15 +120,15 @@ macro_rules! forward_deserializer {

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
E: de::Error,
E: $crate::de::Error,
{
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
V: de::Visitor<'de>,
V: $crate::de::Visitor<'de>,
{
visitor.$visit(self.value)
}
Expand Down
1 change: 1 addition & 0 deletions serde/src/lib.rs
Expand Up @@ -256,6 +256,7 @@ mod macros;
#[macro_use]
mod integer128;

#[macro_use]
pub mod de;
pub mod ser;

Expand Down
30 changes: 2 additions & 28 deletions serde/src/private/de.rs
Expand Up @@ -2554,10 +2554,7 @@ where
}
}

pub struct StrDeserializer<'a, E> {
value: &'a str,
marker: PhantomData<E>,
}
forward_deserializer!(ref StrDeserializer<'a>(&'a str) => visit_str);

impl<'a, E> IdentifierDeserializer<'a, E> for &'a str
where
Expand All @@ -2566,30 +2563,7 @@ where
type Deserializer = StrDeserializer<'a, E>;

fn from(self) -> Self::Deserializer {
StrDeserializer {
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
StrDeserializer::new(self)
}
}

Expand Down

0 comments on commit 9e1f573

Please sign in to comment.