macro_rules! derive { (impl($($trait:ident),+) for $struct_name:ident <$lifetime:lifetime> { $($field:ident),+ }) => { ... }; (@step1 { $trait:ident, $($rest_traits:ident),* } $struct_name:ident <$lifetime:lifetime> $fields:tt) => { ... }; (@step1 { $trait:ident } $struct_name:ident <$lifetime:lifetime> $fields:tt) => { ... }; (@step1 { } $struct_name:ident <$lifetime:lifetime> $fields:tt) => { ... }; (impl($($trait:ident),+) for $struct_name:ident { $($field:ident),+ }) => { ... }; (@step1 { $trait:ident, $($rest_traits:ident),* } $struct_name:ident $fields:tt) => { ... }; (@step1 { $trait:ident } $struct_name:ident $fields:tt) => { ... }; (@step1 { } $struct_name:ident $fields:tt) => { ... }; }
Expand description
Derives the specified traits for a struct.
This macro can be used to generate implementations of [JsonSerialize], ValueDeserialize,
and IntoStatic traits for a given struct.
§Usage
use merde::ValueDeserialize;
use merde::CowStr;
use merde::json::JsonSerialize;
#[derive(Debug, PartialEq)]
struct MyStruct<'s> {
field1: CowStr<'s>,
field2: i32,
field3: bool,
}
merde::derive! {
impl(JsonSerialize, ValueDeserialize, IntoStatic) for MyStruct<'s> {
field1,
field2,
field3
}
}In this example, all three traits are derived, of course you can omit the ones you don’t need.
The struct must have exactly one lifetime parameter. Additionally, even if there are no
borrowed fields, the struct must include a _phantom field of type PhantomData<&'a ()>,
where 'a is the lifetime parameter.
Implementing other variants (no lifetimes, multiple lifetimes, etc.) with declarative macros would be too complicated. At this point we’d want a whole parser / compiler / code generator for this — or a proc macro, see serde’s serde_derive.