lib.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 use_on_destroy;
  55. pub use use_on_destroy::*;
  56. mod use_context;
  57. pub use use_context::*;
  58. mod use_coroutine;
  59. pub use use_coroutine::*;
  60. mod use_future;
  61. pub use use_future::*;
  62. // mod use_on_create;
  63. // pub use use_on_create::*;
  64. mod use_root_context;
  65. pub use use_root_context::*;