[−][src]Macro serde::forward_to_deserialize_any
Helper macro when implementing the Deserializer
part of a new data format
for Serde.
Some Deserializer
implementations for self-describing formats do not
care what hint the Visitor
gives them, they just want to blindly call
the Visitor
method corresponding to the data they can tell is in the
input. This requires repetitive implementations of all the Deserializer
trait methods.
# use serde::forward_to_deserialize_any;
# use serde::de::{value, Deserializer, Visitor};
#
# struct MyDeserializer;
#
# impl<'de> Deserializer<'de> for MyDeserializer {
# type Error = value::Error;
#
# fn deserialize_any<V>(self, _: V) -> Result<V::Value, Self::Error>
# where
# V: Visitor<'de>,
# {
# unimplemented!()
# }
#
#[inline]
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
self.deserialize_any(visitor)
}
#
# forward_to_deserialize_any! {
# 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
# }
# }
The forward_to_deserialize_any!
macro implements these simple forwarding
methods so that they forward directly to Deserializer::deserialize_any
.
You can choose which methods to forward.
# use serde::forward_to_deserialize_any;
# use serde::de::{value, Deserializer, Visitor};
#
# struct MyDeserializer;
#
impl<'de> Deserializer<'de> for MyDeserializer {
# type Error = value::Error;
#
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
/* ... */
# let _ = visitor;
# unimplemented!()
}
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
}
}
The macro assumes the convention that your Deserializer
lifetime parameter
is called 'de
and that the Visitor
type parameters on each method are
called V
. A different type parameter and a different lifetime can be
specified explicitly if necessary.
# use std::marker::PhantomData;
#
# use serde::forward_to_deserialize_any;
# use serde::de::{value, Deserializer, Visitor};
#
# struct MyDeserializer<V>(PhantomData<V>);
#
# impl<'q, V> Deserializer<'q> for MyDeserializer<V> {
# type Error = value::Error;
#
# fn deserialize_any<W>(self, visitor: W) -> Result<W::Value, Self::Error>
# where
# W: Visitor<'q>,
# {
# unimplemented!()
# }
#
forward_to_deserialize_any! {
<W: Visitor<'q>>
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
}
# }