alternative.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //! An alternative function syntax
  2. //!
  3. use std::marker::PhantomData;
  4. use bumpalo::Bump;
  5. use dioxus_core::prelude::{DomTree, VNode};
  6. fn main() {}
  7. struct Context2<'a, P> {
  8. _props: &'a P, // _p: PhantomData<&'a ()>,
  9. rops: &'a P, // _p: PhantomData<&'a ()>,
  10. }
  11. impl<'a, P> Context2<'a, P> {
  12. fn view(self, f: impl FnOnce(&'a Bump) -> VNode<'a>) -> DTree {
  13. DTree {}
  14. }
  15. fn props(&self) -> &'a P {
  16. todo!()
  17. }
  18. pub fn use_hook<'scope, InternalHookState: 'static, Output: 'a>(
  19. &'scope self,
  20. initializer: impl FnOnce() -> InternalHookState,
  21. runner: impl FnOnce(&'a mut InternalHookState) -> Output,
  22. cleanup: impl FnOnce(InternalHookState),
  23. ) -> Output {
  24. todo!()
  25. }
  26. }
  27. trait Properties {}
  28. struct DTree;
  29. // type FC2<'a, T: 'a> = fn(Context2<T>) -> DTree;
  30. fn virtual_child<'a, T: 'a>(bump: &'a Bump, props: T, f: FC2<T>) -> VNode<'a> {
  31. todo!()
  32. }
  33. struct Props {
  34. c: String,
  35. }
  36. fn Example(ctx: Context2<Props>) -> DTree {
  37. let val = use_state(&ctx, || String::from("asd"));
  38. let props = ctx.props();
  39. ctx.view(move |b| {
  40. dioxus_core::nodebuilder::div(b)
  41. .child(dioxus_core::nodebuilder::text(props.c.as_str()))
  42. .child(virtual_child(b, Props2 { a: val }, AltChild))
  43. .finish()
  44. })
  45. }
  46. // #[fc]
  47. fn Example2(ctx: Context2<()>, name: &str, blah: &str) -> DTree {
  48. let val = use_state(&ctx, || String::from("asd"));
  49. ctx.view(move |b| {
  50. dioxus_core::nodebuilder::div(b)
  51. .child(dioxus_core::nodebuilder::text(name))
  52. .child(virtual_child(b, Props2 { a: val }, AltChild))
  53. .finish()
  54. })
  55. }
  56. type FC2<'a, T: 'a> = fn(Context2<T>) -> DTree;
  57. // still works if you don't take any references in your props (ie, something copy or cloneable)
  58. static CHILD: FC2<Props2> = |ctx: Context2<Props2>| {
  59. //
  60. todo!()
  61. };
  62. struct Props2<'a> {
  63. a: &'a String,
  64. }
  65. impl Properties for Props2<'_> {}
  66. fn AltChild(ctx: Context2<Props2>) -> DTree {
  67. ctx.view(|b| {
  68. //
  69. todo!()
  70. })
  71. }
  72. fn use_state<'a, 'c, P, T: 'static, F: FnOnce() -> T>(
  73. ctx: &'_ Context2<'a, P>,
  74. initial_state_fn: F,
  75. ) -> (&'a T) {
  76. todo!()
  77. }