#枚举 #特质 #变体 #自动 #委托 #结构体 #转换

polymorphic_enum

自动将枚举变体的数据包装在结构体中。为枚举实现一个给定的特质,并将其委托给结构体。同时实现From和To以在枚举和结构体之间进行转换。

5个版本

0.1.5 2023年8月27日
0.1.4 2023年8月27日
0.1.2 2023年8月27日
0.1.1 2023年8月27日
0.1.0 2023年8月27日

#434 in 过程宏

LGPL-3.0

13KB
171

polymorphic_enum

示例

输入

polymorphic_enum!(
    trait Move {
        fn execute(&self);
        fn valid_for_state(&self, state: u8) -> bool;
    }

    #[derive(Debug, Clone)]
    enum Moves {
        Attack { enemy_id: u32 },
        Defend,
    }
);

将扩展为

trait Move {
    fn execute(&self);
    fn valid_for_state(&self, state: u8) -> bool;
}

#[derive(Debug, Clone)]
struct Attack{ enemy_id: u32 }
#[derive(Debug, Clone)]
struct Defend;

#[derive(Debug, Clone)]
enum Moves {
    Attack(Attack),
    Defend(Defend)
}

impl Move for Moves {
    fn execute(&self) {
        match self {
            Moves::Attack(inner) => inner.execute(),
            Moves::Defend(inner) => inner.execute(),
        }
    }

    fn valid_for_state(&self, state: u8) -> bool {
        match self {
            Moves::Attack(inner) => inner.valid_for_state(state),
            Moves::Defend(inner) => inner.valid_for_state(state),
        }
    }
}

impl From<Attack> for Moves {
    fn from(attack: Attack) -> Self {
        Moves::Attack(attack)
    }
}
impl From<Defend> for Moves {
    fn from(defend: Defend) -> Self {
        Moves::Defend(defend)
    }
}
impl Into<Attack> for Moves {
    fn into(self) -> Attack {
        match self {
            Moves::Attack(attack) => attack,
            _ => panic!("Tried to convert a Moves into a Attack but the enum variant was not Attack"),
        }
    }
}
impl Into<Defend> for Moves {
    fn into(self) -> Defend {
        match self {
            Moves::Defend(defend) => defend,
            _ => panic!("Tried to convert a Moves into a Defend but the enum variant was not Defend"),
        }
    }
}

现在你需要为生成的每个结构体实现该特质,如下所示

impl Move for Attack {
    fn execute(&self) {
        println!("Attack!");
    }

    fn valid_for_state(&self, state: u8) -> bool {
        state == 0
    }
}

impl Move for Defend {
    fn execute(&self) {
        println!("Defend!");
    }

    fn valid_for_state(&self, state: u8) -> bool {
        state == 1
    }
}

然后我们可以这样做

fn test_moves() {
    // Create a list of Moves
    let moves: Vec<Moves> = vec![Attack { enemy_id: 1 }.into(), Defend.into()];

    for m in moves {
        m.execute(); // Prints "Attack!" and "Defend!"
    }
}

依赖项

~290–740KB
~18K SLoC