Skip to main content

SnapshotState

Struct SnapshotState 

Source
pub struct SnapshotState<T>
where 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 mutableStateOf / MutableState::with_runtime.

Implementations§

Source§

impl<T> MutableState<T>
where T: Clone + 'static,

Source

pub fn with_runtime(value: T, runtime: RuntimeHandle) -> MutableState<T>

Creates a runtime-owned persistent state handle.

Source

pub fn is_alive(&self) -> bool

Returns true if the underlying state cell is still alive (not released).

Source

pub fn try_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R>

Like with, but returns None if the state cell has been released.

Use this in frame callbacks, fling animations, and other contexts where the owning composition group may have been disposed (e.g., tab switch).

Source

pub fn try_value(&self) -> Option<T>

Like value, but returns None if the state cell has been released.

Source

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

Source

pub fn try_retain(&self) -> Option<OwnedMutableState<T>>

Upgrades this handle into an owning state value if the cell is still alive.

Source

pub fn retain(&self) -> OwnedMutableState<T>

Upgrades this handle into an owning state value.

Source

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

Source

pub fn update<R>(&self, f: impl FnOnce(&mut T) -> R) -> R

Source

pub fn replace(&self, value: T)

Sets a new value, silently skipping the write if the state cell was released.

State cells can be released between when a SideEffect closure captures a MutableState handle and when the side effect actually runs (command application disposes composition groups, freeing their state slots, before side effects execute). Using with_typed_opt instead of with_typed avoids a panic on the stale handle.

Source

pub fn set_value(&self, value: T)

Source

pub fn set(&self, value: T)

Source

pub fn value(&self) -> T

Source

pub fn get(&self) -> T

Source

pub fn get_non_reactive(&self) -> T

Gets the current value WITHOUT subscribing to recomposition.

Use this in layout/measure/draw phases to read state without causing the current composition scope to recompose when the state changes.

§When to use
  • In modifier nodes (like ScrollNode) during measure()
  • In any layout phase code that reads state but shouldn’t trigger recomposition
§When NOT to use
  • In composable functions that should update when state changes
  • When you want reactive UI updates

Trait Implementations§

Source§

impl<T> Clone for MutableState<T>
where T: Clone + 'static,

Source§

fn clone(&self) -> MutableState<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for MutableState<T>
where T: Debug + Clone + 'static,

Source§

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

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

impl<T> PartialEq for MutableState<T>
where T: Clone + 'static,

Source§

fn eq(&self, other: &MutableState<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Copy for MutableState<T>
where T: Clone + 'static,

Source§

impl<T> Eq for MutableState<T>
where T: Clone + 'static,

Auto Trait Implementations§

§

impl<T> Freeze for MutableState<T>

§

impl<T> RefUnwindSafe for MutableState<T>

§

impl<T> Send for MutableState<T>

§

impl<T> Sync for MutableState<T>

§

impl<T> Unpin for MutableState<T>

§

impl<T> UnsafeUnpin for MutableState<T>

§

impl<T> UnwindSafe for MutableState<T>

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.