lib.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #[macro_export]
  2. /// A helper macro for using hooks and properties in async environements.
  3. ///
  4. /// # Usage
  5. ///
  6. ///
  7. /// ```
  8. /// # use dioxus::prelude::*;
  9. /// #
  10. /// # #[derive(Props, PartialEq)]
  11. /// # struct Props {
  12. /// # prop: String,
  13. /// # }
  14. /// # fn Component(cx: Scope<Props>) -> Element {
  15. ///
  16. /// let (data) = use_ref(cx, || {});
  17. ///
  18. /// let handle_thing = move |_| {
  19. /// to_owned![data, cx.props.prop];
  20. /// cx.spawn(async move {
  21. /// // do stuff
  22. /// });
  23. /// };
  24. /// # handle_thing(());
  25. /// # None }
  26. /// ```
  27. macro_rules! to_owned {
  28. // Rule matching simple symbols without a path
  29. ($es:ident $(, $($rest:tt)*)?) => {
  30. #[allow(unused_mut)]
  31. let mut $es = $es.to_owned();
  32. $( to_owned![$($rest)*] )?
  33. };
  34. // We need to find the last element in a path, for this we need to unstack the path part by
  35. // part using, separating what we have with a '@'
  36. ($($deref:ident).* $(, $($rest:tt)*)?) => {
  37. to_owned![@ $($deref).* $(, $($rest)*)?]
  38. };
  39. // Take the head of the path and add it to the list of $deref
  40. ($($deref:ident)* @ $head:ident $( . $tail:ident)+ $(, $($rest:tt)*)?) => {
  41. to_owned![$($deref)* $head @ $($tail).+ $(, $($rest)*)?]
  42. };
  43. // We have exhausted the path, use the last as a name
  44. ($($deref:ident)* @ $last:ident $(, $($rest:tt)*)? ) => {
  45. #[allow(unused_mut)]
  46. let mut $last = $($deref .)* $last .to_owned();
  47. $(to_owned![$($rest)*])?
  48. };
  49. }
  50. mod usecontext;
  51. pub use usecontext::*;
  52. mod usestate;
  53. pub use usestate::{use_state, UseState};
  54. mod useref;
  55. pub use useref::*;
  56. mod use_shared_state;
  57. pub use use_shared_state::*;
  58. mod usecoroutine;
  59. pub use usecoroutine::*;
  60. mod usefuture;
  61. pub use usefuture::*;
  62. mod useeffect;
  63. pub use useeffect::*;
  64. mod usecallback;
  65. pub use usecallback::*;
  66. mod usememo;
  67. pub use usememo::*;