Use a struct for NodeGraph

This commit is contained in:
Shadowfacts 2024-10-31 11:03:58 -04:00
parent 7c554f731a
commit d8f2a393ba

View File

@ -10,12 +10,26 @@ use std::cell::{Cell, RefCell};
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::future::Future; use std::future::Future;
use std::ops::Deref; use std::ops::{Deref, DerefMut};
use std::pin::Pin; use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
// TODO: consider using a struct for this, because generic bounds of type aliases aren't enforced // use a struct for this, not a type alias, because generic bounds of type aliases aren't enforced
type NodeGraph<S: Synchronicity> = StableDiGraph<ErasedNode<S>, (), u32>; struct NodeGraph<S: Synchronicity>(StableDiGraph<ErasedNode<S>, (), u32>);
impl<S: Synchronicity> Deref for NodeGraph<S> {
type Target = StableDiGraph<ErasedNode<S>, (), u32>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<S: Synchronicity> DerefMut for NodeGraph<S> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub trait Synchronicity: 'static { pub trait Synchronicity: 'static {
type UpdateFn; type UpdateFn;
@ -75,7 +89,7 @@ pub struct Graph<Output, Synch: Synchronicity> {
impl<Output: Clone + 'static> Graph<Output, Synchronous> { impl<Output: Clone + 'static> Graph<Output, Synchronous> {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
node_graph: Rc::new(RefCell::new(StableDiGraph::new())), node_graph: Rc::new(RefCell::new(NodeGraph(StableDiGraph::new()))),
output: None, output: None,
output_type: std::marker::PhantomData, output_type: std::marker::PhantomData,
} }
@ -85,7 +99,7 @@ impl<Output: Clone + 'static> Graph<Output, Synchronous> {
impl<Output: Clone + 'static> Graph<Output, Asynchronous> { impl<Output: Clone + 'static> Graph<Output, Asynchronous> {
pub fn new_async() -> Self { pub fn new_async() -> Self {
Self { Self {
node_graph: Rc::new(RefCell::new(StableDiGraph::new())), node_graph: Rc::new(RefCell::new(NodeGraph(StableDiGraph::new()))),
output: None, output: None,
output_type: std::marker::PhantomData, output_type: std::marker::PhantomData,
} }
@ -157,7 +171,7 @@ impl<O: Clone + 'static, S: Synchronicity> Graph<O, S> {
util::remove_nodes_not_connected_to(&mut *graph, output); util::remove_nodes_not_connected_to(&mut *graph, output);
drop(graph); drop(graph);
let sorted = petgraph::algo::toposort(&*self.node_graph.borrow(), None); let sorted = petgraph::algo::toposort(&**self.node_graph.borrow(), None);
if let Err(_cycle) = sorted { if let Err(_cycle) = sorted {
self.node_graph.borrow_mut().clear_edges(); self.node_graph.borrow_mut().clear_edges();
// TODO: actually build a vec describing the cycle path for debugging // TODO: actually build a vec describing the cycle path for debugging