pub struct StateStore<Id>{ /* private fields */ }Expand description
Generic state store for managing entity properties with change detection
The store is generic over the entity ID type, allowing it to be used with any identifier (strings, custom IDs, etc.).
§Features
- Type-safe property storage and retrieval
- Change detection (only emits events when values actually change)
- Watch pattern (register interest in property changes)
- Blocking iteration over change events
§Example
use state_store::{StateStore, Property};
#[derive(Clone, PartialEq, Debug)]
struct Temperature(f32);
impl Property for Temperature {
const KEY: &'static str = "temperature";
}
let store = StateStore::<String>::new();
let sensor_id = "sensor-1".to_string();
// Watch for temperature changes on sensor-1
store.watch(sensor_id.clone(), Temperature::KEY);
// Set temperature (will emit change event since watched)
store.set(&sensor_id, Temperature(72.5));
// Get current value
let temp = store.get::<Temperature>(&sensor_id);
assert_eq!(temp, Some(Temperature(72.5)));Implementations§
Source§impl<Id> StateStore<Id>
impl<Id> StateStore<Id>
Sourcepub fn get<P: Property>(&self, entity_id: &Id) -> Option<P>
pub fn get<P: Property>(&self, entity_id: &Id) -> Option<P>
Get a property value for an entity
Returns None if the entity doesn’t exist or the property isn’t set.
Sourcepub fn set<P: Property>(&self, entity_id: &Id, value: P)
pub fn set<P: Property>(&self, entity_id: &Id, value: P)
Set a property value for an entity
If the value changes and the property is being watched, a change event is emitted.
Sourcepub fn watch(&self, entity_id: Id, property_key: &'static str)
pub fn watch(&self, entity_id: Id, property_key: &'static str)
Register interest in a property for an entity
After watching, changes to this property will appear in iter().
Sourcepub fn unwatch(&self, entity_id: &Id, property_key: &'static str)
pub fn unwatch(&self, entity_id: &Id, property_key: &'static str)
Unregister interest in a property
Sourcepub fn is_watched(&self, entity_id: &Id, property_key: &'static str) -> bool
pub fn is_watched(&self, entity_id: &Id, property_key: &'static str) -> bool
Check if a property is being watched
Sourcepub fn iter(&self) -> ChangeIterator<Id> ⓘ
pub fn iter(&self) -> ChangeIterator<Id> ⓘ
Create a blocking iterator over change events
Only emits events for properties that have been watched.
Sourcepub fn entity_count(&self) -> usize
pub fn entity_count(&self) -> usize
Get the number of entities in the store
Sourcepub fn entity_ids(&self) -> Vec<Id>
pub fn entity_ids(&self) -> Vec<Id>
Get all entity IDs
Sourcepub fn remove_entity(&self, entity_id: &Id) -> bool
pub fn remove_entity(&self, entity_id: &Id) -> bool
Remove an entity and all its properties
Sourcepub fn event_sender(&self) -> Sender<ChangeEvent<Id>>
pub fn event_sender(&self) -> Sender<ChangeEvent<Id>>
Get the event sender for external event injection
This is useful for testing or for injecting events from external sources (e.g., network callbacks).