lib.rs 2.0 KB

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