Input Nodes
In the context of a computation graph, Input Nodes are nodes that have no dependencies. These are commonly referred to as leaf nodes and their values are sourced from outside the graph.
UpdateInput
To facilitate a Value being used in an input node we need to implement the
UpdateInput
trait.
The simplest implementation can be seen below:
#[derive(Value, Hash)]
pub struct SomeNumber {
pub value: i32,
}
impl UpdateInput for SomeNumber {
type Update = i32;
fn update_mut(&mut self, update: Self::Update) {
// Simply replace the value with the update.
self.value = update;
}
}
It's now possible to create an InputNode
from this data and interact with it by passing in updates:
// `input` is an `Rc`, so can be cloned and passed to dependents.
let input = InputNode::new(SomeNumber { value: 2 });
// The `update` method allows us to change the value of the node. It will
// fail if the node is currently being read/written to.
input.update(6).unwrap();
// The `value` method allows us to read the value of the node.
assert_eq!(input.value().unwrap().value, 6);