Add staging api #18
@@ -17,22 +17,20 @@ pub trait Game: Clone {
|
||||
) -> impl Iterator<Item = Self::Instruction> + use<Self>;
|
||||
fn is_instruction_valid(&self, config: &Self::Config, instruction: &Self::Instruction) -> bool;
|
||||
fn is_win(&self) -> bool;
|
||||
/// Validate an instruction to prepare to run it.
|
||||
fn stage_instruction<'a>(
|
||||
&'a mut self,
|
||||
stats: &'a mut Self::Stats,
|
||||
config: &'a Self::Config,
|
||||
instruction: Self::Instruction,
|
||||
) -> Option<StagedInstruction<'a, Self>> {
|
||||
if self.is_instruction_valid(config, &instruction) {
|
||||
Some(StagedInstruction {
|
||||
self.is_instruction_valid(config, &instruction)
|
||||
.then_some(StagedInstruction {
|
||||
game: self,
|
||||
stats,
|
||||
config,
|
||||
instruction,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +42,7 @@ pub struct StagedInstruction<'a, G: Game> {
|
||||
instruction: G::Instruction,
|
||||
}
|
||||
impl<'a, G: Game> StagedInstruction<'a, G> {
|
||||
/// Consume the staged instruction. Use this in your implementation of InstructionConsumer::process_instruction for your game.
|
||||
pub fn consume(self) -> (&'a mut G, &'a mut G::Stats, &'a G::Config, G::Instruction) {
|
||||
let StagedInstruction {
|
||||
game,
|
||||
@@ -58,6 +57,7 @@ impl<G: Game> StagedInstruction<'_, G>
|
||||
where
|
||||
G: InstructionConsumer,
|
||||
{
|
||||
/// Process the staged instruction.
|
||||
pub fn commit(self) {
|
||||
G::process_instruction(self);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user