lib.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #![allow(non_snake_case)]
  2. #![doc = include_str!("../README.md")]
  3. #![warn(missing_docs)]
  4. pub(crate) mod diff;
  5. pub(crate) mod events;
  6. pub(crate) mod lazynodes;
  7. pub(crate) mod mutations;
  8. pub(crate) mod nodes;
  9. pub(crate) mod properties;
  10. pub(crate) mod scopes;
  11. pub(crate) mod virtual_dom;
  12. pub(crate) mod innerlude {
  13. pub use crate::events::*;
  14. pub use crate::lazynodes::*;
  15. pub use crate::mutations::*;
  16. pub use crate::nodes::*;
  17. pub use crate::properties::*;
  18. pub use crate::scopes::*;
  19. pub use crate::virtual_dom::*;
  20. /// An [`Element`] is a possibly-none [`VNode`] created by calling `render` on [`Scope`] or [`ScopeState`].
  21. ///
  22. /// Any [`None`] [`Element`] will automatically be coerced into a placeholder [`VNode`] with the [`VNode::Placeholder`] variant.
  23. pub type Element<'a> = Option<VNode<'a>>;
  24. /// A [`Component`] is a function that takes a [`Scope`] and returns an [`Element`].
  25. ///
  26. /// Components can be used in other components with two syntax options:
  27. /// - lowercase as a function call with named arguments (rust style)
  28. /// - uppercase as an element (react style)
  29. ///
  30. /// ## Rust-Style
  31. ///
  32. /// ```rust, ignore
  33. /// fn example(cx: Scope<Props>) -> Element {
  34. /// // ...
  35. /// }
  36. ///
  37. /// rsx!(
  38. /// example()
  39. /// )
  40. /// ```
  41. /// ## React-Style
  42. /// ```rust, ignore
  43. /// fn Example(cx: Scope<Props>) -> Element {
  44. /// // ...
  45. /// }
  46. ///
  47. /// rsx!(
  48. /// Example {}
  49. /// )
  50. /// ```
  51. pub type Component<P = ()> = fn(Scope<P>) -> Element;
  52. /// A list of attributes
  53. pub type Attributes<'a> = Option<&'a [Attribute<'a>]>;
  54. }
  55. pub use crate::innerlude::{
  56. AnyAttributeValue, AnyEvent, Attribute, AttributeValue, Component, Element, ElementId,
  57. EventHandler, EventPriority, IntoVNode, LazyNodes, Listener, NodeFactory, Properties, Renderer,
  58. SchedulerMsg, Scope, ScopeId, ScopeState, TaskId, Template, TemplateAttribute, TemplateNode,
  59. UiEvent, UserEvent, VComponent, VElement, VNode, VTemplate, VText, VirtualDom,
  60. };
  61. /// The purpose of this module is to alleviate imports of many common types
  62. ///
  63. /// This includes types like [`Scope`], [`Element`], and [`Component`].
  64. pub mod prelude {
  65. pub use crate::innerlude::{
  66. fc_to_builder, Attributes, Component, Element, EventHandler, LazyNodes, NodeFactory,
  67. Properties, Scope, ScopeId, ScopeState, Template, VNode, VirtualDom,
  68. };
  69. }
  70. pub mod exports {
  71. //! Important dependencies that are used by the rest of the library
  72. //! Feel free to just add the dependencies in your own Crates.toml
  73. pub use bumpalo;
  74. pub use futures_channel;
  75. }
  76. /// Functions that wrap unsafe functionality to prevent us from misusing it at the callsite
  77. pub(crate) mod unsafe_utils {
  78. use crate::VNode;
  79. pub(crate) unsafe fn extend_vnode<'a, 'b>(node: &'a VNode<'a>) -> &'b VNode<'b> {
  80. std::mem::transmute(node)
  81. }
  82. }
  83. #[macro_export]
  84. /// A helper macro for using hooks in async environements.
  85. ///
  86. /// # Usage
  87. ///
  88. ///
  89. /// ```ignore
  90. /// let (data) = use_ref(&cx, || {});
  91. ///
  92. /// let handle_thing = move |_| {
  93. /// to_owned![data]
  94. /// cx.spawn(async move {
  95. /// // do stuff
  96. /// });
  97. /// }
  98. /// ```
  99. macro_rules! to_owned {
  100. ($($es:ident),+) => {$(
  101. #[allow(unused_mut)]
  102. let mut $es = $es.to_owned();
  103. )*}
  104. }
  105. /// get the code location of the code that called this function
  106. #[macro_export]
  107. macro_rules! get_line_num {
  108. () => {
  109. concat!(
  110. file!(),
  111. ":",
  112. line!(),
  113. ":",
  114. column!(),
  115. ":",
  116. env!("CARGO_MANIFEST_DIR")
  117. )
  118. };
  119. }