Skip to main content

State

Struct State 

Source
pub struct State<P, Q> { /* private fields */ }
Expand description

State split into a public zone and per-player private zones, plus the match’s random generator.

Redaction is mechanical: a player’s observation is the public zone plus their own private entry, so there is no field to forget to strip. The backing map is not exposed; games reach private data through the accessors. Using this type is a convenience, not a requirement (Game::State is an associated type and can be any shape) but it makes the common hidden-info game turnkey.

The Prng lives here so it clones, serializes, and rewinds together with everything else: snapshot and resume are O(1), and a Reversible undo record can restore the stream position (see ARCHITECTURE.md). Games with their own State type should embed a Prng the same way.

Implementations§

Source§

impl<P, Q> State<P, Q>

Source

pub const fn new(public: P, seed: u64) -> Self

Creates a state with the given public zone, no private entries, and a generator seeded from seed.

Source

pub const fn public(&self) -> &P

Returns the public zone, visible to everyone.

Source

pub const fn public_mut(&mut self) -> &mut P

Returns the public zone for mutation inside apply.

Source

pub fn insert_private(&mut self, player: PlayerId, value: Q) -> Option<Q>

Sets player’s private zone, returning the previous value if any.

Source

pub fn private(&self, player: PlayerId) -> Option<&Q>

Returns player’s private zone, or None if they have none.

Source

pub fn private_mut(&mut self, player: PlayerId) -> Option<&mut Q>

Returns a mutable reference to player’s private zone.

Source

pub fn remove_private(&mut self, player: PlayerId) -> Option<Q>

Removes and returns player’s private zone, if any.

Needed to reverse a deal exactly: undoing a chance move that dealt a private card must leave no stale entry behind, so a Reversible::undo calls this to drop the card it handed out.

§Example
use turnbase::{PlayerId, State};
let mut state: State<(), u8> = State::new((), 0);
state.insert_private(PlayerId::new(0), 7); // deal
assert_eq!(state.remove_private(PlayerId::new(0)), Some(7)); // undo the deal
assert_eq!(state.private(PlayerId::new(0)), None);
Source

pub const fn rng(&self) -> &Prng

Returns the match’s generator.

Source

pub const fn rng_mut(&mut self) -> &mut Prng

Returns the match’s generator for drawing randomness inside apply.

Source§

impl<P: Clone, Q: Clone> State<P, Q>

Source

pub fn view_for(&self, viewer: Option<PlayerId>) -> PlayerView<P, Q>

Produces the standard observation for viewer: the public zone plus their own private zone (None viewer, a spectator, sees public only).

This is the default visibility rule. Games whose rule is inverted or otherwise non-standard (e.g. Hanabi) build their PlayerView directly instead of calling this. Clones the observed data, so games with a large public zone may prefer a cheaper custom projection.

Trait Implementations§

Source§

impl<P: Clone, Q: Clone> Clone for State<P, Q>

Source§

fn clone(&self) -> State<P, Q>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: Debug, Q: Debug> Debug for State<P, Q>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, P, Q> Deserialize<'de> for State<P, Q>
where P: Deserialize<'de>, Q: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<P: PartialEq, Q: PartialEq> PartialEq for State<P, Q>

Source§

fn eq(&self, other: &State<P, Q>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<P, Q> Serialize for State<P, Q>
where P: Serialize, Q: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<P: Eq, Q: Eq> Eq for State<P, Q>

Source§

impl<P, Q> StructuralPartialEq for State<P, Q>

Auto Trait Implementations§

§

impl<P, Q> Freeze for State<P, Q>
where P: Freeze,

§

impl<P, Q> RefUnwindSafe for State<P, Q>

§

impl<P, Q> Send for State<P, Q>
where P: Send, Q: Send,

§

impl<P, Q> Sync for State<P, Q>
where P: Sync, Q: Sync,

§

impl<P, Q> Unpin for State<P, Q>
where P: Unpin,

§

impl<P, Q> UnsafeUnpin for State<P, Q>
where P: UnsafeUnpin,

§

impl<P, Q> UnwindSafe for State<P, Q>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,