pub struct MutableState<T> { /* private fields */ }Expand description
A handle to a value that composition observes. Compose’s MutableState.
The handle is Copy: it names a place in an arena, and the value is owned by the
composition. The value lives as long as the remember call that made it stays in the
composition and is dropped, once, when that call’s group is removed. A handle used
after that is released: set and update do nothing, get panics, and the try_
forms return StateError::Released. A slot reused for another state has a new
generation, so an old handle never reaches the new value.
Two handles are equal when they name 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. Equal values in two different states are not equal handles.
A handle is Send and Sync, so a worker thread can take one by move 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. A read on a worker clones the value and subscribes to
nothing.
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: Send + 'static> MutableState<T>
impl<T: Send + 'static> MutableState<T>
Sourcepub fn is_released(&self) -> bool
pub fn is_released(&self) -> bool
Whether the state is still in the composition.
Sourcepub fn get(&self) -> Twhere
T: Clone,
pub fn get(&self) -> Twhere
T: Clone,
The value, recorded as read by the scope composing now. Panics if the state is released.
Sourcepub fn try_get(&self) -> Result<T, StateError>where
T: Clone,
pub fn try_get(&self) -> Result<T, StateError>where
T: Clone,
Like MutableState::get, and Err(StateError::Released) if the state is
released.
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. Panics if the state is released.
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. Panics if the state is released.
Sourcepub fn set(&self, value: T)
pub fn set(&self, value: T)
Replaces the value. Under the default policy an equal value changes nothing. Does nothing if the state is released.
Sourcepub fn try_set(&self, value: T) -> Result<(), StateError>
pub fn try_set(&self, value: T) -> Result<(), StateError>
Like MutableState::set, and Err(StateError::Released) if the state is
released.
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. Does
nothing if the state is released.
Sourcepub fn try_update(&self, change: impl FnOnce(&mut T)) -> Result<(), StateError>
pub fn try_update(&self, change: impl FnOnce(&mut T)) -> Result<(), StateError>
Like MutableState::update, and Err(StateError::Released) if the state is
released.
Sourcepub fn as_state(&self) -> MutableState<T>
pub fn as_state(&self) -> MutableState<T>
This state as a read-only State.