pub struct State<S> { /* private fields */ }Expand description
A thread-safe state, that can be used to share application state globally.
It is similar to a RWLock<S>, but it also allows asynchronous waiting for state changes.
This can be useful to coordinate between different parts of an application.
Implementations§
Source§impl<S> State<S>
impl<S> State<S>
Sourcepub fn new_with_on_change(
state: S,
on_change: impl Fn(&S, &S) + 'static,
) -> Self
pub fn new_with_on_change( state: S, on_change: impl Fn(&S, &S) + 'static, ) -> Self
Creates a new state with the given on_change callback.
§Notes
The callback is not called when the state is set for the first time, as well as on
the State::update method. You must call the callback manually in these cases.
Sourcepub fn set_on_change(
&mut self,
on_change: impl Fn(&S, &S) + 'static,
) -> Result<(), UpdateOnChangeError>
pub fn set_on_change( &mut self, on_change: impl Fn(&S, &S) + 'static, ) -> Result<(), UpdateOnChangeError>
Tries to set the on_change callback, to the new callback.
§Notes
The callback is not called when the state is set for the first time, as well as on
the State::update method. You must call the callback manually in these cases.
Sourcepub fn ref_count(&self) -> usize
pub fn ref_count(&self) -> usize
Returns the number of references to the state. This can be used to check if there are any other references to the state.
Sourcepub fn get_ref(&self) -> StateRef<'_, S>
pub fn get_ref(&self) -> StateRef<'_, S>
Returns a reference to the current state.
This can be used if the state does not implement Clone or if you want to avoid cloning.
Sourcepub fn wait_for<C>(&self, wait_for: C) -> StateFuture<S, C> ⓘ
pub fn wait_for<C>(&self, wait_for: C) -> StateFuture<S, C> ⓘ
Returns a future that completes when the state matches the given predicate.