simplify api and pass clippy

This commit is contained in:
Evan Almloff 2023-01-21 18:30:42 -06:00
parent adff97036d
commit 0a5499508a
2 changed files with 62 additions and 154 deletions

View file

@ -2,6 +2,7 @@ use dioxus_core::{BorrowedAttributeValue, ElementId, Mutations, TemplateNode};
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use std::any::Any; use std::any::Any;
use std::collections::VecDeque;
use crate::node::{ use crate::node::{
ElementNode, FromAnyValue, NodeData, NodeType, OwnedAttributeDiscription, OwnedAttributeValue, ElementNode, FromAnyValue, NodeData, NodeType, OwnedAttributeDiscription, OwnedAttributeValue,
@ -37,7 +38,7 @@ impl<V: FromAnyValue + Send + Sync> RealDom<V> {
pub fn new(mut passes: Box<[TypeErasedPass<V>]>) -> RealDom<V> { pub fn new(mut passes: Box<[TypeErasedPass<V>]>) -> RealDom<V> {
let mut tree = Tree::new(); let mut tree = Tree::new();
let root_id = tree.root(); let root_id = tree.root();
let mut root = tree.get_node(root_id); let root: &mut NodeData<V> = tree.write(root_id).unwrap();
let mut root_node: NodeData<V> = NodeData::new(NodeType::Element(ElementNode { let mut root_node: NodeData<V> = NodeData::new(NodeType::Element(ElementNode {
tag: "Root".to_string(), tag: "Root".to_string(),
namespace: Some("Root".to_string()), namespace: Some("Root".to_string()),
@ -46,7 +47,7 @@ impl<V: FromAnyValue + Send + Sync> RealDom<V> {
})); }));
root_node.element_id = Some(ElementId(0)); root_node.element_id = Some(ElementId(0));
root_node.node_id = root_id; root_node.node_id = root_id;
root.insert(root_node); *root = root_node;
// resolve dependants for each pass // resolve dependants for each pass
for i in 1..passes.len() { for i in 1..passes.len() {
@ -477,6 +478,61 @@ impl<V: FromAnyValue + Send + Sync> RealDom<V> {
} }
self.tree.insert_after(id, new); self.tree.insert_after(id, new);
} }
pub fn traverse_depth_first(&self, mut f: impl FnMut(NodeRef<V>)) {
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<V>)) {
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<V>)) {
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<V>)) {
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<V: FromAnyValue> Index<ElementId> for RealDom<V> { // impl<V: FromAnyValue> Index<ElementId> for RealDom<V> {

View file

@ -17,50 +17,6 @@ pub(crate) struct Node {
height: u16, height: u16,
} }
pub(crate) struct NodeView<'a> {
tree: &'a mut Tree,
id: NodeId,
}
impl NodeView<'_> {
pub fn insert<T>(&mut self, data: T)
where
T: Any,
{
self.tree.nodes.write_slab::<T>().insert(self.id, data);
}
pub fn get<T>(&self) -> &T
where
T: Any,
{
self.tree.nodes.read_slab::<T>().get(self.id).unwrap()
}
pub fn get_mut<T>(&mut self) -> &mut T
where
T: Any,
{
self.tree.nodes.write_slab::<T>().get_mut(self.id).unwrap()
}
pub fn height(&self) -> u16 {
self.get::<Node>().height
}
pub fn children_ids(&self) -> Vec<NodeId> {
self.get::<Node>().children.clone()
}
pub fn parent_id(&self) -> Option<NodeId> {
self.get::<Node>().parent
}
pub fn id(&self) -> NodeId {
self.id
}
}
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Tree { pub(crate) struct Tree {
nodes: AnySlab, nodes: AnySlab,
@ -198,10 +154,6 @@ impl Tree {
self.set_height(new_id, height); self.set_height(new_id, height);
} }
pub fn insert<T: Any + Send + Sync>(&mut self, id: NodeId, value: T) {
self.nodes.add(id, value);
}
pub fn size(&self) -> usize { pub fn size(&self) -> usize {
self.nodes.len() self.nodes.len()
} }
@ -209,7 +161,6 @@ impl Tree {
pub fn dynamically_borrowed(&mut self) -> DynamicallyBorrowedTree<'_> { pub fn dynamically_borrowed(&mut self) -> DynamicallyBorrowedTree<'_> {
DynamicallyBorrowedTree { DynamicallyBorrowedTree {
nodes: self.nodes.dynamically_borrowed(), nodes: self.nodes.dynamically_borrowed(),
root: self.root,
} }
} }
@ -221,10 +172,6 @@ impl Tree {
self.nodes.write_slab().get_mut(id) 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 { pub fn contains(&self, id: NodeId) -> bool {
self.nodes.contains(id) self.nodes.contains(id)
} }
@ -248,7 +195,6 @@ impl Tree {
pub(crate) struct DynamicallyBorrowedTree<'a> { pub(crate) struct DynamicallyBorrowedTree<'a> {
nodes: DynamiclyBorrowedAnySlabView<'a>, nodes: DynamiclyBorrowedAnySlabView<'a>,
root: NodeId,
} }
impl<'a> DynamicallyBorrowedTree<'a> { impl<'a> DynamicallyBorrowedTree<'a> {
@ -259,7 +205,7 @@ impl<'a> DynamicallyBorrowedTree<'a> {
mut f: impl FnMut(TreeStateView), mut f: impl FnMut(TreeStateView),
) { ) {
let nodes_data = self.node_slab(); let nodes_data = self.node_slab();
let nodes_data: &SlabStorage<Node> = &*nodes_data; let nodes_data: &SlabStorage<Node> = &nodes_data;
let mut nodes = FxHashMap::default(); let mut nodes = FxHashMap::default();
for id in immutable { for id in immutable {
nodes.insert(id, MaybeRead::Read(self.nodes.get_slab(id).unwrap())); 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())); nodes.insert(id, MaybeRead::Write(self.nodes.get_slab_mut(id).unwrap()));
} }
{ {
let view = TreeStateView { let view = TreeStateView { nodes_data, nodes };
root: self.root,
nodes_data,
nodes,
};
f(view) f(view)
} }
} }
fn node_slab<'b>(&'b self) -> MappedRwLockReadGuard<'b, SlabStorage<Node>> { fn node_slab(&self) -> MappedRwLockReadGuard<SlabStorage<Node>> {
RwLockReadGuard::map(self.nodes.get_slab(TypeId::of::<Node>()).unwrap(), |slab| { RwLockReadGuard::map(self.nodes.get_slab(TypeId::of::<Node>()).unwrap(), |slab| {
slab.as_any().downcast_ref().unwrap() slab.as_any().downcast_ref().unwrap()
}) })
@ -304,7 +246,6 @@ impl<'a, 'b> AnyMapLike<'a> for TreeStateViewEntry<'a, 'b> {
pub struct TreeStateView<'a, 'b> { pub struct TreeStateView<'a, 'b> {
nodes_data: &'a SlabStorage<Node>, nodes_data: &'a SlabStorage<Node>,
nodes: FxHashMap<TypeId, MaybeRead<'a, 'b>>, nodes: FxHashMap<TypeId, MaybeRead<'a, 'b>>,
root: NodeId,
} }
impl<'a, 'b> TreeStateView<'a, 'b> { 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<Vec<NodeId>> { pub fn children_ids(&self, id: NodeId) -> Option<Vec<NodeId>> {
self.nodes_data.get(id).map(|node| node.children.clone()) self.nodes_data.get(id).map(|node| node.children.clone())
} }
@ -370,33 +307,6 @@ impl<'a, 'b> TreeStateView<'a, 'b> {
pub fn parent<T: Dependancy>(&self, id: NodeId) -> Option<T::ElementBorrowed<'_>> { pub fn parent<T: Dependancy>(&self, id: NodeId) -> Option<T::ElementBorrowed<'_>> {
T::borrow_elements_from(self.entry(id)) T::borrow_elements_from(self.entry(id))
} }
fn traverse_depth_first<T: Dependancy>(&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::<T>(id) {
f(node);
if let Some(children) = self.children_ids(id) {
stack.extend(children.iter().copied().rev());
}
}
}
}
fn traverse_breadth_first<T: Dependancy>(&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::<T>(id) {
f(node);
if let Some(children) = self.children_ids(id) {
for id in children {
queue.push_back(id);
}
}
}
}
}
} }
#[test] #[test]
@ -583,21 +493,10 @@ impl<T> SlabStorage<T> {
self.data.get(id.0).and_then(|x| x.as_ref()) 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> { fn get_mut(&mut self, id: NodeId) -> Option<&mut T> {
self.data.get_mut(id.0).and_then(|x| x.as_mut()) 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) { fn insert(&mut self, id: NodeId, value: T) {
let idx = id.0; let idx = id.0;
if idx >= self.data.len() { if idx >= self.data.len() {
@ -605,40 +504,6 @@ impl<T> SlabStorage<T> {
} }
self.data[idx] = Some(value); 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<Item = NodeId>,
) -> Option<Vec<&mut T>> {
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<T: 'static + Send + Sync> AnySlabStorageImpl for SlabStorage<T> { impl<T: 'static + Send + Sync> AnySlabStorageImpl for SlabStorage<T> {
@ -732,19 +597,10 @@ impl AnySlab {
self.try_write_slab().unwrap() self.try_write_slab().unwrap()
} }
fn get_slab_mut_borrow<T: Any>(&mut self) -> &mut SlabStorage<T> {
self.data
.get_mut(&TypeId::of::<T>())
.unwrap()
.as_any_mut()
.downcast_mut()
.unwrap()
}
fn get_or_insert_slab_mut<T: Any + Send + Sync>(&mut self) -> &mut SlabStorage<T> { fn get_or_insert_slab_mut<T: Any + Send + Sync>(&mut self) -> &mut SlabStorage<T> {
self.data self.data
.entry(TypeId::of::<T>()) .entry(TypeId::of::<T>())
.or_insert_with(|| Box::new(SlabStorage::<T>::default())) .or_insert_with(|| Box::<SlabStorage<T>>::default())
.as_any_mut() .as_any_mut()
.downcast_mut() .downcast_mut()
.unwrap() .unwrap()
@ -763,10 +619,6 @@ impl AnySlab {
EntryBuilder { id, inner: self } EntryBuilder { id, inner: self }
} }
fn add<T: Any + Send + Sync>(&mut self, id: NodeId, value: T) {
self.get_or_insert_slab_mut().insert(id, value);
}
fn remove(&mut self, id: NodeId) { fn remove(&mut self, id: NodeId) {
for slab in self.data.values_mut() { for slab in self.data.values_mut() {
slab.remove(id); slab.remove(id);