States

Trait States 

Source
pub trait States: Copy + Default {
    // Required methods
    fn index(&self) -> usize;
    fn from_index(index: usize) -> Self;
}
Expand description

Trait for state types in a state machine.

Implement this trait to define the states of your DFA. Each state must be convertible to and from a unique index for efficient matrix-based lookups.

§Example

use tinystate::States;

#[derive(Debug, Clone, Copy, Default)]
enum DoorState {
    #[default]
    Closed,
    Open,
    Locked,
}

impl States for DoorState {
    fn index(&self) -> usize {
        *self as usize
    }

    fn from_index(i: usize) -> Self {
        match i {
            0 => Self::Closed,
            1 => Self::Open,
            2 => Self::Locked,
            _ => unreachable!(),
        }
    }
}

Or you can use the #[derive(States)] macro to automatically do this. (Works only on unfielded enums)

Required Methods§

Source

fn index(&self) -> usize

Returns the unique index for this state.

Source

fn from_index(index: usize) -> Self

Constructs a state from its index.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§