Incr

Struct Incr 

Source
pub struct Incr<T> { /* private fields */ }

Implementations§

Source§

impl<T> Incr<T>

Source

pub fn weak(&self) -> WeakIncr<T>

Source

pub fn set_graphviz_user_data(&self, data: impl Debug + NotObserver + 'static)

Source

pub fn with_graphviz_user_data( self, data: impl Debug + NotObserver + 'static, ) -> Self

Source

pub fn state(&self) -> WeakState

Source§

impl<T: Value> Incr<T>

Source

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.

Source

pub fn pipe1<R, A1>( &self, f: impl FnOnce(Incr<T>, A1) -> Incr<R>, arg1: A1, ) -> Incr<R>

Source

pub fn pipe2<R, A1, A2>( &self, f: impl FnOnce(Incr<T>, A1, A2) -> Incr<R>, arg1: A1, arg2: A2, ) -> Incr<R>

Source

pub fn enumerate<R, F>(&self, f: F) -> Incr<R>
where R: Value, F: FnMut(usize, &T) -> R + 'static + NotObserver,

A simple variation on Incr::map that tells you how many times the incremental has recomputed before this time.

Source

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.

Source

pub fn map_ref<F, R: Value>(&self, f: F) -> Incr<R>
where F: for<'a> Fn(&'a T) -> &'a R + 'static + NotObserver,

Source

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.

Source

pub fn map_with_old<R, F>(&self, f: F) -> Incr<R>
where R: Value, F: FnMut(Option<R>, &T) -> (R, bool) + 'static + NotObserver,

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.

Source

pub fn binds<F, R>(&self, f: F) -> Incr<R>
where R: Value, F: FnMut(&WeakState, &T) -> Incr<R> + 'static + NotObserver,

A version of bind that includes a copy of the crate::IncrState (as crate::WeakState) to help you construct new incrementals within the bind.

Source

pub fn bind<F, R>(&self, f: F) -> Incr<R>
where R: Value, F: FnMut(&T) -> Incr<R> + 'static + NotObserver,

Source

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.

Source

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));
Source

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);
Source

pub fn set_cutoff_fn_boxed<F>(&self, cutoff_fn: F)
where F: FnMut(&T, &T) -> bool + Clone + 'static + NotObserver,

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());
Source

pub fn save_dot_to_file(&self, named: &str)

Source

pub fn depend_on<T2: Value>(&self, on: &Incr<T2>) -> Incr<T>

Source

pub fn on_update(&self, f: impl FnMut(NodeUpdate<&T>) + 'static + NotObserver)

Source§

impl<T1: Value> Incr<T1>

Source

pub fn map<F1, R>(&self, f: F1) -> Incr<R>
where R: Value, F1: FnMut(&T1) -> R + 'static + NotObserver,

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>

Source

pub fn map2<F2, T2, R>(&self, two: &Incr<T2>, f: F2) -> Incr<R>
where T2: Value, R: Value, F2: FnMut(&T1, &T2) -> R + 'static + NotObserver,

Like Incr::map, but with two inputs.

let state = IncrState::new();
let v1 = state.var(1);
let v2 = state.var(1);
let add = v1.map2(&v2, |a, b| *a + *b);
Source§

impl<T1: Value> Incr<T1>

Source

pub fn map3<F3, T2, T3, R>( &self, two: &Incr<T2>, three: &Incr<T3>, f: F3, ) -> Incr<R>
where T2: Value, T3: Value, R: Value, F3: FnMut(&T1, &T2, &T3) -> R + 'static + NotObserver,

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.

Source§

impl<T1: Value> Incr<T1>

Source

pub fn map4<F4, T2, T3, T4, R>( &self, two: &Incr<T2>, three: &Incr<T3>, four: &Incr<T4>, f: F4, ) -> Incr<R>
where T2: Value, T3: Value, T4: Value, R: Value, F4: FnMut(&T1, &T2, &T3, &T4) -> R + 'static + NotObserver,

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.

Source§

impl<T1: Value> Incr<T1>

Source

pub fn map5<F5, T2, T3, T4, T5, R>( &self, two: &Incr<T2>, three: &Incr<T3>, four: &Incr<T4>, five: &Incr<T5>, f: F5, ) -> Incr<R>
where T2: Value, T3: Value, T4: Value, T5: Value, R: Value, F5: FnMut(&T1, &T2, &T3, &T4, &T5) -> R + 'static + NotObserver,

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.

Source§

impl<T1: Value> Incr<T1>

Source

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>
where T2: Value, T3: Value, T4: Value, T5: Value, T6: Value, R: Value, F6: FnMut(&T1, &T2, &T3, &T4, &T5, &T6) -> R + 'static + NotObserver,

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.

Trait Implementations§

Source§

impl<T: Value> AsRef<Incr<T>> for Incr<T>

Source§

fn as_ref(&self) -> &Incr<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Value> AsRef<Incr<T>> for Node<T>

Source§

fn as_ref(&self) -> &Incr<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Value> AsRef<Incr<T>> for Var<T>

Source§

fn as_ref(&self) -> &Incr<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Clone for Incr<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Incr<T>

Source§

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

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

impl<T> From<Rc<dyn Incremental<T>>> for Incr<T>

Source§

fn from(node: Rc<dyn Incremental<T>>) -> Self

Converts to this type from the input type.
Source§

impl<T> Hash for Incr<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> IntoIncr<T> for &Incr<T>

Source§

fn into_incr(self) -> Incr<T>

Source§

impl<T> IntoIncr<T> for Incr<T>

Source§

fn into_incr(self) -> Incr<T>

Source§

impl<T> PartialEq for Incr<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<I1, I2> Rem<&Incr<I2>> for &Incr<I1>

Source§

type Output = MapBuilder2<I1, I2>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: &Incr<I2>) -> Self::Output

Performs the % operation. Read more
Source§

impl<I1, I2> Rem<Incr<I2>> for Incr<I1>

Source§

type Output = MapBuilder2<I1, I2>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Incr<I2>) -> Self::Output

Performs the % operation. Read more
Source§

impl<T> Eq for Incr<T>

Auto Trait Implementations§

§

impl<T> Freeze for Incr<T>

§

impl<T> !RefUnwindSafe for Incr<T>

§

impl<T> !Send for Incr<T>

§

impl<T> !Sync for Incr<T>

§

impl<T> Unpin for Incr<T>

§

impl<T> !UnwindSafe for Incr<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> Value for T
where T: Debug + Clone + PartialEq + NotObserver + 'static + Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> NotObserver for T
where T: ?Sized,