Use boxes instead of bumpslab for hooks
This commit is contained in:
1 parent
911d1b4b2f
commit
47513e8b9e
3 files changed
+10
-15
No files matched your search
@@ -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(..) {
|
scope.hooks.get_mut().clear();
|
||||||
drop(unsafe { BumpBox::from_raw(hook) });
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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() {
|
||||||
|
|||||||
@@ -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(),
|
hooks: Default::default(),
|
||||||
hook_list: 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(),
|
||||||
|
|||||||
@@ -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) hooks: RefCell<Vec<Box<UnsafeCell<dyn Any>>>>,
|
||||||
pub(crate) hook_list: RefCell<Vec<*mut 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() {
|
if cur_hook >= hooks.len() {
|
||||||
hook_list.push(self.hook_arena.alloc(initializer()));
|
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 };
|
let raw_ref = unsafe { &mut *inn.get() };
|
||||||
raw_box.downcast_mut::<State>()
|
raw_ref.downcast_mut::<State>()
|
||||||
})
|
})
|
||||||
.expect(
|
.expect(
|
||||||
r###"
|
r###"
|
||||||
|
|||||||
Reference in new issue
Block a user