Skip to main content

State

Trait State 

Source
pub trait State:
    Clone
    + Send
    + Sync
    + Serialize
    + for<'de> Deserialize<'de>
    + Debug
    + 'static {
    // Required method
    fn merge(&mut self, other: Self) -> Result<()>;

    // Provided methods
    fn to_value(&self) -> Result<Value> { ... }
    fn from_value(value: Value) -> Result<Self> { ... }
}
Expand description

The core trait for graph state.

Implementors of this trait define how state is merged when multiple nodes produce updates that need to be combined.

§Example

use rust_langgraph::{State, Error};

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
struct CounterState {
    count: i32,
}

impl State for CounterState {
    fn merge(&mut self, other: Self) -> Result<(), Error> {
        self.count += other.count;
        Ok(())
    }
}

Required Methods§

Source

fn merge(&mut self, other: Self) -> Result<()>

Merge another state into this one.

This is called when multiple nodes write to the same state, or when resuming from a checkpoint.

Provided Methods§

Source

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

Convert state to JSON value (default implementation)

Source

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

Create state from JSON value (default implementation)

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§