From 359b4e514b477b7a800a67fbfe64eab1097bd262 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sun, 1 Jan 2023 18:44:54 -0600 Subject: [PATCH] remove some more unnessisary usages of unsafe --- packages/native-core-macro/src/lib.rs | 3 +- packages/native-core/src/state.rs | 12 +-- packages/native-core/src/tree.rs | 136 -------------------------- 3 files changed, 8 insertions(+), 143 deletions(-) diff --git a/packages/native-core-macro/src/lib.rs b/packages/native-core-macro/src/lib.rs index 5bf50afcc..75b814347 100644 --- a/packages/native-core-macro/src/lib.rs +++ b/packages/native-core-macro/src/lib.rs @@ -393,7 +393,8 @@ impl<'a> StateMember<'a> { let ty = &self.mem.ty; let unit_type = &self.mem.unit_type; - let node_view = quote!(dioxus_native_core::node_ref::NodeView::new(unsafe{&*{&node.node_data as *const _}}, #ty::NODE_MASK)); + let node_view = + quote!(dioxus_native_core::node_ref::NodeView::new(&node.node_data, #ty::NODE_MASK)); let dep_idents = self.dep_mems.iter().map(|m| &m.ident); let impl_specific = match self.dep_kind { DependencyKind::Node => { diff --git a/packages/native-core/src/state.rs b/packages/native-core/src/state.rs index 9bedd19be..acdffa483 100644 --- a/packages/native-core/src/state.rs +++ b/packages/native-core/src/state.rs @@ -87,7 +87,7 @@ pub trait ChildDepState { /// Resolve the state current node's state from the state of the children, the state of the node, and some external context. fn reduce<'a>( &mut self, - node: NodeView, + node: NodeView<'a>, children: impl Iterator::ElementBorrowed<'a>>, ctx: &Self::Ctx, ) -> bool @@ -149,7 +149,7 @@ pub trait ParentDepState { /// Resolve the state current node's state from the state of the parent node, the state of the node, and some external context. fn reduce<'a>( &mut self, - node: NodeView, + node: NodeView<'a>, parent: Option<::ElementBorrowed<'a>>, ctx: &Self::Ctx, ) -> bool; @@ -203,7 +203,7 @@ pub trait NodeDepState { /// Resolve the state current node's state from the state of the sibling states, the state of the node, and some external context. fn reduce<'a>( &mut self, - node: NodeView, + node: NodeView<'a>, node_state: ::ElementBorrowed<'a>, ctx: &Self::Ctx, ) -> bool; @@ -240,7 +240,7 @@ pub trait State: Default + Clone + 'static { impl ChildDepState for () { type Ctx = (); type DepState = (); - fn reduce<'a>(&mut self, _: NodeView, _: impl Iterator, _: &Self::Ctx) -> bool + fn reduce<'a>(&mut self, _: NodeView<'a>, _: impl Iterator, _: &Self::Ctx) -> bool where Self::DepState: 'a, { @@ -251,7 +251,7 @@ impl ChildDepState for () { impl ParentDepState for () { type Ctx = (); type DepState = (); - fn reduce<'a>(&mut self, _: NodeView, _: Option<()>, _: &Self::Ctx) -> bool { + fn reduce<'a>(&mut self, _: NodeView<'a>, _: Option<()>, _: &Self::Ctx) -> bool { false } } @@ -259,7 +259,7 @@ impl ParentDepState for () { impl NodeDepState for () { type DepState = (); type Ctx = (); - fn reduce(&mut self, _: NodeView, _sibling: (), _: &Self::Ctx) -> bool { + fn reduce<'a>(&mut self, _: NodeView<'a>, _sibling: (), _: &Self::Ctx) -> bool { false } } diff --git a/packages/native-core/src/tree.rs b/packages/native-core/src/tree.rs index 796105a1b..b81c6a5c2 100644 --- a/packages/native-core/src/tree.rs +++ b/packages/native-core/src/tree.rs @@ -558,142 +558,6 @@ where } } -/// A view into a tree that can be shared between multiple threads. Nodes are locked invividually. -pub struct SharedView<'a, T, Tr: TreeView> { - tree: Arc>, - node_locks: Arc>>, - node_type: PhantomData, -} - -impl<'a, T, Tr: TreeView> SharedView<'a, T, Tr> { - /// Checks if a node is currently locked. Returns None if the node does not exist. - pub fn check_lock(&self, id: NodeId) -> Option { - let locks = self.node_locks.read(); - locks.get(id.0).map(|lock| lock.is_locked()) - } -} - -unsafe impl<'a, T, Tr: TreeView + Send> Send for SharedView<'a, T, Tr> {} -unsafe impl<'a, T, Tr: TreeView + Sync> Sync for SharedView<'a, T, Tr> {} -impl<'a, T, Tr: TreeView> Clone for SharedView<'a, T, Tr> { - fn clone(&self) -> Self { - Self { - tree: self.tree.clone(), - node_locks: self.node_locks.clone(), - node_type: PhantomData, - } - } -} - -impl<'a, T, Tr: TreeView> SharedView<'a, T, Tr> { - pub fn new(tree: &'a mut Tr) -> Self { - let tree = Arc::new(UnsafeCell::new(tree)); - let mut node_locks = Vec::new(); - for _ in 0..unsafe { (*tree.get()).size() } { - node_locks.push(RawMutex::INIT); - } - Self { - tree, - node_locks: Arc::new(RwLock::new(node_locks)), - node_type: PhantomData, - } - } - - fn lock_node(&self, node: NodeId) { - let read = self.node_locks.read(); - let lock = read.get(node.0); - match lock { - Some(lock) => lock.lock(), - None => { - drop(read); - let mut write = self.node_locks.write(); - write.resize_with(node.0 + 1, || RawMutex::INIT); - unsafe { write.get_unchecked(node.0).lock() } - } - } - } - - fn unlock_node(&self, node: NodeId) { - let read = self.node_locks.read(); - let lock = read.get(node.0); - match lock { - Some(lock) => unsafe { lock.unlock() }, - None => { - panic!("unlocking node that was not locked") - } - } - } - - fn with_node(&self, node_id: NodeId, f: impl FnOnce(&'a mut Tr) -> R) -> R { - self.lock_node(node_id); - let tree = unsafe { &mut *self.tree.get() }; - let r = f(tree); - self.unlock_node(node_id); - r - } -} - -impl<'a, T, Tr: TreeView> TreeView for SharedView<'a, T, Tr> { - type Iterator<'b> = Tr::Iterator<'b> where T: 'b, Self: 'b; - - type IteratorMut<'b>=Tr::IteratorMut<'b> - where - T: 'b, - Self: 'b; - - fn root(&self) -> NodeId { - unsafe { (*self.tree.get()).root() } - } - - fn get(&self, id: NodeId) -> Option<&T> { - self.with_node(id, |t| t.get(id)) - } - - fn get_mut(&mut self, id: NodeId) -> Option<&mut T> { - self.with_node(id, |t| t.get_mut(id)) - } - - fn children(&self, id: NodeId) -> Option> { - self.with_node(id, |t| t.children(id)) - } - - fn children_mut(&mut self, id: NodeId) -> Option> { - self.with_node(id, |t| t.children_mut(id)) - } - - fn children_ids(&self, id: NodeId) -> Option<&[NodeId]> { - self.with_node(id, |t| t.children_ids(id)) - } - - fn parent(&self, id: NodeId) -> Option<&T> { - self.with_node(id, |t| t.get(id)) - } - - fn parent_mut(&mut self, id: NodeId) -> Option<&mut T> { - self.with_node(id, |t| t.parent_mut(id)) - } - - fn parent_id(&self, id: NodeId) -> Option { - self.with_node(id, |t| t.parent_id(id)) - } - - fn height(&self, id: NodeId) -> Option { - unsafe { (*self.tree.get()).height(id) } - } - - fn size(&self) -> usize { - unsafe { (*self.tree.get()).size() } - } - - fn node_parent_mut(&mut self, id: NodeId) -> Option<(&mut T, Option<&mut T>)> { - let parent_id = self.parent_id(id)?; - self.lock_node(parent_id); - let return_value = self.with_node(id, |t| t.node_parent_mut(id)); - self.unlock_node(parent_id); - return_value - } -} - #[test] fn creation() { let mut tree = Tree::new(1);