Feat: finally solve the component lifetime problem <3
This commit is contained in:
1 parent
7a6aabe4f3
commit
bdc25b581b
4 files changed
+89
-22
No files matched your search
Vendored
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"rust-analyzer.inlayHints.enable": true
|
||||
"rust-analyzer.inlayHints.enable": false
|
||||
}
|
||||
@@ -9,13 +9,18 @@ use dioxus_core::prelude::{DomTree, VNode};
|
||||
fn main() {}
|
||||
|
||||
struct Context2<'a, P> {
|
||||
props: &'a P, // _p: PhantomData<&'a ()>,
|
||||
_props: &'a P, // _p: PhantomData<&'a ()>,
|
||||
rops: &'a P, // _p: PhantomData<&'a ()>,
|
||||
}
|
||||
impl<'a, P> Context2<'a, P> {
|
||||
fn view(&self, f: impl FnOnce(&'a Bump) -> VNode<'a>) -> DTree {
|
||||
fn view(self, f: impl FnOnce(&'a Bump) -> VNode<'a>) -> DTree {
|
||||
DTree {}
|
||||
}
|
||||
|
||||
fn props(&self) -> &'a P {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn use_hook<'scope, InternalHookState: 'static, Output: 'a>(
|
||||
&'scope self,
|
||||
initializer: impl FnOnce() -> InternalHookState,
|
||||
@@ -26,45 +31,64 @@ impl<'a, P> Context2<'a, P> {
|
||||
}
|
||||
}
|
||||
|
||||
trait Properties {}
|
||||
|
||||
struct DTree;
|
||||
type FC2<'a, T: 'a> = fn(&'a Context2<T>) -> DTree;
|
||||
// type FC2<'a, T: 'a> = fn(Context2<T>) -> DTree;
|
||||
fn virtual_child<'a, T: 'a>(bump: &'a Bump, props: T, f: FC2<T>) -> VNode<'a> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
struct Props {
|
||||
c: String,
|
||||
}
|
||||
|
||||
static Example: FC2<Props> = |ctx| {
|
||||
fn Example(ctx: Context2<Props>) -> DTree {
|
||||
let val = use_state(&ctx, || String::from("asd"));
|
||||
let props = ctx.props();
|
||||
|
||||
ctx.view(move |b| {
|
||||
dioxus_core::nodebuilder::div(b)
|
||||
.child(dioxus_core::nodebuilder::text(props.c.as_str()))
|
||||
.child(virtual_child(b, Props2 { a: val }, AltChild))
|
||||
.finish()
|
||||
})
|
||||
}
|
||||
|
||||
// #[fc]
|
||||
fn Example2(ctx: Context2<()>, name: &str, blah: &str) -> DTree {
|
||||
let val = use_state(&ctx, || String::from("asd"));
|
||||
|
||||
ctx.view(move |b| {
|
||||
let g: VNode<'_> = virtual_child(b, Props2 { a: val }, alt_child);
|
||||
//
|
||||
dioxus_core::nodebuilder::div(b)
|
||||
.child(dioxus_core::nodebuilder::text(ctx.props.c.as_str()))
|
||||
.child(g)
|
||||
.child(dioxus_core::nodebuilder::text(name))
|
||||
.child(virtual_child(b, Props2 { a: val }, AltChild))
|
||||
.finish()
|
||||
})
|
||||
};
|
||||
|
||||
fn virtual_child<'a, T: 'a>(bump: &'a Bump, props: T, f: FC2<T>) -> VNode<'a> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
struct Props2<'a> {
|
||||
a: &'a String,
|
||||
}
|
||||
type FC2<'a, T: 'a> = fn(Context2<T>) -> DTree;
|
||||
|
||||
fn alt_child<'a>(ctx: &Context2<'a, Props2<'_>>) -> DomTree {
|
||||
todo!()
|
||||
}
|
||||
|
||||
static CHILD: FC2<Props2> = |ctx| {
|
||||
// still works if you don't take any references in your props (ie, something copy or cloneable)
|
||||
static CHILD: FC2<Props2> = |ctx: Context2<Props2>| {
|
||||
//
|
||||
todo!()
|
||||
};
|
||||
|
||||
struct Props2<'a> {
|
||||
a: &'a String,
|
||||
}
|
||||
impl Properties for Props2<'_> {}
|
||||
|
||||
fn AltChild(ctx: Context2<Props2>) -> DTree {
|
||||
ctx.view(|b| {
|
||||
//
|
||||
todo!()
|
||||
})
|
||||
}
|
||||
|
||||
fn use_state<'a, 'c, P, T: 'static, F: FnOnce() -> T>(
|
||||
ctx: &'a Context2<'a, P>,
|
||||
ctx: &'_ Context2<'a, P>,
|
||||
initial_state_fn: F,
|
||||
) -> (&'a T) {
|
||||
todo!()
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
//! Demonstrate that borrowed data is possible as a property type
|
||||
//! Borrowing (rather than cloning) is very important for speed and ergonomics.
|
||||
|
||||
fn main() {}
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
|
||||
struct Props {
|
||||
items: Vec<ListItem>,
|
||||
}
|
||||
|
||||
struct ListItem {
|
||||
name: String,
|
||||
age: u32,
|
||||
}
|
||||
|
||||
fn app(ctx: Context, props: &Props) -> DomTree {
|
||||
ctx.view(move |b| {
|
||||
let mut root = builder::div(b);
|
||||
for child in &props.items {
|
||||
// notice that the child directly borrows from our vec
|
||||
// this makes lists very fast (simply views reusing lifetimes)
|
||||
root = root.child(builder::virtual_child(
|
||||
b,
|
||||
ChildProps { item: child },
|
||||
child_item,
|
||||
));
|
||||
}
|
||||
root.finish()
|
||||
})
|
||||
}
|
||||
|
||||
struct ChildProps<'a> {
|
||||
item: &'a ListItem,
|
||||
}
|
||||
|
||||
fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
|
||||
todo!()
|
||||
}
|
||||
@@ -41,6 +41,10 @@ pub struct Context<'src> {
|
||||
}
|
||||
|
||||
impl<'a> Context<'a> {
|
||||
pub fn props<P>() -> &'a P {
|
||||
todo!()
|
||||
}
|
||||
|
||||
// impl<'a, PropType> Context<'a, PropType> {
|
||||
/// Access the children elements passed into the component
|
||||
pub fn children(&self) -> Vec<VNode> {
|
||||
|
||||
Reference in new issue
Block a user