Skip to main content

StateStoreExt

Trait StateStoreExt 

Source
pub trait StateStoreExt: StateStore {
    // Provided methods
    fn get_typed<T>(&self, key: &[u8]) -> Result<Option<T>, StateError>
       where T: Archive,
             T::Archived: for<'a> CheckBytes<HighValidator<'a, RkyvError>> + RkyvDeserialize<T, HighDeserializer<RkyvError>> { ... }
    fn put_typed<T>(&mut self, key: &[u8], value: &T) -> Result<(), StateError>
       where T: for<'a> RkyvSerialize<HighSerializer<AlignedVec, ArenaHandle<'a>, RkyvError>> { ... }
    fn update<F>(&mut self, key: &[u8], f: F) -> Result<(), StateError>
       where F: FnOnce(Option<Bytes>) -> Option<Vec<u8>> { ... }
}
Expand description

Extension trait for StateStore providing typed access methods.

These methods use generics and thus cannot be part of the dyn-compatible StateStore trait. Import this trait to use typed access on any state store.

Uses rkyv for zero-copy serialization. Types must derive Archive, rkyv::Serialize, and rkyv::Deserialize.

§Example

use laminar_core::state::{StateStore, StateStoreExt, InMemoryStore};
use rkyv::{Archive, Deserialize, Serialize};

#[derive(Archive, Serialize, Deserialize)]
#[rkyv(check_bytes)]
struct Counter { value: u64 }

let mut store = InMemoryStore::new();
store.put_typed(b"count", &Counter { value: 42 }).unwrap();
let count: Counter = store.get_typed(b"count").unwrap().unwrap();
assert_eq!(count.value, 42);

Provided Methods§

Source

fn get_typed<T>(&self, key: &[u8]) -> Result<Option<T>, StateError>

Get a value and deserialize it using rkyv.

Uses zero-copy access where possible, falling back to full deserialization to return an owned value.

§Errors

Returns StateError::Serialization if deserialization fails.

Source

fn put_typed<T>(&mut self, key: &[u8], value: &T) -> Result<(), StateError>

Serialize and store a value using rkyv.

Uses aligned buffers for optimal performance on the hot path.

§Errors

Returns StateError::Serialization if serialization fails.

Source

fn update<F>(&mut self, key: &[u8], f: F) -> Result<(), StateError>
where F: FnOnce(Option<Bytes>) -> Option<Vec<u8>>,

Update a value in place using a closure.

The update function receives the current value (or None) and returns the new value. If None is returned, the key is deleted.

§Errors

Returns StateError if the put or delete operation fails.

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§