pub struct Watchable<T> { /* private fields */ }Expand description
A wrapper around a value that notifies Watchers when the value is modified.
Only the most recent value is available to any observer, but the observer is guaranteed to be notified of the most recent value.
Implementations§
Source§impl<T: Clone + Eq> Watchable<T>
impl<T: Clone + Eq> Watchable<T>
Sourcepub fn set(&self, value: T) -> Result<T, T>
pub fn set(&self, value: T) -> Result<T, T>
Sets a new value.
Returns Ok(previous_value) if the value was different from the one set, or
returns the provided value back as Err(value) if the value didn’t change.
Watchers are only notified if the value changed.
Sourcepub fn watch_lazy(&self) -> LazyDirect<T>where
T: Default,
pub fn watch_lazy(&self) -> LazyDirect<T>where
T: Default,
Creates a LazyDirect Watcher, allowing the value to be observed, but not modified.
The LazyDirect watcher does not store the current value, making it smaller. If the watchable
is dropped, LazyDirect::get returns T::default.
Sourcepub fn weak_watcher(&self) -> WeakWatcher<T>
pub fn weak_watcher(&self) -> WeakWatcher<T>
Creates a WeakWatcher, which is a weak reference to the watchable’s shared state.
It has the size of a single pointer, and can be upgraded to a Direct or LazyDirect.
Sourcepub fn has_watchers(&self) -> bool
pub fn has_watchers(&self) -> bool
Returns true when there are any watchers actively listening on changes, or false when all watchers have been dropped or none have been created yet.