Explorar o código

Merge pull request #1160 from DioxusLabs/jk/remove-bumpslab-for-hooks

Jon Kelley %!s(int64=2) %!d(string=hai) anos
pai
achega
388fb30ed3

+ 1 - 4
packages/core/src/arena.rs

@@ -4,7 +4,6 @@ use crate::{
     innerlude::DirtyScope, nodes::RenderReturn, nodes::VNode, virtual_dom::VirtualDom,
     innerlude::DirtyScope, nodes::RenderReturn, nodes::VNode, virtual_dom::VirtualDom,
     AttributeValue, DynamicNode, ScopeId,
     AttributeValue, DynamicNode, ScopeId,
 };
 };
-use bumpalo::boxed::Box as BumpBox;
 
 
 /// An Element's unique identifier.
 /// An Element's unique identifier.
 ///
 ///
@@ -110,9 +109,7 @@ impl VirtualDom {
 
 
         // Drop all the hooks once the children are dropped
         // Drop all the hooks once the children are dropped
         // this means we'll drop hooks bottom-up
         // this means we'll drop hooks bottom-up
-        for hook in scope.hook_list.get_mut().drain(..) {
-            drop(unsafe { BumpBox::from_raw(hook) });
-        }
+        scope.hooks.get_mut().clear();
 
 
         // Drop all the futures once the hooks are dropped
         // Drop all the futures once the hooks are dropped
         for task_id in scope.spawned_tasks.borrow_mut().drain() {
         for task_id in scope.spawned_tasks.borrow_mut().drain() {

+ 1 - 2
packages/core/src/scope_arena.rs

@@ -38,8 +38,7 @@ impl VirtualDom {
             node_arena_2: BumpFrame::new(0),
             node_arena_2: BumpFrame::new(0),
             spawned_tasks: Default::default(),
             spawned_tasks: Default::default(),
             render_cnt: Default::default(),
             render_cnt: Default::default(),
-            hook_arena: Default::default(),
-            hook_list: Default::default(),
+            hooks: Default::default(),
             hook_idx: Default::default(),
             hook_idx: Default::default(),
             shared_contexts: Default::default(),
             shared_contexts: Default::default(),
             borrowed_props: Default::default(),
             borrowed_props: Default::default(),

+ 8 - 9
packages/core/src/scopes.rs

@@ -15,7 +15,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
 use slab::{Slab, VacantEntry};
 use slab::{Slab, VacantEntry};
 use std::{
 use std::{
     any::{Any, TypeId},
     any::{Any, TypeId},
-    cell::{Cell, RefCell},
+    cell::{Cell, RefCell, UnsafeCell},
     fmt::{Arguments, Debug},
     fmt::{Arguments, Debug},
     future::Future,
     future::Future,
     ops::{Index, IndexMut},
     ops::{Index, IndexMut},
@@ -170,8 +170,7 @@ pub struct ScopeState {
 
 
     pub(crate) height: u32,
     pub(crate) height: u32,
 
 
-    pub(crate) hook_arena: Bump,
-    pub(crate) hook_list: RefCell<Vec<*mut dyn Any>>,
+    pub(crate) hooks: RefCell<Vec<Box<UnsafeCell<dyn Any>>>>,
     pub(crate) hook_idx: Cell<usize>,
     pub(crate) hook_idx: Cell<usize>,
 
 
     pub(crate) shared_contexts: RefCell<FxHashMap<TypeId, Box<dyn Any>>>,
     pub(crate) shared_contexts: RefCell<FxHashMap<TypeId, Box<dyn Any>>>,
@@ -641,18 +640,18 @@ impl<'src> ScopeState {
     #[allow(clippy::mut_from_ref)]
     #[allow(clippy::mut_from_ref)]
     pub fn use_hook<State: 'static>(&self, initializer: impl FnOnce() -> State) -> &mut State {
     pub fn use_hook<State: 'static>(&self, initializer: impl FnOnce() -> State) -> &mut State {
         let cur_hook = self.hook_idx.get();
         let cur_hook = self.hook_idx.get();
-        let mut hook_list = self.hook_list.try_borrow_mut().expect("The hook list is already borrowed: This error is likely caused by trying to use a hook inside a hook which violates the rules of hooks.");
+        let mut hooks = self.hooks.try_borrow_mut().expect("The hook list is already borrowed: This error is likely caused by trying to use a hook inside a hook which violates the rules of hooks.");
 
 
-        if cur_hook >= hook_list.len() {
-            hook_list.push(self.hook_arena.alloc(initializer()));
+        if cur_hook >= hooks.len() {
+            hooks.push(Box::new(UnsafeCell::new(initializer())));
         }
         }
 
 
-        hook_list
+        hooks
             .get(cur_hook)
             .get(cur_hook)
             .and_then(|inn| {
             .and_then(|inn| {
                 self.hook_idx.set(cur_hook + 1);
                 self.hook_idx.set(cur_hook + 1);
-                let raw_box = unsafe { &mut **inn };
-                raw_box.downcast_mut::<State>()
+                let raw_ref = unsafe { &mut *inn.get() };
+                raw_ref.downcast_mut::<State>()
             })
             })
             .expect(
             .expect(
                 r###"
                 r###"