borrowed.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //! Demonstrate that borrowed data is possible as a property type
  2. //! Borrowing (rather than cloning) is very important for speed and ergonomics.
  3. //!
  4. //! It's slightly more advanced than just cloning, but well worth the investment.
  5. //!
  6. //! If you use the FC macro, we handle the lifetimes automatically, making it easy to write efficient & performant components.
  7. fn main() {}
  8. use dioxus_core::prelude::*;
  9. use std::rc::Rc;
  10. struct AppProps {
  11. items: Vec<Rc<ListItem>>,
  12. }
  13. #[derive(PartialEq)]
  14. struct ListItem {
  15. name: String,
  16. age: u32,
  17. }
  18. fn app<'a>(cx: Context<'a>, props: &AppProps) -> Element<'a> {
  19. // let (val, set_val) = use_state_classic(cx, || 0);
  20. todo!()
  21. // cx.render(LazyNodes::new(move |_nodecx| {
  22. // todo!()
  23. // // builder::ElementBuilder::new(_nodecx, "div")
  24. // // .iter_child({
  25. // // cx.items.iter().map(|child| {
  26. // // builder::virtual_child(
  27. // // _nodecx,
  28. // // ChildItem,
  29. // // ChildProps {
  30. // // item: child.clone(),
  31. // // item_handler: set_val.clone(),
  32. // // },
  33. // // None,
  34. // // &[],
  35. // // )
  36. // // })
  37. // // })
  38. // // .iter_child([builder::ElementBuilder::new(_nodecx, "div")
  39. // // .iter_child([builder::text3(_nodecx.bump(), format_args!("{}", val))])
  40. // // .finish()])
  41. // // .finish()
  42. // }))
  43. }
  44. // props should derive a partialeq implementation automatically, but implement ptr compare for & fields
  45. struct ChildProps {
  46. // Pass down complex structs
  47. item: Rc<ListItem>,
  48. // Even pass down handlers!
  49. item_handler: Rc<dyn Fn(i32)>,
  50. }
  51. fn ChildItem<'a>(cx: Context<'a>, props: &'a ChildProps) -> Element<'a> {
  52. todo!()
  53. }
  54. impl PartialEq for ChildProps {
  55. fn eq(&self, _other: &Self) -> bool {
  56. false
  57. }
  58. }
  59. impl Properties for ChildProps {
  60. type Builder = ();
  61. const IS_STATIC: bool = true;
  62. fn builder() -> Self::Builder {
  63. ()
  64. }
  65. unsafe fn memoize(&self, other: &Self) -> bool {
  66. self == other
  67. }
  68. }