[][src]Crate mode

A simple and effective behavioral state machine library, written in in idiomatic, 100% safe, stable Rust code.

This library provides three main types, Automaton, Mode, and Family, which facilitate the creation of behavioral state machines. An Automaton can be used to quickly create a state machine over some set of concrete types that implement the Mode trait, called a Family. This can contain either:

  • a single, concrete type representing all states in the state machine, e.g. a struct or an enum, or
  • one or more separate types that all implement some common dyn Trait, with each type representing a distinct state in the state machine.

The Automaton keeps track of a current instance of Mode, and provides external access to any members that are common to all Modes in the Family. A flexible transition system provides a way for the current Mode to swap in a new Mode in the same Family when it is ready, and is designed such that the current Mode can move large amounts of its own state directly into the new Mode being created. This can help prevent spikes in memory and CPU usage when switching between Modes.

Examples

  • For a simple example of how to use this library to implement a simple state machine over a single, concrete type, please see examples/enum.rs.
  • For a more advanced example demonstrating a state machine over several types in the same Family, please see examples/activity.rs.
  • For an example of how to use Family::Input and Family::Output to pass context into and out of the swap() function, please see examples/turing.rs.

You can run the examples using the following Cargo commands:

cargo run --example enum
cargo run --example activity
cargo run --example turing

Getting started

A good place to start reading would be the Automaton documentation, followed by Mode and then Family.

Modules

boxed

Defines types that can be used to set up an Automaton that stores a Box<Mode> instead of a Mode in place.

rc

Defines types that can be used to set up an Automaton that stores an Rc<Mode> instead of a Mode in place.

sync

Defines types that can be used to set up an Automaton that stores an Arc<Mode> instead of a Mode in place.

Structs

Automaton

Represents a state machine over a set of Modes within the same Family.

Traits

Family

A meta-trait defining the common Base type and Mode::swap() conventions used by a related group of Mode implementations. Modes can only transition to other Modes within the same Family, i.e. where both Modes share the same Family associated type.

Mode

Trait that defines the transition behavior of a state within an Automaton.