Move rules to rule.rs

This commit is contained in:
Shadowfacts 2024-11-02 10:56:32 -04:00
parent c18c1ced59
commit 05348a5dbc
3 changed files with 29 additions and 24 deletions

View File

@ -1,8 +1,9 @@
use crate::node::{AsyncRuleNode, ConstNode, Node, NodeValue, RuleNode}; use crate::node::{AsyncRuleNode, ConstNode, Node, NodeValue, RuleNode};
use crate::rule::{AsyncRule, Rule};
use crate::util; use crate::util;
use crate::{ use crate::{
AsyncRule, Asynchronous, ErasedNode, Graph, Input, InvalidationSignal, NodeGraph, NodeId, Rule, Asynchronous, ErasedNode, Graph, Input, InvalidationSignal, NodeGraph, NodeId, Synchronicity,
Synchronicity, Synchronous, Synchronous,
}; };
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::rc::Rc; use std::rc::Rc;

View File

@ -1,12 +1,14 @@
mod builder; mod builder;
mod node; mod node;
mod rule;
mod synchronicity; mod synchronicity;
mod util; mod util;
use builder::{BuildGraphError, GraphBuilder}; use builder::{BuildGraphError, GraphBuilder};
use node::{ErasedNode, NodeValue}; use node::ErasedNode;
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 std::cell::{Cell, Ref, RefCell}; use std::cell::{Cell, Ref, RefCell};
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::VecDeque; use std::collections::VecDeque;
@ -239,32 +241,12 @@ impl<S: Synchronicity> InvalidationSignal<S> {
// TODO: i really want Input to be able to implement Deref somehow // TODO: i really want Input to be able to implement Deref somehow
pub trait Rule: 'static {
type Output: NodeValue;
fn visit_inputs(&self, visitor: &mut impl InputVisitor);
fn evaluate(&mut self) -> Self::Output;
}
pub trait AsyncRule: 'static {
type Output: NodeValue;
fn visit_inputs(&self, visitor: &mut impl InputVisitor);
async fn evaluate(&mut self) -> Self::Output;
}
pub trait InputVisitor {
fn visit<T>(&mut self, input: &Input<T>);
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
struct ConstantRule<T>(T); struct ConstantRule<T>(T);
impl<T: Clone + NodeValue> Rule for ConstantRule<T> { impl<T: Clone + crate::node::NodeValue> Rule for ConstantRule<T> {
type Output = T; type Output = T;
fn visit_inputs(&self, _visitor: &mut impl InputVisitor) {} fn visit_inputs(&self, _visitor: &mut impl InputVisitor) {}
fn evaluate(&mut self) -> Self::Output { fn evaluate(&mut self) -> Self::Output {

22
crates/graph/src/rule.rs Normal file
View File

@ -0,0 +1,22 @@
use crate::node::NodeValue;
use crate::Input;
pub trait Rule: 'static {
type Output: NodeValue;
fn visit_inputs(&self, visitor: &mut impl InputVisitor);
fn evaluate(&mut self) -> Self::Output;
}
pub trait AsyncRule: 'static {
type Output: NodeValue;
fn visit_inputs(&self, visitor: &mut impl InputVisitor);
async fn evaluate(&mut self) -> Self::Output;
}
pub trait InputVisitor {
fn visit<T>(&mut self, input: &Input<T>);
}