pub struct Incr<T> { /* private fields */ }Implementations§
Source§impl<T> Incr<T>
impl<T> Incr<T>
pub fn weak(&self) -> WeakIncr<T>
pub fn set_graphviz_user_data(&self, data: impl Debug + NotObserver + 'static)
pub fn with_graphviz_user_data( self, data: impl Debug + NotObserver + 'static, ) -> Self
pub fn state(&self) -> WeakState
Source§impl<T: Value> Incr<T>
impl<T: Value> Incr<T>
Sourcepub fn pipe<R>(&self, f: impl FnOnce(Incr<T>) -> Incr<R>) -> Incr<R>
pub fn pipe<R>(&self, f: impl FnOnce(Incr<T>) -> Incr<R>) -> Incr<R>
A convenience function for taking a function of type fn(Incr<T>) -> Incr<R> and
applying it to self. This enables you to put your own functions
into the middle of a chain of method calls on Incr.
pub fn pipe1<R, A1>( &self, f: impl FnOnce(Incr<T>, A1) -> Incr<R>, arg1: A1, ) -> Incr<R>
pub fn pipe2<R, A1, A2>( &self, f: impl FnOnce(Incr<T>, A1, A2) -> Incr<R>, arg1: A1, arg2: A2, ) -> Incr<R>
Sourcepub fn enumerate<R, F>(&self, f: F) -> Incr<R>
pub fn enumerate<R, F>(&self, f: F) -> Incr<R>
A simple variation on Incr::map that tells you how many times the incremental has recomputed before this time.
Sourcepub fn zip<T2: Value>(&self, other: &Incr<T2>) -> Incr<(T, T2)>
pub fn zip<T2: Value>(&self, other: &Incr<T2>) -> Incr<(T, T2)>
Turn two incrementals into a tuple incremental.
Aka both in OCaml. This is named zip to match Option::zip and Iterator::zip.
pub fn map_ref<F, R: Value>(&self, f: F) -> Incr<R>
Sourcepub fn map_cyclic<R: Value>(
&self,
cyclic: impl FnMut(WeakIncr<R>, &T) -> R + 'static + NotObserver,
) -> Incr<R>
pub fn map_cyclic<R: Value>( &self, cyclic: impl FnMut(WeakIncr<R>, &T) -> R + 'static + NotObserver, ) -> Incr<R>
A version of map that gives you a (weak) reference to the map node you’re making, in the closure.
Useful for advanced usage where you want to add manual dependencies with the crate::expert constructs.
Sourcepub fn map_with_old<R, F>(&self, f: F) -> Incr<R>
pub fn map_with_old<R, F>(&self, f: F) -> Incr<R>
A version of Incr::map that allows reuse of the old value. You can use it to produce a new value. The main use case is avoiding allocation.
The return type of the closure is (R, bool). The boolean
value is a replacement for the Cutoff system, because
the Cutoff functions require access to an old value and
a new value. With Incr::map_with_old, you must figure out yourself
(without relying on PartialEq, for example) whether the
incremental node should propagate its changes.
Sourcepub fn binds<F, R>(&self, f: F) -> Incr<R>
pub fn binds<F, R>(&self, f: F) -> Incr<R>
A version of bind that includes a copy of the crate::IncrState (as crate::WeakState) to help you construct new incrementals within the bind.
pub fn bind<F, R>(&self, f: F) -> Incr<R>
Sourcepub fn observe(&self) -> Observer<T>
pub fn observe(&self) -> Observer<T>
Creates an observer for this incremental.
Observers are the primary way to get data out of the computation. Their creation and lifetime inform Incremental which parts of the computation graph are necessary, such that if you create many variables and computations based on them, but only hook up some of that to an observer, only the parts transitively necessary to supply the observer with values are queued to be recomputed.
That means, without an observer, var.set(new_value) does essentially
nothing, even if you have created incrementals like
var.map(...).bind(...).map(...). In this fashion, you can safely
set up computation graphs before you need them, or refuse to dismantle
them, knowing the expensive computations they contain will not
grace the CPU until they’re explicitly put back under the purview
of an Observer.
Calling this multiple times on the same node produces multiple observers. Only one is necessary to keep a part of a computation graph alive and ticking.
Sourcepub fn set_cutoff(&self, cutoff: Cutoff<T>)
pub fn set_cutoff(&self, cutoff: Cutoff<T>)
Sets the cutoff function that determines (if it returns true)
whether to stop (cut off) propagating changes through the graph.
Note that this method can be called on Var as well as any
other Incr.
The default is Cutoff::PartialEq. So if your values do not change,
they will cut off propagation. There is a bound on all T in
Incr<T> used in incremental-rs, all values you pass around
must be PartialEq.
You can also supply your own comparison function. This will chiefly
be useful for types like Rc<T>, not to avoid T: PartialEq (you
can’t avoid that) but rather to avoid comparing a large structure
and simply compare the allocation’s pointer value instead.
In that case, you can:
use std::rc::Rc;
use incremental::{IncrState, Cutoff};
let incr = IncrState::new();
let var = incr.var(Rc::new(5));
var.set_cutoff(Cutoff::Fn(Rc::ptr_eq));
// but note that doing this will now cause the change below
// to propagate, whereas before it would not as the two
// numbers are == equal:
var.set(Rc::new(5));Sourcepub fn set_cutoff_fn(&self, cutoff_fn: fn(&T, &T) -> bool)
pub fn set_cutoff_fn(&self, cutoff_fn: fn(&T, &T) -> bool)
A shorthand for using Incr::set_cutoff with a function pointer, i.e. with Cutoff::Fn. Most comparison functions, including closures, can be cast to a function pointer because they won’t capture any values.
use incremental::IncrState;
let incr = IncrState::new();
let var = incr.var(5);
var.set_cutoff_fn(|a, b| a == b);
var.set_cutoff_fn(i32::eq);
var.set_cutoff_fn(PartialEq::eq);Sourcepub fn set_cutoff_fn_boxed<F>(&self, cutoff_fn: F)
pub fn set_cutoff_fn_boxed<F>(&self, cutoff_fn: F)
A shorthand for using Incr::set_cutoff with Cutoff::FnBoxed and a closure that may capture its environment and mutate its captures.
use std::{cell::Cell, rc::Rc};
use incremental::IncrState;
let incr = IncrState::new();
let var = incr.var(5);
let capture = Rc::new(Cell::new(false));
var.set_cutoff_fn_boxed(move |_, _| capture.get());pub fn save_dot_to_file(&self, named: &str)
pub fn depend_on<T2: Value>(&self, on: &Incr<T2>) -> Incr<T>
pub fn on_update(&self, f: impl FnMut(NodeUpdate<&T>) + 'static + NotObserver)
Source§impl<T1: Value> Incr<T1>
impl<T1: Value> Incr<T1>
Sourcepub fn map<F1, R>(&self, f: F1) -> Incr<R>
pub fn map<F1, R>(&self, f: F1) -> Incr<R>
Takes an incremental (self), and produces a new incremental whose value
is the result of applying a function f to the first value.
§Example
let state = IncrState::new();
let var = state.var(20);
// produce a new incremental that adds ten
let plus10 = var.map(|x| *x + 10);
let observer = plus10.observe();
state.stabilise();
assert_eq!(observer.value(), 30);
var.set(400);
state.stabilise();
assert_eq!(observer.value(), 410);Source§impl<T1: Value> Incr<T1>
impl<T1: Value> Incr<T1>
Source§impl<T1: Value> Incr<T1>
impl<T1: Value> Incr<T1>
Source§impl<T1: Value> Incr<T1>
impl<T1: Value> Incr<T1>
Source§impl<T1: Value> Incr<T1>
impl<T1: Value> Incr<T1>
Sourcepub fn map6<F6, T2, T3, T4, T5, T6, R>(
&self,
two: &Incr<T2>,
three: &Incr<T3>,
four: &Incr<T4>,
five: &Incr<T5>,
six: &Incr<T6>,
f: F6,
) -> Incr<R>
pub fn map6<F6, T2, T3, T4, T5, T6, R>( &self, two: &Incr<T2>, three: &Incr<T3>, four: &Incr<T4>, five: &Incr<T5>, six: &Incr<T6>, f: F6, ) -> Incr<R>
Like Incr::map and Incr::map2, but with more input incrementals.
If you don’t feel like counting, try using the (i1 % i2 % ...).map(|_, _, ...| ...) syntax.