Input:
```rust polymorphicenum!( trait Move { fn execute(&self); fn validfor_state(&self, state: u8) -> bool; }
#[derive(Debug, Clone)]
enum Moves {
Attack { enemy_id: u32 },
Defend,
}
); ```
Will expand to:
```rust trait Move { fn execute(&self); fn validforstate(&self, state: u8) -> bool; }
struct Attack{ enemy_id: u32 }
struct Defend;
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
You are now expected to implement the trait for each of the generated structs like so: ```rust 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
}
} ```
Then we can do something like this:
```rust
fn testmoves() {
// Create a list of Moves
let moves: Vec
for m in moves {
m.execute(); // Prints "Attack!" and "Defend!"
}
} ```