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>
impl<T: Send + Sync + 'static> Var<T>
Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Creates a new Var in the global registry with the specified initial value.
Sourcepub fn read<'a>(&'a self) -> VarReadGuard<'a, T>
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.
Sourcepub fn write<'a>(&'a self) -> VarWriteGuard<'a, T>
pub fn write<'a>(&'a self) -> VarWriteGuard<'a, T>
Sourcepub fn get_version(&self) -> u64
pub fn get_version(&self) -> u64
Returns the current version of the variable.
Sourcepub fn get(&self) -> Twhere
T: Clone,
pub fn get(&self) -> Twhere
T: Clone,
Returns a clone of the stored value if it is still alive, marking it as read from.
Sourcepub fn set(&self, new: T)where
T: PartialEq,
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.
Sourcepub fn replace(&self, new: T) -> T
pub fn replace(&self, new: T) -> T
Replaces the value in the registry and returns the old value.
Sourcepub fn take(&self) -> Twhere
T: Default,
pub fn take(&self) -> Twhere
T: Default,
Takes the current value, leaving Default::default() in its place.
Methods from Deref<Target = WeakVar<T>>§
Sourcepub fn get_version(&self) -> Option<u64>
pub fn get_version(&self) -> Option<u64>
Returns the current version of the variable.
Sourcepub fn read<'a>(&'a self) -> Option<VarReadGuard<'a, T>>
pub fn read<'a>(&'a self) -> Option<VarReadGuard<'a, T>>
Sourcepub fn write<'a>(&'a self) -> Option<VarWriteGuard<'a, T>>
pub fn write<'a>(&'a self) -> Option<VarWriteGuard<'a, T>>
Sourcepub fn get(&self) -> Option<T>where
T: Clone,
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.
Sourcepub fn get_or(&self, default: T) -> Twhere
T: Clone,
pub fn get_or(&self, default: T) -> Twhere
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.
Sourcepub fn get_or_else(&self, default: impl FnOnce() -> T) -> Twhere
T: Clone,
pub fn get_or_else(&self, default: impl FnOnce() -> T) -> Twhere
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.
Sourcepub fn set(&self, new: T) -> Option<()>where
T: PartialEq,
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.
Sourcepub fn replace(&self, new: T) -> Option<T>
pub fn replace(&self, new: T) -> Option<T>
Replaces the value in the registry and returns the old value.
Sourcepub fn take(&self) -> Option<T>where
T: Default,
pub fn take(&self) -> Option<T>where
T: Default,
Takes the current value, leaving Default::default() in its place.