refactor(scenegraph): use RcCell<Node> instead of Rc<RefCell<Node>>

This commit is contained in:
Nova
2022-05-22 10:11:55 -04:00
parent 323d1e01fe
commit 73a0c6ff7a
5 changed files with 37 additions and 17 deletions

View File

@@ -10,6 +10,7 @@ libstardustxr = {path = "../libstardustxr-rs"}
anyhow = "1.0.57" anyhow = "1.0.57"
ctrlc = "3.2.2" ctrlc = "3.2.2"
mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]} mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]}
rccell = "0.1.3"
slab = "0.4.6" slab = "0.4.6"
thiserror = "1.0.31" thiserror = "1.0.31"
vek = "0.15.8" vek = "0.15.8"

View File

@@ -5,7 +5,7 @@ use mio::net::UnixStream;
use std::rc::{Rc, Weak}; use std::rc::{Rc, Weak};
pub struct Client<'a> { pub struct Client<'a> {
pub messenger: Rc<Messenger<'a>>, messenger: Rc<Messenger<'a>>,
pub scenegraph: Scenegraph<'a>, pub scenegraph: Scenegraph<'a>,
} }

View File

@@ -4,17 +4,17 @@ use crate::nodes::spatial::Spatial;
use anyhow::Result; use anyhow::Result;
use libstardustxr::scenegraph; use libstardustxr::scenegraph;
use libstardustxr::scenegraph::ScenegraphError; use libstardustxr::scenegraph::ScenegraphError;
use std::{cell::RefCell, collections::HashMap, rc::Rc, rc::Weak}; use rccell::{RcCell, WeakCell};
use std::collections::HashMap;
#[derive(Default)]
pub struct Scenegraph<'a> { pub struct Scenegraph<'a> {
nodes: HashMap<String, Rc<RefCell<Node<'a>>>>, nodes: HashMap<String, RcCell<Node<'a>>>,
root: NodeRef<'a>, root: NodeRef<'a>,
hmd: NodeRef<'a>, hmd: NodeRef<'a>,
} }
impl<'a> Scenegraph<'a> { impl<'a> Scenegraph<'a> {
pub fn add_node(&mut self, node: Rc<RefCell<Node<'a>>>) { pub fn add_node(&mut self, node: RcCell<Node<'a>>) {
let path = node.borrow().get_path().to_string(); let path = node.borrow().get_path().to_string();
self.nodes.insert(path, node); self.nodes.insert(path, node);
} }
@@ -23,8 +23,10 @@ impl<'a> Scenegraph<'a> {
self.nodes.remove(path); self.nodes.remove(path);
} }
pub fn get_node(&self, path: &str) -> Weak<RefCell<Node<'a>>> { pub fn get_node(&self, path: &str) -> WeakCell<Node<'a>> {
self.nodes.get(path).map_or(Weak::default(), Rc::downgrade) self.nodes
.get(path)
.map_or(WeakCell::new(), RcCell::downgrade)
} }
pub fn add_interfaces(&mut self, client: &mut Client<'a>) -> Result<()> { pub fn add_interfaces(&mut self, client: &mut Client<'a>) -> Result<()> {
@@ -34,6 +36,16 @@ impl<'a> Scenegraph<'a> {
} }
} }
impl<'a> Default for Scenegraph<'a> {
fn default() -> Self {
Scenegraph {
nodes: HashMap::new(),
root: WeakCell::new(),
hmd: WeakCell::new(),
}
}
}
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

View File

@@ -1,6 +1,7 @@
use crate::core::client::Client; use crate::core::client::Client;
use anyhow::{anyhow, ensure, Result}; use anyhow::{anyhow, ensure, Result};
use libstardustxr::messenger::Messenger; use libstardustxr::messenger::Messenger;
use rccell::{RcCell, WeakCell};
use std::{ use std::{
cell::RefCell, cell::RefCell,
collections::HashMap, collections::HashMap,
@@ -13,7 +14,12 @@ use super::spatial::Spatial;
pub type Signal<'a> = dyn Fn(&[u8]) + 'a; pub type Signal<'a> = dyn Fn(&[u8]) + 'a;
pub type Method<'a> = dyn Fn(&[u8]) -> Vec<u8> + 'a; pub type Method<'a> = dyn Fn(&[u8]) -> Vec<u8> + 'a;
pub type NodeRef<'a> = Weak<RefCell<Node<'a>>>; pub type NodeRef<'a> = WeakCell<Node<'a>>;
pub enum NodeData<'a> {
None,
Spatial(Spatial<'a>),
}
pub struct Node<'a> { pub struct Node<'a> {
path: String, path: String,
@@ -22,7 +28,7 @@ pub struct Node<'a> {
local_signals: HashMap<String, Box<Signal<'a>>>, local_signals: HashMap<String, Box<Signal<'a>>>,
local_methods: HashMap<String, Box<Method<'a>>>, local_methods: HashMap<String, Box<Method<'a>>>,
pub spatial: Option<Spatial>, pub data: NodeData<'a>,
} }
impl<'a> Node<'a> { impl<'a> Node<'a> {
@@ -48,10 +54,10 @@ impl<'a> Node<'a> {
local_signals: HashMap::new(), local_signals: HashMap::new(),
local_methods: HashMap::new(), local_methods: HashMap::new(),
spatial: None, data: NodeData::None,
}; };
let node_ref = Rc::new(RefCell::new(node)); let node_ref = RcCell::new(node);
let weak_node = Rc::downgrade(&node_ref); let weak_node = node_ref.downgrade();
match client { match client {
Some(client_) => client_.scenegraph.add_node(node_ref), Some(client_) => client_.scenegraph.add_node(node_ref),
None => {} None => {}

View File

@@ -1,15 +1,16 @@
use super::core::{Node, NodeRef}; use super::core::{Node, NodeData, NodeRef};
use crate::core::client::Client; use crate::core::client::Client;
use anyhow::Result; use anyhow::Result;
use vek::mat::repr_c::row_major::Mat4; use vek::mat::repr_c::row_major::Mat4;
pub struct Spatial { pub struct Spatial<'a> {
node: &'a Node<'a>,
transform: Mat4<f32>, transform: Mat4<f32>,
} }
impl<'a> Spatial { impl<'a> Spatial<'a> {
pub fn new(transform: Mat4<f32>) -> Self { pub fn new(node: &'a Node<'a>, transform: Mat4<f32>) -> Self {
Spatial { transform } Spatial { node, transform }
} }
pub fn new_node( pub fn new_node(