diff --git a/card_game/src/lib.rs b/card_game/src/lib.rs index d085580..5732300 100644 --- a/card_game/src/lib.rs +++ b/card_game/src/lib.rs @@ -17,22 +17,20 @@ pub trait Game: Clone { ) -> impl Iterator + use; 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> { - 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 StagedInstruction<'_, G> where G: InstructionConsumer, { + /// Process the staged instruction. pub fn commit(self) { G::process_instruction(self); }