2021-02-03 07:26:04 +00:00
|
|
|
//! Virtual Node Support
|
|
|
|
//!
|
2021-09-01 04:57:04 +00:00
|
|
|
//! VNodes represent lazily-constructed VDom trees that support diffing and event handlers. These VNodes should be *very*
|
|
|
|
//! cheap and *very* fast to construct - building a full tree should be quick.
|
|
|
|
|
2021-11-01 06:41:23 +00:00
|
|
|
use crate::{
|
2021-11-09 07:16:25 +00:00
|
|
|
innerlude::{Context, Element, Properties, Scope, ScopeId},
|
2021-11-01 06:41:23 +00:00
|
|
|
lazynodes::LazyNodes,
|
2021-03-11 00:42:10 +00:00
|
|
|
};
|
2021-07-29 22:04:09 +00:00
|
|
|
use bumpalo::{boxed::Box as BumpBox, Bump};
|
2021-06-03 14:42:28 +00:00
|
|
|
use std::{
|
2021-11-03 23:55:02 +00:00
|
|
|
any::Any,
|
2021-07-24 04:29:23 +00:00
|
|
|
cell::{Cell, RefCell},
|
2021-06-23 05:44:48 +00:00
|
|
|
fmt::{Arguments, Debug, Formatter},
|
2021-06-03 14:42:28 +00:00
|
|
|
};
|
2021-02-21 02:59:16 +00:00
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// A composable "VirtualNode" to declare a User Interface in the Dioxus VirtualDOM.
|
2021-02-03 07:26:04 +00:00
|
|
|
///
|
2021-10-24 17:30:36 +00:00
|
|
|
/// VNodes are designed to be lightweight and used with with a bump allocator. To create a VNode, you can use either of:
|
2021-11-09 19:36:26 +00:00
|
|
|
///
|
2021-09-01 04:57:04 +00:00
|
|
|
/// - the [`rsx`] macro
|
|
|
|
/// - the [`NodeFactory`] API
|
2021-08-20 14:34:41 +00:00
|
|
|
pub enum VNode<'src> {
|
2021-09-01 04:57:04 +00:00
|
|
|
/// Text VNodes simply bump-allocated (or static) string slices
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2021-09-10 00:58:48 +00:00
|
|
|
/// ```
|
2021-11-09 19:36:26 +00:00
|
|
|
/// let mut vdom = VirtualDom::new();
|
|
|
|
/// let node = vdom.render_vnode(rsx!( "hello" ));
|
2021-09-01 04:57:04 +00:00
|
|
|
///
|
|
|
|
/// if let VNode::Text(vtext) = node {
|
|
|
|
/// assert_eq!(vtext.text, "hello");
|
|
|
|
/// assert_eq!(vtext.dom_id.get(), None);
|
|
|
|
/// assert_eq!(vtext.is_static, true);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-10-31 00:28:58 +00:00
|
|
|
Text(&'src VText<'src>),
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// Element VNodes are VNodes that may contain attributes, listeners, a key, a tag, and children.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2021-11-09 19:36:26 +00:00
|
|
|
/// let mut vdom = VirtualDom::new();
|
|
|
|
///
|
|
|
|
/// let node = vdom.render_vnode(rsx!{
|
2021-09-01 04:57:04 +00:00
|
|
|
/// div {
|
|
|
|
/// key: "a",
|
|
|
|
/// onclick: |e| log::info!("clicked"),
|
|
|
|
/// hidden: "true",
|
|
|
|
/// style: { background_color: "red" }
|
|
|
|
/// "hello"
|
|
|
|
/// }
|
2021-11-09 19:36:26 +00:00
|
|
|
/// });
|
|
|
|
///
|
2021-09-01 04:57:04 +00:00
|
|
|
/// if let VNode::Element(velement) = node {
|
|
|
|
/// assert_eq!(velement.tag_name, "div");
|
|
|
|
/// assert_eq!(velement.namespace, None);
|
|
|
|
/// assert_eq!(velement.key, Some("a));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-07-12 22:19:27 +00:00
|
|
|
Element(&'src VElement<'src>),
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// Fragment nodes may contain many VNodes without a single root.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// rsx!{
|
|
|
|
/// a {}
|
|
|
|
/// link {}
|
|
|
|
/// style {}
|
|
|
|
/// "asd"
|
|
|
|
/// Example {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-07-12 22:19:27 +00:00
|
|
|
Fragment(VFragment<'src>),
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// Component nodes represent a mounted component with props, children, and a key.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2021-11-09 19:36:26 +00:00
|
|
|
/// fn Example(cx: Context, props: &()) -> Element {
|
2021-09-01 04:57:04 +00:00
|
|
|
/// todo!()
|
|
|
|
/// }
|
|
|
|
///
|
2021-11-09 19:36:26 +00:00
|
|
|
/// let mut vdom = VirtualDom::new();
|
|
|
|
///
|
|
|
|
/// let node = vdom.render_vnode(rsx!( Example {} ));
|
2021-09-01 04:57:04 +00:00
|
|
|
///
|
|
|
|
/// if let VNode::Component(vcomp) = node {
|
|
|
|
/// assert_eq!(vcomp.user_fc, Example as *const ());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-06-01 22:33:15 +00:00
|
|
|
Component(&'src VComponent<'src>),
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// Suspended VNodes represent chunks of the UI tree that are not yet ready to be displayed.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2021-11-09 19:36:26 +00:00
|
|
|
///
|
|
|
|
///
|
2021-09-01 04:57:04 +00:00
|
|
|
/// ```
|
2021-08-25 20:40:18 +00:00
|
|
|
Suspended(&'src VSuspended<'src>),
|
2021-07-29 22:04:09 +00:00
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// Anchors are a type of placeholder VNode used when fragments don't contain any children.
|
|
|
|
///
|
|
|
|
/// Anchors cannot be directly constructed via public APIs.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2021-11-09 19:36:26 +00:00
|
|
|
/// let mut vdom = VirtualDom::new();
|
|
|
|
///
|
|
|
|
/// let node = vdom.render_vnode(rsx!( Fragment {} ));
|
|
|
|
///
|
2021-09-01 04:57:04 +00:00
|
|
|
/// if let VNode::Fragment(frag) = node {
|
|
|
|
/// let root = &frag.children[0];
|
|
|
|
/// assert_eq!(root, VNode::Anchor);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-10-31 00:28:58 +00:00
|
|
|
Anchor(&'src VAnchor),
|
2021-11-07 03:11:17 +00:00
|
|
|
|
2021-11-09 19:36:26 +00:00
|
|
|
/// A VNode that is actually a pointer to some nodes rather than the nodes directly. Useful when rendering portals
|
|
|
|
/// or eliding lifetimes on VNodes through runtime checks.
|
|
|
|
///
|
|
|
|
/// Linked VNodes can only be made through the [`Context::render`] method
|
|
|
|
///
|
|
|
|
/// Typically, linked nodes are found *not* in a VNode. When NodeLinks are in a VNode, the NodeLink was passed into
|
|
|
|
/// an `rsx!` call.
|
2021-11-07 03:11:17 +00:00
|
|
|
///
|
2021-11-09 19:36:26 +00:00
|
|
|
/// # Example
|
|
|
|
/// ```rust
|
|
|
|
/// let mut vdom = VirtualDom::new();
|
2021-11-07 03:11:17 +00:00
|
|
|
///
|
2021-11-09 19:36:26 +00:00
|
|
|
/// let node: NodeLink = vdom.render_vnode(rsx!( "hello" ));
|
|
|
|
/// ```
|
2021-11-07 03:11:17 +00:00
|
|
|
Linked(NodeLink),
|
2021-07-29 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 14:34:41 +00:00
|
|
|
impl<'src> VNode<'src> {
|
2021-09-01 04:57:04 +00:00
|
|
|
/// Get the VNode's "key" used in the keyed diffing algorithm.
|
2021-08-20 14:34:41 +00:00
|
|
|
pub fn key(&self) -> Option<&'src str> {
|
|
|
|
match &self {
|
2021-08-21 17:24:47 +00:00
|
|
|
VNode::Element(el) => el.key,
|
|
|
|
VNode::Component(c) => c.key,
|
|
|
|
VNode::Fragment(f) => f.key,
|
2021-11-07 03:11:17 +00:00
|
|
|
|
2021-08-21 17:24:47 +00:00
|
|
|
VNode::Text(_t) => None,
|
2021-08-27 13:53:26 +00:00
|
|
|
VNode::Suspended(_s) => None,
|
|
|
|
VNode::Anchor(_f) => None,
|
2021-11-07 03:11:17 +00:00
|
|
|
VNode::Linked(_c) => None,
|
2021-08-20 14:34:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-01 04:57:04 +00:00
|
|
|
|
|
|
|
/// Get the ElementID of the mounted VNode.
|
|
|
|
///
|
|
|
|
/// Panics if the mounted ID is None or if the VNode is not represented by a single Element.
|
|
|
|
pub fn mounted_id(&self) -> ElementId {
|
|
|
|
self.try_mounted_id().unwrap()
|
2021-08-20 14:34:41 +00:00
|
|
|
}
|
2021-09-01 04:57:04 +00:00
|
|
|
|
|
|
|
/// Try to get the ElementID of the mounted VNode.
|
|
|
|
///
|
|
|
|
/// Returns None if the VNode is not mounted, or if the VNode cannot be presented by a mounted ID (Fragment/Component)
|
|
|
|
pub fn try_mounted_id(&self) -> Option<ElementId> {
|
2021-08-20 14:34:41 +00:00
|
|
|
match &self {
|
|
|
|
VNode::Text(el) => el.dom_id.get(),
|
|
|
|
VNode::Element(el) => el.dom_id.get(),
|
|
|
|
VNode::Anchor(el) => el.dom_id.get(),
|
2021-09-01 04:57:04 +00:00
|
|
|
VNode::Suspended(el) => el.dom_id.get(),
|
2021-11-11 16:49:07 +00:00
|
|
|
|
2021-11-07 03:11:17 +00:00
|
|
|
VNode::Linked(_) => None,
|
2021-08-20 14:34:41 +00:00
|
|
|
VNode::Fragment(_) => None,
|
|
|
|
VNode::Component(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 07:29:50 +00:00
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
pub(crate) fn children(&self) -> &[VNode<'src>] {
|
2021-11-05 20:28:08 +00:00
|
|
|
match &self {
|
2021-11-07 14:58:19 +00:00
|
|
|
VNode::Fragment(f) => f.children,
|
2021-11-10 22:09:52 +00:00
|
|
|
VNode::Component(_c) => todo!("children are not accessible through this"),
|
2021-11-05 20:28:08 +00:00
|
|
|
_ => &[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 07:29:50 +00:00
|
|
|
// Create an "owned" version of the vnode.
|
|
|
|
pub fn decouple(&self) -> VNode<'src> {
|
|
|
|
match self {
|
|
|
|
VNode::Text(t) => VNode::Text(*t),
|
|
|
|
VNode::Element(e) => VNode::Element(*e),
|
|
|
|
VNode::Component(c) => VNode::Component(*c),
|
|
|
|
VNode::Suspended(s) => VNode::Suspended(*s),
|
|
|
|
VNode::Anchor(a) => VNode::Anchor(*a),
|
|
|
|
VNode::Fragment(f) => VNode::Fragment(VFragment {
|
|
|
|
children: f.children,
|
|
|
|
key: f.key,
|
|
|
|
}),
|
2021-11-07 03:11:17 +00:00
|
|
|
VNode::Linked(c) => VNode::Linked(NodeLink {
|
2021-11-10 22:09:52 +00:00
|
|
|
scope_id: c.scope_id.clone(),
|
|
|
|
link_idx: c.link_idx.clone(),
|
|
|
|
node: c.node.clone(),
|
2021-11-07 03:11:17 +00:00
|
|
|
}),
|
2021-11-01 07:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-20 14:34:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 07:35:26 +00:00
|
|
|
impl Debug for VNode<'_> {
|
|
|
|
fn fmt(&self, s: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
|
|
match &self {
|
|
|
|
VNode::Element(el) => s
|
2021-11-08 03:36:57 +00:00
|
|
|
.debug_struct("VNode::VElement")
|
2021-11-01 07:35:26 +00:00
|
|
|
.field("name", &el.tag_name)
|
|
|
|
.field("key", &el.key)
|
|
|
|
.finish(),
|
|
|
|
|
2021-11-08 03:36:57 +00:00
|
|
|
VNode::Text(t) => write!(s, "VNode::VText {{ text: {} }}", t.text),
|
|
|
|
VNode::Anchor(_) => write!(s, "VNode::VAnchor"),
|
2021-11-01 07:35:26 +00:00
|
|
|
|
2021-11-08 03:36:57 +00:00
|
|
|
VNode::Fragment(frag) => {
|
|
|
|
write!(s, "VNode::VFragment {{ children: {:?} }}", frag.children)
|
|
|
|
}
|
|
|
|
VNode::Suspended { .. } => write!(s, "VNode::VSuspended"),
|
|
|
|
VNode::Component(comp) => write!(s, "VNode::VComponent {{ fc: {:?}}}", comp.user_fc),
|
2021-11-10 22:09:52 +00:00
|
|
|
VNode::Linked(c) => write!(s, "VNode::VCached {{ scope_id: {:?} }}", c.scope_id.get()),
|
2021-11-01 07:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 07:16:25 +00:00
|
|
|
/// An Element's unique identifier.
|
|
|
|
///
|
|
|
|
/// `ElementId` is a `usize` that is unique across the entire VirtualDOM - but not unique across time. If a component is
|
|
|
|
/// unmounted, then the `ElementId` will be reused for a new component.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub struct ElementId(pub usize);
|
|
|
|
impl std::fmt::Display for ElementId {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ElementId {
|
|
|
|
pub fn as_u64(self) -> u64 {
|
|
|
|
self.0 as u64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty_cell() -> Cell<Option<ElementId>> {
|
|
|
|
Cell::new(None)
|
|
|
|
}
|
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// A placeholder node only generated when Fragments don't have any children.
|
2021-07-29 22:04:09 +00:00
|
|
|
pub struct VAnchor {
|
|
|
|
pub dom_id: Cell<Option<ElementId>>,
|
2021-06-01 22:33:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 17:30:36 +00:00
|
|
|
/// A bump-allocated string slice and metadata.
|
2021-06-20 05:52:32 +00:00
|
|
|
pub struct VText<'src> {
|
|
|
|
pub text: &'src str,
|
2021-08-27 13:53:26 +00:00
|
|
|
|
2021-07-29 22:04:09 +00:00
|
|
|
pub dom_id: Cell<Option<ElementId>>,
|
2021-08-27 13:53:26 +00:00
|
|
|
|
2021-07-11 18:49:52 +00:00
|
|
|
pub is_static: bool,
|
2021-06-20 05:52:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// A list of VNodes with no single root.
|
2021-07-12 22:19:27 +00:00
|
|
|
pub struct VFragment<'src> {
|
2021-08-20 14:34:41 +00:00
|
|
|
pub key: Option<&'src str>,
|
2021-08-27 13:53:26 +00:00
|
|
|
pub children: &'src [VNode<'src>],
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
2021-06-26 01:15:33 +00:00
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// An element like a "div" with children, listeners, and attributes.
|
2021-08-27 13:53:26 +00:00
|
|
|
pub struct VElement<'a> {
|
|
|
|
pub tag_name: &'static str,
|
|
|
|
|
|
|
|
pub namespace: Option<&'static str>,
|
|
|
|
|
|
|
|
pub key: Option<&'a str>,
|
|
|
|
|
|
|
|
pub dom_id: Cell<Option<ElementId>>,
|
|
|
|
|
2021-09-20 16:32:21 +00:00
|
|
|
pub parent_id: Cell<Option<ElementId>>,
|
|
|
|
|
2021-08-27 13:53:26 +00:00
|
|
|
pub listeners: &'a [Listener<'a>],
|
|
|
|
|
|
|
|
pub attributes: &'a [Attribute<'a>],
|
|
|
|
|
|
|
|
pub children: &'a [VNode<'a>],
|
|
|
|
}
|
|
|
|
|
2021-09-24 04:05:56 +00:00
|
|
|
impl Debug for VElement<'_> {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("VElement")
|
|
|
|
.field("tag_name", &self.tag_name)
|
|
|
|
.field("namespace", &self.namespace)
|
|
|
|
.field("key", &self.key)
|
|
|
|
.field("dom_id", &self.dom_id)
|
|
|
|
.field("parent_id", &self.parent_id)
|
|
|
|
.field("listeners", &self.listeners.len())
|
|
|
|
.field("attributes", &self.attributes)
|
|
|
|
.field("children", &self.children)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// A trait for any generic Dioxus Element.
|
|
|
|
///
|
|
|
|
/// This trait provides the ability to use custom elements in the `rsx!` macro.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct my_element;
|
|
|
|
///
|
|
|
|
/// impl DioxusElement for my_element {
|
|
|
|
/// const TAG_NAME: "my_element";
|
|
|
|
/// const NAME_SPACE: None;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let _ = rsx!{
|
|
|
|
/// my_element {}
|
|
|
|
/// };
|
|
|
|
/// ```
|
2021-07-12 22:19:27 +00:00
|
|
|
pub trait DioxusElement {
|
|
|
|
const TAG_NAME: &'static str;
|
|
|
|
const NAME_SPACE: Option<&'static str>;
|
2021-07-13 04:56:39 +00:00
|
|
|
#[inline]
|
2021-07-12 22:19:27 +00:00
|
|
|
fn tag_name(&self) -> &'static str {
|
|
|
|
Self::TAG_NAME
|
|
|
|
}
|
2021-07-13 04:56:39 +00:00
|
|
|
#[inline]
|
|
|
|
fn namespace(&self) -> Option<&'static str> {
|
|
|
|
Self::NAME_SPACE
|
|
|
|
}
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-03-11 00:42:10 +00:00
|
|
|
/// An attribute on a DOM node, such as `id="my-thing"` or
|
|
|
|
/// `href="https://example.com"`.
|
2021-03-12 21:58:30 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2021-03-11 00:42:10 +00:00
|
|
|
pub struct Attribute<'a> {
|
|
|
|
pub name: &'static str,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-03-11 00:42:10 +00:00
|
|
|
pub value: &'a str,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-11 18:49:52 +00:00
|
|
|
pub is_static: bool,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
pub is_volatile: bool,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
// Doesn't exist in the html spec.
|
|
|
|
// Used in Dioxus to denote "style" tags.
|
2021-07-07 03:04:33 +00:00
|
|
|
pub namespace: Option<&'static str>,
|
2021-03-11 00:42:10 +00:00
|
|
|
}
|
2021-02-03 07:26:04 +00:00
|
|
|
|
2021-03-11 00:42:10 +00:00
|
|
|
/// An event listener.
|
2021-07-12 22:19:27 +00:00
|
|
|
/// IE onclick, onkeydown, etc
|
2021-03-11 00:42:10 +00:00
|
|
|
pub struct Listener<'bump> {
|
2021-09-13 16:42:38 +00:00
|
|
|
/// The ID of the node that this listener is mounted to
|
|
|
|
/// Used to generate the event listener's ID on the DOM
|
2021-07-23 14:27:43 +00:00
|
|
|
pub mounted_node: Cell<Option<ElementId>>,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// The type of event to listen for.
|
|
|
|
///
|
2021-09-10 00:58:48 +00:00
|
|
|
/// IE "click" - whatever the renderer needs to attach the listener by name.
|
2021-09-01 04:57:04 +00:00
|
|
|
pub event: &'static str,
|
|
|
|
|
2021-09-13 16:42:38 +00:00
|
|
|
/// The actual callback that the user specified
|
2021-11-03 23:55:02 +00:00
|
|
|
pub(crate) callback: RefCell<Option<BumpBox<'bump, dyn FnMut(Box<dyn Any + Send>) + 'bump>>>,
|
2021-03-11 00:42:10 +00:00
|
|
|
}
|
2021-02-03 07:26:04 +00:00
|
|
|
|
2021-11-08 01:59:09 +00:00
|
|
|
pub type VCompCaller<'src> = BumpBox<'src, dyn Fn(Context) -> Element + 'src>;
|
2021-07-12 22:19:27 +00:00
|
|
|
/// Virtual Components for custom user-defined components
|
|
|
|
/// Only supports the functional syntax
|
|
|
|
pub struct VComponent<'src> {
|
2021-08-20 14:34:41 +00:00
|
|
|
pub key: Option<&'src str>,
|
|
|
|
|
2021-11-07 00:59:46 +00:00
|
|
|
pub associated_scope: Cell<Option<ScopeId>>,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
// Function pointer to the FC that was used to generate this component
|
|
|
|
pub user_fc: *const (),
|
2021-11-08 01:59:09 +00:00
|
|
|
|
2021-08-27 13:53:26 +00:00
|
|
|
pub(crate) can_memoize: bool,
|
2021-08-08 19:15:16 +00:00
|
|
|
|
2021-11-08 01:59:09 +00:00
|
|
|
pub(crate) hard_allocation: Cell<Option<*const ()>>,
|
|
|
|
|
2021-08-27 13:53:26 +00:00
|
|
|
// Raw pointer into the bump arena for the props of the component
|
2021-11-08 01:59:09 +00:00
|
|
|
pub(crate) bump_props: *const (),
|
2021-11-05 20:28:08 +00:00
|
|
|
|
|
|
|
// during the "teardown" process we'll take the caller out so it can be dropped properly
|
2021-11-09 07:11:44 +00:00
|
|
|
// pub(crate) caller: Option<VCompCaller<'src>>,
|
|
|
|
pub(crate) caller: &'src dyn Fn(&'src Scope) -> Element,
|
|
|
|
|
|
|
|
pub(crate) comparator: Option<&'src dyn Fn(&VComponent) -> bool>,
|
|
|
|
|
|
|
|
pub(crate) drop_props: RefCell<Option<BumpBox<'src, dyn FnMut()>>>,
|
2021-11-05 20:28:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 20:40:18 +00:00
|
|
|
pub struct VSuspended<'a> {
|
|
|
|
pub task_id: u64,
|
2021-08-27 13:53:26 +00:00
|
|
|
pub dom_id: Cell<Option<ElementId>>,
|
2021-10-18 00:18:30 +00:00
|
|
|
|
|
|
|
#[allow(clippy::type_complexity)]
|
2021-11-07 01:07:01 +00:00
|
|
|
pub callback: RefCell<Option<BumpBox<'a, dyn FnMut() -> Element + 'a>>>,
|
2021-07-29 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 19:36:26 +00:00
|
|
|
/// A cached node is a "pointer" to a "rendered" node in a particular scope
|
|
|
|
///
|
|
|
|
/// It does not provide direct access to the node, so it doesn't carry any lifetime information with it
|
|
|
|
///
|
|
|
|
/// It is used during the diffing/rendering process as a runtime key into an existing set of nodes. The "render" key
|
|
|
|
/// is essentially a unique key to guarantee safe usage of the Node.
|
|
|
|
///
|
|
|
|
/// Linked VNodes can only be made through the [`Context::render`] method
|
|
|
|
///
|
|
|
|
/// Typically, NodeLinks are found *not* in a VNode. When NodeLinks are in a VNode, the NodeLink was passed into
|
|
|
|
/// an `rsx!` call.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NodeLink {
|
2021-11-10 22:09:52 +00:00
|
|
|
pub(crate) link_idx: Cell<usize>,
|
|
|
|
pub(crate) scope_id: Cell<Option<ScopeId>>,
|
|
|
|
pub(crate) node: *const VNode<'static>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for NodeLink {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.node == other.node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl NodeLink {
|
|
|
|
// we don't want to let users clone NodeLinks
|
|
|
|
pub(crate) fn clone_inner(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
link_idx: self.link_idx.clone(),
|
|
|
|
scope_id: self.scope_id.clone(),
|
|
|
|
node: self.node.clone(),
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 19:36:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
/// This struct provides an ergonomic API to quickly build VNodes.
|
|
|
|
///
|
|
|
|
/// NodeFactory is used to build VNodes in the component's memory space.
|
|
|
|
/// This struct adds metadata to the final VNode about listeners, attributes, and children
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct NodeFactory<'a> {
|
2021-07-29 22:04:09 +00:00
|
|
|
pub(crate) bump: &'a Bump,
|
2021-03-11 00:42:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
impl<'a> NodeFactory<'a> {
|
2021-07-29 22:04:09 +00:00
|
|
|
pub fn new(bump: &'a Bump) -> NodeFactory<'a> {
|
|
|
|
NodeFactory { bump }
|
|
|
|
}
|
|
|
|
|
2021-03-11 00:42:10 +00:00
|
|
|
#[inline]
|
2021-07-12 22:19:27 +00:00
|
|
|
pub fn bump(&self) -> &'a bumpalo::Bump {
|
2021-11-10 22:09:52 +00:00
|
|
|
&self.bump
|
2021-07-29 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 04:57:04 +00:00
|
|
|
/// Directly pass in text blocks without the need to use the format_args macro.
|
2021-07-23 14:27:43 +00:00
|
|
|
pub fn static_text(&self, text: &'static str) -> VNode<'a> {
|
2021-10-31 00:28:58 +00:00
|
|
|
VNode::Text(self.bump.alloc(VText {
|
2021-08-20 14:34:41 +00:00
|
|
|
dom_id: empty_cell(),
|
|
|
|
text,
|
|
|
|
is_static: true,
|
2021-10-31 00:28:58 +00:00
|
|
|
}))
|
2021-02-03 07:26:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 08:09:28 +00:00
|
|
|
/// Parses a lazy text Arguments and returns a string and a flag indicating if the text is 'static
|
|
|
|
///
|
|
|
|
/// Text that's static may be pointer compared, making it cheaper to diff
|
2021-07-12 22:19:27 +00:00
|
|
|
pub fn raw_text(&self, args: Arguments) -> (&'a str, bool) {
|
|
|
|
match args.as_str() {
|
|
|
|
Some(static_str) => (static_str, true),
|
|
|
|
None => {
|
|
|
|
use bumpalo::core_alloc::fmt::Write;
|
2021-11-10 22:09:52 +00:00
|
|
|
let mut str_buf = bumpalo::collections::String::new_in(self.bump);
|
2021-09-01 04:57:04 +00:00
|
|
|
str_buf.write_fmt(args).unwrap();
|
|
|
|
(str_buf.into_bump_str(), false)
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 00:42:10 +00:00
|
|
|
}
|
2021-07-02 05:30:52 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
/// Create some text that's allocated along with the other vnodes
|
2021-07-15 08:09:28 +00:00
|
|
|
///
|
2021-07-12 22:19:27 +00:00
|
|
|
pub fn text(&self, args: Arguments) -> VNode<'a> {
|
|
|
|
let (text, is_static) = self.raw_text(args);
|
2021-08-20 14:34:41 +00:00
|
|
|
|
2021-10-31 00:28:58 +00:00
|
|
|
VNode::Text(self.bump.alloc(VText {
|
2021-08-20 14:34:41 +00:00
|
|
|
text,
|
|
|
|
is_static,
|
|
|
|
dom_id: empty_cell(),
|
2021-10-31 00:28:58 +00:00
|
|
|
}))
|
2021-07-02 05:30:52 +00:00
|
|
|
}
|
2021-03-11 00:42:10 +00:00
|
|
|
|
2021-07-24 04:29:23 +00:00
|
|
|
pub fn element<L, A, V>(
|
2021-07-12 22:19:27 +00:00
|
|
|
&self,
|
2021-07-15 08:09:28 +00:00
|
|
|
el: impl DioxusElement,
|
2021-07-24 04:29:23 +00:00
|
|
|
listeners: L,
|
|
|
|
attributes: A,
|
|
|
|
children: V,
|
2021-07-30 14:35:47 +00:00
|
|
|
key: Option<Arguments>,
|
2021-07-24 04:29:23 +00:00
|
|
|
) -> VNode<'a>
|
|
|
|
where
|
|
|
|
L: 'a + AsRef<[Listener<'a>]>,
|
|
|
|
A: 'a + AsRef<[Attribute<'a>]>,
|
|
|
|
V: 'a + AsRef<[VNode<'a>]>,
|
|
|
|
{
|
2021-07-15 08:09:28 +00:00
|
|
|
self.raw_element(
|
|
|
|
el.tag_name(),
|
|
|
|
el.namespace(),
|
|
|
|
listeners,
|
|
|
|
attributes,
|
|
|
|
children,
|
|
|
|
key,
|
|
|
|
)
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
2021-06-06 03:47:54 +00:00
|
|
|
|
2021-07-24 04:29:23 +00:00
|
|
|
pub fn raw_element<L, A, V>(
|
2021-07-13 04:56:39 +00:00
|
|
|
&self,
|
2021-09-01 04:57:04 +00:00
|
|
|
tag_name: &'static str,
|
2021-07-15 08:09:28 +00:00
|
|
|
namespace: Option<&'static str>,
|
2021-07-24 04:29:23 +00:00
|
|
|
listeners: L,
|
|
|
|
attributes: A,
|
|
|
|
children: V,
|
2021-07-30 14:35:47 +00:00
|
|
|
key: Option<Arguments>,
|
2021-07-24 04:29:23 +00:00
|
|
|
) -> VNode<'a>
|
|
|
|
where
|
|
|
|
L: 'a + AsRef<[Listener<'a>]>,
|
|
|
|
A: 'a + AsRef<[Attribute<'a>]>,
|
|
|
|
V: 'a + AsRef<[VNode<'a>]>,
|
|
|
|
{
|
2021-11-10 22:09:52 +00:00
|
|
|
let listeners: &'a L = self.bump.alloc(listeners);
|
2021-07-24 04:29:23 +00:00
|
|
|
let listeners = listeners.as_ref();
|
|
|
|
|
2021-11-10 22:09:52 +00:00
|
|
|
let attributes: &'a A = self.bump.alloc(attributes);
|
2021-07-24 04:29:23 +00:00
|
|
|
let attributes = attributes.as_ref();
|
|
|
|
|
2021-11-10 22:09:52 +00:00
|
|
|
let children: &'a V = self.bump.alloc(children);
|
2021-07-24 04:29:23 +00:00
|
|
|
let children = children.as_ref();
|
|
|
|
|
2021-07-30 14:35:47 +00:00
|
|
|
let key = key.map(|f| self.raw_text(f).0);
|
|
|
|
|
2021-11-10 22:09:52 +00:00
|
|
|
VNode::Element(self.bump.alloc(VElement {
|
2021-09-01 04:57:04 +00:00
|
|
|
tag_name,
|
2021-07-13 04:56:39 +00:00
|
|
|
key,
|
2021-08-20 14:34:41 +00:00
|
|
|
namespace,
|
|
|
|
listeners,
|
|
|
|
attributes,
|
|
|
|
children,
|
|
|
|
dom_id: empty_cell(),
|
2021-09-20 16:32:21 +00:00
|
|
|
parent_id: empty_cell(),
|
2021-08-20 14:34:41 +00:00
|
|
|
}))
|
2021-07-13 04:56:39 +00:00
|
|
|
}
|
2021-07-11 18:49:52 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
pub fn attr(
|
|
|
|
&self,
|
|
|
|
name: &'static str,
|
|
|
|
val: Arguments,
|
|
|
|
namespace: Option<&'static str>,
|
|
|
|
is_volatile: bool,
|
|
|
|
) -> Attribute<'a> {
|
|
|
|
let (value, is_static) = self.raw_text(val);
|
|
|
|
Attribute {
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
is_static,
|
|
|
|
namespace,
|
|
|
|
is_volatile,
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 05:29:46 +00:00
|
|
|
|
2021-11-07 03:11:17 +00:00
|
|
|
pub fn component<P>(
|
2021-07-12 22:19:27 +00:00
|
|
|
&self,
|
2021-11-08 01:59:09 +00:00
|
|
|
component: fn(Context<'a>, &'a P) -> Element,
|
2021-06-02 15:07:30 +00:00
|
|
|
props: P,
|
2021-07-30 14:35:47 +00:00
|
|
|
key: Option<Arguments>,
|
2021-07-12 22:19:27 +00:00
|
|
|
) -> VNode<'a>
|
|
|
|
where
|
|
|
|
P: Properties + 'a,
|
|
|
|
{
|
2021-11-10 22:09:52 +00:00
|
|
|
let bump = self.bump;
|
2021-11-08 03:45:41 +00:00
|
|
|
let props = bump.alloc(props);
|
|
|
|
let bump_props = props as *mut P as *mut ();
|
|
|
|
let user_fc = component as *const ();
|
2021-11-05 20:28:08 +00:00
|
|
|
|
2021-11-08 03:45:41 +00:00
|
|
|
let comparator: &mut dyn Fn(&VComponent) -> bool = bump.alloc_with(|| {
|
|
|
|
move |other: &VComponent| {
|
|
|
|
if user_fc == other.user_fc {
|
|
|
|
// Safety
|
|
|
|
// - We guarantee that FC<P> is the same by function pointer
|
|
|
|
// - Because FC<P> is the same, then P must be the same (even with generics)
|
|
|
|
// - Non-static P are autoderived to memoize as false
|
|
|
|
// - This comparator is only called on a corresponding set of bumpframes
|
|
|
|
let props_memoized = unsafe {
|
|
|
|
let real_other: &P = &*(other.bump_props as *const _ as *const P);
|
|
|
|
props.memoize(real_other)
|
|
|
|
};
|
|
|
|
|
|
|
|
// It's only okay to memoize if there are no children and the props can be memoized
|
|
|
|
// Implementing memoize is unsafe and done automatically with the props trait
|
|
|
|
props_memoized
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-11-05 20:28:08 +00:00
|
|
|
|
2021-11-08 03:45:41 +00:00
|
|
|
let drop_props = {
|
|
|
|
// create a closure to drop the props
|
|
|
|
let mut has_dropped = false;
|
|
|
|
|
|
|
|
let drop_props: &mut dyn FnMut() = bump.alloc_with(|| {
|
|
|
|
move || unsafe {
|
|
|
|
if !has_dropped {
|
|
|
|
let real_other = bump_props as *mut _ as *mut P;
|
|
|
|
let b = BumpBox::from_raw(real_other);
|
|
|
|
std::mem::drop(b);
|
2021-07-12 22:19:27 +00:00
|
|
|
|
2021-11-08 03:45:41 +00:00
|
|
|
has_dropped = true;
|
|
|
|
} else {
|
|
|
|
panic!("Drop props called twice - this is an internal failure of Dioxus");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let drop_props = unsafe { BumpBox::from_raw(drop_props) };
|
|
|
|
|
|
|
|
RefCell::new(Some(drop_props))
|
|
|
|
};
|
2021-07-27 15:28:05 +00:00
|
|
|
|
2021-07-30 14:35:47 +00:00
|
|
|
let key = key.map(|f| self.raw_text(f).0);
|
|
|
|
|
2021-11-09 07:11:44 +00:00
|
|
|
let caller: &'a mut dyn Fn(&'a Scope) -> Element =
|
2021-11-08 03:45:41 +00:00
|
|
|
bump.alloc(move |scope: &Scope| -> Element {
|
|
|
|
let props: &'_ P = unsafe { &*(bump_props as *const P) };
|
|
|
|
let res = component(scope, props);
|
2021-11-09 07:11:44 +00:00
|
|
|
res
|
2021-11-08 03:45:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let can_memoize = P::IS_STATIC;
|
|
|
|
|
|
|
|
VNode::Component(bump.alloc(VComponent {
|
|
|
|
user_fc,
|
2021-11-09 07:11:44 +00:00
|
|
|
comparator: Some(comparator),
|
2021-11-08 03:45:41 +00:00
|
|
|
bump_props,
|
|
|
|
caller,
|
|
|
|
key,
|
|
|
|
can_memoize,
|
|
|
|
drop_props,
|
|
|
|
associated_scope: Cell::new(None),
|
2021-11-09 07:11:44 +00:00
|
|
|
hard_allocation: Cell::new(None),
|
2021-11-08 03:45:41 +00:00
|
|
|
}))
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
2021-06-01 22:33:15 +00:00
|
|
|
|
2021-11-07 03:11:17 +00:00
|
|
|
pub fn listener(
|
|
|
|
self,
|
|
|
|
event: &'static str,
|
|
|
|
callback: BumpBox<'a, dyn FnMut(Box<dyn Any + Send>) + 'a>,
|
|
|
|
) -> Listener<'a> {
|
|
|
|
Listener {
|
|
|
|
mounted_node: Cell::new(None),
|
|
|
|
event,
|
|
|
|
callback: RefCell::new(Some(callback)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 22:23:28 +00:00
|
|
|
pub fn fragment_from_iter(
|
|
|
|
self,
|
|
|
|
node_iter: impl IntoIterator<Item = impl IntoVNode<'a>>,
|
|
|
|
) -> VNode<'a> {
|
2021-11-10 22:09:52 +00:00
|
|
|
let bump = self.bump;
|
2021-10-30 22:23:28 +00:00
|
|
|
let mut nodes = bumpalo::collections::Vec::new_in(bump);
|
|
|
|
|
|
|
|
for node in node_iter {
|
|
|
|
nodes.push(node.into_vnode(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodes.is_empty() {
|
2021-10-31 00:28:58 +00:00
|
|
|
nodes.push(VNode::Anchor(bump.alloc(VAnchor {
|
2021-10-30 22:23:28 +00:00
|
|
|
dom_id: empty_cell(),
|
2021-10-31 00:28:58 +00:00
|
|
|
})));
|
2021-10-30 22:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let children = nodes.into_bump_slice();
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-08-22 21:08:25 +00:00
|
|
|
// TODO
|
|
|
|
// We need a dedicated path in the rsx! macro that will trigger the "you need keys" warning
|
|
|
|
//
|
|
|
|
// if cfg!(debug_assertions) {
|
|
|
|
// if children.len() > 1 {
|
|
|
|
// if children.last().unwrap().key().is_none() {
|
|
|
|
// log::error!(
|
|
|
|
// r#"
|
|
|
|
// Warning: Each child in an array or iterator should have a unique "key" prop.
|
|
|
|
// Not providing a key will lead to poor performance with lists.
|
|
|
|
// See docs.rs/dioxus for more information.
|
|
|
|
// ---
|
|
|
|
// To help you identify where this error is coming from, we've generated a backtrace.
|
|
|
|
// "#,
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2021-08-20 14:34:41 +00:00
|
|
|
VNode::Fragment(VFragment {
|
|
|
|
children,
|
2021-07-12 22:19:27 +00:00
|
|
|
key: None,
|
2021-08-20 14:34:41 +00:00
|
|
|
})
|
2021-02-03 07:26:04 +00:00
|
|
|
}
|
2021-10-30 22:23:28 +00:00
|
|
|
|
2021-11-10 22:09:52 +00:00
|
|
|
// this isn't quite feasible yet
|
|
|
|
// I think we need some form of interior mutability or state on nodefactory that stores which subtree was created
|
|
|
|
pub fn create_children(
|
|
|
|
self,
|
|
|
|
node_iter: impl IntoIterator<Item = impl IntoVNode<'a>>,
|
|
|
|
) -> Element {
|
|
|
|
let bump = self.bump;
|
|
|
|
let mut nodes = bumpalo::collections::Vec::new_in(bump);
|
|
|
|
|
|
|
|
for node in node_iter {
|
|
|
|
nodes.push(node.into_vnode(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodes.is_empty() {
|
|
|
|
nodes.push(VNode::Anchor(bump.alloc(VAnchor {
|
|
|
|
dom_id: empty_cell(),
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
|
|
|
let children = nodes.into_bump_slice();
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// We need a dedicated path in the rsx! macro that will trigger the "you need keys" warning
|
|
|
|
//
|
|
|
|
// if cfg!(debug_assertions) {
|
|
|
|
// if children.len() > 1 {
|
|
|
|
// if children.last().unwrap().key().is_none() {
|
|
|
|
// log::error!(
|
|
|
|
// r#"
|
|
|
|
// Warning: Each child in an array or iterator should have a unique "key" prop.
|
|
|
|
// Not providing a key will lead to poor performance with lists.
|
|
|
|
// See docs.rs/dioxus for more information.
|
|
|
|
// ---
|
|
|
|
// To help you identify where this error is coming from, we've generated a backtrace.
|
|
|
|
// "#,
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
let frag = VNode::Fragment(VFragment {
|
|
|
|
children,
|
|
|
|
key: None,
|
|
|
|
});
|
|
|
|
let ptr = self.bump.alloc(frag) as *const _;
|
|
|
|
Some(NodeLink {
|
|
|
|
link_idx: Default::default(),
|
|
|
|
scope_id: Default::default(),
|
|
|
|
node: unsafe { std::mem::transmute(ptr) },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-07 06:49:53 +00:00
|
|
|
pub fn annotate_lazy<'z, 'b>(
|
|
|
|
f: impl FnOnce(NodeFactory<'z>) -> VNode<'z> + 'b,
|
|
|
|
) -> Option<LazyNodes<'z, 'b>> {
|
2021-11-01 06:41:23 +00:00
|
|
|
Some(LazyNodes::new(f))
|
2021-10-30 22:23:28 +00:00
|
|
|
}
|
2021-02-03 07:26:04 +00:00
|
|
|
}
|
2021-06-03 14:42:28 +00:00
|
|
|
|
2021-11-01 07:35:26 +00:00
|
|
|
impl Debug for NodeFactory<'_> {
|
|
|
|
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
/// Trait implementations for use in the rsx! and html! macros.
|
|
|
|
///
|
|
|
|
/// ## Details
|
|
|
|
///
|
|
|
|
/// This section provides convenience methods and trait implementations for converting common structs into a format accepted
|
|
|
|
/// by the macros.
|
|
|
|
///
|
|
|
|
/// All dynamic content in the macros must flow in through `fragment_from_iter`. Everything else must be statically layed out.
|
|
|
|
/// We pipe basically everything through `fragment_from_iter`, so we expect a very specific type:
|
|
|
|
/// ```
|
|
|
|
/// impl IntoIterator<Item = impl IntoVNode<'a>>
|
|
|
|
/// ```
|
|
|
|
///
|
2021-10-24 17:30:36 +00:00
|
|
|
/// As such, all node creation must go through the factory, which is only available in the component context.
|
2021-07-15 07:38:09 +00:00
|
|
|
/// These strict requirements make it possible to manage lifetimes and state.
|
2021-10-30 22:23:28 +00:00
|
|
|
pub trait IntoVNode<'a> {
|
|
|
|
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a>;
|
2021-07-26 16:14:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
// For the case where a rendered VNode is passed into the rsx! macro through curly braces
|
2021-07-12 22:19:27 +00:00
|
|
|
impl<'a> IntoIterator for VNode<'a> {
|
|
|
|
type Item = VNode<'a>;
|
|
|
|
type IntoIter = std::iter::Once<Self::Item>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
std::iter::once(self)
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 07:38:09 +00:00
|
|
|
|
2021-10-30 22:23:28 +00:00
|
|
|
// TODO: do we even need this? It almost seems better not to
|
2021-10-30 21:11:15 +00:00
|
|
|
// // For the case where a rendered VNode is passed into the rsx! macro through curly braces
|
2021-11-01 06:22:08 +00:00
|
|
|
impl<'a> IntoVNode<'a> for VNode<'a> {
|
|
|
|
fn into_vnode(self, _: NodeFactory<'a>) -> VNode<'a> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 22:19:27 +00:00
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
// Conveniently, we also support "null" (nothing) passed in
|
2021-11-01 06:22:08 +00:00
|
|
|
impl IntoVNode<'_> for () {
|
|
|
|
fn into_vnode(self, cx: NodeFactory) -> VNode {
|
|
|
|
cx.fragment_from_iter(None as Option<VNode>)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conveniently, we also support "None"
|
|
|
|
impl IntoVNode<'_> for Option<()> {
|
|
|
|
fn into_vnode(self, cx: NodeFactory) -> VNode {
|
|
|
|
cx.fragment_from_iter(None as Option<VNode>)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoVNode<'a> for Option<VNode<'a>> {
|
|
|
|
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
|
|
|
self.unwrap_or_else(|| cx.fragment_from_iter(None as Option<VNode>))
|
|
|
|
}
|
|
|
|
}
|
2021-10-30 21:11:15 +00:00
|
|
|
|
2021-11-01 06:41:23 +00:00
|
|
|
impl<'a> IntoVNode<'a> for Option<LazyNodes<'a, '_>> {
|
2021-10-30 22:23:28 +00:00
|
|
|
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
2021-07-20 23:03:49 +00:00
|
|
|
match self {
|
2021-11-01 06:41:23 +00:00
|
|
|
Some(lazy) => lazy.call(cx),
|
2021-10-30 21:11:15 +00:00
|
|
|
None => VNode::Fragment(VFragment {
|
|
|
|
children: &[],
|
|
|
|
key: None,
|
|
|
|
}),
|
2021-07-20 23:03:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 22:19:27 +00:00
|
|
|
|
2021-11-01 06:41:23 +00:00
|
|
|
impl<'a> IntoVNode<'a> for LazyNodes<'a, '_> {
|
2021-10-30 22:23:28 +00:00
|
|
|
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
2021-11-01 06:41:23 +00:00
|
|
|
self.call(cx)
|
2021-10-29 21:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 22:23:28 +00:00
|
|
|
impl IntoVNode<'_> for &'static str {
|
2021-09-21 22:13:09 +00:00
|
|
|
fn into_vnode(self, cx: NodeFactory) -> VNode {
|
2021-07-23 14:27:43 +00:00
|
|
|
cx.static_text(self)
|
2021-07-21 21:05:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 06:22:08 +00:00
|
|
|
|
2021-10-30 22:23:28 +00:00
|
|
|
impl IntoVNode<'_> for Arguments<'_> {
|
2021-09-21 22:13:09 +00:00
|
|
|
fn into_vnode(self, cx: NodeFactory) -> VNode {
|
2021-07-21 21:05:48 +00:00
|
|
|
cx.text(self)
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 07:49:32 +00:00
|
|
|
|
2021-11-10 22:09:52 +00:00
|
|
|
// called cx.render from a helper function
|
2021-11-09 19:36:26 +00:00
|
|
|
impl IntoVNode<'_> for Option<NodeLink> {
|
2021-11-10 22:09:52 +00:00
|
|
|
fn into_vnode(self, _cx: NodeFactory) -> VNode {
|
|
|
|
match self {
|
|
|
|
Some(node) => VNode::Linked(node),
|
|
|
|
None => {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 17:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 22:09:52 +00:00
|
|
|
// essentially passing elements through props
|
|
|
|
// just build a new element in place
|
2021-11-09 19:36:26 +00:00
|
|
|
impl IntoVNode<'_> for &Option<NodeLink> {
|
2021-11-10 22:09:52 +00:00
|
|
|
fn into_vnode(self, _cx: NodeFactory) -> VNode {
|
|
|
|
match self {
|
|
|
|
Some(node) => VNode::Linked(NodeLink {
|
|
|
|
link_idx: node.link_idx.clone(),
|
|
|
|
scope_id: node.scope_id.clone(),
|
|
|
|
node: node.node,
|
|
|
|
}),
|
|
|
|
None => {
|
|
|
|
//
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 19:13:50 +00:00
|
|
|
}
|
2021-11-01 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 19:36:26 +00:00
|
|
|
impl IntoVNode<'_> for NodeLink {
|
2021-11-10 22:09:52 +00:00
|
|
|
fn into_vnode(self, _cx: NodeFactory) -> VNode {
|
|
|
|
VNode::Linked(self)
|
2021-11-01 07:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 19:36:26 +00:00
|
|
|
impl IntoVNode<'_> for &NodeLink {
|
2021-11-10 22:09:52 +00:00
|
|
|
fn into_vnode(self, _cx: NodeFactory) -> VNode {
|
|
|
|
VNode::Linked(NodeLink {
|
|
|
|
link_idx: self.link_idx.clone(),
|
|
|
|
scope_id: self.scope_id.clone(),
|
|
|
|
node: self.node,
|
|
|
|
})
|
2021-11-01 07:49:32 +00:00
|
|
|
}
|
|
|
|
}
|