diff --git a/.vscode/settings.json b/.vscode/settings.json index 2f418701b..80f51cff8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "rust-analyzer.inlayHints.enable": true + "rust-analyzer.inlayHints.enable": false } \ No newline at end of file diff --git a/packages/core/examples/alternative.rs b/packages/core/examples/alternative.rs index f31085f52..7f6100677 100644 --- a/packages/core/examples/alternative.rs +++ b/packages/core/examples/alternative.rs @@ -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) -> DTree; +// type FC2<'a, T: 'a> = fn(Context2) -> DTree; +fn virtual_child<'a, T: 'a>(bump: &'a Bump, props: T, f: FC2) -> VNode<'a> { + todo!() +} struct Props { c: String, } -static Example: FC2 = |ctx| { +fn Example(ctx: Context2) -> 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) -> VNode<'a> { - todo!() } -struct Props2<'a> { - a: &'a String, -} +type FC2<'a, T: 'a> = fn(Context2) -> DTree; -fn alt_child<'a>(ctx: &Context2<'a, Props2<'_>>) -> DomTree { - todo!() -} - -static CHILD: FC2 = |ctx| { +// still works if you don't take any references in your props (ie, something copy or cloneable) +static CHILD: FC2 = |ctx: Context2| { // todo!() }; +struct Props2<'a> { + a: &'a String, +} +impl Properties for Props2<'_> {} + +fn AltChild(ctx: Context2) -> 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!() diff --git a/packages/core/examples/borrowed.rs b/packages/core/examples/borrowed.rs new file mode 100644 index 000000000..adce4232f --- /dev/null +++ b/packages/core/examples/borrowed.rs @@ -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, +} + +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!() +} diff --git a/packages/core/src/context.rs b/packages/core/src/context.rs index 1044c8a77..8a0772a6f 100644 --- a/packages/core/src/context.rs +++ b/packages/core/src/context.rs @@ -41,6 +41,10 @@ pub struct Context<'src> { } impl<'a> Context<'a> { + pub fn props

() -> &'a P { + todo!() + } + // impl<'a, PropType> Context<'a, PropType> { /// Access the children elements passed into the component pub fn children(&self) -> Vec {