pub trait Observable<T> {
// Required methods
fn source_id(&self) -> SourceId;
fn source_name(&self) -> Arc<str> ⓘ;
fn get(&self) -> T;
fn subscribe(&self, observer: Observer) -> Subscription;
// Provided methods
fn read(&self, reader: &mut dyn FnMut(&T)) { ... }
fn value_version(&self) -> Option<u64> { ... }
fn changed(&self) -> Changed ⓘ
where Self: Sized { ... }
}Expand description
Readable value that can notify subscribers after meaningful changes.
SUI’s runtime consumes this trait, so applications may provide adapters for reducer stores, channels, actor snapshots, or other state architectures.
The simplest custom implementation keeps a Signal<u64> revision next to
its data, calls Signal::mark_changed after each change, and delegates
source_id, source_name, subscribe, and value_version to it. That
gives it batching and observer pruning for free.
Required Methods§
fn source_id(&self) -> SourceId
fn source_name(&self) -> Arc<str> ⓘ
fn get(&self) -> T
fn subscribe(&self, observer: Observer) -> Subscription
Provided Methods§
Sourcefn read(&self, reader: &mut dyn FnMut(&T))
fn read(&self, reader: &mut dyn FnMut(&T))
Call reader once with a borrow of the current value.
The default clones through get. Implementations that
store the value should override it to avoid the copy; selectors read
their source through this method.
Sourcefn value_version(&self) -> Option<u64>
fn value_version(&self) -> Option<u64>
A counter identifying the current value, or None if unknown.
Selectors use it to reuse a derived value while the source has not
changed. It must increase after every change to the value, so that a
version read before read never claims a newer value
than the one read. Return None if that cannot be guaranteed.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".