1
0
Эх сурвалжийг харах

remove some more unnessisary usages of unsafe

Evan Almloff 2 жил өмнө
parent
commit
359b4e514b

+ 2 - 1
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 => {

+ 6 - 6
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<Item = <Self::DepState as ElementBorrowable>::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<<Self::DepState as ElementBorrowable>::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: <Self::DepState as ElementBorrowable>::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<Item = ()>, _: &Self::Ctx) -> bool
+    fn reduce<'a>(&mut self, _: NodeView<'a>, _: impl Iterator<Item = ()>, _: &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
     }
 }

+ 0 - 136
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<T>> {
-    tree: Arc<UnsafeCell<&'a mut Tr>>,
-    node_locks: Arc<RwLock<Vec<RawMutex>>>,
-    node_type: PhantomData<T>,
-}
-
-impl<'a, T, Tr: TreeView<T>> 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<bool> {
-        let locks = self.node_locks.read();
-        locks.get(id.0).map(|lock| lock.is_locked())
-    }
-}
-
-unsafe impl<'a, T, Tr: TreeView<T> + Send> Send for SharedView<'a, T, Tr> {}
-unsafe impl<'a, T, Tr: TreeView<T> + Sync> Sync for SharedView<'a, T, Tr> {}
-impl<'a, T, Tr: TreeView<T>> 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<T>> 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<R>(&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<T>> TreeView<T> 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::Iterator<'_>> {
-        self.with_node(id, |t| t.children(id))
-    }
-
-    fn children_mut(&mut self, id: NodeId) -> Option<Self::IteratorMut<'_>> {
-        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<NodeId> {
-        self.with_node(id, |t| t.parent_id(id))
-    }
-
-    fn height(&self, id: NodeId) -> Option<u16> {
-        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);