glib_macros::downgrade_derive::enums

Function derive_downgrade_for_enum

source
pub fn derive_downgrade_for_enum(
    ident: Ident,
    generics: Generics,
    data_enum: DataEnum,
) -> TokenStream
Expand description

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

§Example

#[derive(glib::Downgrade)]
enum Choice {
    This(X, Y),
    That { x: X, y: Y },
}

Here is what will be derived:

enum ChoiceWeak {
    This(<X as Downgrade>::Weak, <Y as Downgrade>::Weak),
    That {
        x: <X as Downgrade>::Weak,
        y: <Y as Downgrade>::Weak,
    },
}

impl glib::clone::Downgrade for Choice {
    type Weak = ChoiceWeak;

    fn downgrade(&self) -> Self::Weak {
        match self {
            Self::This(ref _0, ref _1) => Self::Weak::This(
                glib::clone::Downgrade::downgrade(_0),
                glib::clone::Downgrade::downgrade(_1),
            ),
            Self::That { ref x, ref y } => Self::Weak::That(
                glib::clone::Downgrade::downgrade(x),
                glib::clone::Downgrade::downgrade(y),
            ),
        }
    }
}

impl glib::clone::Upgrade for ChoiceWeak {
    type Strong = Choice;

    fn upgrade(&self) -> Option<Self::Strong> {
        Some(match self {
            Self::This(ref _0, ref _1) => Self::Strong::This(
                glib::clone::Upgrade::upgrade(_0)?,
                glib::clone::Upgrade::upgrade(_1)?,
            ),
            Self::That { ref x, ref y } => Self::Strong::That(
                glib::clone::Upgrade::upgrade(x)?,
                glib::clone::Upgrade::upgrade(y)?,
            ),
        })
    }
}