pub struct MutableState<T> { /* private fields */ }Expand description
A value that composition observes. Compose’s MutableState.
Cloning gives another handle to the same value. Two handles compare equal when they are the same state, which is what makes a state a stable parameter: passing the same state to a composable again does not make it run.
It is Send and Sync when the value is Send, so a worker thread can hold one and
write it. The write is visible at once; the composables that read it run again on the
next frame, which the write asks for.
Reading takes a lock for as long as the read lasts. Reading the same state again inside
MutableState::with would wait on itself.
Implementations§
Source§impl<T: 'static> MutableState<T>
impl<T: 'static> MutableState<T>
Sourcepub fn get_untracked(&self) -> Twhere
T: Clone,
pub fn get_untracked(&self) -> Twhere
T: Clone,
The value, without recording a read. A scope that only peeks is not run again when the value changes.
Sourcepub fn with<R>(&self, read: impl FnOnce(&T) -> R) -> R
pub fn with<R>(&self, read: impl FnOnce(&T) -> R) -> R
Reads the value in place, recorded as read.
Sourcepub fn set(&self, value: T)
pub fn set(&self, value: T)
Replaces the value. Under the default policy an equal value changes nothing.
Sourcepub fn update(&self, change: impl FnOnce(&mut T))
pub fn update(&self, change: impl FnOnce(&mut T))
Changes the value in place. Always a change: there is no old value left to compare
against without a copy, and a caller who wants the comparison can use set.
Sourcepub fn as_state(&self) -> MutableState<T>
pub fn as_state(&self) -> MutableState<T>
This state as a read-only State.