| Crates.io | directed |
| lib.rs | directed |
| version | 0.3.0 |
| created_at | 2025-04-20 02:44:05.044984+00 |
| updated_at | 2025-08-01 02:04:58.35486+00 |
| description | Evaluate programs based on Directed Acyclic Graphs |
| homepage | https://github.com/Eolu/directed |
| repository | https://github.com/Eolu/directed |
| max_upload_size | |
| id | 1641334 |
| size | 103,956 |
This crate is a Directed-Acyclic-Graph (DAG)-based evaluation system for Rust. It allows you to wrap functions in a way that converts them into stateful Nodes in a graph. These can then be executed in the shortest-path to be able to evaluate one or more output nodes. Inputs and outputs can be cached (memoization), and nodes can have internal state (or not, anything can be stateless as well). Graph connections can be rewired at runtime without the loss of node state.
A Stage is a wrapped function that can be used to create a Node. Think of a Stage as a definition and a Node as a stateful instantiation.
When a function is annotated with the #[stage] macro, it will be converted to a struct of the same name, and given an implementation of the Stage trait. For this reason, struct naming conventions should be followed rather than function naming conventions:
use directed::*;
#[stage]
fn SimpleStage() -> String {
String::from("Hello graph!")
}
Stages can support multiple named outputs by making use of out sttribute and the output macro. This can be used to make connections between specific outputs of one node to specific inputs of another:
use directed::*;
// When multiple outputs exist, they must be specified within 'out'. Syntax is siumilar to typical input arguments.
#[stage(out(output1: u32, output2: String))]
fn MultiOutputStage() -> _ {
let output2 = String::from("Hello graph!");
output! {
output1: 42,
// Typical struct creation rules apply, no need to specify the name twice
output2
}
}
TODO: Create docs explaining evaluation of specific nodes
Stages can be annotated as cache_last. This will indicate that if reevaluated with identical inputs to the previous evaluation, it will just return cached outputs without rerunning the function:
use directed::*;
// If this is run with 31 as an input twice, "to_string" will not be called the 2nd time.
#[stage(cache_last)]
fn CacheLastStage(num: u32) -> String {
num.to_string()
}
Preconditions:
PartialEq (compile-time error if condition is not met)Clone (compile-time error if condition is not met)Stages can be annotated with cache_all. This means that for any previously identical input, return the associated output without reevaluating.
Preconditions:
cache_lastHashStages can be annotated with state. This will indicate fields that can be used to store internal state for the node. This could also be used to store some kind of configuration for the node that might be modified outside the graph's evaluation time. State is never accessed by other nodes or transferred throughout the graph in any way. Fields are always accessible as &mut references within the body of the stage:
use directed::*;
// Use `state(name: TypeOfState)` to indicate the usage of state
#[stage(state(num_times_run: u32, some_string: String))]
fn StatefulStage() -> String {
// this will automatically put an '&mut SomeState' in scope called 'state'
let result = if *num_times_run == 0 {
format!("I've never been run!")
} else {
format!("I've been run {} times.", num_times_run)
};
*num_times_run += 1;
result
}
See the Node State section for details on how to initlialize the state.
It is possible to access or mutate state outside of graph evaluation. See the Registry section for more details.
A Registry stores nodes and their state. It's distinctly seperate from Graph itself which just stores information on how nodes are connected. This come swith a few benefits:
Graphs can be created for a single Registry. Node state (including cached inputs and outputs) can be reused to evaluate a single graph or among distinct graphs.&mut Registry is passed in. Graphs don't take exclusive ownership of the registry, and are thus stateless.Here's an example of creating a registry and adding nodes to it:
use directed::*;
#[stage]
fn SimpleStage() -> String {
String::from("Hello graph!")
}
fn main() {
let mut registry = Registry::new();
// This returns a NodeId, which can be used to lookup the node in the registry.
let node_1 = registry.register(SimpleStage);
// We can also create simple nodes for when an input is relatively constant.
let simple_node = registry.value::<u32>(100);
}
As mentioned in the Stage section, nodes can have internal state. When creating a node, the following method is provided:
let mut registry = Registry::new();
let node_1 = registry.register_with_state(StatefulStage, state!(StatefulStage { num_times_run: 0 }));
// `registry.register(StageName)` can be used when state is not needed.
The state! macro uses struct-construction syntax to initialize state fields.
State can also be accessed via one of these methods:
let mut registry = Registry::new();
let node_1 = registry.register(StatefulStage);
// Get a reference to internal state
println!("Node 1 state: {:?}", registry.state(node_1));
// Get a mutable reference to internal state
registry.state_mut(node_1).num_times_run = 10;
Putting it all together, the Graph struct stores node IDs and the connections between the outputs of nodes to the inputs of other nodes. Creating one is easy, and the graph macro exists to make the connections more visually intuitive. See the example below of putting a variety of concepts together and finally making a graph:
use directed::*;
#[stage(cache_last)]
fn TinyStage1() -> String {
println!("Running stage 1");
String::from("This is the output!")
}
#[stage]
fn TinyStage2(input: String, input2: String) -> String {
println!("Running stage 2");
input.to_uppercase() + " [" + &input2.chars().count().to_string() + " chars in 2nd string]"
}
#[stage]
fn TinyStage3(input: String) {
println!("Running stage 3");
assert_eq!("THIS IS THE OUTPUT! [19 chars in 2nd string]", input);
}
fn main() {
let mut registry = Registry::new();
let node_1 = registry.register(TinyStage1);
let node_2 = registry.register(TinyStage2);
let node_3 = registry.register(TinyStage3);
// This macro is basic syntax sugar for a few calls.
let graph = graph! {
// Nodes that will be a part of the graph must be defined.
nodes: (node_1, node_2, node_3),
connections: {
// The below uses unnamed outputs only. Named outputs can be
// indicated the same way as named inputs, `node_name: output_name`
node_1 => {node_2: input, node_2: input2}
node_2 => {node_3: input}
// It is also possible to make connections between nodes without
// any data being passed between them by leaving out the names
// of both the input and output parameters:
// `node_name => dependant_node_name`
}
}
.unwrap();
// This will do the following:
// - Start evaluation of the specified node (node_3).
// - Recursively evaluate it's parents (so node_3 will request node_2, which will request node_1 twice)
// - node_1 will evaluate, printing "Running stage 1", and pass a clone of its output to "input" on node_2.
// - node_1 will not evaluate again, and just pass a clone of its output to "input2" on node_2.
// - node_2 will evaluate, printing its output then moving (no`t cloning) its output to node_3.
// - node_3 will evaluate, printing its output that passing the assert successfully.
graph.execute(&mut registry, node_3).unwrap();
}
As stated before, multiple graphs can be created from that same registry, executed in any order.
The tokio feature adds async evaluation. This simply means that a node will evaluate all of its parents nodes concurrently before evaluating itself. Enabling this kind of execution is simple:
tokio featureArc: let graph = Arc::new(graph);execute, call execute_async on the Arc<Graph>.Stages marked async will behave as expected - executing within the async context.
TODO: Add pallatable example. For now, Take a look at this test for an example.