Make BytesDeserializer public

This commit is contained in:
Mingun 2020-10-03 16:30:19 +05:00 committed by Mingun
parent 8084258a3e
commit 42fa79455e
2 changed files with 46 additions and 32 deletions

View File

@ -665,6 +665,47 @@ 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<'a, E> BytesDeserializer<'a, E> {
/// Create a new deserializer from the given slice.
pub fn new(value: &'a [u8]) -> Self {
BytesDeserializer {
value: value,
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 /// A deserializer holding a `&[u8]` with a lifetime tied to another
/// deserializer. /// deserializer.
#[derive(Debug)] #[derive(Debug)]
@ -699,9 +740,9 @@ where
} }
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str
bytes byte_buf option unit unit_struct newtype_struct seq tuple string bytes byte_buf option unit unit_struct newtype_struct seq
tuple_struct map struct identifier ignored_any enum tuple tuple_struct map struct enum identifier ignored_any
} }
} }

View File

@ -1,6 +1,7 @@
use lib::*; use lib::*;
use de::{Deserialize, DeserializeSeed, Deserializer, Error, IntoDeserializer, Visitor}; use de::{Deserialize, DeserializeSeed, Deserializer, Error, IntoDeserializer, Visitor};
use de::value::BytesDeserializer;
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
use de::{MapAccess, Unexpected}; use de::{MapAccess, Unexpected};
@ -2592,11 +2593,6 @@ where
} }
} }
pub struct BytesDeserializer<'a, E> {
value: &'a [u8],
marker: PhantomData<E>,
}
impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8] impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8]
where where
E: Error, E: Error,
@ -2604,30 +2600,7 @@ where
type Deserializer = BytesDeserializer<'a, E>; type Deserializer = BytesDeserializer<'a, E>;
fn from(self) -> Self::Deserializer { fn from(self) -> Self::Deserializer {
BytesDeserializer { BytesDeserializer::new(self)
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
} }
} }