From 0a5499508a43c39271c035c2b578b6b026cbd5f9 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sat, 21 Jan 2023 18:30:42 -0600 Subject: [PATCH] simplify api and pass clippy --- packages/native-core/src/real_dom.rs | 60 ++++++++++- packages/native-core/src/tree.rs | 156 +-------------------------- 2 files changed, 62 insertions(+), 154 deletions(-) diff --git a/packages/native-core/src/real_dom.rs b/packages/native-core/src/real_dom.rs index 7febed2bb..b856c0d6c 100644 --- a/packages/native-core/src/real_dom.rs +++ b/packages/native-core/src/real_dom.rs @@ -2,6 +2,7 @@ use dioxus_core::{BorrowedAttributeValue, ElementId, Mutations, TemplateNode}; use rustc_hash::{FxHashMap, FxHashSet}; use std::any::Any; +use std::collections::VecDeque; use crate::node::{ ElementNode, FromAnyValue, NodeData, NodeType, OwnedAttributeDiscription, OwnedAttributeValue, @@ -37,7 +38,7 @@ impl RealDom { pub fn new(mut passes: Box<[TypeErasedPass]>) -> RealDom { let mut tree = Tree::new(); let root_id = tree.root(); - let mut root = tree.get_node(root_id); + let root: &mut NodeData = tree.write(root_id).unwrap(); let mut root_node: NodeData = NodeData::new(NodeType::Element(ElementNode { tag: "Root".to_string(), namespace: Some("Root".to_string()), @@ -46,7 +47,7 @@ impl RealDom { })); root_node.element_id = Some(ElementId(0)); root_node.node_id = root_id; - root.insert(root_node); + *root = root_node; // resolve dependants for each pass for i in 1..passes.len() { @@ -477,6 +478,61 @@ impl RealDom { } self.tree.insert_after(id, new); } + + pub fn traverse_depth_first(&self, mut f: impl FnMut(NodeRef)) { + let mut stack = vec![self.root()]; + while let Some(id) = stack.pop() { + if let Some(node) = self.get(id) { + f(node); + if let Some(children) = self.tree.children_ids(id) { + stack.extend(children.iter().copied().rev()); + } + } + } + } + + pub fn traverse_breadth_first(&self, mut f: impl FnMut(NodeRef)) { + let mut queue = VecDeque::new(); + queue.push_back(self.root()); + while let Some(id) = queue.pop_front() { + if let Some(node) = self.get(id) { + f(node); + if let Some(children) = self.tree.children_ids(id) { + for id in children { + queue.push_back(id); + } + } + } + } + } + + pub fn traverse_depth_first_mut(&mut self, mut f: impl FnMut(NodeMut)) { + let mut stack = vec![self.root()]; + while let Some(id) = stack.pop() { + if let Some(children) = self.tree.children_ids(id) { + if let Some(node) = self.get_mut(id) { + let node = node; + f(node); + stack.extend(children.iter().copied().rev()); + } + } + } + } + + pub fn traverse_breadth_first_mut(&mut self, mut f: impl FnMut(NodeMut)) { + let mut queue = VecDeque::new(); + queue.push_back(self.root()); + while let Some(id) = queue.pop_front() { + if let Some(children) = self.tree.children_ids(id) { + if let Some(node) = self.get_mut(id) { + f(node); + for id in children { + queue.push_back(id); + } + } + } + } + } } // impl Index for RealDom { diff --git a/packages/native-core/src/tree.rs b/packages/native-core/src/tree.rs index babdaded7..bb550c5c4 100644 --- a/packages/native-core/src/tree.rs +++ b/packages/native-core/src/tree.rs @@ -17,50 +17,6 @@ pub(crate) struct Node { height: u16, } -pub(crate) struct NodeView<'a> { - tree: &'a mut Tree, - id: NodeId, -} - -impl NodeView<'_> { - pub fn insert(&mut self, data: T) - where - T: Any, - { - self.tree.nodes.write_slab::().insert(self.id, data); - } - - pub fn get(&self) -> &T - where - T: Any, - { - self.tree.nodes.read_slab::().get(self.id).unwrap() - } - - pub fn get_mut(&mut self) -> &mut T - where - T: Any, - { - self.tree.nodes.write_slab::().get_mut(self.id).unwrap() - } - - pub fn height(&self) -> u16 { - self.get::().height - } - - pub fn children_ids(&self) -> Vec { - self.get::().children.clone() - } - - pub fn parent_id(&self) -> Option { - self.get::().parent - } - - pub fn id(&self) -> NodeId { - self.id - } -} - #[derive(Debug)] pub(crate) struct Tree { nodes: AnySlab, @@ -198,10 +154,6 @@ impl Tree { self.set_height(new_id, height); } - pub fn insert(&mut self, id: NodeId, value: T) { - self.nodes.add(id, value); - } - pub fn size(&self) -> usize { self.nodes.len() } @@ -209,7 +161,6 @@ impl Tree { pub fn dynamically_borrowed(&mut self) -> DynamicallyBorrowedTree<'_> { DynamicallyBorrowedTree { nodes: self.nodes.dynamically_borrowed(), - root: self.root, } } @@ -221,10 +172,6 @@ impl Tree { self.nodes.write_slab().get_mut(id) } - pub fn get_node(&mut self, id: NodeId) -> NodeView<'_> { - NodeView { tree: self, id } - } - pub fn contains(&self, id: NodeId) -> bool { self.nodes.contains(id) } @@ -248,7 +195,6 @@ impl Tree { pub(crate) struct DynamicallyBorrowedTree<'a> { nodes: DynamiclyBorrowedAnySlabView<'a>, - root: NodeId, } impl<'a> DynamicallyBorrowedTree<'a> { @@ -259,7 +205,7 @@ impl<'a> DynamicallyBorrowedTree<'a> { mut f: impl FnMut(TreeStateView), ) { let nodes_data = self.node_slab(); - let nodes_data: &SlabStorage = &*nodes_data; + let nodes_data: &SlabStorage = &nodes_data; let mut nodes = FxHashMap::default(); for id in immutable { nodes.insert(id, MaybeRead::Read(self.nodes.get_slab(id).unwrap())); @@ -268,16 +214,12 @@ impl<'a> DynamicallyBorrowedTree<'a> { nodes.insert(id, MaybeRead::Write(self.nodes.get_slab_mut(id).unwrap())); } { - let view = TreeStateView { - root: self.root, - nodes_data, - nodes, - }; + let view = TreeStateView { nodes_data, nodes }; f(view) } } - fn node_slab<'b>(&'b self) -> MappedRwLockReadGuard<'b, SlabStorage> { + fn node_slab(&self) -> MappedRwLockReadGuard> { RwLockReadGuard::map(self.nodes.get_slab(TypeId::of::()).unwrap(), |slab| { slab.as_any().downcast_ref().unwrap() }) @@ -304,7 +246,6 @@ impl<'a, 'b> AnyMapLike<'a> for TreeStateViewEntry<'a, 'b> { pub struct TreeStateView<'a, 'b> { nodes_data: &'a SlabStorage, nodes: FxHashMap>, - root: NodeId, } impl<'a, 'b> TreeStateView<'a, 'b> { @@ -326,10 +267,6 @@ impl<'a, 'b> TreeStateView<'a, 'b> { }) } - fn root(&self) -> NodeId { - self.root - } - pub fn children_ids(&self, id: NodeId) -> Option> { self.nodes_data.get(id).map(|node| node.children.clone()) } @@ -370,33 +307,6 @@ impl<'a, 'b> TreeStateView<'a, 'b> { pub fn parent(&self, id: NodeId) -> Option> { T::borrow_elements_from(self.entry(id)) } - - fn traverse_depth_first(&self, mut f: impl FnMut(T::ElementBorrowed<'_>)) { - let mut stack = vec![self.root()]; - while let Some(id) = stack.pop() { - if let Some(node) = self.get::(id) { - f(node); - if let Some(children) = self.children_ids(id) { - stack.extend(children.iter().copied().rev()); - } - } - } - } - - fn traverse_breadth_first(&self, mut f: impl FnMut(T::ElementBorrowed<'_>)) { - let mut queue = VecDeque::new(); - queue.push_back(self.root()); - while let Some(id) = queue.pop_front() { - if let Some(node) = self.get::(id) { - f(node); - if let Some(children) = self.children_ids(id) { - for id in children { - queue.push_back(id); - } - } - } - } - } } #[test] @@ -583,21 +493,10 @@ impl SlabStorage { self.data.get(id.0).and_then(|x| x.as_ref()) } - unsafe fn get_unchecked(&self, id: NodeId) -> &T { - self.data.get_unchecked(id.0).as_ref().unwrap_unchecked() - } - fn get_mut(&mut self, id: NodeId) -> Option<&mut T> { self.data.get_mut(id.0).and_then(|x| x.as_mut()) } - unsafe fn get_unchecked_mut(&mut self, id: NodeId) -> &mut T { - self.data - .get_unchecked_mut(id.0) - .as_mut() - .unwrap_unchecked() - } - fn insert(&mut self, id: NodeId, value: T) { let idx = id.0; if idx >= self.data.len() { @@ -605,40 +504,6 @@ impl SlabStorage { } self.data[idx] = Some(value); } - - fn get2_mut(&mut self, id1: NodeId, id2: NodeId) -> Option<(&mut T, &mut T)> { - assert!(id1 != id2); - let (idx1, idx2) = (id1.0, id2.0); - let ptr = self.data.as_mut_ptr(); - let first = unsafe { &mut *ptr.add(idx1) }; - let second = unsafe { &mut *ptr.add(idx2) }; - if let (Some(first), Some(second)) = (first, second) { - Some((first, second)) - } else { - None - } - } - - unsafe fn get_many_mut_unchecked( - &mut self, - ids: impl Iterator, - ) -> Option> { - let ptr = self.data.as_mut_ptr(); - let mut result = Vec::new(); - for id in ids { - let idx = id.0; - if idx >= self.data.len() { - return None; - } - let item = unsafe { &mut *ptr.add(idx) }; - if let Some(item) = item { - result.push(item); - } else { - return None; - } - } - Some(result) - } } impl AnySlabStorageImpl for SlabStorage { @@ -732,19 +597,10 @@ impl AnySlab { self.try_write_slab().unwrap() } - fn get_slab_mut_borrow(&mut self) -> &mut SlabStorage { - self.data - .get_mut(&TypeId::of::()) - .unwrap() - .as_any_mut() - .downcast_mut() - .unwrap() - } - fn get_or_insert_slab_mut(&mut self) -> &mut SlabStorage { self.data .entry(TypeId::of::()) - .or_insert_with(|| Box::new(SlabStorage::::default())) + .or_insert_with(|| Box::>::default()) .as_any_mut() .downcast_mut() .unwrap() @@ -763,10 +619,6 @@ impl AnySlab { EntryBuilder { id, inner: self } } - fn add(&mut self, id: NodeId, value: T) { - self.get_or_insert_slab_mut().insert(id, value); - } - fn remove(&mut self, id: NodeId) { for slab in self.data.values_mut() { slab.remove(id);