macro_rules! extends {
(
$(
$(#[$($base_child_meta:meta),*])*
$base_pub_i:vis $base_field:ident: $base_type:ty
),* $(,)? ;
$(#[$($top_meta:meta),*])* $pub:vis struct $name:ident$(<$l:lifetime>)? {
$(
$(#[$($child_meta:meta),*])*
$pub_i:vis $field:ident: $type:ty
),* $(,)?
}
) => { ... };
(
$(#[$($base_top_meta:meta),*])* $base_pub:vis base struct $base_name:ident$(<$base_l:lifetime>)? {$(
$(#[$($base_child_meta:meta),*])*
$base_pub_i:vis $base_field:ident: $base_type:ty),* $(,)?
}
$(
$(#[$($top_meta:meta),*])* $pub:vis struct $name:ident$(<$l:lifetime>)? {
$(
$(#[$($child_meta:meta),*])*
$pub_i:vis $field:ident: $type:ty),* $(,)?
}
)*
) => { ... };
}
Expand description
§Inherit Struct Fields
This macro is used to inherit common fields one struct to another. You simply define the struct with different fields as you would normally do in rust and wrap it in this macro.
§Usage syntax:
ⓘ
extends!(
[meta]*
[pub] base struct [name]{
[
[meta]*
[pub] [field]: [type]
],*
}
[meta]*
[pub] struct [name]{
[
[meta]*
[pub] [field]: [type]
],*
}
);
§Usage Example:
rust_helpers::extends!{
pub base struct Base{
pub a: u32,
}
#[derive(Debug, Clone)]
pub struct Inherited{
pub b:String,
//#[serde(skip)]
pub others: Vec<usize>,
}
}
let a = Inherited{
a: 1,
b: "hello".to_string(),
others: vec![1,2,3],
};
Both struct will have the common fields from the base struct.