contextapi.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. use std::{marker::PhantomData, ops::Deref};
  2. use builder::{button, div};
  3. use dioxus_core::prelude::*;
  4. fn main() {}
  5. struct SomeContext {
  6. items: Vec<String>,
  7. }
  8. /*
  9. desired behavior:
  10. free to move the context guard around
  11. not free to move contents of context guard into closure
  12. rules:
  13. can deref in a function
  14. cannot drag the refs into the closure w
  15. */
  16. static Example: FC<()> = |ctx| {
  17. let value = use_context(&ctx, |ctx: &SomeContext| ctx.items.last().unwrap());
  18. // let b = *value;
  19. // let v2 = *value;
  20. let cb = move |e| {
  21. // let g = b.as_str();
  22. // let g = (v2).as_str();
  23. let g = (value).as_str();
  24. // let g = b.as_str();
  25. };
  26. // let r = *value;
  27. // let r2 = *r;
  28. ctx.view(|bump| {
  29. button(bump)
  30. .listeners([builder::on(bump, "click", cb)])
  31. .finish()
  32. })
  33. // ctx.view(html! {
  34. // <div>
  35. // <button onclick={move |_| println!("Value is {}", value)} />
  36. // <button onclick={move |_| println!("Value is {}", value)} />
  37. // <button onclick={move |_| println!("Value is {}", value)} />
  38. // <div>
  39. // <p> "Value is: {val}" </p>
  40. // </div>
  41. // </div>
  42. // })
  43. };
  44. #[derive(Clone, Copy)]
  45. struct ContextGuard<T> {
  46. val: PhantomData<T>,
  47. }
  48. impl<'a, T> Deref for ContextGuard<T> {
  49. type Target = T;
  50. fn deref(&self) -> &Self::Target {
  51. todo!()
  52. }
  53. }
  54. fn use_context<'scope, 'dope, 'a, P: Properties, I, O: 'a>(
  55. ctx: &'scope Context<P>,
  56. s: fn(&'a I) -> O,
  57. ) -> &'scope ContextGuard<O> {
  58. // ) -> &'scope ContextGuard<O> {
  59. todo!()
  60. }