glib_macros::downgrade_derive::structs

Function derive_downgrade_for_struct

source
pub fn derive_downgrade_for_struct(
    ident: Ident,
    generics: Generics,
    data_struct: DataStruct,
) -> TokenStream
Expand description

This function derives a weak type for a given strong struct and implementations of Downgrade and Upgrade traits.

§Example

#[derive(glib::Downgrade)]
struct Unnamed(X, Y);

#[derive(glib::Downgrade)]
struct Named {
    x: X,
    y: Y,
}

Here is what will be derived:

pub struct UnnamedWeak(<X as Downgrade>::Weak, <Y as Downgrade>::Weak);

impl glib::clone::Downgrade for Unnamed {
    type Weak = UnnamedWeak;

    fn downgrade(&self) -> Self::Weak {
        let Self (ref _0, ref _1) = self;
        UnnamedWeak (
            glib::clone::Downgrade::downgrade(_0),
            glib::clone::Downgrade::downgrade(_1),
        )
    }
}

impl glib::clone::Upgrade for UnnamedWeak {
    type Strong = Unnamed;

    fn upgrade(&self) -> Option<Self::Strong> {
        let Self (ref _0, ref _1) = self;
        Some(Unnamed (
            glib::clone::Upgrade::upgrade(_0)?,
            glib::clone::Upgrade::upgrade(_1)?,
        ))
    }
}

pub struct NamedWeak {
    x: <X as Downgrade>::Weak,
    y: <Y as Downgrade>::Weak,
}

impl glib::clone::Downgrade for Named {
    type Weak = NamedWeak;

    fn downgrade(&self) -> Self::Weak {
        let Self { ref x, ref y } = self;
        NamedWeak {
            glib::clone::Downgrade::downgrade(x),
            glib::clone::Downgrade::downgrade(y),
        }
    }
}

impl glib::clone::Upgrade for NamedWeak {
    type Strong = Named;

    fn upgrade(&self) -> Option<Self::Strong> {
        let Self { ref x, ref y } = self;
        Some(Named {
            glib::clone::Upgrade::upgrade(x)?,
            glib::clone::Upgrade::upgrade(y)?,
        })
    }
}