Skip to main content

Machine

Trait Machine 

Source
pub trait Machine: Serialize + for<'de> Deserialize<'de> {
    type State: Debug + Clone + Serialize + for<'de> Deserialize<'de>;

    // Required method
    fn current_state(&self) -> &Self::State;

    // Provided methods
    fn to_json(&self) -> Result<String, Error> { ... }
    fn from_json(json: &str) -> Result<Self, Error>
       where Self: Sized { ... }
}
Expand description

The base trait for all Gust state machines.

Every machine generated by the Gust compiler implements Machine, which provides state inspection and JSON round-trip serialization. The trait requires Serialize and Deserialize so that machine instances can be persisted, transferred, or logged as JSON.

§Associated types

  • State – the enum of possible states for this machine. Must be Debug + Clone + Serialize + Deserialize.

§Examples

use gust_runtime::prelude::*;

// Given a generated TrafficLight machine:
let light = TrafficLight::new();
println!("Current state: {:?}", light.current_state());

// Serialize to JSON for persistence
let json = light.to_json().unwrap();

// Restore from JSON
let restored = TrafficLight::from_json(&json).unwrap();

Required Associated Types§

Source

type State: Debug + Clone + Serialize + for<'de> Deserialize<'de>

The enum type representing all possible states of this machine.

Required Methods§

Source

fn current_state(&self) -> &Self::State

Returns a reference to the machine’s current state.

Provided Methods§

Source

fn to_json(&self) -> Result<String, Error>

Serializes the entire machine to a pretty-printed JSON string.

This captures both the current state and any associated data, allowing the machine to be persisted or transmitted.

Source

fn from_json(json: &str) -> Result<Self, Error>
where Self: Sized,

Deserializes a machine from a JSON string.

The JSON must have been produced by to_json (or an equivalent serializer) for the same machine type.

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§