dummy.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // #![allow(unused, non_upper_case_globals)]
  2. // use bumpalo::Bump;
  3. // use dioxus_core::nodebuilder::*;
  4. // use dioxus_core::prelude::VNode;
  5. // use dioxus_core::prelude::*;
  6. // use once_cell::sync::{Lazy, OnceCell};
  7. use std::ops::Deref;
  8. /*
  9. A guard over underlying T that provides access in callbacks via "Copy"
  10. */
  11. // #[derive(Clone)]
  12. struct ContextGuard2<T> {
  13. _val: std::marker::PhantomData<T>,
  14. }
  15. impl<T> Clone for ContextGuard2<T> {
  16. // we aren't cloning the underlying data so clone isn't necessary
  17. fn clone(&self) -> Self {
  18. todo!()
  19. }
  20. }
  21. impl<T> Copy for ContextGuard2<T> {}
  22. impl<T> ContextGuard2<T> {
  23. fn get<'a>(&'a self) -> ContextLock<'a, T> {
  24. todo!()
  25. }
  26. }
  27. struct ContextLock<'a, T> {
  28. _val: std::marker::PhantomData<&'a T>,
  29. }
  30. impl<'a, T: 'a + 'static> Deref for ContextLock<'a, T> {
  31. type Target = T;
  32. fn deref<'b>(&'b self) -> &'b T {
  33. todo!()
  34. }
  35. }
  36. /*
  37. The source of the data that gives out context guards
  38. */
  39. struct Context<'a> {
  40. _p: std::marker::PhantomData<&'a ()>,
  41. }
  42. impl<'a> Context<'a> {
  43. fn use_context<'b, I, O: 'b>(&self, _f: fn(&'b I) -> O) -> ContextGuard2<O> {
  44. todo!()
  45. }
  46. fn add_listener(&self, _f: impl Fn(()) + 'a) {
  47. todo!()
  48. }
  49. fn render(self, _f: impl FnOnce(&'a String) + 'a) {}
  50. // fn view(self, f: impl for<'b> FnOnce(&'a String) + 'a) {}
  51. // fn view(self, f: impl for<'b> FnOnce(&'b String) + 'a) {}
  52. }
  53. struct Example {
  54. value: String,
  55. }
  56. /*
  57. Example compiling
  58. */
  59. fn t<'a>(ctx: Context<'a>) {
  60. let value = ctx.use_context(|b: &Example| &b.value);
  61. // Works properly, value is moved by copy into the closure
  62. let refed = value.get();
  63. println!("Value is {}", refed.as_str());
  64. let r2 = refed.as_str();
  65. ctx.add_listener(move |_| {
  66. // let val = value.get().as_str();
  67. let _val2 = r2.as_bytes();
  68. println!("v2 is {}", r2);
  69. // println!("refed is {}", refed);
  70. });
  71. // let refed = value.deref();
  72. // returns &String
  73. // returns &String
  74. // let refed = value.deref(); // returns &String
  75. // let refed = value.deref(); // returns &String
  76. // Why does this work? This closure should be static but is holding a reference to refed
  77. // The context guard is meant to prevent any references moving into the closure
  78. // if the references move they might become invalid due to mutlithreading issues
  79. ctx.add_listener(move |_| {
  80. // let val = value.as_str();
  81. // let val2 = refed.as_bytes();
  82. });
  83. ctx.render(move |_b| {});
  84. }
  85. fn main() {}