1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/// Creates C-like inherited structures. First member of the struct is instance of the parent class.
/// This macro also implements [`core::ops::Deref`] for child class so you can easily access parent's fields.
/// # Note
/// You will problaly want to put `#[repr(C)]` on structures because this macro doesn't do this by default.
/// ```
/// # use faithe::parent;
///
/// parent! {
/// #[repr(C)]
/// pub struct Parent {
/// pub c: i32,
/// pub d: i32
/// }
///
/// #[repr(C)]
/// pub struct Child(pub Parent) {
/// pub a: i32,
/// pub b: i32
/// }
/// }
///
/// struct Parent {
///
/// }
/// ```
#[macro_export]
macro_rules! parent {
{
$(
$(#[$($attr:tt)*])*
$svs:vis struct $name:ident$(($pvs:vis $parent:ident))? {
$($fvs:vis $fname:ident: $fty:ty),*
}
)*
} => {
$(
$(
#[$($attr)*]
)*
$svs struct $name {
$($pvs parent: $parent,)?
$(
$fvs $fname: $fty,
)*
}
$(
impl core::ops::Deref for $name {
type Target = $parent;
fn deref(&self) -> &Self::Target {
&self.parent
}
}
)?
)*
};
}