Struct Var

Source
pub struct Var<T> { /* private fields */ }
Expand description

A mutable variable that is tracked by the reax runtime.

Today this is a wrapper around a RefCell and a Node but may be more generic in the future once GATs are stable.

Implementations§

Source§

impl<T> Var<T>

Source

pub fn new(value: T) -> Self

Creates a new interiorly mutable wrapper around the given value.

Source

pub fn set(&self, value: T)

Sets the value of the variable, instantly marking all upstream variables as dirty. This method will panic if any reference given by a call to mutate is still alive.

let var = Var::new(1);
let double = computed! { var.get_copy() * 2 };
var.set(2);
assert!(double.node().is_dirty());
Source

pub fn set_checked(&self, value: T)
where T: PartialEq,

Sets the value of the variable. Unlike set, upstream variables are not marked as dirty if the given value is equal to the current value.

Source

pub fn mutate(&self) -> RefMut<'_, T>

Provides a mutable reference to the variable, instantly marking all upstream variables as dirty. This method will panic if any reference given by a previous call to mutate is still alive.

let var = Var::new(1);
let double = computed! { var.get_copy() * 2 };
*var.mutate() += 1;
assert!(double.node().is_dirty());

Trait Implementations§

Source§

impl<T: Debug> Debug for Var<T>

Source§

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

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

impl<T> Variable for Var<T>

Source§

type Value = T

The type of this variable.
Source§

fn node(&self) -> &Node

Returns a handle to this variable’s node in the reax runtime’s dependency graph.
Source§

fn get(&self) -> Ref<'_, T>

Returns the value of this variable. Read more
Source§

fn get_non_reactive(&self) -> Ref<'_, T>

Returns the value of this variable without alerting the reax runtime that the variable is used in the current context. See also get.
Source§

fn get_copy(&self) -> Self::Value
where Self::Value: Copy,

Returns the value of this variable by copy where possible. See also get.
Source§

fn map<T, F>(self, func: F) -> ComputedVar<FunctionMapped<Self, T, F>>
where F: FnMut(&Self::Value) -> T,

Create a new ComputedVar which depends only on this variable. The value of the returned cell is lazily computed by the given function. That is, the function will not be executed until someone retrieves the value of the cell. This can be used similarly to EagerCompute::watch if the resulting cell is checked frequently. See also ComputedVar::new. Read more
Source§

fn map_mutate<T, F>( self, initial: T, func: F, ) -> ComputedVar<MutatorMapped<Self, T, F>>
where F: FnMut(&Self::Value, &mut T),

Create a new ComputedVar which depends only on this variable. The value of the returned cell is lazily updated by the given function. That is, the function will not be executed until someone retrieves the value of the cell. This can be used similarly to EagerCompute::watch if the resulting cell is checked frequently. See also ComputedVar::new_mutate. Read more
Source§

fn with_label(self, label: impl Display) -> Self

Provides the runtime with a variable name to use in debug outputs. This does nothing in release builds.

Auto Trait Implementations§

§

impl<T> !Freeze for Var<T>

§

impl<T> !RefUnwindSafe 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> UnwindSafe for Var<T>
where 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> 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, 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.