Move input to rule.rs

This commit is contained in:
Shadowfacts 2024-11-02 11:31:55 -04:00
parent b73d205456
commit c1c594d4f7
2 changed files with 33 additions and 28 deletions

View File

@ -8,8 +8,8 @@ use builder::{BuildGraphError, GraphBuilder};
use node::{ErasedNode, NodeValue}; use node::{ErasedNode, NodeValue};
use petgraph::visit::{IntoEdgeReferences, NodeIndexable}; use petgraph::visit::{IntoEdgeReferences, NodeIndexable};
use petgraph::{stable_graph::StableDiGraph, visit::EdgeRef}; use petgraph::{stable_graph::StableDiGraph, visit::EdgeRef};
use rule::{AsyncRule, InputVisitor, Rule}; use rule::{AsyncRule, Input, InputVisitor, Rule};
use std::cell::{Cell, Ref, RefCell}; use std::cell::{Cell, RefCell};
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
@ -198,31 +198,6 @@ impl<O: 'static> Graph<O, Asynchronous> {
} }
} }
#[derive(Debug)]
pub struct Input<T> {
node_idx: NodeId,
value: Rc<RefCell<Option<T>>>,
}
impl<T> Input<T> {
pub fn value(&self) -> impl Deref<Target = T> + '_ {
Ref::map(self.value.borrow(), |opt| {
opt.as_ref()
.expect("node must be evaluated before reading value")
})
}
}
// can't derive this impl because it incorectly adds the bound T: Clone
impl<T> Clone for Input<T> {
fn clone(&self) -> Self {
Self {
node_idx: self.node_idx,
value: Rc::clone(&self.value),
}
}
}
// TODO: there's a lot happening here, make sure this doesn't create a reference cycle // TODO: there's a lot happening here, make sure this doesn't create a reference cycle
pub struct InvalidationSignal<Synch: Synchronicity> { pub struct InvalidationSignal<Synch: Synchronicity> {
node_idx: Rc<Cell<Option<NodeId>>>, node_idx: Rc<Cell<Option<NodeId>>>,

View File

@ -1,5 +1,8 @@
use crate::node::NodeValue; use crate::node::NodeValue;
use crate::Input; use crate::NodeId;
use std::cell::{Ref, RefCell};
use std::ops::Deref;
use std::rc::Rc;
pub trait Rule: 'static { pub trait Rule: 'static {
type Output: NodeValue; type Output: NodeValue;
@ -17,6 +20,33 @@ pub trait AsyncRule: 'static {
async fn evaluate(&mut self) -> Self::Output; async fn evaluate(&mut self) -> Self::Output;
} }
#[derive(Debug)]
pub struct Input<T> {
pub(crate) node_idx: NodeId,
pub(crate) value: Rc<RefCell<Option<T>>>,
}
impl<T> Input<T> {
pub fn value(&self) -> impl Deref<Target = T> + '_ {
Ref::map(self.value.borrow(), |opt| {
opt.as_ref()
.expect("node must be evaluated before reading value")
})
}
}
// can't derive this impl because it incorectly adds the bound T: Clone
impl<T> Clone for Input<T> {
fn clone(&self) -> Self {
Self {
node_idx: self.node_idx,
value: Rc::clone(&self.value),
}
}
}
// TODO: i really want Input to be able to implement Deref somehow
pub trait InputVisitor { pub trait InputVisitor {
fn visit<T>(&mut self, input: &Input<T>); fn visit<T>(&mut self, input: &Input<T>);
} }