Struct EagerCompute

Source
pub struct EagerCompute<'f, C: 'f> {
    pub context: C,
    /* private fields */
}
Expand description

A helper which eagerly updates any outdated variables it is given.

The run method can be used to integrate with an async runtime. Otherwise, updates are performed by repeatedly calling tick.

Fields§

§context: C

The data passed to each computed variable on update.

Implementations§

Source§

impl<'f, C> EagerCompute<'f, C>

Source

pub fn new(context: C) -> Self

Creates a new eagerly updating environment. It will only evaluate variables that have been passed explicitly.

Source

pub fn install<V>(&mut self, computed: ComputedVar<V>)
where V: ComputedValue<Value = (), Context = C> + 'f,

Add the given empty computed variable to this environment. The variable will be recomputed every time tick is called if needed. This is a good way to watch many variables at once.

Note that outdated or uncomputed variables will be computed immediately when first installed.

let a = Var::new(1);
let b = Var::new(2);

let mut eval = EagerCompute::new(());
eval.install(computed! {
   a.get();
   b.get();
   println!("a or b changed")
});
Source

pub fn install_boxed(&mut self, computed: BoxedComputedVar<'f, (), C>)

Like install but relies on the user to pre-box the computed cell.

Source

pub fn uninstall_by_id( &mut self, node_id: usize, ) -> Option<BoxedComputedVar<'f, (), C>>

Removes the computed variable with the given node id from this environment.

Source

pub fn tick(&mut self)

Recompute any installed variables which are outdated. This method may deadlock if two watchers repeatedly trigger each-other in a loop. If that is an issue, try using tick_timeout.

Source

pub fn tick_timeout(&mut self, end: Instant) -> bool

Recompute any installed variables which are outdated. This method will return true if all variables are up-to-date or false if the end time was reached first.

Source

pub async fn run_once(&mut self) -> usize

Returns a future which updates a few installed variables once they become outdated then resolves to the number of updates performed. This is similar to run_forever except it only runs until at least 1 update has occurred.

Source

pub async fn run_forever(self)

Returns a future which runs until dropped, updating any installed variables when they become outdated. This works well with async event schedulers since the future will yield until a Var used by this environment is manually changed, at which point the future’s context will be instantly notified by Node::on_write.

Source§

impl<'f> EagerCompute<'f, ()>

Source

pub fn watch<V: Variable + 'f>( &mut self, var: V, watcher: impl FnMut(&V::Value) + 'f, )

Call a function whenever the given variable is changed and tick is called.

let a = Var::new(1);

let mut eval = EagerCompute::new(());
eval.watch(&a, |a| println!("a changed to {}", a));
Source

pub fn dbg<V: Variable + 'f>(&mut self, var: V)
where V::Value: Debug,

Print the value of the given variable to stderr whenever it changes and tick is called.

Trait Implementations§

Source§

impl<'f, C: Default> Default for EagerCompute<'f, C>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'f, C> Freeze for EagerCompute<'f, C>
where C: Freeze,

§

impl<'f, C> !RefUnwindSafe for EagerCompute<'f, C>

§

impl<'f, C> !Send for EagerCompute<'f, C>

§

impl<'f, C> !Sync for EagerCompute<'f, C>

§

impl<'f, C> Unpin for EagerCompute<'f, C>
where C: Unpin,

§

impl<'f, C> !UnwindSafe for EagerCompute<'f, C>

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.