lazynodes.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //! Support for storing lazy-nodes on the stack
  2. //!
  3. //! This module provides support for a type called `LazyNodes` which is a micro-heap located on the stack to make calls
  4. //! to `rsx!` more efficient.
  5. //!
  6. //! To support returning rsx! from branches in match statements, we need to use dynamic dispatch on [`ScopeState`] closures.
  7. //!
  8. //! This can be done either through boxing directly, or by using dynamic-sized-types and a custom allocator. In our case,
  9. //! we build a tiny alloactor in the stack and allocate the closure into that.
  10. //!
  11. //! The logic for this was borrowed from <https://docs.rs/stack_dst/0.6.1/stack_dst/>. Unfortunately, this crate does not
  12. //! support non-static closures, so we've implemented the core logic of `ValueA` in this module.
  13. #[allow(unused_imports)]
  14. use smallbox::{smallbox, space::S16, SmallBox};
  15. use crate::{innerlude::VNode, ScopeState};
  16. /// A concrete type provider for closures that build [`VNode`] structures.
  17. ///
  18. /// This struct wraps lazy structs that build [`VNode`] trees Normally, we cannot perform a blanket implementation over
  19. /// closures, but if we wrap the closure in a concrete type, we can use it for different branches in matching.
  20. ///
  21. ///
  22. /// ```rust, ignore
  23. /// LazyNodes::new(|f| f.element("div", [], [], [] None))
  24. /// ```
  25. pub struct LazyNodes<'a, 'b> {
  26. #[cfg(not(miri))]
  27. inner: SmallBox<dyn FnMut(&'a ScopeState) -> VNode<'a> + 'b, S16>,
  28. #[cfg(miri)]
  29. inner: Box<dyn FnMut(&'a ScopeState) -> VNode<'a> + 'b>,
  30. }
  31. impl<'a, 'b> LazyNodes<'a, 'b> {
  32. /// Create a new [`LazyNodes`] closure, optimistically placing it onto the stack.
  33. ///
  34. /// If the closure cannot fit into the stack allocation (16 bytes), then it
  35. /// is placed on the heap. Most closures will fit into the stack, and is
  36. /// the most optimal way to use the creation function.
  37. pub fn new(val: impl FnOnce(&'a ScopeState) -> VNode<'a> + 'b) -> Self {
  38. // there's no way to call FnOnce without a box, so we need to store it in a slot and use static dispatch
  39. let mut slot = Some(val);
  40. Self {
  41. #[cfg(not(miri))]
  42. inner: smallbox!(move |f| {
  43. let val = slot.take().expect("cannot call LazyNodes twice");
  44. val(f)
  45. }),
  46. #[cfg(miri)]
  47. inner: Box::new(move |f| {
  48. let val = slot.take().expect("cannot call LazyNodes twice");
  49. val(f)
  50. }),
  51. }
  52. }
  53. /// Call the closure with the given factory to produce real [`VNode`].
  54. ///
  55. /// ```rust, ignore
  56. /// let f = LazyNodes::new(move |f| f.element("div", [], [], [] None));
  57. ///
  58. /// let node = f.call(cac);
  59. /// ```
  60. #[must_use]
  61. pub fn call(mut self, f: &'a ScopeState) -> VNode<'a> {
  62. (self.inner)(f)
  63. }
  64. }