Skip to content

Commit

Permalink
Make impl IntoDeserializer for &[u8] public
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Oct 3, 2020
1 parent be7d0e7 commit 2e821ea
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
45 changes: 45 additions & 0 deletions serde/src/de/value.rs
Expand Up @@ -665,6 +665,51 @@ where

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

/// A deserializer holding a `&[u8]`.
#[derive(Debug)]
pub struct BytesDeserializer<'a, E> {
value: &'a [u8],
marker: PhantomData<E>,
}

impl_copy_clone!(BytesDeserializer<'de>);

impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a [u8]
where
E: de::Error,
{
type Deserializer = BytesDeserializer<'a, E>;

fn into_deserializer(self) -> BytesDeserializer<'a, E> {
BytesDeserializer {
value: self,
marker: PhantomData,
}
}
}

impl<'de, 'a, E> de::Deserializer<'de> for BytesDeserializer<'a, E>
where
E: de::Error,
{
type Error = E;

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_bytes(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
}
}

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

/// A deserializer holding a `&[u8]` with a lifetime tied to another
/// deserializer.
#[derive(Debug)]
Expand Down
31 changes: 2 additions & 29 deletions serde/src/private/de.rs
@@ -1,6 +1,7 @@
use lib::*;

use de::{Deserialize, DeserializeSeed, Deserializer, Error, IntoDeserializer, Visitor};
use de::value::BytesDeserializer;

#[cfg(any(feature = "std", feature = "alloc"))]
use de::{MapAccess, Unexpected};
Expand Down Expand Up @@ -2592,42 +2593,14 @@ where
}
}

pub struct BytesDeserializer<'a, E> {
value: &'a [u8],
marker: PhantomData<E>,
}

impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8]
where
E: Error,
{
type Deserializer = BytesDeserializer<'a, E>;

fn from(self) -> Self::Deserializer {
BytesDeserializer {
value: self,
marker: PhantomData,
}
}
}

impl<'de, 'a, E> Deserializer<'de> for BytesDeserializer<'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_bytes(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
self.into_deserializer()
}
}

Expand Down

0 comments on commit 2e821ea

Please sign in to comment.