Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make impl IntoDeserializer for &[u8] public #1898

Merged
merged 2 commits into from Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 104 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 Expand Up @@ -707,6 +752,65 @@ where

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

/// A deserializer holding a `Cow<[u8]>`.
#[cfg(any(feature = "std", feature = "alloc"))]
#[derive(Debug)]
pub struct CowBytesDeserializer<'a, E> {
value: Cow<'a, [u8]>,
marker: PhantomData<E>,
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, E> Clone for CowBytesDeserializer<'a, E> {
fn clone(&self) -> Self {
CowBytesDeserializer {
value: self.value.clone(),
marker: PhantomData,
}
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, [u8]>
where
E: de::Error,
{
type Deserializer = CowBytesDeserializer<'a, E>;

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

#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, 'a, E> de::Deserializer<'de> for CowBytesDeserializer<'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>,
{
match self.value {
Cow::Borrowed(bytes) => visitor.visit_bytes(bytes),
Cow::Owned(bytes) => visitor.visit_byte_buf(bytes),
}
}

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 identifier ignored_any enum
}
}

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

/// A deserializer that iterates over a sequence.
#[derive(Clone, Debug)]
pub struct SeqDeserializer<I, E> {
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