pub trait Node<T: Default> {
type Consumer: Copy + Hash;
type Producer: Copy + Hash;
// Provided methods
fn tick(&mut self) { ... }
fn read(&self, producer: Self::Producer) -> T { ... }
fn write(&mut self, consumer: Self::Consumer, input: T) { ... }
}
Expand description
This trait must be implemented by the user per each node that is to be registered in the signal graph.
§Example
The following code presents a Sum
node which offers two consumers
SumConsumer::In1
and SumConsumer::In2
and one producer SumProducer
.
When ticked, this node sums inputs on both consumers and writes the result
to the producer.
#[derive(Default)]
pub struct Sum {
input1: i32,
input2: i32,
output: i32,
}
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum SumConsumer {
In1,
In2,
}
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct SumProducer;
impl Node<i32> for Sum {
type Consumer = SumConsumer;
type Producer = SumProducer;
fn tick(&mut self) {
self.output = self.input1 + self.input2;
}
fn read(&self, _producer: Self::Producer) -> i32 {
self.output
}
fn write(&mut self, consumer: Self::Consumer, input: i32) {
match consumer {
Self::Consumer::In1 => self.input1 = input,
Self::Consumer::In2 => self.input2 = input,
}
}
}
Required Associated Types§
Sourcetype Consumer: Copy + Hash
type Consumer: Copy + Hash
User-defined type allowing selection of a specific consumer (input pin) of the given node.
§Example
In case multiple consumers are offered by the node:
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
enum ExampleConsumer {
Input1,
Input2,
}
In case only a single consumer is availble:
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
struct ExampleConsumer;
In case there are no consumers avaialble:
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
enum ExampleConsumer {}
Sourcetype Producer: Copy + Hash
type Producer: Copy + Hash
User-defined type allowing selection of a specific producer (output pin) of the given node.
§Example
In case multiple producers are offered by the node:
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
enum ExampleProducer {
Input1,
Input2,
}
In case only a single producer is availble:
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
struct ExampleProducer;
In case there are no producers avaialble:
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
enum ExampleProducer {}
Provided Methods§
Sourcefn tick(&mut self)
fn tick(&mut self)
Tick signalizes that all the input data were provided and the node can perform its operation on them.
Default implementation does nothing, allowing users to ommit it from their implementation.