Skip to main content

Var

Struct Var 

Source
pub struct Var<T: Send + Sync + 'static>(/* private fields */);
Expand description

A reactive value that affects on-screen content.

Viewports track when a Var is read, automatically determining which parts of the UI it affects. When the variable is modified, the required updates will be applied to the screen.

This derefs to WeakVar a non-owning, Copy handle.

Anything visible and dynamic should be stored in a Var.

Deadlock note: like other synchronization primitives, accessing Vars across threads in an inconsistent order can cause lock inversion, potentially leading to deadlocks. Always acquire locks in a consistent order to prevent this.

Implementations§

Source§

impl<T: Send + Sync + 'static> Var<T>

Source

pub fn new(value: T) -> Self

Creates a new Var in the global registry with the specified initial value.

Source

pub fn read<'a>(&'a self) -> VarReadGuard<'a, T>

Returns a read guard to the value if the Var is alive, marking it as read from.

Source

pub fn write<'a>(&'a self) -> VarWriteGuard<'a, T>

Returns a write guard to the value if the Var is alive.

The guard handles marking the Var as written to and read from when it is dropped.

Source

pub fn get_version(&self) -> u64

Returns the current version of the variable.

Source

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

Returns a clone of the stored value if it is still alive, marking it as read from.

Source

pub fn set(&self, new: T)
where T: PartialEq,

Sets the value of the variable, but only bumps the version if the value actually changed.

Source

pub fn replace(&self, new: T) -> T

Replaces the value in the registry and returns the old value.

Source

pub fn take(&self) -> T
where T: Default,

Takes the current value, leaving Default::default() in its place.

Source

pub fn downgrade(&self) -> WeakVar<T>

Returns a WeakVar that references the same value without taking ownership.

This is useful for storing or passing a handle to the value without keeping it alive. The returned WeakVar can be cheaply copied and used to access or update the value as long as the original Var is still alive.

Methods from Deref<Target = WeakVar<T>>§

Source

pub fn is_alive(&self) -> bool

Checks if the associated Var has been dropped.

Source

pub fn get_version(&self) -> Option<u64>

Returns the current version of the variable.

Source

pub fn read<'a>(&'a self) -> Option<VarReadGuard<'a, T>>

Returns a read guard to the value if the Var is alive, marking it as read from.

Returns None if the Var has been destroyed.

Source

pub fn write<'a>(&'a self) -> Option<VarWriteGuard<'a, T>>

Returns a write guard to the value if the Var is alive.

The guard handles marking the Var as written to and read from when it is dropped.

Returns None if the Var has been destroyed.

Source

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

Returns a clone of the stored value if it is still alive, marking it as read from. Returns None otherwise.

Source

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

Returns the value if the Var is alive, or default if it has been dropped.

This method evaluates default eagerly, even if the value is alive. Use WeakVar::get_or_else to evaluate the default lazily.

Source

pub fn get_or_else(&self, default: impl FnOnce() -> T) -> T
where T: Clone,

Returns a clone of the value if alive, or computes a default from the given closure.

Unlike WeakVar::get_or, this only evaluates default if the Var is dead.

Source

pub fn set(&self, new: T) -> Option<()>
where T: PartialEq,

Sets the value of the variable, but only bumps the version if the value actually changed.

Returns None if the Var has been destroyed, Some(()) otherwise.

Source

pub fn replace(&self, new: T) -> Option<T>

Replaces the value in the registry and returns the old value.

Source

pub fn take(&self) -> Option<T>
where T: Default,

Takes the current value, leaving Default::default() in its place.

Source

pub fn mark_read(&self)

Marks a Var as if it had been read from.

This is a no-op if the Var has been destroyed.

Trait Implementations§

Source§

impl<T: Debug + Send + Sync + 'static> Debug for Var<T>

Source§

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

Debug formatting is side-effect free, so viewports won’t register the value as having been read from.

Source§

impl<T: Send + Sync + Default + 'static> Default for Var<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Send + Sync + 'static> Deref for Var<T>

Source§

type Target = WeakVar<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: Send + Sync + 'static> Drop for Var<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Send + Sync + 'static> From<T> for Var<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Var<T>

§

impl<T> !UnwindSafe for Var<T>

§

impl<T> Freeze for Var<T>

§

impl<T> Send for Var<T>

§

impl<T> Sync for Var<T>

§

impl<T> Unpin for Var<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Var<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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,