Skip to main content

MutableState

Struct MutableState 

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

Source

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

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

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.

Source

pub fn with<R>(&self, read: impl FnOnce(&T) -> R) -> R

Reads the value in place, recorded as read.

Source

pub fn set(&self, value: T)

Replaces the value. Under the default policy an equal value changes nothing.

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.

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: Debug> 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> 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 + '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> Freeze for MutableState<T>
where Arc<StateCell<T>>: Freeze,

§

impl<T> RefUnwindSafe for MutableState<T>
where Arc<StateCell<T>>: RefUnwindSafe,

§

impl<T> Send for MutableState<T>
where Arc<StateCell<T>>: Send,

§

impl<T> Sync for MutableState<T>
where Arc<StateCell<T>>: Sync,

§

impl<T> Unpin for MutableState<T>
where Arc<StateCell<T>>: Unpin,

§

impl<T> UnsafeUnpin for MutableState<T>
where Arc<StateCell<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for MutableState<T>
where Arc<StateCell<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.