incremental-rs
A port of Jane Street's Incremental library.
- Pretty rigorous implementation of the basic Incr features, moderately well tested and benchmarked, performs very similarly to the original.
- Partial implementation of incremental-map
Install
Examples
Basic usage:
use IncrState;
let state = new;
let variable = state.var;
let times_10 = variable.map;
let observer = times_10.observe;
// stabilise will propagate any changes
state.stabilise;
let value = observer.value;
assert_eq!;
// now mutate
variable.set;
state.stabilise;
// watch as var was propagated through the tree, and reached our observer
assert_eq!;
Subscriptions, and an illustration of how propagation stops when nodes produce the same value as last time:
use ;
// A little system to compute the absolute value of an input
// Note that the input could change (e.g. 5 to -5), but the
// output may stay the same (5 both times).
let state = new;
let variable = state.var;
let absolute = variable.map;
let observer = absolute.observe;
// set up a subscription.
use ;
let latest = new;
let latest_clone = latest.clone;
let subscription_token = observer.subscribe;
// initial stabilisation
state.stabilise;
assert_eq!;
assert_eq!;
// now mutate, but such that the output of abs() won't change
variable.set;
state.stabilise;
// The subscription function was not called, because the `absolute` node
// produced the same value as last time we stabilised.
assert_eq!;
assert_eq!;
// now mutate such that the output changes too
variable.set;
state.stabilise;
// The observer did get a new value, and did call the subscription function
assert_eq!;
assert_eq!;
// now unsubscribe. this also implicitly happens if you drop the observer,
// but you can individually unsubscribe particular subscriptions if you wish.
observer.unsubscribe;
// dropping the observer also unloads any part of the computation graph
// that was only running for the purposes of this particular observer
drop;
// now that the observer is dead, we can mutate the variable and nothing will
// happen, like, at all. The absolute value will not be computed.
variable.set;
let recomputed = state.stats.recomputed;
state.stabilise;
assert_eq!;