pub struct MutableState<T: Clone + 'static> { /* private fields */ }Expand description
Cheap copyable mutable view of a state cell.
Ownership lives elsewhere: a composition slot, an OwnedMutableState, or
the runtime for states created with crate::mutableStateOf /
MutableState::with_runtime.
Implementations§
Source§impl<T: Clone + 'static> MutableState<T>
impl<T: Clone + 'static> MutableState<T>
Sourcepub fn with_runtime(value: T, runtime: RuntimeHandle) -> Self
pub fn with_runtime(value: T, runtime: RuntimeHandle) -> Self
Creates a state holding value in runtime. Every write counts as a change, even of an
equal value. Unless a remember owns the state, the runtime keeps it for its own lifetime.
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 try_retain(&self) -> Option<OwnedMutableState<T>>
pub fn try_retain(&self) -> Option<OwnedMutableState<T>>
Returns an OwnedMutableState that keeps this state alive until its last clone is
dropped. Returns None if the state was already released.
Sourcepub fn retain(&self) -> OwnedMutableState<T>
pub fn retain(&self) -> OwnedMutableState<T>
Returns an OwnedMutableState that keeps this state alive until its last clone is
dropped. Panics if the state was released.
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 update<R>(&self, f: impl FnOnce(&mut T) -> R) -> R
pub fn update<R>(&self, f: impl FnOnce(&mut T) -> R) -> R
Runs f on a copy of the value and stores the copy, unless f wrote this state itself.
Scopes that read the state are notified if the value changed.
Sourcepub fn replace(&self, value: T)
pub fn replace(&self, value: T)
Stores value and notifies the scopes that read the state, if the state’s equality policy
sees a change. Does nothing if the state was released.
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
Returns a copy of the value without subscribing the current scope.