Depends
A library for ergonomic, performant, incremental computation between arbitrary types.
Why would I want that
Most applications rely on some core logic which must respond to external events. Often, the logic to transform each event in to an action is straightforward, but as the application scales, many hard to reason-with situations emerge from the combinatorial explosion of states.
Dependency graphs are an excellent code architecture to tame complexity in such scenarios.
use ;
use ;
// A `Leaf` is a node which takes new values from outside the graph. Nodes must
// implement `Hash`, attribute a hashable field as `#[depends(hash)]` or specify
// `#[depends(unhashable)]`.
// `Leaf` types must provide a way for code outside to update their internal state.
// This is just a simple replace for now.
// `dependencies` are derived to state what references `Dependee` nodes need to
// calculate their state on-demand. These could be any number of other `Dependee`s
// or `Leaf`s.
// A `Dependee` i.e. its state is a pure transformation of other nodes
// This trait specifies how a `Dependee` updates its internal state given its dependencies.
// Compose a graph!
let left = default.into_leaf;
let right = default.into_leaf;
let sum = default.into_node;
let graph = MyGraph ;
// A `Visitor` is a collection which tracks which nodes have been visited each run.
let mut visitor = new;
// Resolving the graph from any node will traverse via Depth First Search, prompting
// recalculation for an node which has `State::Dirty`.
//
// Using `resolve_root` ensures the visitor is cleaned each time, ready for use again.
assert_eq!;
// Update the leaves. Any dependent nodes will recompute their internal values.
graph.left.update;
graph.right.update;
// We've successfully implemented simple addition!
assert_eq!;
Clearly, to implement a simple addition problem, a dependency graph is
overkill. However, for more complex problems, where many inputs can change
and the output is a combination of many transformations on that input (and
derivations of it), depends
can help you produce scalable, performant,
testable code out of the box.
Graphviz
Any graph built using depends
can be converted to a Graphviz representation
by passing a GraphvizVisitor
(this requires the feature graphviz
).
let graph = my_graph;
let mut visitor = new;
graph.answer.resolve_root;
assert_eq!;
The graph in the above example is rendered below.
Current Status
This crate should be considered a Proof Of Concept only and treated with huge amounts of scepticism and distrust.
The guarantees we would like to offer, before considering this crate production-worthy are:
- Determinism of output given any sequence of inputs and actions.
- Graphs cannot yield different results between calls to
resolve
without a change in input, other than whenClean
has been implemented incorrectly. - Correctness of the internal caching logic.
Feel free to experiment with the crate, apply it to your problems and pass on any feedback you have.