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>
pub fn is_alive(&self) -> bool
pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R>
pub fn try_value(&self) -> Option<T>
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.
pub fn value(&self) -> T
pub fn get(&self) -> T
pub fn has_subscribers(&self) -> bool
pub fn on_subscriber(&self, callback: Rc<dyn Fn()>)
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.