Macro match_next

Source
macro_rules! match_next {
    { $curr:ident => $next:ident, $($from:pat => $to:pat),* $(,)? } => { ... };
}
Expand description

A convenient macro for implementation of Behavior::filter_next.

§Usage

For any given pair of states curr and next, this match expands into a match arm such that:

match curr {
    From => matches!(next, To)
}

where From is the current possible state, and To is the next allowed state.

§Example

#[derive(Component, Debug)]
enum Soldier {
    Idle,
    Crouch,
    Fire,
    Sprint,
    Jump,
}

impl Behavior for Soldier {
    fn filter_next(&self, next: &Self) -> bool {
        use Soldier::*;
        match_next! {
            self => next,
            Idle => Crouch | Sprint | Fire | Jump,
            Crouch => Sprint | Fire,
            Sprint => Jump,
        }
    }
}