wip: go back to noisy lifetime solution
This commit is contained in:
1 parent
0545d27182
commit
8daf7a6ed8
31 files changed
+214
-170
No files matched your search
@@ -5,17 +5,18 @@
|
||||
//! if the type supports PartialEq. The Properties trait is used by the rsx! and html! macros to generate the type-safe builder
|
||||
//! that ensures compile-time required and optional fields on cx.
|
||||
|
||||
use crate::innerlude::{Context, Element, LazyNodes, VPortal};
|
||||
use crate::innerlude::{Element, LazyNodes, Scope, VPortal};
|
||||
|
||||
pub struct FragmentProps(Element);
|
||||
pub struct FragmentBuilder<const BUILT: bool>(Element);
|
||||
impl FragmentBuilder<false> {
|
||||
pub fn children(self, children: Option<VPortal>) -> FragmentBuilder<true> {
|
||||
FragmentBuilder(children)
|
||||
pub struct FragmentProps<'a>(Element<'a>);
|
||||
pub struct FragmentBuilder<'a, const BUILT: bool>(Element<'a>);
|
||||
impl<'a> FragmentBuilder<'a, false> {
|
||||
pub fn children(self, children: Element<'a>) -> FragmentBuilder<'a, true> {
|
||||
todo!()
|
||||
// FragmentBuilder(children)
|
||||
}
|
||||
}
|
||||
impl<const A: bool> FragmentBuilder<A> {
|
||||
pub fn build(self) -> FragmentProps {
|
||||
impl<'a, const A: bool> FragmentBuilder<'a, A> {
|
||||
pub fn build(self) -> FragmentProps<'a> {
|
||||
FragmentProps(self.0)
|
||||
}
|
||||
}
|
||||
@@ -60,8 +61,8 @@ impl<const A: bool> FragmentBuilder<A> {
|
||||
/// })
|
||||
/// }
|
||||
/// ```
|
||||
impl Properties for FragmentProps {
|
||||
type Builder = FragmentBuilder<false>;
|
||||
impl<'a> Properties for FragmentProps<'a> {
|
||||
type Builder = FragmentBuilder<'a, false>;
|
||||
const IS_STATIC: bool = false;
|
||||
fn builder() -> Self::Builder {
|
||||
FragmentBuilder(None)
|
||||
@@ -97,8 +98,9 @@ impl Properties for FragmentProps {
|
||||
///
|
||||
/// You want to use this free-function when your fragment needs a key and simply returning multiple nodes from rsx! won't cut it.
|
||||
#[allow(non_upper_case_globals, non_snake_case)]
|
||||
pub fn Fragment(cx: Context<FragmentProps>) -> Element {
|
||||
cx.render(Some(LazyNodes::new(|f| f.fragment_from_iter(&cx.props.0))))
|
||||
pub fn Fragment<'a>(cx: Scope<'a, FragmentProps<'a>>) -> Element {
|
||||
todo!()
|
||||
// cx.render(Some(LazyNodes::new(|f| f.fragment_from_iter(&cx.props.0))))
|
||||
}
|
||||
|
||||
/// Every "Props" used for a component must implement the `Properties` trait. This trait gives some hints to Dioxus
|
||||
@@ -165,6 +167,6 @@ impl EmptyBuilder {
|
||||
|
||||
/// This utility function launches the builder method so rsx! and html! macros can use the typed-builder pattern
|
||||
/// to initialize a component's props.
|
||||
pub fn fc_to_builder<'a, T: Properties + 'a>(_: fn(Context<'a, T>) -> Element) -> T::Builder {
|
||||
pub fn fc_to_builder<'a, T: Properties + 'a>(_: fn(Scope<'a, T>) -> Element) -> T::Builder {
|
||||
T::builder()
|
||||
}
|
||||
@@ -1370,7 +1370,7 @@ impl<'bump> DiffState<'bump> {
|
||||
}
|
||||
|
||||
/// Adds a listener closure to a scope during diff.
|
||||
fn attach_listener_to_scope(&mut self, listener: &'bump Listener<'bump>, scope: &Scope) {
|
||||
fn attach_listener_to_scope(&mut self, listener: &'bump Listener<'bump>, scope: &ScopeState) {
|
||||
let long_listener = unsafe { std::mem::transmute(listener) };
|
||||
scope.items.borrow_mut().listeners.push(long_listener)
|
||||
}
|
||||
|
||||
@@ -20,21 +20,21 @@ pub(crate) mod innerlude {
|
||||
pub(crate) use crate::scopearena::*;
|
||||
pub use crate::virtual_dom::*;
|
||||
|
||||
pub type Element = Option<VPortal>;
|
||||
pub type Component<P> = for<'a> fn(Context<'a, P>) -> Element;
|
||||
pub type Element<'a> = Option<VNode<'a>>;
|
||||
pub type Component<P> = for<'a> fn(Scope<'a, P>) -> Element<'a>;
|
||||
}
|
||||
|
||||
pub use crate::innerlude::{
|
||||
AnyContext, Attribute, Component, Context, DioxusElement, DomEdit, Element, ElementId,
|
||||
EventHandler, EventPriority, IntoVNode, LazyNodes, Listener, Mutations, NodeFactory,
|
||||
Properties, SchedulerMsg, Scope, ScopeId, UserEvent, VElement, VFragment, VNode, VirtualDom,
|
||||
Attribute, Component, DioxusElement, DomEdit, Element, ElementId, EventHandler, EventPriority,
|
||||
IntoVNode, LazyNodes, Listener, Mutations, NodeFactory, Properties, SchedulerMsg, Scope,
|
||||
ScopeId, ScopeState, UserEvent, VElement, VFragment, VNode, VirtualDom,
|
||||
};
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::component::{fc_to_builder, Fragment, Properties};
|
||||
pub use crate::innerlude::Context;
|
||||
pub use crate::innerlude::Scope;
|
||||
pub use crate::innerlude::{
|
||||
AnyContext, Component, DioxusElement, Element, EventHandler, LazyNodes, NodeFactory, Scope,
|
||||
Component, DioxusElement, Element, EventHandler, LazyNodes, NodeFactory, ScopeState,
|
||||
};
|
||||
pub use crate::nodes::VNode;
|
||||
pub use crate::VirtualDom;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! cheap and *very* fast to construct - building a full tree should be quick.
|
||||
|
||||
use crate::{
|
||||
innerlude::{Context, Element, Properties, Scope, ScopeId},
|
||||
innerlude::{Element, Properties, Scope, ScopeId, ScopeState},
|
||||
lazynodes::LazyNodes,
|
||||
};
|
||||
use bumpalo::{boxed::Box as BumpBox, Bump};
|
||||
@@ -412,7 +412,7 @@ pub struct VComponent<'src> {
|
||||
pub(crate) bump_props: *const (),
|
||||
|
||||
// during the "teardown" process we'll take the caller out so it can be dropped properly
|
||||
pub(crate) caller: &'src dyn Fn(&'src Scope) -> Element,
|
||||
pub(crate) caller: &'src dyn Fn(&'src ScopeState) -> Element,
|
||||
|
||||
pub(crate) comparator: Option<&'src dyn Fn(&VComponent) -> bool>,
|
||||
|
||||
@@ -554,7 +554,7 @@ impl<'a> NodeFactory<'a> {
|
||||
|
||||
pub fn component<P>(
|
||||
&self,
|
||||
component: fn(Context<'a, P>) -> Element,
|
||||
component: fn(Scope<'a, P>) -> Element,
|
||||
props: P,
|
||||
key: Option<Arguments>,
|
||||
) -> VNode<'a>
|
||||
@@ -612,10 +612,10 @@ impl<'a> NodeFactory<'a> {
|
||||
|
||||
let key = key.map(|f| self.raw_text(f).0);
|
||||
|
||||
let caller: &'a mut dyn Fn(&'a Scope) -> Element =
|
||||
bump.alloc(move |scope: &Scope| -> Element {
|
||||
let caller: &'a mut dyn Fn(&'a ScopeState) -> Element =
|
||||
bump.alloc(move |scope: &ScopeState| -> Element {
|
||||
let props: &'_ P = unsafe { &*(bump_props as *const P) };
|
||||
component(Context { scope, props })
|
||||
component(Scope { scope, props })
|
||||
});
|
||||
|
||||
let can_memoize = P::IS_STATIC;
|
||||
@@ -707,7 +707,7 @@ impl<'a> NodeFactory<'a> {
|
||||
pub fn create_children(
|
||||
self,
|
||||
node_iter: impl IntoIterator<Item = impl IntoVNode<'a>>,
|
||||
) -> Element {
|
||||
) -> Element<'a> {
|
||||
let bump = self.bump;
|
||||
let mut nodes = bumpalo::collections::Vec::new_in(bump);
|
||||
|
||||
@@ -726,16 +726,10 @@ impl<'a> NodeFactory<'a> {
|
||||
// TODO
|
||||
// We need a dedicated path in the rsx! macro that will trigger the "you need keys" warning
|
||||
|
||||
let frag = VNode::Fragment(VFragment {
|
||||
Some(VNode::Fragment(VFragment {
|
||||
children,
|
||||
key: None,
|
||||
});
|
||||
let ptr = self.bump.alloc(frag) as *const _;
|
||||
Some(VPortal {
|
||||
link_idx: Default::default(),
|
||||
scope_id: Default::default(),
|
||||
node: unsafe { std::mem::transmute(ptr) },
|
||||
})
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+64
-49
@@ -31,11 +31,39 @@ use bumpalo::{boxed::Box as BumpBox, Bump};
|
||||
/// cx.render(rsx!{ div {"Hello, {props.name}"} })
|
||||
/// }
|
||||
/// ```
|
||||
pub struct Context<'a, P: 'static> {
|
||||
pub scope: &'a Scope,
|
||||
pub struct Scope<'a, P> {
|
||||
pub scope: &'a ScopeState,
|
||||
pub props: &'a P,
|
||||
}
|
||||
impl<P> Clone for Context<'_, P> {
|
||||
|
||||
impl<'a, P> Scope<'a, P> {
|
||||
/// Take a lazy VNode structure and actually build it with the context of the VDom's efficient VNode allocator.
|
||||
///
|
||||
/// This function consumes the context and absorb the lifetime, so these VNodes *must* be returned.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// fn Component(cx: Scope, props: &Props) -> Element {
|
||||
/// // Lazy assemble the VNode tree
|
||||
/// let lazy_nodes = rsx!("hello world");
|
||||
///
|
||||
/// // Actually build the tree and allocate it
|
||||
/// cx.render(lazy_tree)
|
||||
/// }
|
||||
///```
|
||||
pub fn render(self, rsx: Option<LazyNodes<'a, '_>>) -> Option<VNode<'a>> {
|
||||
let fac = NodeFactory {
|
||||
bump: &self.scope.wip_frame().bump,
|
||||
};
|
||||
match rsx {
|
||||
Some(s) => Some(s.call(fac)),
|
||||
None => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> Clone for Scope<'_, P> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
scope: self.scope,
|
||||
@@ -43,21 +71,13 @@ impl<P> Clone for Context<'_, P> {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<P> Copy for Context<'_, P> {}
|
||||
impl<P> std::ops::Deref for Context<'_, P> {
|
||||
type Target = Scope;
|
||||
impl<P> Copy for Scope<'_, P> {}
|
||||
impl<P> std::ops::Deref for Scope<'_, P> {
|
||||
type Target = ScopeState;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.scope
|
||||
}
|
||||
}
|
||||
pub trait AnyContext<'a> {
|
||||
fn get_scope(&self) -> &'a Scope;
|
||||
}
|
||||
impl<'a, P> AnyContext<'a> for Context<'a, P> {
|
||||
fn get_scope(&self) -> &'a Scope {
|
||||
&self.scope
|
||||
}
|
||||
}
|
||||
|
||||
/// A component's unique identifier.
|
||||
///
|
||||
@@ -76,8 +96,8 @@ pub struct ScopeId(pub usize);
|
||||
///
|
||||
/// We expose the `Scope` type so downstream users can traverse the Dioxus VirtualDOM for whatever
|
||||
/// use case they might have.
|
||||
pub struct Scope {
|
||||
pub(crate) parent_scope: Option<*mut Scope>,
|
||||
pub struct ScopeState {
|
||||
pub(crate) parent_scope: Option<*mut ScopeState>,
|
||||
|
||||
pub(crate) container: ElementId,
|
||||
|
||||
@@ -93,7 +113,7 @@ pub struct Scope {
|
||||
|
||||
pub(crate) frames: [BumpFrame; 2],
|
||||
|
||||
pub(crate) caller: *const dyn Fn(&Scope) -> Element,
|
||||
pub(crate) caller: *const dyn Fn(&ScopeState) -> Element,
|
||||
|
||||
pub(crate) items: RefCell<SelfReferentialItems<'static>>,
|
||||
|
||||
@@ -113,7 +133,7 @@ pub struct SelfReferentialItems<'a> {
|
||||
}
|
||||
|
||||
// Public methods exposed to libraries and components
|
||||
impl Scope {
|
||||
impl ScopeState {
|
||||
/// Get the subtree ID that this scope belongs to.
|
||||
///
|
||||
/// Each component has its own subtree ID - the root subtree has an ID of 0. This ID is used by the renderer to route
|
||||
@@ -342,36 +362,31 @@ impl Scope {
|
||||
items.tasks.len() - 1
|
||||
}
|
||||
|
||||
/// Take a lazy VNode structure and actually build it with the context of the VDom's efficient VNode allocator.
|
||||
///
|
||||
/// This function consumes the context and absorb the lifetime, so these VNodes *must* be returned.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// fn Component(cx: Scope, props: &Props) -> Element {
|
||||
/// // Lazy assemble the VNode tree
|
||||
/// let lazy_nodes = rsx!("hello world");
|
||||
///
|
||||
/// // Actually build the tree and allocate it
|
||||
/// cx.render(lazy_tree)
|
||||
/// }
|
||||
///```
|
||||
pub fn render<'src>(&'src self, rsx: Option<LazyNodes<'src, '_>>) -> Option<VPortal> {
|
||||
let bump = &self.wip_frame().bump;
|
||||
|
||||
let owned_node: VNode<'src> = rsx.map(|f| f.call(NodeFactory { bump }))?;
|
||||
let alloced_vnode: &'src mut VNode<'src> = bump.alloc(owned_node);
|
||||
let node_ptr: *mut VNode<'src> = alloced_vnode as *mut _;
|
||||
|
||||
let node: *mut VNode<'static> = unsafe { std::mem::transmute(node_ptr) };
|
||||
|
||||
Some(VPortal {
|
||||
scope_id: Cell::new(Some(self.our_arena_idx)),
|
||||
link_idx: Cell::new(0),
|
||||
node,
|
||||
})
|
||||
}
|
||||
// /// Take a lazy VNode structure and actually build it with the context of the VDom's efficient VNode allocator.
|
||||
// ///
|
||||
// /// This function consumes the context and absorb the lifetime, so these VNodes *must* be returned.
|
||||
// ///
|
||||
// /// ## Example
|
||||
// ///
|
||||
// /// ```ignore
|
||||
// /// fn Component(cx: Scope, props: &Props) -> Element {
|
||||
// /// // Lazy assemble the VNode tree
|
||||
// /// let lazy_nodes = rsx!("hello world");
|
||||
// ///
|
||||
// /// // Actually build the tree and allocate it
|
||||
// /// cx.render(lazy_tree)
|
||||
// /// }
|
||||
// ///```
|
||||
// pub fn render<'src>(&self, rsx: Option<LazyNodes<'src, '_>>) -> Option<VNode<'src>> {
|
||||
// let fac = NodeFactory {
|
||||
// bump: &self.wip_frame().bump,
|
||||
// };
|
||||
// match rsx {
|
||||
// Some(s) => Some(s.call(fac)),
|
||||
// None => todo!(),
|
||||
// }
|
||||
// // rsx.map(|f| f.call(fac))
|
||||
// }
|
||||
|
||||
/// Store a value between renders
|
||||
///
|
||||
@@ -505,5 +520,5 @@ impl BumpFrame {
|
||||
|
||||
#[test]
|
||||
fn sizeof() {
|
||||
dbg!(std::mem::size_of::<Scope>());
|
||||
dbg!(std::mem::size_of::<ScopeState>());
|
||||
}
|
||||
@@ -24,9 +24,9 @@ pub(crate) struct ScopeArena {
|
||||
bump: Bump,
|
||||
pub pending_futures: RefCell<FxHashSet<ScopeId>>,
|
||||
scope_counter: Cell<usize>,
|
||||
pub scopes: RefCell<FxHashMap<ScopeId, *mut Scope>>,
|
||||
pub scopes: RefCell<FxHashMap<ScopeId, *mut ScopeState>>,
|
||||
pub heuristics: RefCell<FxHashMap<FcSlot, Heuristic>>,
|
||||
free_scopes: RefCell<Vec<*mut Scope>>,
|
||||
free_scopes: RefCell<Vec<*mut ScopeState>>,
|
||||
nodes: RefCell<Slab<*const VNode<'static>>>,
|
||||
pub(crate) sender: UnboundedSender<SchedulerMsg>,
|
||||
}
|
||||
@@ -68,23 +68,23 @@ impl ScopeArena {
|
||||
/// Safety:
|
||||
/// - Obtaining a mutable refernece to any Scope is unsafe
|
||||
/// - Scopes use interior mutability when sharing data into components
|
||||
pub(crate) fn get_scope(&self, id: &ScopeId) -> Option<&Scope> {
|
||||
pub(crate) fn get_scope(&self, id: &ScopeId) -> Option<&ScopeState> {
|
||||
unsafe { self.scopes.borrow().get(id).map(|f| &**f) }
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn get_scope_raw(&self, id: &ScopeId) -> Option<*mut Scope> {
|
||||
pub(crate) unsafe fn get_scope_raw(&self, id: &ScopeId) -> Option<*mut ScopeState> {
|
||||
self.scopes.borrow().get(id).copied()
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn get_scope_mut(&self, id: &ScopeId) -> Option<&mut Scope> {
|
||||
pub(crate) unsafe fn get_scope_mut(&self, id: &ScopeId) -> Option<&mut ScopeState> {
|
||||
self.scopes.borrow().get(id).map(|s| &mut **s)
|
||||
}
|
||||
|
||||
pub(crate) fn new_with_key(
|
||||
&self,
|
||||
fc_ptr: *const (),
|
||||
caller: *const dyn Fn(&Scope) -> Element,
|
||||
parent_scope: Option<*mut Scope>,
|
||||
caller: *const dyn Fn(&ScopeState) -> Element,
|
||||
parent_scope: Option<*mut ScopeState>,
|
||||
container: ElementId,
|
||||
height: u32,
|
||||
subtree: u32,
|
||||
@@ -160,7 +160,7 @@ impl ScopeArena {
|
||||
unsafe { std::mem::transmute(vnode as *mut VNode) }
|
||||
});
|
||||
|
||||
let scope = self.bump.alloc(Scope {
|
||||
let scope = self.bump.alloc(ScopeState {
|
||||
sender: self.sender.clone(),
|
||||
container,
|
||||
our_arena_idx: new_scope_id,
|
||||
@@ -330,18 +330,9 @@ impl ScopeArena {
|
||||
scope.wip_frame().nodes.borrow_mut().clear();
|
||||
}
|
||||
|
||||
let render: &dyn Fn(&Scope) -> Element = unsafe { &*scope.caller };
|
||||
let render: &dyn Fn(&ScopeState) -> Element = unsafe { &*scope.caller };
|
||||
|
||||
if let Some(link) = render(scope) {
|
||||
// right now, it's a panic to render a nodelink from another scope
|
||||
// todo: enable this. it should (reasonably) work even if it doesnt make much sense
|
||||
assert_eq!(link.scope_id.get(), Some(*id));
|
||||
|
||||
// nodelinks are not assigned when called and must be done so through the create/diff phase
|
||||
// however, we need to link this one up since it will never be used in diffing
|
||||
scope.wip_frame().assign_nodelink(&link);
|
||||
debug_assert_eq!(scope.wip_frame().nodes.borrow().len(), 1);
|
||||
|
||||
if !scope.items.borrow().tasks.is_empty() {
|
||||
self.pending_futures.borrow_mut().insert(*id);
|
||||
}
|
||||
|
||||
@@ -196,13 +196,18 @@ impl VirtualDom {
|
||||
) -> Self {
|
||||
let scopes = ScopeArena::new(sender.clone());
|
||||
|
||||
let mut caller = Box::new(move |scp: &Scope| -> Element {
|
||||
root(Context {
|
||||
scope: scp,
|
||||
props: &root_props,
|
||||
})
|
||||
});
|
||||
let caller_ref: *mut dyn Fn(&Scope) -> Element = caller.as_mut() as *mut _;
|
||||
let root_props = Box::new(root_props);
|
||||
let props_ref: *const P = root_props.as_ref();
|
||||
let mut caller: Box<dyn Fn(&ScopeState) -> Element> =
|
||||
Box::new(move |scp: &ScopeState| -> Element {
|
||||
let p = unsafe { &*props_ref };
|
||||
todo!()
|
||||
// root(Context {
|
||||
// scope: scp,
|
||||
// props: p,
|
||||
// })
|
||||
});
|
||||
let caller_ref: *mut dyn Fn(&ScopeState) -> Element = caller.as_mut();
|
||||
let base_scope = scopes.new_with_key(root as _, caller_ref, None, ElementId(0), 0, 0);
|
||||
|
||||
let pending_messages = VecDeque::new();
|
||||
@@ -213,7 +218,7 @@ impl VirtualDom {
|
||||
scopes: Box::new(scopes),
|
||||
base_scope,
|
||||
receiver,
|
||||
_root_props: caller,
|
||||
_root_props: root_props,
|
||||
pending_messages,
|
||||
dirty_scopes,
|
||||
sender,
|
||||
@@ -226,7 +231,7 @@ impl VirtualDom {
|
||||
/// directly.
|
||||
///
|
||||
/// # Example
|
||||
pub fn base_scope(&self) -> &Scope {
|
||||
pub fn base_scope(&self) -> &ScopeState {
|
||||
self.get_scope(&self.base_scope).unwrap()
|
||||
}
|
||||
|
||||
@@ -236,7 +241,7 @@ impl VirtualDom {
|
||||
///
|
||||
///
|
||||
///
|
||||
pub fn get_scope<'a>(&'a self, id: &ScopeId) -> Option<&'a Scope> {
|
||||
pub fn get_scope<'a>(&'a self, id: &ScopeId) -> Option<&'a ScopeState> {
|
||||
self.scopes.get_scope(id)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user