pub struct State<T: Clone + 'static> { /* private fields */ }Expand description
Cheap copyable read-only view of a state cell.
Implementations§
Source§impl<T: Clone + 'static> State<T>
impl<T: Clone + 'static> State<T>
Sourcepub fn is_alive(&self) -> bool
pub fn is_alive(&self) -> bool
Returns true while the state exists. It ends when its owner releases it or its runtime is
dropped.
Sourcepub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R>
pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R>
Reads the value through f without subscribing the current scope. Returns None if the
state was released or has no readable value.
Sourcepub fn try_value(&self) -> Option<T>
pub fn try_value(&self) -> Option<T>
Returns a copy of the value without subscribing the current scope, or None if the state
was released or has no readable value.
Sourcepub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R
Reads a copy of the value through f and subscribes the current
scope. f may write this state; see Self::read for a read that
borrows instead of copying.
Sourcepub fn read<R>(&self, f: impl FnOnce(&T) -> R) -> R
pub fn read<R>(&self, f: impl FnOnce(&T) -> R) -> R
Reads the value in place through f and subscribes the current
scope. Nothing is cloned, so a read of a large value costs nothing
beyond f; in return f borrows the stored value and must not write
this state.
Sourcepub fn value(&self) -> T
pub fn value(&self) -> T
Returns a copy of the value and subscribes the current scope, which recomposes when the value changes. Panics if the state was released.
Sourcepub fn get_non_reactive(&self) -> T
pub fn get_non_reactive(&self) -> T
Reads the value without subscribing the current scope, for a composition that changes what it shows by the value only through a layer or a draw that reads it again.
Sourcepub fn has_subscribers(&self) -> bool
pub fn has_subscribers(&self) -> bool
Returns true if any scope or observer is subscribed to this state.
Sourcepub fn on_subscriber(&self, callback: impl Fn() + 'static)
pub fn on_subscriber(&self, callback: impl Fn() + 'static)
Runs callback each time this state gains its first subscriber, and
right away when it already has one.
The state owns callback for as long as the state lives, so nothing
has to be kept alive on the caller’s side.
Sourcepub fn subscription_hold(&self) -> StateSubscriptionHold
pub fn subscription_hold(&self) -> StateSubscriptionHold
Counts this state as subscribed for as long as the returned guard lives, without observing it from any scope.
Snapshot observers subscribe reads made under composition or the
draw phase, and producers gated on State::has_subscribers — an
infinite transition, for one — stop themselves when the last such
subscriber leaves. A consumer that reads the value from a polling
context (a frame effect stepping it into a derived state, a
background task) is invisible to that accounting and would starve
the producer it depends on. Holding this guard keeps the producer
alive; dropping it releases the count.