Skip to main content

MutableState

Struct MutableState 

Source
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>

Source

pub fn is_released(&self) -> bool

Whether the state is still in the composition.

Source

pub fn get(&self) -> T
where T: Clone,

The value, recorded as read by the scope composing now. Panics if the state is released.

Source

pub fn try_get(&self) -> Result<T, StateError>
where T: Clone,

Like MutableState::get, and Err(StateError::Released) if the state is released.

Source

pub fn get_untracked(&self) -> T
where 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.

Source

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.

Source

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.

Source

pub fn try_set(&self, value: T) -> Result<(), StateError>

Like MutableState::set, and Err(StateError::Released) if the state is released.

Source

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.

Source

pub fn try_update(&self, change: impl FnOnce(&mut T)) -> Result<(), StateError>

Like MutableState::update, and Err(StateError::Released) if the state is released.

Source

pub fn as_state(&self) -> MutableState<T>

This state as a read-only State.

Trait Implementations§

Source§

impl<T> Clone for MutableState<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Copy for MutableState<T>

Source§

impl<T> Debug for MutableState<T>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Eq for MutableState<T>

Source§

impl<T> Hash for MutableState<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> PartialEq for MutableState<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: Clone + Send + 'static> State<T> for MutableState<T>

Source§

fn value(&self) -> T

The value, recorded as read by the scope composing now.

Auto Trait Implementations§

§

impl<T> Forget for MutableState<T>

§

impl<T> Freeze for MutableState<T>
where PhantomData<fn() -> T>: Freeze,

§

impl<T> Move for MutableState<T>

§

impl<T> RefUnwindSafe for MutableState<T>
where PhantomData<fn() -> T>: RefUnwindSafe,

§

impl<T> Send for MutableState<T>
where PhantomData<fn() -> T>: Send,

§

impl<T> Sync for MutableState<T>
where PhantomData<fn() -> T>: Sync,

§

impl<T> Unpin for MutableState<T>
where PhantomData<fn() -> T>: Unpin,

§

impl<T> UnsafeUnpin for MutableState<T>
where PhantomData<fn() -> T>: UnsafeUnpin,

§

impl<T> UnwindSafe for MutableState<T>
where PhantomData<fn() -> T>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.