From b7d0271f4e8a5bc00e816b185ba7fd9475711c24 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 29 Oct 2024 10:57:07 -0400 Subject: [PATCH] Consolidate external inputs with rules --- src/graph/mod.rs | 70 ++++++++---------------------------------------- 1 file changed, 11 insertions(+), 59 deletions(-) diff --git a/src/graph/mod.rs b/src/graph/mod.rs index ccde36d..2b81879 100644 --- a/src/graph/mod.rs +++ b/src/graph/mod.rs @@ -48,20 +48,18 @@ impl Graph { return self.add_node(RuleNode::new(rule)); } - pub fn add_input(&mut self, mut f: F) -> Input + pub fn add_invalidatable_rule(&mut self, mut f: F) -> Input where - I: ExternalInput + 'static, - I::Output: Clone, - F: FnMut(InvalidationSignal) -> I, + R: Rule + 'static, + V: Clone + 'static, + F: FnMut(InvalidationSignal) -> R, { let node_idx = Rc::new(Cell::new(None)); let signal = InvalidationSignal { node_idx: Rc::clone(&node_idx), graph: Rc::clone(&self.node_graph), }; - let input = f(signal); - let node = ExternalInputNode::::new(input); - let input = self.add_node(node); + let input = self.add_rule(f(signal)); node_idx.set(Some(input.node_idx)); input } @@ -306,6 +304,7 @@ impl + 'static, V: Clone + 'static> Node for RuleNode { } fn update(&mut self) { + self.valid = true; let new_value = self.rule.evaluate(); *self.value.borrow_mut() = Some(new_value); } @@ -323,47 +322,6 @@ impl + 'static, V: Clone + 'static> Node for RuleNode { } } -struct ExternalInputNode { - input: I, - value: Rc>>, - valid: bool, -} - -impl ExternalInputNode { - fn new(input: I) -> Self { - Self { - input, - value: Rc::new(RefCell::new(None)), - valid: false, - } - } -} - -impl Node for ExternalInputNode { - fn is_valid(&self) -> bool { - self.valid - } - - fn invalidate(&mut self) { - self.valid = false; - } - - fn visit_inputs(&mut self, _visitor: &mut dyn FnMut(NodeIndex) -> ()) {} - - fn update(&mut self) { - self.valid = true; - *self.value.borrow_mut() = Some(self.input.value()); - } - - fn value_rc(&self) -> Rc>> { - Rc::clone(&self.value) - } - - fn value(&self) -> I::Output { - todo!() - } -} - pub trait Rule { fn visit_inputs(&mut self, visitor: &mut impl InputVisitor); @@ -374,12 +332,6 @@ pub trait InputVisitor { fn visit(&mut self, input: &mut Input); } -pub trait ExternalInput { - type Output; - - fn value(&mut self) -> Self::Output; -} - #[cfg(test)] mod tests { use super::*; @@ -434,18 +386,18 @@ mod tests { } #[test] - fn external_input() { + fn invalidatable_rule() { struct Inc(i32); - impl ExternalInput for Inc { - type Output = i32; - fn value(&mut self) -> i32 { + impl Rule for Inc { + fn visit_inputs(&mut self, _visitor: &mut impl InputVisitor) {} + fn evaluate(&mut self) -> i32 { self.0 += 1; return self.0; } } let mut graph = Graph::new(); let mut invalidate = None; - let input = graph.add_input(|inv| { + let input = graph.add_invalidatable_rule(|inv| { invalidate = Some(inv); Inc(0) });