From 7c554f731ab16a10e703279f311e48573168a3ed Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 31 Oct 2024 11:00:48 -0400 Subject: [PATCH] Remove Synchronicity::AnyStorage indirection --- crates/graph/src/lib.rs | 77 +++++++++++------------------------------ 1 file changed, 21 insertions(+), 56 deletions(-) diff --git a/crates/graph/src/lib.rs b/crates/graph/src/lib.rs index 42d281c..a7b36c5 100644 --- a/crates/graph/src/lib.rs +++ b/crates/graph/src/lib.rs @@ -6,11 +6,11 @@ mod util; use petgraph::visit::{IntoEdgeReferences, NodeIndexable}; use petgraph::{graph::NodeIndex, stable_graph::StableDiGraph, visit::EdgeRef}; use std::any::Any; -use std::cell::{Cell, Ref, RefCell, RefMut}; +use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::collections::VecDeque; use std::future::Future; -use std::ops::{Deref, DerefMut}; +use std::ops::Deref; use std::pin::Pin; use std::rc::Rc; @@ -18,13 +18,6 @@ use std::rc::Rc; type NodeGraph = StableDiGraph, (), u32>; pub trait Synchronicity: 'static { - type AnyStorage; - fn make_any_storage(value: T) -> Self::AnyStorage; - fn unbox_any_storage(storage: &Self::AnyStorage) -> impl Deref; - fn unbox_any_storage_mut( - storage: &mut Self::AnyStorage, - ) -> impl DerefMut; - type UpdateFn; fn make_update_fn() -> Self::UpdateFn; @@ -35,22 +28,6 @@ pub trait Synchronicity: 'static { pub enum Synchronous {} impl Synchronicity for Synchronous { - type AnyStorage = Box; - - fn make_any_storage(value: T) -> Self::AnyStorage { - Box::new(value) - } - - fn unbox_any_storage(storage: &Self::AnyStorage) -> impl Deref { - storage.downcast_ref().unwrap() - } - - fn unbox_any_storage_mut( - storage: &mut Self::AnyStorage, - ) -> impl DerefMut { - storage.downcast_mut().unwrap() - } - type UpdateFn = Box) -> ()>; fn make_update_fn() -> Self::UpdateFn { @@ -68,23 +45,8 @@ impl Synchronicity for Synchronous { pub enum Asynchronous {} impl Synchronicity for Asynchronous { - type AnyStorage = Rc>>; - - fn make_any_storage(value: T) -> Self::AnyStorage { - Rc::new(RefCell::new(Box::new(value))) - } - - fn unbox_any_storage(storage: &Self::AnyStorage) -> impl Deref { - Ref::map(storage.borrow(), |any| any.downcast_ref().unwrap()) - } - - fn unbox_any_storage_mut( - storage: &mut Self::AnyStorage, - ) -> impl DerefMut { - RefMut::map(storage.borrow_mut(), |any| any.downcast_mut().unwrap()) - } - - type UpdateFn = Box>>) -> Pin>>>; + type UpdateFn = + Box Fn(&'a mut Box) -> Pin + 'a>>>; fn make_update_fn() -> Self::UpdateFn { Box::new(|any| Box::pin(Asynchronous::do_async_update::(any))) @@ -98,9 +60,8 @@ impl Synchronicity for Asynchronous { } impl Asynchronous { - async fn do_async_update(any: Rc>>) { - let mut any_ = any.borrow_mut(); - let x = any_.downcast_mut::>>().unwrap(); + async fn do_async_update(any: &mut Box) { + let x = any.downcast_mut::>>().unwrap(); x.update().await; } } @@ -362,8 +323,9 @@ impl FrozenGraph { Box::pin(self.update_node_async(source)).await; } - let node = &self.node_graph.borrow()[idx]; - (node.update)(Rc::clone(&node.any)).await; + let mut graph = self.node_graph.borrow_mut(); + let node = &mut graph[idx]; + (node.update)(&mut node.any).await; } } @@ -424,10 +386,10 @@ fn invalidate_nodes( // TODO: i really want Input to be able to implement Deref somehow pub struct ErasedNode { - any: Synch::AnyStorage, - is_valid: Box bool>, - invalidate: Box ()>, - visit_inputs: Box) -> ()) -> ()>, + any: Box, + is_valid: Box) -> bool>, + invalidate: Box) -> ()>, + visit_inputs: Box, &mut dyn FnMut(NodeIndex) -> ()) -> ()>, update: Synch::UpdateFn, } @@ -435,18 +397,19 @@ impl ErasedNode { fn new + 'static, V: 'static>(base: N) -> Self { // i don't love the double boxing, but i'm not sure how else to do this let thing: Box> = Box::new(base); + let any: Box = Box::new(thing); Self { - any: S::make_any_storage(thing), + any, is_valid: Box::new(|any| { - let x = S::unbox_any_storage::>>(any); + let x = any.downcast_ref::>>().unwrap(); x.is_valid() }), invalidate: Box::new(|any| { - let mut x = S::unbox_any_storage_mut::>>(any); + let x = any.downcast_mut::>>().unwrap(); x.invalidate(); }), visit_inputs: Box::new(|any, visitor| { - let mut x = S::unbox_any_storage_mut::>>(any); + let x = any.downcast_mut::>>().unwrap(); x.visit_inputs(visitor); }), update: S::make_update_fn::(), @@ -454,7 +417,9 @@ impl ErasedNode { } fn expect_type<'a, V: 'static>(&'a self) -> impl Deref>> + 'a { - S::unbox_any_storage::>>(&self.any) + self.any + .downcast_ref::>>() + .expect("node should match expected type") } fn is_valid(&self) -> bool {