refactor(scenegraph): remove refcell inside scenegraph

This commit is contained in:
Nova
2022-05-17 02:50:30 -04:00
parent 3335a679aa
commit 42c98eacc9
3 changed files with 8 additions and 11 deletions

View File

@@ -6,7 +6,7 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc, rc::Weak};
#[derive(Default)] #[derive(Default)]
pub struct Scenegraph<'a> { pub struct Scenegraph<'a> {
nodes: RefCell<HashMap<String, Rc<RefCell<Node<'a>>>>>, nodes: HashMap<String, Rc<RefCell<Node<'a>>>>,
} }
impl<'a> Scenegraph<'a> { impl<'a> Scenegraph<'a> {
@@ -14,18 +14,17 @@ impl<'a> Scenegraph<'a> {
Default::default() Default::default()
} }
pub fn add_node(&self, node: Rc<RefCell<Node<'a>>>) { pub fn add_node(&mut self, node: Rc<RefCell<Node<'a>>>) {
let path = node.borrow().get_path().to_string(); 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) { pub fn remove_node(&mut self, path: &str) {
self.nodes.borrow_mut().remove(path); self.nodes.remove(path);
} }
pub fn get_node(&self, path: &str) -> Weak<RefCell<Node<'a>>> { pub fn get_node(&self, path: &str) -> Weak<RefCell<Node<'a>>> {
self.nodes self.nodes
.borrow()
.get(path) .get(path)
.map_or(Weak::default(), |node| Rc::downgrade(node)) .map_or(Weak::default(), |node| Rc::downgrade(node))
} }
@@ -34,7 +33,6 @@ impl<'a> Scenegraph<'a> {
impl<'a> scenegraph::Scenegraph for Scenegraph<'a> { impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> { fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> {
self.nodes self.nodes
.borrow()
.get(path) .get(path)
.ok_or(ScenegraphError::NodeNotFound)? .ok_or(ScenegraphError::NodeNotFound)?
.borrow() .borrow()
@@ -48,7 +46,6 @@ impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
data: &[u8], data: &[u8],
) -> Result<Vec<u8>, ScenegraphError> { ) -> Result<Vec<u8>, ScenegraphError> {
self.nodes self.nodes
.borrow()
.get(path) .get(path)
.ok_or(ScenegraphError::NodeNotFound)? .ok_or(ScenegraphError::NodeNotFound)?
.borrow() .borrow()

View File

@@ -31,11 +31,11 @@ impl<'a> Node<'a> {
self.path.as_str() self.path.as_str()
} }
pub fn from_path(client: Option<&Client<'a>>, path: &str) -> Result<Weak<RefCell<Self>>> { pub fn from_path(client: Option<&mut Client<'a>>, path: &str) -> Result<Weak<RefCell<Self>>> {
ensure!(path.starts_with('/'), "Invalid path {}", path); ensure!(path.starts_with('/'), "Invalid path {}", path);
let mut weak_messenger = Weak::default(); let mut weak_messenger = Weak::default();
if client.is_some() { if client.is_some() {
weak_messenger = client.unwrap().get_weak_messenger(); weak_messenger = client.as_ref().unwrap().get_weak_messenger();
} }
let node = Node { let node = Node {
path: path.to_string(), path: path.to_string(),

View File

@@ -15,7 +15,7 @@ impl<'a> Spatial {
} }
pub fn new_node( pub fn new_node(
client: Option<&'a Client<'a>>, client: Option<&'a mut Client<'a>>,
path: &str, path: &str,
transform: Mat4<f32>, transform: Mat4<f32>,
) -> Result<Weak<RefCell<Node<'a>>>> { ) -> Result<Weak<RefCell<Node<'a>>>> {