pub trait TurnRules {
type Command;
type Event;
type Error;
type Outcome;
// Required methods
fn check(&self, command: &Self::Command) -> Result<(), Self::Error>;
fn apply(
&mut self,
command: &Self::Command,
) -> Result<Vec<Self::Event>, Self::Error>;
fn legal_commands(&self) -> Vec<Self::Command>;
fn outcome(&self) -> Option<Self::Outcome>;
}Expand description
A turn-based game’s state, and the commands that change it.
§Contract
- Deterministic. The same state and command always give the same
events and the same new state. Luck comes from a seeded RNG kept in
the state (
SimRng), never from outside, so a game can be rebuilt by replaying its commands. - All or nothing.
applyeither changes the state and returns what happened, or refuses the command with an error and changes nothing. checkgivesapply’s answer without changing anything, luck included.legal_commandslists commandscheckaccepts, always in the same order. A game may leave some out (when there are too many to list), and says which.- Once
outcomeisSome, the game is over andlegal_commandsis empty.
Required Associated Types§
Required Methods§
Sourcefn check(&self, command: &Self::Command) -> Result<(), Self::Error>
fn check(&self, command: &Self::Command) -> Result<(), Self::Error>
Whether apply would accept command, and if not
why, without changing anything.
Sourcefn apply(
&mut self,
command: &Self::Command,
) -> Result<Vec<Self::Event>, Self::Error>
fn apply( &mut self, command: &Self::Command, ) -> Result<Vec<Self::Event>, Self::Error>
Applies command and returns what happened, or refuses it and
changes nothing.
Sourcefn legal_commands(&self) -> Vec<Self::Command>
fn legal_commands(&self) -> Vec<Self::Command>
The commands check accepts now, in a fixed order.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".