Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Allow usage of imported nodes
Browse files Browse the repository at this point in the history
The existing implementation of the graphity! macro does not allow for
import of an external node. That was due to a limitation caused by
rust-lang/rust#20400.

With this patch, the signature of the macro changes. It now accepts
direct reference to the node and its producer and consumer.

With this change, it is possible to use nodes defined in other crates.

Signed-off-by: Petr Horáček <phoracek@redhat.com>
  • Loading branch information
phoracek committed Jan 3, 2021
1 parent aa69ce8 commit 55581a4
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 277 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Main

* **Breaking change:** The signature of the `graphity!` macro has been changed
to allow imports of external nodes. Read documentation of the macro to learn
more.

## 1.0.0

* Full implementation of the core feature-set. Allows users to define their
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ impl Node<i32> for Sum {
...
}

mod g {
use super::{Echo, Generator, Sum};
graphity!(Graph<i32>; Echo, Generator, Sum);
}
graphity!(
Graph<i32>;
Generator = {Generator, GeneratorConsumer, GeneratorProducer},
Sum = {Sum, SumConsumer, SumProducer},
Echo = {Echo, EchoConsumer, EchoProducer},
);

fn main() {
let mut graph = g::Graph::new();
let mut graph = Graph::new();

let one = graph.add_node(Generator(1));
let two = graph.add_node(Generator(2));
Expand Down
96 changes: 12 additions & 84 deletions examples/graph.rs
Original file line number Diff line number Diff line change
@@ -1,95 +1,23 @@
#[macro_use]
extern crate graphity;

use graphity::{Node, NodeIndex};
use graphity::NodeIndex;

mod g {
use super::{Echo, Generator, Sum};
graphity!(Graph<i32>; Echo, Generator, Sum);
}

#[derive(Default)]
pub struct Echo {
input: i32,
}

#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct EchoConsumer;

#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum EchoProducer {}

impl Node<i32> for Echo {
type Consumer = EchoConsumer;
type Producer = EchoProducer;

fn tick(&mut self) {
println!("Echo: {}", self.input);
}

fn write(&mut self, _consumer: Self::Consumer, input: i32) {
self.input = input;
}
}

pub struct Generator(i32);

#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum GeneratorConsumer {}

#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct GeneratorProducer;
// See definitions of nodes used here under nodes/src/lib.rs
use graphity_nodes::*;

impl Node<i32> for Generator {
type Consumer = GeneratorConsumer;
type Producer = GeneratorProducer;

fn read(&self, _producer: Self::Producer) -> i32 {
self.0
}
}

#[derive(Default)]
pub struct Sum {
input1: i32,
input2: i32,
output: i32,
}

#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum SumConsumer {
In1,
In2,
}

#[derive(PartialEq, Eq, Copy, Clone, Hash, 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,
}
}
}
graphity!(
Graph<i32>;
Generator = {Generator, GeneratorConsumer, GeneratorProducer},
Sum = {Sum, SumConsumer, SumProducer},
Echo = {Echo, EchoConsumer, EchoProducer},
);

fn main() {
let mut graph = g::Graph::new();
let mut graph = Graph::new();

let one = graph.add_node(Generator(1));
let two = graph.add_node(Generator(2));
let one = graph.add_node(Generator::new(1));
let two = graph.add_node(Generator::new(2));
let sum = graph.add_node(Sum::default());
let echo = graph.add_node(Echo::default());

Expand Down
Loading

0 comments on commit 55581a4

Please sign in to comment.