From 42c98eacc913ce6eda40fe745ebd206f33c7c1b4 Mon Sep 17 00:00:00 2001 From: Nova Date: Tue, 17 May 2022 02:50:30 -0400 Subject: [PATCH] refactor(scenegraph): remove refcell inside scenegraph --- src/core/scenegraph.rs | 13 +++++-------- src/nodes/core.rs | 4 ++-- src/nodes/spatial.rs | 2 +- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/core/scenegraph.rs b/src/core/scenegraph.rs index 1bcc804..214c9f8 100644 --- a/src/core/scenegraph.rs +++ b/src/core/scenegraph.rs @@ -6,7 +6,7 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc, rc::Weak}; #[derive(Default)] pub struct Scenegraph<'a> { - nodes: RefCell>>>>, + nodes: HashMap>>>, } impl<'a> Scenegraph<'a> { @@ -14,18 +14,17 @@ impl<'a> Scenegraph<'a> { Default::default() } - pub fn add_node(&self, node: Rc>>) { + pub fn add_node(&mut self, node: Rc>>) { let path = node.borrow().get_path().to_string(); - self.nodes.borrow_mut().insert(path, node); + self.nodes.insert(path, node); } - pub fn remove_node(&self, path: &str) { - self.nodes.borrow_mut().remove(path); + pub fn remove_node(&mut self, path: &str) { + self.nodes.remove(path); } pub fn get_node(&self, path: &str) -> Weak>> { self.nodes - .borrow() .get(path) .map_or(Weak::default(), |node| Rc::downgrade(node)) } @@ -34,7 +33,6 @@ impl<'a> Scenegraph<'a> { impl<'a> scenegraph::Scenegraph for Scenegraph<'a> { fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> { self.nodes - .borrow() .get(path) .ok_or(ScenegraphError::NodeNotFound)? .borrow() @@ -48,7 +46,6 @@ impl<'a> scenegraph::Scenegraph for Scenegraph<'a> { data: &[u8], ) -> Result, ScenegraphError> { self.nodes - .borrow() .get(path) .ok_or(ScenegraphError::NodeNotFound)? .borrow() diff --git a/src/nodes/core.rs b/src/nodes/core.rs index bced367..87c1c16 100644 --- a/src/nodes/core.rs +++ b/src/nodes/core.rs @@ -31,11 +31,11 @@ impl<'a> Node<'a> { self.path.as_str() } - pub fn from_path(client: Option<&Client<'a>>, path: &str) -> Result>> { + pub fn from_path(client: Option<&mut Client<'a>>, path: &str) -> Result>> { ensure!(path.starts_with('/'), "Invalid path {}", path); let mut weak_messenger = Weak::default(); if client.is_some() { - weak_messenger = client.unwrap().get_weak_messenger(); + weak_messenger = client.as_ref().unwrap().get_weak_messenger(); } let node = Node { path: path.to_string(), diff --git a/src/nodes/spatial.rs b/src/nodes/spatial.rs index 0fc4313..6d0e5f7 100644 --- a/src/nodes/spatial.rs +++ b/src/nodes/spatial.rs @@ -15,7 +15,7 @@ impl<'a> Spatial { } pub fn new_node( - client: Option<&'a Client<'a>>, + client: Option<&'a mut Client<'a>>, path: &str, transform: Mat4, ) -> Result>>> {