From d8f2a393baad2c2319ca1f8db82ad1bc79bbe9dc Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 31 Oct 2024 11:03:58 -0400 Subject: [PATCH] Use a struct for NodeGraph --- crates/graph/src/lib.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/crates/graph/src/lib.rs b/crates/graph/src/lib.rs index a7b36c5..7dfa89e 100644 --- a/crates/graph/src/lib.rs +++ b/crates/graph/src/lib.rs @@ -10,12 +10,26 @@ use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::collections::VecDeque; use std::future::Future; -use std::ops::Deref; +use std::ops::{Deref, DerefMut}; use std::pin::Pin; use std::rc::Rc; -// TODO: consider using a struct for this, because generic bounds of type aliases aren't enforced -type NodeGraph = StableDiGraph, (), u32>; +// use a struct for this, not a type alias, because generic bounds of type aliases aren't enforced +struct NodeGraph(StableDiGraph, (), u32>); + +impl Deref for NodeGraph { + type Target = StableDiGraph, (), u32>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for NodeGraph { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} pub trait Synchronicity: 'static { type UpdateFn; @@ -75,7 +89,7 @@ pub struct Graph { impl Graph { pub fn new() -> Self { Self { - node_graph: Rc::new(RefCell::new(StableDiGraph::new())), + node_graph: Rc::new(RefCell::new(NodeGraph(StableDiGraph::new()))), output: None, output_type: std::marker::PhantomData, } @@ -85,7 +99,7 @@ impl Graph { impl Graph { pub fn new_async() -> Self { Self { - node_graph: Rc::new(RefCell::new(StableDiGraph::new())), + node_graph: Rc::new(RefCell::new(NodeGraph(StableDiGraph::new()))), output: None, output_type: std::marker::PhantomData, } @@ -157,7 +171,7 @@ impl Graph { util::remove_nodes_not_connected_to(&mut *graph, output); 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 { self.node_graph.borrow_mut().clear_edges(); // TODO: actually build a vec describing the cycle path for debugging