bdk_chain::bitcoin::hashes::serde

Macro forward_to_deserialize_any

Source
macro_rules! forward_to_deserialize_any {
    (<$visitor:ident: Visitor<$lifetime:tt>> $($func:ident)*) => { ... };
    ($($func:ident)*) => { ... };
}
Expand description

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.

#[inline]
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
    V: Visitor<'de>,
{
    self.deserialize_any(visitor)
}

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.

impl<'de> Deserializer<'de> for MyDeserializer {
    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        /* ... */
    }

    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.

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
}