Inline forward_deserializer for bytes deserializers

This commit is contained in:
David Tolnay 2021-01-23 13:05:21 -08:00
parent c858a1fa77
commit 85de92e6f7
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -44,101 +44,6 @@ macro_rules! impl_copy_clone {
};
}
// Creates a deserializer any method of which forwards to the specified visitor
// method.
macro_rules! forward_deserializer {
// Non-borrowed references
(
$(#[$doc:meta])*
// Actually, * in lifetime should be ?, but that syntax is not supported
// on old Rust versions (<= 1.28) or in 2015 edition
ref $deserializer:ident $(<$lifetime:tt>)* ($ty:ty) => $visit:ident
) => {
$(#[$doc])*
#[derive(Debug)]
pub struct $deserializer<$($lifetime,)* E> {
value: $ty,
marker: PhantomData<E>,
}
impl<$($lifetime,)* E> $deserializer<$($lifetime,)* E> {
/// Create a new deserializer from the given value.
pub fn new(value: $ty) -> Self {
$deserializer {
value: value,
marker: PhantomData,
}
}
}
impl_copy_clone!($deserializer $(<$lifetime>)*);
impl<'de, $($lifetime,)* E> Deserializer<'de> for $deserializer<$($lifetime,)* E>
where
E: de::Error,
{
type Error = E;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.$visit(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
}
}
};
// Borrowed references
(
$(#[$doc:meta])*
borrowed $deserializer:ident($ty:ty) => $visit:ident
) => {
$(#[$doc])*
#[derive(Debug)]
pub struct $deserializer<'de, E> {
value: $ty,
marker: PhantomData<E>,
}
impl<'de, E> $deserializer<'de, E> {
/// Create a new borrowed deserializer from the given value.
pub fn new(value: $ty) -> Self {
$deserializer {
value: value,
marker: PhantomData,
}
}
}
impl_copy_clone!($deserializer<'de>);
impl<'de, E> Deserializer<'de> for $deserializer<'de, E>
where
E: de::Error,
{
type Error = E;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.$visit(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 minimal representation of all possible errors that can occur using the
@ -760,19 +665,88 @@ where
////////////////////////////////////////////////////////////////////////////////
forward_deserializer!(
/// A deserializer holding a `&[u8]`. Always call [`Visitor::visit_bytes`]
///
/// [`Visitor::visit_bytes`]: ../struct.Visitor.html#method.visit_bytes
ref BytesDeserializer<'a>(&'a [u8]) => visit_bytes
);
forward_deserializer!(
/// A deserializer holding a `&[u8]` with a lifetime tied to another
/// deserializer. Always call [`Visitor::visit_borrowed_bytes`]
///
/// [`Visitor::visit_borrowed_bytes`]: ../struct.Visitor.html#method.visit_borrowed_bytes
borrowed BorrowedBytesDeserializer(&'de [u8]) => visit_borrowed_bytes
);
/// A deserializer holding a `&[u8]`. Always call [`Visitor::visit_bytes`]
///
/// [`Visitor::visit_bytes`]: ../struct.Visitor.html#method.visit_bytes
#[derive(Debug)]
pub struct BytesDeserializer<'a, E> {
value: &'a [u8],
marker: PhantomData<E>,
}
impl<'a, E> BytesDeserializer<'a, E> {
/// Create a new deserializer from the given value.
pub fn new(value: &'a [u8]) -> Self {
BytesDeserializer {
value: value,
marker: PhantomData,
}
}
}
impl_copy_clone!(BytesDeserializer<'a>);
impl<'de, 'a, E> 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: 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. Always call [`Visitor::visit_borrowed_bytes`]
///
/// [`Visitor::visit_borrowed_bytes`]: ../struct.Visitor.html#method.visit_borrowed_bytes
#[derive(Debug)]
pub struct BorrowedBytesDeserializer<'de, E> {
value: &'de [u8],
marker: PhantomData<E>,
}
impl<'de, E> BorrowedBytesDeserializer<'de, E> {
/// Create a new borrowed deserializer from the given value.
pub fn new(value: &'de [u8]) -> Self {
BorrowedBytesDeserializer {
value: value,
marker: PhantomData,
}
}
}
impl_copy_clone!(BorrowedBytesDeserializer<'de>);
impl<'de, E> Deserializer<'de> for BorrowedBytesDeserializer<'de, E>
where
E: de::Error,
{
type Error = E;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_borrowed_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
}
}
////////////////////////////////////////////////////////////////////////////////