From eac07676dd3ed2ae9f5fd372b3e9732f6183833c Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Wed, 8 Jul 2026 09:34:49 -0700 Subject: [PATCH] remove separate trait --- card_game/src/lib.rs | 25 +++++-------------------- klondike/src/lib.rs | 24 +++++++++++------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/card_game/src/lib.rs b/card_game/src/lib.rs index 601db9f..2f59235 100644 --- a/card_game/src/lib.rs +++ b/card_game/src/lib.rs @@ -16,6 +16,7 @@ pub trait Game: Clone { config: &Self::Config, ) -> impl Iterator + use; fn is_instruction_valid(&self, config: &Self::Config, instruction: &Self::Instruction) -> bool; + fn process_instruction(valid_instruction: StagedInstruction<'_, Self>); fn is_win(&self) -> bool; /// Validate an instruction to prepare to run it. fn stage_instruction<'a>( @@ -54,21 +55,13 @@ impl<'a, G: Game> StagedInstruction<'a, G> { (game, stats, config, instruction) } } -impl StagedInstruction<'_, G> -where - G: InstructionConsumer, -{ +impl StagedInstruction<'_, G> { /// Process the staged instruction. pub fn commit(self) { G::process_instruction(self); } } -/// Apply an atomic state update -pub trait InstructionConsumer: Game { - fn process_instruction(valid_instruction: StagedInstruction<'_, Self>); -} - /// card_game supports up to 4 identifiably separate decks. #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub enum Deck { @@ -558,7 +551,6 @@ where G: Eq + core::hash::Hash, G::Stats: Default, G::Instruction: Eq + core::hash::Hash, - G: InstructionConsumer, { pub fn new(state: G, config: SessionConfig) -> Self { Self { @@ -678,15 +670,6 @@ where } } } - fn is_win(&self) -> bool { - self.state.is_win() - } -} -impl InstructionConsumer for SessionState -where - G: Game, - G: InstructionConsumer, -{ fn process_instruction(valid_instruction: StagedInstruction<'_, Self>) { let (game, stats, config, instruction) = valid_instruction.consume(); match instruction { @@ -711,6 +694,9 @@ where } } } + fn is_win(&self) -> bool { + self.state.is_win() + } } #[cfg(feature = "serde")] @@ -719,7 +705,6 @@ where G: serde::Deserialize<'de> + Clone + Eq + core::hash::Hash, G::Stats: Default, G::Instruction: serde::Deserialize<'de> + Eq + core::hash::Hash, - G: InstructionConsumer, SessionConfig: serde::Deserialize<'de>, Vec: serde::Deserialize<'de>, { diff --git a/klondike/src/lib.rs b/klondike/src/lib.rs index 67dd402..2a7a5c7 100644 --- a/klondike/src/lib.rs +++ b/klondike/src/lib.rs @@ -1,6 +1,6 @@ pub type Rng = rand::rngs::StdRng; -use card_game::{Card, Game, InstructionConsumer, Pile, Rank, Stack, StagedInstruction}; +use card_game::{Card, Game, Pile, Rank, Stack, StagedInstruction}; #[cfg(test)] mod test; @@ -835,18 +835,6 @@ impl Game for Klondike { fn is_instruction_valid(&self, config: &Self::Config, instruction: &Self::Instruction) -> bool { self.state.is_instruction_valid(config, instruction) } - fn is_win(&self) -> bool { - // all foundations contain all ranks - self.state.foundations.iter().all(|foundation| { - foundation.len() == Rank::RANKS.len() - && foundation - .iter() - .zip(Rank::RANKS) - .all(|(card, rank)| card.rank() == rank) - }) - } -} -impl InstructionConsumer for Klondike { fn process_instruction(valid_instruction: StagedInstruction<'_, Self>) { let (game, stats, config, instruction) = valid_instruction.consume(); stats.increment_moves(); @@ -886,6 +874,16 @@ impl InstructionConsumer for Klondike { } } } + fn is_win(&self) -> bool { + // all foundations contain all ranks + self.state.foundations.iter().all(|foundation| { + foundation.len() == Rank::RANKS.len() + && foundation + .iter() + .zip(Rank::RANKS) + .all(|(card, rank)| card.rank() == rank) + }) + } } #[cfg(feature = "serde")]