-
Notifications
You must be signed in to change notification settings - Fork 1
/
variable.rs
60 lines (51 loc) · 1.25 KB
/
variable.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use crate::{InputId, Node, OutputId};
/// Node that holds a variable value.
pub struct Variable {
value: f64,
}
impl Variable {
/// Creates new variable node with initial value.
pub fn new(value: f64) -> Self {
Variable { value }
}
}
impl Node for Variable {
fn delayed_processing(&self) -> bool {
false
}
fn get_output(&self, id: OutputId) -> f64 {
match id.0 {
0 => self.value,
_ => panic!("Output with id {} does not exist.", id.0),
}
}
fn list_inputs(&self) -> &[InputId] {
// 0 -> value.
&[InputId(0)]
}
fn list_outputs(&self) -> &[OutputId] {
// 0 -> value.
&[OutputId(0)]
}
fn process(&mut self) {
// Passthrough noop.
}
fn set_input(&mut self, id: InputId, value: f64) {
match id.0 {
0 => self.value = value,
_ => panic!("Input with id {} does not exist.", id.0),
}
}
}
/// Unit tests.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn holds_value() {
let mut var = Variable::new(42.0);
assert_eq!(var.get_output(OutputId(0)), 42.0);
var.set_input(InputId(0), 2.0);
assert_eq!(var.get_output(OutputId(0)), 2.0);
}
}