properties.rs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. use crate::innerlude::*;
  2. /// Every "Props" used for a component must implement the `Properties` trait. This trait gives some hints to Dioxus
  3. /// on how to memoize the props and some additional optimizations that can be made. We strongly encourage using the
  4. /// derive macro to implement the `Properties` trait automatically as guarantee that your memoization strategy is safe.
  5. ///
  6. /// If your props are 'static, then Dioxus will require that they also be PartialEq for the derived memoize strategy. However,
  7. /// if your props borrow data, then the memoization strategy will simply default to "false" and the PartialEq will be ignored.
  8. /// This tends to be useful when props borrow something that simply cannot be compared (IE a reference to a closure);
  9. ///
  10. /// By default, the memoization strategy is very conservative, but can be tuned to be more aggressive manually. However,
  11. /// this is only safe if the props are 'static - otherwise you might borrow references after-free.
  12. ///
  13. /// We strongly suggest that any changes to memoization be done at the "PartialEq" level for 'static props. Additionally,
  14. /// we advise the use of smart pointers in cases where memoization is important.
  15. ///
  16. /// ## Example
  17. ///
  18. /// For props that are 'static:
  19. /// ```rust, ignore
  20. /// #[derive(Props, PartialEq)]
  21. /// struct MyProps {
  22. /// data: String
  23. /// }
  24. /// ```
  25. ///
  26. /// For props that borrow:
  27. ///
  28. /// ```rust, ignore
  29. /// #[derive(Props)]
  30. /// struct MyProps<'a >{
  31. /// data: &'a str
  32. /// }
  33. /// ```
  34. pub trait Properties: Sized {
  35. /// The type of the builder for this component.
  36. /// Used to create "in-progress" versions of the props.
  37. type Builder;
  38. /// An indication if these props are can be memoized automatically.
  39. const IS_STATIC: bool;
  40. /// Create a builder for this component.
  41. fn builder() -> Self::Builder;
  42. /// Memoization can only happen if the props are valid for the 'static lifetime
  43. ///
  44. /// # Safety
  45. /// The user must know if their props are static, but if they make a mistake, UB happens
  46. /// Therefore it's unsafe to memoize.
  47. unsafe fn memoize(&self, other: &Self) -> bool;
  48. }
  49. impl Properties for () {
  50. type Builder = EmptyBuilder;
  51. const IS_STATIC: bool = true;
  52. fn builder() -> Self::Builder {
  53. EmptyBuilder {}
  54. }
  55. unsafe fn memoize(&self, _other: &Self) -> bool {
  56. true
  57. }
  58. }
  59. // We allow components to use the () generic parameter if they have no props. This impl enables the "build" method
  60. // that the macros use to anonymously complete prop construction.
  61. pub struct EmptyBuilder;
  62. impl EmptyBuilder {
  63. pub fn build(self) {}
  64. }
  65. /// This utility function launches the builder method so rsx! and html! macros can use the typed-builder pattern
  66. /// to initialize a component's props.
  67. pub fn fc_to_builder<'a, T: Properties + 'a>(_: fn(Scope<'a, T>) -> Element<'a>) -> T::Builder {
  68. T::builder()
  69. }
  70. #[cfg(not(miri))]
  71. #[test]
  72. fn unsafe_props_fail() {
  73. let t = trybuild::TestCases::new();
  74. t.compile_fail("compile_tests/props_safety.rs");
  75. }