Skip to content

Commit

Permalink
Add support to allow for optionally typed inputs and outputs
Browse files Browse the repository at this point in the history
This allows for optionally specifying the full types of the inputs and
outputs of a `Node` during implementation by allowing to specify a full,
freestanding function, rather than only an expression.

The function's arguments and return type will be parsed to produce the
number of inputs and outputs for the node, where the number of arguments
is the number of inputs, and the number of tuple arguments in the output
is the number of outputs (1 if no tuple output type).

Some of this may have to be re-written when addressing a few follow-up
issues including nannou-org#29, nannou-org#19, nannou-org#21 and nannou-org#22, but I think it's helpful to
break up progress into achievable steps!

Closes nannou-org#27 and makes nannou-org#20 much more feasible.
  • Loading branch information
mitchmindtree committed Jun 13, 2019
1 parent 7d1350c commit acb446b
Show file tree
Hide file tree
Showing 6 changed files with 444 additions and 175 deletions.
59 changes: 23 additions & 36 deletions examples/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ struct Add;
struct Debug;

impl gantz::Node for One {
fn n_inputs(&self) -> u32 {
0
}

fn n_outputs(&self) -> u32 {
1
}

fn expr(&self, args: Vec<syn::Expr>) -> syn::Expr {
assert!(args.is_empty());
syn::parse_quote! { 1 }
fn evaluator(&self) -> gantz::node::Evaluator {
let n_inputs = 0;
let n_outputs = 1;
let gen_expr = Box::new(|_| syn::parse_quote! { 1 });
gantz::node::Evaluator::Expr { n_inputs, n_outputs, gen_expr }
}

fn push_eval(&self) -> Option<gantz::node::PushEval> {
Expand All @@ -31,35 +25,28 @@ impl gantz::Node for One {
}

impl gantz::Node for Add {
fn n_inputs(&self) -> u32 {
2
}

fn n_outputs(&self) -> u32 {
1
}

fn expr(&self, args: Vec<syn::Expr>) -> syn::Expr {
assert_eq!(args.len(), 2);
let l = &args[0];
let r = &args[1];
syn::parse_quote! { #l + #r }
fn evaluator(&self) -> gantz::node::Evaluator {
let n_inputs = 2;
let n_outputs = 1;
let gen_expr = Box::new(move |args: Vec<syn::Expr>| {
let l = &args[0];
let r = &args[1];
syn::parse_quote! { #l + #r }
});
gantz::node::Evaluator::Expr { n_inputs, n_outputs, gen_expr }
}
}

impl gantz::Node for Debug {
fn n_inputs(&self) -> u32 {
1
}

fn n_outputs(&self) -> u32 {
0
}

fn expr(&self, args: Vec<syn::Expr>) -> syn::Expr {
assert_eq!(args.len(), 1);
let input = &args[0];
syn::parse_quote! { println!("{:?}", #input) }
fn evaluator(&self) -> gantz::node::Evaluator {
let n_inputs = 1;
let n_outputs = 0;
let gen_expr = Box::new(move |args: Vec<syn::Expr>| {
assert_eq!(args.len(), 1);
let input = &args[0];
syn::parse_quote! { println!("{:?}", #input) }
});
gantz::node::Evaluator::Expr { n_inputs, n_outputs, gen_expr }
}
}

Expand Down
Loading

0 comments on commit acb446b

Please sign in to comment.