From 6b2645fd80ab20153e7bb51887443e994fbab5cb Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sat, 30 Oct 2021 21:07:47 -0400 Subject: [PATCH] feat: improve FC handling to allow lifetimes of parent --- .vscode/settings.json | 2 +- packages/core/examples/syntax2.rs | 78 ++++++++++++++++++------------- packages/core/src/nodes.rs | 29 ++++++++++-- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index b3bde867c..6b608c765 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "rust-analyzer.cargo.allFeatures": true + // "rust-analyzer.cargo.allFeatures": true } diff --git a/packages/core/examples/syntax2.rs b/packages/core/examples/syntax2.rs index d3af16013..6ee3985d3 100644 --- a/packages/core/examples/syntax2.rs +++ b/packages/core/examples/syntax2.rs @@ -252,39 +252,49 @@ fn Child2<'a>((cx, props): Scope<'a, Child2Props>) -> Element<'a> { // - borrowed // - memoized - cx.render({ - NodeFactory::annotate_lazy(move |__cx: NodeFactory| -> VNode { - use dioxus_elements::{GlobalAttributes, SvgAttributes}; - __cx.element( - dioxus_elements::div, - [], - [], - [], - // [__cx.component( - // ChildrenComp, - // { - // let mut __manual_props = ChildrenTest { node: children }; - // __manual_props - // }, - // None, - // [], - // )], - None, - ) - }) - }) - // cx.render(rsx! { - // div { - // // div {} - - // ChildrenComp { - // // node: children - // ..ChildrenTest { - // node: children, - // } - // } - // } + // cx.render({ + // NodeFactory::annotate_lazy(move |__cx: NodeFactory| -> VNode { + // use dioxus_elements::{GlobalAttributes, SvgAttributes}; + // __cx.element( + // dioxus_elements::div, + // [], + // [], + // [ + // __cx.component(ChildrenMemo, (), None, []), + // __cx.component( + // ChildrenComp, + // // + // ChildrenTest { node: children }, + // None, + // [], + // ), + // // { + // // let _props: &_ = __cx.bump().alloc(ChildrenTest { node: children }); + // // __cx.component_v2_borrowed( + // // // + // // move |c| ChildrenComp((c, _props)), + // // ChildrenComp, + // // _props, + // // ) + // // }, + // // { + // // let _props: &_ = __cx.bump().alloc(()); + // // __cx.component_v2_borrowed(move |c| ChildrenMemo((c, _props))) + // // }, + // ], + // None, + // ) + // }) // }) + cx.render(rsx! { + div { + ChildrenComp { + ..ChildrenTest { + node: children, + } + } + } + }) } #[derive(Props)] @@ -333,3 +343,7 @@ fn ChildrenComp<'a>((cx, props): Scope<'a, ChildrenTest<'a>>) -> Element<'a> { } }) } + +fn ChildrenMemo((cx, props): Scope<()>) -> Element { + todo!() +} diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs index fb250f1c4..255f7ca74 100644 --- a/packages/core/src/nodes.rs +++ b/packages/core/src/nodes.rs @@ -4,7 +4,8 @@ //! cheap and *very* fast to construct - building a full tree should be quick. use crate::innerlude::{ - empty_cell, Context, Element, ElementId, Properties, ScopeId, ScopeInner, SuspendedContext, FC, + empty_cell, Context, Element, ElementId, Properties, Scope, ScopeId, ScopeInner, + SuspendedContext, FC, }; use bumpalo::{boxed::Box as BumpBox, Bump}; use std::{ @@ -448,9 +449,26 @@ impl<'a> NodeFactory<'a> { } } + pub fn component_v2_borrowed( + self, + f: impl Fn(Context<'a>) -> Option> + 'a, + fc: fn(Scope<'a, P>) -> Element<'a>, + p: &'a P, + ) -> VNode<'a> { + // + todo!() + } + // pub fn component_v2_memoized( + // self, + // f: impl for<'b> Fn(Context<'b>) -> Option> + 'static, + // ) -> VNode<'a> { + // // + // todo!() + // } + pub fn component( &self, - component: FC

, + component: fn(Scope<'a, P>) -> Element<'a>, props: P, key: Option, children: V, @@ -458,7 +476,6 @@ impl<'a> NodeFactory<'a> { where P: Properties + 'a, V: AsRef<[VNode<'a>]> + 'a, - // V: 'a + AsRef<[VNode<'a>]>, { let bump = self.bump(); let children: &'a V = bump.alloc(children); @@ -523,7 +540,11 @@ impl<'a> NodeFactory<'a> { bump.alloc(move |scope: &ScopeInner| -> Element { log::debug!("calling component renderr {:?}", scope.our_arena_idx); let props: &'_ P = unsafe { &*(raw_props as *const P) }; - let res: Element = component((Context { scope }, props)); + + let scp: &'a ScopeInner = unsafe { std::mem::transmute(scope) }; + let s: Scope<'a, P> = (Context { scope: scp }, props); + + let res: Element = component(s); unsafe { std::mem::transmute(res) } });