handlers
This commit is contained in:
parent
f0dbc0cbe2
commit
6f0c9cf943
1 changed files with 53 additions and 0 deletions
53
src/node/handler.rs
Normal file
53
src/node/handler.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
pub trait NodeHandler {
|
||||||
|
fn run(&self, inputs: Vec<f64>) -> f64;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct NodeFunction {
|
||||||
|
function: fn(Vec<f64>) -> f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeFunction {
|
||||||
|
pub fn new(function: fn(Vec<f64>) -> f64) -> NodeFunction {
|
||||||
|
NodeFunction { function }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeHandler for NodeFunction {
|
||||||
|
fn run(&self, inputs: Vec<f64>) -> f64 {
|
||||||
|
(self.function)(inputs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct NodeNumber {
|
||||||
|
number: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeNumber {
|
||||||
|
pub fn new(number: Option<f64>) -> NodeNumber {
|
||||||
|
NodeNumber {
|
||||||
|
number: number.unwrap_or(0f64),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeHandler for NodeNumber {
|
||||||
|
fn run(&self, inputs: Vec<f64>) -> f64 {
|
||||||
|
self.number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct NodeVariable {
|
||||||
|
getter: fn() -> f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeVariable {
|
||||||
|
pub fn new(getter: fn() -> f64) -> NodeVariable {
|
||||||
|
NodeVariable { getter }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeHandler for NodeVariable {
|
||||||
|
fn run(&self, inputs: Vec<f64>) -> f64 {
|
||||||
|
(self.getter)()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue