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§
Sourcefn channels() -> HashMap<String, ChannelSpec>
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§
Sourcefn field_names() -> Vec<&'static str>
fn field_names() -> Vec<&'static str>
Get the list of field names in the state.
Sourcefn apply_update(&mut self, update: Value) -> Result<()>
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".