Configurable forward_to_deserialize

This commit is contained in:
David Tolnay 2017-04-06 15:24:16 -07:00
parent cc933b9cdb
commit 74e15ea9f5
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -1,11 +1,14 @@
#[doc(hidden)] #[doc(hidden)]
#[macro_export] #[macro_export]
macro_rules! forward_to_deserialize_method { macro_rules! forward_to_deserialize_method {
($func:ident($($arg:ty),*)) => { ($func:ident<$l:tt, $v:ident>($($arg:ident : $ty:ty),*)) => {
#[inline] #[inline]
fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> $crate::export::Result<__V::Value, Self::Error> fn $func<$v>(self, $($arg: $ty,)* visitor: $v) -> $crate::export::Result<$v::Value, Self::Error>
where __V: $crate::de::Visitor<'de> where $v: $crate::de::Visitor<$l>
{ {
$(
let _ = $arg;
)*
self.deserialize(visitor) self.deserialize(visitor)
} }
}; };
@ -14,92 +17,92 @@ macro_rules! forward_to_deserialize_method {
#[doc(hidden)] #[doc(hidden)]
#[macro_export] #[macro_export]
macro_rules! forward_to_deserialize_helper { macro_rules! forward_to_deserialize_helper {
(bool) => { (bool<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_bool()} forward_to_deserialize_method!{deserialize_bool<$l, $v>()}
}; };
(u8) => { (u8<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_u8()} forward_to_deserialize_method!{deserialize_u8<$l, $v>()}
}; };
(u16) => { (u16<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_u16()} forward_to_deserialize_method!{deserialize_u16<$l, $v>()}
}; };
(u32) => { (u32<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_u32()} forward_to_deserialize_method!{deserialize_u32<$l, $v>()}
}; };
(u64) => { (u64<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_u64()} forward_to_deserialize_method!{deserialize_u64<$l, $v>()}
}; };
(i8) => { (i8<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_i8()} forward_to_deserialize_method!{deserialize_i8<$l, $v>()}
}; };
(i16) => { (i16<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_i16()} forward_to_deserialize_method!{deserialize_i16<$l, $v>()}
}; };
(i32) => { (i32<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_i32()} forward_to_deserialize_method!{deserialize_i32<$l, $v>()}
}; };
(i64) => { (i64<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_i64()} forward_to_deserialize_method!{deserialize_i64<$l, $v>()}
}; };
(f32) => { (f32<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_f32()} forward_to_deserialize_method!{deserialize_f32<$l, $v>()}
}; };
(f64) => { (f64<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_f64()} forward_to_deserialize_method!{deserialize_f64<$l, $v>()}
}; };
(char) => { (char<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_char()} forward_to_deserialize_method!{deserialize_char<$l, $v>()}
}; };
(str) => { (str<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_str()} forward_to_deserialize_method!{deserialize_str<$l, $v>()}
}; };
(string) => { (string<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_string()} forward_to_deserialize_method!{deserialize_string<$l, $v>()}
}; };
(unit) => { (unit<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_unit()} forward_to_deserialize_method!{deserialize_unit<$l, $v>()}
}; };
(option) => { (option<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_option()} forward_to_deserialize_method!{deserialize_option<$l, $v>()}
}; };
(seq) => { (seq<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_seq()} forward_to_deserialize_method!{deserialize_seq<$l, $v>()}
}; };
(seq_fixed_size) => { (seq_fixed_size<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_seq_fixed_size(usize)} forward_to_deserialize_method!{deserialize_seq_fixed_size<$l, $v>(len: usize)}
}; };
(bytes) => { (bytes<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_bytes()} forward_to_deserialize_method!{deserialize_bytes<$l, $v>()}
}; };
(byte_buf) => { (byte_buf<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_byte_buf()} forward_to_deserialize_method!{deserialize_byte_buf<$l, $v>()}
}; };
(map) => { (map<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_map()} forward_to_deserialize_method!{deserialize_map<$l, $v>()}
}; };
(unit_struct) => { (unit_struct<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_unit_struct(&'static str)} forward_to_deserialize_method!{deserialize_unit_struct<$l, $v>(name: &'static str)}
}; };
(newtype_struct) => { (newtype_struct<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_newtype_struct(&'static str)} forward_to_deserialize_method!{deserialize_newtype_struct<$l, $v>(name: &'static str)}
}; };
(tuple_struct) => { (tuple_struct<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_tuple_struct(&'static str, usize)} forward_to_deserialize_method!{deserialize_tuple_struct<$l, $v>(name: &'static str, len: usize)}
}; };
(struct) => { (struct<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_struct(&'static str, &'static [&'static str])} forward_to_deserialize_method!{deserialize_struct<$l, $v>(name: &'static str, fields: &'static [&'static str])}
}; };
(struct_field) => { (struct_field<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_struct_field()} forward_to_deserialize_method!{deserialize_struct_field<$l, $v>()}
}; };
(tuple) => { (tuple<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_tuple(usize)} forward_to_deserialize_method!{deserialize_tuple<$l, $v>(len: usize)}
}; };
(enum) => { (enum<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_enum(&'static str, &'static [&'static str])} forward_to_deserialize_method!{deserialize_enum<$l, $v>(name: &'static str, variants: &'static [&'static str])}
}; };
(ignored_any) => { (ignored_any<$l:tt, $v:ident>) => {
forward_to_deserialize_method!{deserialize_ignored_any()} forward_to_deserialize_method!{deserialize_ignored_any<$l, $v>()}
}; };
} }
@ -184,11 +187,33 @@ macro_rules! forward_to_deserialize_helper {
/// ``` /// ```
/// ///
/// The macro assumes the convention that your `Deserializer` lifetime parameter /// The macro assumes the convention that your `Deserializer` lifetime parameter
/// is called `'de`. It will not work if the `Deserializer` lifetime parameter /// is called `'de` and that the `Visitor` type parameters on each method are
/// is called something different. /// called `V`. A different type parameter and a different lifetime can be
/// specified explicitly if necessary.
///
/// ```rust
/// # #[macro_use] extern crate serde;
/// # use serde::de::{value, Deserializer, Visitor};
/// # pub struct MyDeserializer;
/// # impl<'q> Deserializer<'q> for MyDeserializer {
/// # type Error = value::Error;
/// # fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error>
/// # where V: Visitor<'q> { unimplemented!() }
/// forward_to_deserialize! {
/// <W: Visitor<'q>>
/// bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
/// seq seq_fixed_size bytes byte_buf map unit_struct newtype_struct
/// tuple_struct struct struct_field tuple enum ignored_any
/// }
/// # }
/// # fn main() {}
/// ```
#[macro_export] #[macro_export]
macro_rules! forward_to_deserialize { macro_rules! forward_to_deserialize {
(<$visitor:ident: Visitor<$lifetime:tt>> $($func:ident)*) => {
$(forward_to_deserialize_helper!{$func<$lifetime, $visitor>})*
};
($($func:ident)*) => { ($($func:ident)*) => {
$(forward_to_deserialize_helper!{$func})* $(forward_to_deserialize_helper!{$func<'de, V>})*
}; };
} }