lib.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #![doc = include_str!("../README.md")]
  2. #![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
  3. #![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
  4. #![cfg_attr(feature = "nightly-features", feature(debug_refcell))]
  5. #[macro_export]
  6. /// A helper macro for using hooks and properties in async environments.
  7. ///
  8. /// # Usage
  9. ///
  10. ///
  11. /// ```
  12. /// # use dioxus::prelude::*;
  13. /// #
  14. /// # #[derive(Props, PartialEq)]
  15. /// # struct Props {
  16. /// # prop: String,
  17. /// # }
  18. /// # fn Component(cx: Scope<Props>) -> Element {
  19. ///
  20. /// let (data) = use_signal(|| {});
  21. ///
  22. /// let handle_thing = move |_| {
  23. /// to_owned![data, cx.props.prop];
  24. /// cx.spawn(async move {
  25. /// // do stuff
  26. /// });
  27. /// };
  28. /// # handle_thing(());
  29. /// # None }
  30. /// ```
  31. macro_rules! to_owned {
  32. // Rule matching simple symbols without a path
  33. ($es:ident $(, $($rest:tt)*)?) => {
  34. #[allow(unused_mut)]
  35. let mut $es = $es.to_owned();
  36. $( to_owned![$($rest)*] )?
  37. };
  38. // We need to find the last element in a path, for this we need to unstack the path part by
  39. // part using, separating what we have with a '@'
  40. ($($deref:ident).* $(, $($rest:tt)*)?) => {
  41. to_owned![@ $($deref).* $(, $($rest)*)?]
  42. };
  43. // Take the head of the path and add it to the list of $deref
  44. ($($deref:ident)* @ $head:ident $( . $tail:ident)+ $(, $($rest:tt)*)?) => {
  45. to_owned![$($deref)* $head @ $($tail).+ $(, $($rest)*)?]
  46. };
  47. // We have exhausted the path, use the last as a name
  48. ($($deref:ident)* @ $last:ident $(, $($rest:tt)*)? ) => {
  49. #[allow(unused_mut)]
  50. let mut $last = $($deref .)* $last .to_owned();
  51. $(to_owned![$($rest)*])?
  52. };
  53. }
  54. mod dependency;
  55. pub use dependency::*;
  56. mod use_callback;
  57. pub use use_callback::*;
  58. mod use_on_destroy;
  59. pub use use_on_destroy::*;
  60. mod use_context;
  61. pub use use_context::*;
  62. mod use_coroutine;
  63. pub use use_coroutine::*;
  64. mod use_future;
  65. pub use use_future::*;
  66. // mod use_sorted;
  67. // pub use use_sorted::*;
  68. mod use_resource;
  69. pub use use_resource::*;
  70. mod use_effect;
  71. pub use use_effect::*;
  72. mod use_memo;
  73. pub use use_memo::*;
  74. // mod use_on_create;
  75. // pub use use_on_create::*;
  76. mod use_root_context;
  77. pub use use_root_context::*;
  78. mod use_hook_did_run;
  79. pub use use_hook_did_run::*;
  80. mod use_signal;
  81. pub use use_signal::*;