Skip to main content

GraphState

Trait GraphState 

Source
pub trait GraphState:
    Clone
    + Send
    + Sync
    + Serialize
    + DeserializeOwned
    + 'static {
    // Required method
    fn channels() -> HashMap<String, ChannelSpec>;

    // Provided methods
    fn field_names() -> Vec<&'static str> { ... }
    fn to_json(&self) -> Result<Value> { ... }
    fn from_json(value: Value) -> Result<Self> { ... }
    fn apply_update(&mut self, update: Value) -> Result<()> { ... }
}
Expand description

Trait for types that can be used as graph state.

Implement this trait for your state struct to use it with StateGraph. The derive macro #[derive(GraphState)] can auto-implement this trait.

§Requirements

  • Clone: State is cloned during checkpointing and branching.
  • Send + Sync: State must be thread-safe for parallel node execution.
  • Serialize + DeserializeOwned: State must be serializable for checkpoints.

§Examples

ⓘ
use regula_core::{GraphState, ChannelSpec};
use serde::{Serialize, Deserialize};

#[derive(Clone, Serialize, Deserialize)]
struct MyState {
    messages: Vec<String>,
    counter: i32,
}

impl GraphState for MyState {
    fn channels() -> std::collections::HashMap<String, ChannelSpec> {
        let mut channels = std::collections::HashMap::new();
        channels.insert("messages".to_string(), ChannelSpec::LastValue);
        channels.insert("counter".to_string(), ChannelSpec::LastValue);
        channels
    }
}

Required Methods§

Source

fn channels() -> HashMap<String, ChannelSpec>

Returns the channel specifications for each field in the state.

The key is the field name, and the value describes how updates to that field should be handled (last value, reducer, etc.).

Provided Methods§

Source

fn field_names() -> Vec<&'static str>

Get the list of field names in the state.

Source

fn to_json(&self) -> Result<Value>

Convert the state to a JSON value.

Source

fn from_json(value: Value) -> Result<Self>

Create a state instance from a JSON value.

Source

fn apply_update(&mut self, update: Value) -> Result<()>

Apply a partial update to the state.

The update is a JSON object where keys are field names and values are the new values for those fields.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§