瀏覽代碼

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

Jon Kelley 2 年之前
父節點
當前提交
388fb30ed3
共有 3 個文件被更改,包括 10 次插入15 次删除
  1. 1 4
      packages/core/src/arena.rs
  2. 1 2
      packages/core/src/scope_arena.rs
  3. 8 9
      packages/core/src/scopes.rs

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

@@ -4,7 +4,6 @@ use crate::{
     innerlude::DirtyScope, nodes::RenderReturn, nodes::VNode, virtual_dom::VirtualDom,
     AttributeValue, DynamicNode, ScopeId,
 };
-use bumpalo::boxed::Box as BumpBox;
 
 /// An Element's unique identifier.
 ///
@@ -110,9 +109,7 @@ impl VirtualDom {
 
         // Drop all the hooks once the children are dropped
         // 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
         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),
             spawned_tasks: Default::default(),
             render_cnt: Default::default(),
-            hook_arena: Default::default(),
-            hook_list: Default::default(),
+            hooks: Default::default(),
             hook_idx: Default::default(),
             shared_contexts: 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 std::{
     any::{Any, TypeId},
-    cell::{Cell, RefCell},
+    cell::{Cell, RefCell, UnsafeCell},
     fmt::{Arguments, Debug},
     future::Future,
     ops::{Index, IndexMut},
@@ -170,8 +170,7 @@ pub struct ScopeState {
 
     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) shared_contexts: RefCell<FxHashMap<TypeId, Box<dyn Any>>>,
@@ -641,18 +640,18 @@ impl<'src> ScopeState {
     #[allow(clippy::mut_from_ref)]
     pub fn use_hook<State: 'static>(&self, initializer: impl FnOnce() -> State) -> &mut State {
         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)
             .and_then(|inn| {
                 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(
                 r###"