pub trait ContextWriteState<'ctx>: SealedContext<'ctx> {
// Provided methods
fn set<T: Serialize + 'static>(&self, key: &str, t: T) { ... }
fn clear(&self, key: &str) { ... }
fn clear_all(&self) { ... }
}
Expand description
§Writing State
You can store key-value state in Restate. Restate makes sure the state is consistent with the processing of the code execution.
This feature is only available for Virtual Objects and Workflows:
- For Virtual Objects, the state is isolated per Virtual Object and lives forever (across invocations for that object).
- For Workflows, you can think of it as if every workflow execution is a new object. So the state is isolated to a single workflow execution. The state can only be mutated by the
run
handler of the workflow. The other handlers can only read the state.
/// 1. Listing state keys
/// List all the state keys that have entries in the state store for this object, via:
let keys = ctx.get_keys().await?;
/// 2. Getting a state value
let my_string = ctx
.get::<String>("my-key")
.await?
.unwrap_or(String::from("my-default"));
let my_number = ctx.get::<f64>("my-number-key").await?.unwrap_or_default();
/// 3. Setting a state value
ctx.set("my-key", String::from("my-value"));
/// 4. Clearing a state value
ctx.clear("my-key");
/// 5. Clearing all state values
/// Deletes all the state in Restate for the object
ctx.clear_all();
For more info about serialization, see crate::serde
§Command-line introspection
You can inspect and edit the K/V state stored in Restate via psql
and the CLI.
Have a look at the introspection docs for more information.
Provided Methods§
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.