refactor(scenegraph): use dashmap

This commit is contained in:
Nova
2022-06-12 02:25:09 -04:00
parent 8a34286a22
commit 8a3dc83a93
2 changed files with 7 additions and 6 deletions

View File

@@ -9,6 +9,7 @@ version = "0.9.0"
libstardustxr = {path = "../libstardustxr-rs"} libstardustxr = {path = "../libstardustxr-rs"}
anyhow = "1.0.57" anyhow = "1.0.57"
ctrlc = "3.2.2" ctrlc = "3.2.2"
dashmap = "5.3.4"
flexbuffers = "2.0.0" flexbuffers = "2.0.0"
glam = {version = "0.20.5", features = ["mint"]} glam = {version = "0.20.5", features = ["mint"]}
lazy_static = "1.4.0" lazy_static = "1.4.0"

View File

@@ -1,18 +1,17 @@
use crate::core::client::Client; use crate::core::client::Client;
use crate::nodes::core::Node; use crate::nodes::core::Node;
use anyhow::Result; use anyhow::Result;
use dashmap::DashMap;
use libstardustxr::scenegraph; use libstardustxr::scenegraph;
use libstardustxr::scenegraph::ScenegraphError; use libstardustxr::scenegraph::ScenegraphError;
use parking_lot::RwLock;
use rccell::RcCell; use rccell::RcCell;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::{Rc, Weak}; use std::rc::{Rc, Weak};
#[derive(Default)] #[derive(Default)]
pub struct Scenegraph<'a> { pub struct Scenegraph<'a> {
client: RefCell<Weak<Client<'a>>>, client: RefCell<Weak<Client<'a>>>,
pub nodes: RwLock<HashMap<String, RcCell<Node<'a>>>>, nodes: DashMap<String, RcCell<Node<'a>>>,
} }
impl<'a> Scenegraph<'a> { impl<'a> Scenegraph<'a> {
@@ -27,16 +26,17 @@ impl<'a> Scenegraph<'a> {
pub fn add_node(&self, node: Node<'a>) -> RcCell<Node<'a>> { pub fn add_node(&self, node: Node<'a>) -> RcCell<Node<'a>> {
let path = node.get_path().to_string(); let path = node.get_path().to_string();
let node_rc = RcCell::new(node); let node_rc = RcCell::new(node);
self.nodes.write().insert(path, node_rc.clone()); self.nodes.insert(path, node_rc.clone());
node_rc node_rc
} }
pub fn get_node(&self, path: &str) -> Option<RcCell<Node<'a>>> { pub fn get_node(&self, path: &str) -> Option<RcCell<Node<'a>>> {
Some(self.nodes.read().get(path)?.clone()) Some(self.nodes.get(path)?.clone())
} }
pub fn remove_node(&self, path: &str) -> Option<RcCell<Node<'a>>> { pub fn remove_node(&self, path: &str) -> Option<RcCell<Node<'a>>> {
self.nodes.write().remove(path) let (_, node) = self.nodes.remove(path)?;
Some(node)
} }
} }