nested.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #![allow(unused)]
  2. //! Example of components in
  3. use std::{borrow::Borrow, marker::PhantomData};
  4. use dioxus_core::prelude::*;
  5. fn main() {}
  6. static Header: FC<()> = |ctx, props| {
  7. let inner = use_ref(&ctx, || 0);
  8. let handler1 = move || println!("Value is {}", inner.borrow());
  9. ctx.render(dioxus::prelude::LazyNodes::new(|nodectx| {
  10. builder::ElementBuilder::new(nodectx, "div")
  11. .child(VNode::Component(VComponent::new(
  12. Bottom,
  13. nodectx.bump().alloc(()),
  14. None,
  15. )))
  16. .finish()
  17. }))
  18. };
  19. static Bottom: FC<()> = |ctx, props| {
  20. ctx.render(html! {
  21. <div>
  22. <h1> "bruh 1" </h1>
  23. <h1> "bruh 2" </h1>
  24. </div>
  25. })
  26. };
  27. fn Top(ctx: Context, a: &str, b: &i32, c: &impl Fn()) -> DomTree {
  28. ctx.render(html! {
  29. <div>
  30. <h1> "bruh 1" </h1>
  31. <h1> "bruh 2" </h1>
  32. </div>
  33. })
  34. }
  35. struct Callback<T>(Box<T>);
  36. // impl<O, T: Fn() -> O> From<T> for Callback<T> {
  37. // fn from(_: T) -> Self {
  38. // todo!()
  39. // }
  40. // }
  41. impl<O, A> From<&dyn Fn(A) -> O> for Callback<&dyn Fn(A) -> O> {
  42. fn from(_: &dyn Fn(A) -> O) -> Self {
  43. todo!()
  44. }
  45. }
  46. impl<O, A, B> From<&dyn Fn(A, B) -> O> for Callback<&dyn Fn(A, B) -> O> {
  47. fn from(_: &dyn Fn(A, B) -> O) -> Self {
  48. todo!()
  49. }
  50. }
  51. // compile time reordering of arguments
  52. // Allows for transparently calling
  53. #[derive(Default)]
  54. pub struct Args<A, B, C> {
  55. pub a: CuOpt<A>,
  56. pub b: CuOpt<B>,
  57. pub c: CuOpt<C>,
  58. }
  59. pub enum CuOpt<T> {
  60. Some(T),
  61. None,
  62. }
  63. impl<T> Default for CuOpt<T> {
  64. fn default() -> Self {
  65. CuOpt::None
  66. }
  67. }
  68. impl<T> CuOpt<T> {
  69. fn unwrap(self) -> T {
  70. match self {
  71. CuOpt::Some(t) => t,
  72. CuOpt::None => panic!(""),
  73. }
  74. }
  75. }
  76. trait IsMemo {
  77. fn memo(&self, other: &Self) -> bool {
  78. false
  79. }
  80. }
  81. impl<T: PartialEq> IsMemo for CuOpt<T> {
  82. fn memo(&self, other: &Self) -> bool {
  83. self == other
  84. }
  85. }
  86. impl<T: PartialEq> PartialEq for CuOpt<T> {
  87. fn eq(&self, other: &Self) -> bool {
  88. match (self, other) {
  89. (CuOpt::Some(a), CuOpt::Some(b)) => a == b,
  90. (CuOpt::Some(_), CuOpt::None) => false,
  91. (CuOpt::None, CuOpt::Some(_)) => false,
  92. (CuOpt::None, CuOpt::None) => true,
  93. }
  94. }
  95. }
  96. impl<T> IsMemo for &CuOpt<T> {
  97. fn memo(&self, other: &Self) -> bool {
  98. false
  99. }
  100. }
  101. // #[test]
  102. #[test]
  103. fn try_test() {
  104. // test_poc()
  105. }
  106. fn test_poc(ctx: Context) {
  107. let b = Bump::new();
  108. let h = Args {
  109. a: CuOpt::Some("ASD"),
  110. b: CuOpt::Some(123),
  111. c: CuOpt::Some(|| {}),
  112. // c: CuOpt::Some(b.alloc(|| {})),
  113. // c: CuOpt::Some(Box::new(|| {}) as Box<dyn Fn()>),
  114. };
  115. let h2 = Args {
  116. a: CuOpt::Some("ASD"),
  117. b: CuOpt::Some(123),
  118. c: CuOpt::Some(|| {}),
  119. // c: CuOpt::Some(b.alloc(|| {})),
  120. // c: CuOpt::Some(Box::new(|| {}) as Box<dyn Fn()>),
  121. // c: CuOpt::Some(Box::new(|| {}) as Box<dyn Fn()>),
  122. // c: CuOpt::Some(Box::new(|| {}) as Box<dyn Fn()>),
  123. };
  124. // dbg!((&h.a).memo((&&h2.a)));
  125. // dbg!((&h.b).memo((&&h2.b)));
  126. // dbg!((&h.c).memo((&&h2.c)));
  127. //
  128. // ctx: Context
  129. Top(ctx, &h.a.unwrap(), &h.b.unwrap(), &h.c.unwrap());
  130. }
  131. fn test_realzies() {
  132. let h = Args {
  133. a: CuOpt::Some("ASD"),
  134. b: CuOpt::Some(123),
  135. c: CuOpt::Some(|| {}),
  136. };
  137. let g = |ctx: Context| {
  138. //
  139. Top(ctx, &h.a.unwrap(), &h.b.unwrap(), &h.c.unwrap())
  140. };
  141. }