1
0

dummy.rs 2.5 KB

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