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::{
|
2022-01-30 19:08:03 +00:00
|
|
|
innerlude::{Element, FcSlot, Properties, Scope, ScopeId, ScopeState},
|
2021-11-01 06:41:23 +00:00
|
|
|
lazynodes::LazyNodes,
|
2022-01-03 07:06:42 +00:00
|
|
|
AnyEvent, Component,
|
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-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-12-25 22:18:05 +00:00
|
|
|
/// - the `rsx!` macro
|
2021-09-01 04:57:04 +00:00
|
|
|
/// - the [`NodeFactory`] API
|
2021-08-20 14:34:41 +00:00
|
|
|
pub enum VNode<'src> {
|
2022-01-22 03:43:43 +00:00
|
|
|
/// Text VNodes are simply bump-allocated (or static) string slices
|
2021-09-01 04:57:04 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2021-11-12 02:34:20 +00:00
|
|
|
/// ```rust, ignore
|
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
|
|
|
|
///
|
2021-11-12 02:34:20 +00:00
|
|
|
/// ```rust, ignore
|
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",
|
2021-12-25 22:18:05 +00:00
|
|
|
/// style: { background_color: "red" },
|
2021-09-01 04:57:04 +00:00
|
|
|
/// "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);
|
2021-12-25 22:18:05 +00:00
|
|
|
/// assert_eq!(velement.key, Some("a"));
|
2021-09-01 04:57:04 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
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
|
|
|
|
///
|
2021-11-12 02:34:20 +00:00
|
|
|
/// ```rust, ignore
|
2021-09-01 04:57:04 +00:00
|
|
|
/// rsx!{
|
|
|
|
/// a {}
|
|
|
|
/// link {}
|
|
|
|
/// style {}
|
|
|
|
/// "asd"
|
|
|
|
/// Example {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
2022-01-04 00:32:27 +00:00
|
|
|
Fragment(&'src 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
|
|
|
|
///
|
2021-11-12 02:34:20 +00:00
|
|
|
/// ```rust, ignore
|
2021-12-30 02:28:28 +00:00
|
|
|
/// fn Example(cx: Scope) -> Element {
|
2021-12-21 03:33:13 +00:00
|
|
|
/// ...
|
2021-09-01 04:57:04 +00:00
|
|
|
/// }
|
|
|
|
///
|
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-11-23 20:53:57 +00:00
|
|
|
/// Placeholders are a type of placeholder VNode used when fragments don't contain any children.
|
2021-09-01 04:57:04 +00:00
|
|
|
///
|
2021-11-23 20:53:57 +00:00
|
|
|
/// Placeholders cannot be directly constructed via public APIs.
|
2021-09-01 04:57:04 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2021-11-12 02:34:20 +00:00
|
|
|
/// ```rust, ignore
|
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-11-23 20:53:57 +00:00
|
|
|
Placeholder(&'src VPlaceholder),
|
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,
|
|
|
|
VNode::Text(_t) => None,
|
2021-11-23 20:53:57 +00:00
|
|
|
VNode::Placeholder(_f) => 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 {
|
2021-12-21 05:46:10 +00:00
|
|
|
VNode::Text(el) => el.id.get(),
|
|
|
|
VNode::Element(el) => el.id.get(),
|
|
|
|
VNode::Placeholder(el) => el.id.get(),
|
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-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> {
|
2022-01-04 00:32:27 +00:00
|
|
|
match *self {
|
|
|
|
VNode::Text(t) => VNode::Text(t),
|
|
|
|
VNode::Element(e) => VNode::Element(e),
|
|
|
|
VNode::Component(c) => VNode::Component(c),
|
|
|
|
VNode::Placeholder(a) => VNode::Placeholder(a),
|
|
|
|
VNode::Fragment(f) => VNode::Fragment(f),
|
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-12-21 05:46:10 +00:00
|
|
|
.field("name", &el.tag)
|
2021-11-01 07:35:26 +00:00
|
|
|
.field("key", &el.key)
|
2021-12-18 20:49:30 +00:00
|
|
|
.field("attrs", &el.attributes)
|
|
|
|
.field("children", &el.children)
|
2022-01-30 22:47:58 +00:00
|
|
|
.field("id", &el.id)
|
2021-11-01 07:35:26 +00:00
|
|
|
.finish(),
|
2021-11-08 03:36:57 +00:00
|
|
|
VNode::Text(t) => write!(s, "VNode::VText {{ text: {} }}", t.text),
|
2022-01-30 19:08:03 +00:00
|
|
|
VNode::Placeholder(t) => write!(s, "VNode::VPlaceholder {{ id: {:?} }}", t.id),
|
2021-11-08 03:36:57 +00:00
|
|
|
VNode::Fragment(frag) => {
|
|
|
|
write!(s, "VNode::VFragment {{ children: {:?} }}", frag.children)
|
|
|
|
}
|
2022-01-31 06:24:11 +00:00
|
|
|
VNode::Component(comp) => s
|
|
|
|
.debug_struct("VNode::VComponent")
|
|
|
|
.field("fnptr", &comp.user_fc)
|
|
|
|
.field("key", &comp.key)
|
|
|
|
.field("scope", &comp.scope)
|
|
|
|
.field("originator", &comp.originator)
|
|
|
|
.finish(),
|
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-11-23 20:53:57 +00:00
|
|
|
pub struct VPlaceholder {
|
2021-12-21 05:46:10 +00:00
|
|
|
pub 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-12-21 05:46:10 +00:00
|
|
|
pub id: Cell<Option<ElementId>>,
|
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-12-18 20:49:30 +00:00
|
|
|
|
|
|
|
/// Fragments can never have zero children. Enforced by NodeFactory.
|
|
|
|
///
|
|
|
|
/// You *can* make a fragment with no children, but it's not a valid fragment and your VDom will panic.
|
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> {
|
2021-12-21 05:46:10 +00:00
|
|
|
pub tag: &'static str,
|
2021-08-27 13:53:26 +00:00
|
|
|
pub namespace: Option<&'static str>,
|
|
|
|
pub key: Option<&'a str>,
|
2021-12-21 05:46:10 +00:00
|
|
|
pub id: Cell<Option<ElementId>>,
|
|
|
|
pub parent: 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")
|
2021-12-21 05:46:10 +00:00
|
|
|
.field("tag_name", &self.tag)
|
2021-09-24 04:05:56 +00:00
|
|
|
.field("namespace", &self.namespace)
|
|
|
|
.field("key", &self.key)
|
2021-12-21 05:46:10 +00:00
|
|
|
.field("id", &self.id)
|
|
|
|
.field("parent", &self.parent)
|
2021-09-24 04:05:56 +00:00
|
|
|
.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.
|
|
|
|
///
|
2021-11-12 02:34:20 +00:00
|
|
|
/// ```rust, ignore
|
2021-09-01 04:57:04 +00:00
|
|
|
/// 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
|
2022-01-07 16:51:25 +00:00
|
|
|
pub(crate) callback: InternalHandler<'bump>,
|
2021-12-10 02:19:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:51:25 +00:00
|
|
|
pub type InternalHandler<'bump> = &'bump RefCell<Option<InternalListenerCallback<'bump>>>;
|
|
|
|
type InternalListenerCallback<'bump> = BumpBox<'bump, dyn FnMut(AnyEvent) + 'bump>;
|
|
|
|
|
|
|
|
type ExternalListenerCallback<'bump, T> = BumpBox<'bump, dyn FnMut(T) + 'bump>;
|
|
|
|
|
2022-01-07 17:12:13 +00:00
|
|
|
/// The callback type generated by the `rsx!` macro when an `on` field is specified for components.
|
|
|
|
///
|
|
|
|
/// This makes it possible to pass `move |evt| {}` style closures into components as property fields.
|
|
|
|
///
|
2022-01-07 17:12:33 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust, ignore
|
2022-01-07 17:12:13 +00:00
|
|
|
///
|
|
|
|
/// rsx!{
|
|
|
|
/// MyComponent { onclick: move |evt| log::info!("clicked"), }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[derive(Props)]
|
|
|
|
/// struct MyProps<'a> {
|
|
|
|
/// onclick: EventHandler<'a, MouseEvent>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn MyComponent(cx: Scope<'a, MyProps<'a>>) -> Element {
|
|
|
|
/// cx.render(rsx!{
|
|
|
|
/// button {
|
|
|
|
/// onclick: move |evt| cx.props.onclick.call(evt),
|
|
|
|
/// }
|
|
|
|
/// })
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// ```
|
2022-01-07 16:51:25 +00:00
|
|
|
pub struct EventHandler<'bump, T = ()> {
|
2022-01-16 01:17:48 +00:00
|
|
|
pub callback: RefCell<Option<ExternalListenerCallback<'bump, T>>>,
|
2021-12-10 02:19:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 15:42:06 +00:00
|
|
|
impl<'a, T> Default for EventHandler<'a, T> {
|
2022-01-29 15:27:40 +00:00
|
|
|
fn default() -> Self {
|
2022-01-30 22:47:58 +00:00
|
|
|
Self { callback: RefCell::new(None) }
|
2022-01-29 15:27:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:51:25 +00:00
|
|
|
impl<T> EventHandler<'_, T> {
|
2022-01-16 20:13:31 +00:00
|
|
|
/// Call this event handler with the appropriate event type
|
2022-01-07 16:51:25 +00:00
|
|
|
pub fn call(&self, event: T) {
|
2021-12-10 02:19:31 +00:00
|
|
|
if let Some(callback) = self.callback.borrow_mut().as_mut() {
|
|
|
|
callback(event);
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 16:51:25 +00:00
|
|
|
|
2022-01-16 20:13:31 +00:00
|
|
|
/// Forcibly drop the internal handler callback, releasing memory
|
2021-12-10 02:19:31 +00:00
|
|
|
pub fn release(&self) {
|
|
|
|
self.callback.replace(None);
|
|
|
|
}
|
|
|
|
}
|
2021-12-13 05:06:17 +00:00
|
|
|
|
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>,
|
2022-01-05 21:34:24 +00:00
|
|
|
pub originator: ScopeId,
|
2021-12-21 03:33:13 +00:00
|
|
|
pub scope: Cell<Option<ScopeId>>,
|
|
|
|
pub can_memoize: bool,
|
2022-01-30 19:08:03 +00:00
|
|
|
pub user_fc: FcSlot,
|
2021-12-21 04:31:33 +00:00
|
|
|
pub props: RefCell<Option<Box<dyn AnyProps + 'src>>>,
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
2021-08-20 14:34:41 +00:00
|
|
|
|
2021-12-21 03:33:13 +00:00
|
|
|
pub(crate) struct VComponentProps<P> {
|
|
|
|
pub render_fn: Component<P>,
|
|
|
|
pub memo: unsafe fn(&P, &P) -> bool,
|
2021-12-21 05:46:10 +00:00
|
|
|
pub props: P,
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-12-21 03:33:13 +00:00
|
|
|
pub trait AnyProps {
|
|
|
|
fn as_ptr(&self) -> *const ();
|
|
|
|
fn render<'a>(&'a self, bump: &'a ScopeState) -> Element<'a>;
|
|
|
|
unsafe fn memoize(&self, other: &dyn AnyProps) -> bool;
|
|
|
|
}
|
2021-08-08 19:15:16 +00:00
|
|
|
|
2021-12-21 03:33:13 +00:00
|
|
|
impl<P> AnyProps for VComponentProps<P> {
|
|
|
|
fn as_ptr(&self) -> *const () {
|
|
|
|
&self.props as *const _ as *const ()
|
|
|
|
}
|
2021-11-08 01:59:09 +00:00
|
|
|
|
2021-12-21 05:46:10 +00:00
|
|
|
// Safety:
|
2022-01-22 03:43:43 +00:00
|
|
|
// this will downcast the other ptr as our swallowed type!
|
2021-12-21 03:33:13 +00:00
|
|
|
// you *must* make this check *before* calling this method
|
|
|
|
// if your functions are not the same, then you will downcast a pointer into a different type (UB)
|
|
|
|
unsafe fn memoize(&self, other: &dyn AnyProps) -> bool {
|
|
|
|
let real_other: &P = &*(other.as_ptr() as *const _ as *const P);
|
|
|
|
let real_us: &P = &*(self.as_ptr() as *const _ as *const P);
|
|
|
|
(self.memo)(real_us, real_other)
|
|
|
|
}
|
2021-11-05 20:28:08 +00:00
|
|
|
|
2021-12-21 03:33:13 +00:00
|
|
|
fn render<'a>(&'a self, scope: &'a ScopeState) -> Element<'a> {
|
2021-12-21 05:46:10 +00:00
|
|
|
let props = unsafe { std::mem::transmute::<&P, &P>(&self.props) };
|
|
|
|
(self.render_fn)(Scope { scope, props })
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
2021-11-05 20:28:08 +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> {
|
2022-01-05 05:27:22 +00:00
|
|
|
pub(crate) scope: &'a ScopeState,
|
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> {
|
2022-01-05 05:27:22 +00:00
|
|
|
pub fn new(scope: &'a ScopeState) -> NodeFactory<'a> {
|
2022-01-30 22:47:58 +00:00
|
|
|
NodeFactory { scope, bump: &scope.wip_frame().bump }
|
2021-07-29 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
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-12 02:34:20 +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> {
|
2022-01-30 22:47:58 +00:00
|
|
|
VNode::Text(
|
|
|
|
self.bump
|
|
|
|
.alloc(VText { id: empty_cell(), text, is_static: true }),
|
|
|
|
)
|
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
|
|
|
|
2022-01-30 22:47:58 +00:00
|
|
|
VNode::Text(self.bump.alloc(VText { text, is_static, id: empty_cell() }))
|
2021-07-02 05:30:52 +00:00
|
|
|
}
|
2021-03-11 00:42:10 +00:00
|
|
|
|
2022-01-03 22:20:22 +00:00
|
|
|
pub fn element(
|
2021-07-12 22:19:27 +00:00
|
|
|
&self,
|
2021-07-15 08:09:28 +00:00
|
|
|
el: impl DioxusElement,
|
2022-01-03 22:20:22 +00:00
|
|
|
listeners: &'a [Listener<'a>],
|
|
|
|
attributes: &'a [Attribute<'a>],
|
|
|
|
children: &'a [VNode<'a>],
|
2021-07-30 14:35:47 +00:00
|
|
|
key: Option<Arguments>,
|
2022-01-03 22:20:22 +00:00
|
|
|
) -> 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
|
|
|
|
2022-01-03 22:20:22 +00:00
|
|
|
pub fn raw_element(
|
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>,
|
2022-01-03 22:20:22 +00:00
|
|
|
listeners: &'a [Listener<'a>],
|
|
|
|
attributes: &'a [Attribute<'a>],
|
|
|
|
children: &'a [VNode<'a>],
|
2021-07-30 14:35:47 +00:00
|
|
|
key: Option<Arguments>,
|
2022-01-03 22:20:22 +00:00
|
|
|
) -> VNode<'a> {
|
2021-07-30 14:35:47 +00:00
|
|
|
let key = key.map(|f| self.raw_text(f).0);
|
|
|
|
|
2022-01-05 05:27:22 +00:00
|
|
|
let mut items = self.scope.items.borrow_mut();
|
|
|
|
for listener in listeners {
|
|
|
|
let long_listener = unsafe { std::mem::transmute(listener) };
|
|
|
|
items.listeners.push(long_listener);
|
|
|
|
}
|
|
|
|
|
2021-11-10 22:09:52 +00:00
|
|
|
VNode::Element(self.bump.alloc(VElement {
|
2021-12-21 05:46:10 +00:00
|
|
|
tag: tag_name,
|
2021-07-13 04:56:39 +00:00
|
|
|
key,
|
2021-08-20 14:34:41 +00:00
|
|
|
namespace,
|
|
|
|
listeners,
|
|
|
|
attributes,
|
|
|
|
children,
|
2021-12-21 05:46:10 +00:00
|
|
|
id: empty_cell(),
|
|
|
|
parent: 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);
|
2022-01-30 22:47:58 +00:00
|
|
|
Attribute { name, value, is_static, namespace, is_volatile }
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
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-12-14 07:27:59 +00:00
|
|
|
component: fn(Scope<'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,
|
|
|
|
{
|
2022-01-05 05:27:22 +00:00
|
|
|
let vcomp = self.bump.alloc(VComponent {
|
2021-12-21 03:33:13 +00:00
|
|
|
key: key.map(|f| self.raw_text(f).0),
|
|
|
|
scope: Default::default(),
|
|
|
|
can_memoize: P::IS_STATIC,
|
2022-01-30 19:08:03 +00:00
|
|
|
user_fc: component as *mut std::os::raw::c_void,
|
2022-01-05 21:34:24 +00:00
|
|
|
originator: self.scope.scope_id(),
|
2021-12-21 04:31:33 +00:00
|
|
|
props: RefCell::new(Some(Box::new(VComponentProps {
|
2021-12-21 03:33:13 +00:00
|
|
|
// local_props: RefCell::new(Some(props)),
|
|
|
|
// heap_props: RefCell::new(None),
|
|
|
|
props,
|
|
|
|
memo: P::memoize, // smuggle the memoization function across borders
|
|
|
|
|
|
|
|
// i'm sorry but I just need to bludgeon the lifetimes into place here
|
|
|
|
// this is safe because we're managing all lifetimes to originate from previous calls
|
|
|
|
// the intricacies of Rust's lifetime system make it difficult to properly express
|
|
|
|
// the transformation from this specific lifetime to the for<'a> lifetime
|
|
|
|
render_fn: unsafe { std::mem::transmute(component) },
|
|
|
|
}))),
|
2022-01-05 05:27:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if !P::IS_STATIC {
|
|
|
|
let vcomp = &*vcomp;
|
|
|
|
let vcomp = unsafe { std::mem::transmute(vcomp) };
|
|
|
|
self.scope.items.borrow_mut().borrowed_props.push(vcomp);
|
|
|
|
}
|
|
|
|
|
|
|
|
VNode::Component(vcomp)
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
2021-06-01 22:33:15 +00:00
|
|
|
|
2022-01-07 16:51:25 +00:00
|
|
|
pub fn listener(self, event: &'static str, callback: InternalHandler<'a>) -> Listener<'a> {
|
2022-01-30 22:47:58 +00:00
|
|
|
Listener { event, mounted_node: Cell::new(None), callback }
|
2021-11-07 03:11:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 18:09:41 +00:00
|
|
|
pub fn fragment_root<'b, 'c>(
|
|
|
|
self,
|
|
|
|
node_iter: impl IntoIterator<Item = impl IntoVNode<'a> + 'c> + 'b,
|
|
|
|
) -> VNode<'a> {
|
2021-11-28 21:25:42 +00:00
|
|
|
let mut nodes = bumpalo::collections::Vec::new_in(self.bump);
|
2021-11-16 18:09:41 +00:00
|
|
|
|
|
|
|
for node in node_iter {
|
|
|
|
nodes.push(node.into_vnode(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodes.is_empty() {
|
2021-12-21 05:46:10 +00:00
|
|
|
VNode::Placeholder(self.bump.alloc(VPlaceholder { id: empty_cell() }))
|
2021-12-18 20:49:30 +00:00
|
|
|
} else {
|
2022-01-30 22:47:58 +00:00
|
|
|
VNode::Fragment(
|
|
|
|
self.bump
|
|
|
|
.alloc(VFragment { children: nodes.into_bump_slice(), key: None }),
|
|
|
|
)
|
2021-11-16 18:09:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 06:25:38 +00:00
|
|
|
pub fn fragment_from_iter<'b, 'c>(
|
2021-10-30 22:23:28 +00:00
|
|
|
self,
|
2021-11-16 06:25:38 +00:00
|
|
|
node_iter: impl IntoIterator<Item = impl IntoVNode<'a> + 'c> + 'b,
|
2021-10-30 22:23:28 +00:00
|
|
|
) -> VNode<'a> {
|
2021-11-28 21:25:42 +00:00
|
|
|
let mut nodes = bumpalo::collections::Vec::new_in(self.bump);
|
2021-10-30 22:23:28 +00:00
|
|
|
|
|
|
|
for node in node_iter {
|
|
|
|
nodes.push(node.into_vnode(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodes.is_empty() {
|
2021-12-21 05:46:10 +00:00
|
|
|
VNode::Placeholder(self.bump.alloc(VPlaceholder { id: empty_cell() }))
|
2021-12-18 20:49:30 +00:00
|
|
|
} else {
|
|
|
|
let children = nodes.into_bump_slice();
|
|
|
|
|
|
|
|
if cfg!(debug_assertions)
|
|
|
|
&& children.len() > 1
|
|
|
|
&& children.last().unwrap().key().is_none()
|
|
|
|
{
|
|
|
|
// todo: make the backtrace prettier or remove it altogether
|
|
|
|
log::error!(
|
|
|
|
r#"
|
2021-11-16 06:25:38 +00:00
|
|
|
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.
|
|
|
|
-------------
|
2021-11-16 18:09:41 +00:00
|
|
|
{:?}
|
2021-11-16 06:25:38 +00:00
|
|
|
"#,
|
2021-12-18 20:49:30 +00:00
|
|
|
backtrace::Backtrace::new()
|
|
|
|
);
|
|
|
|
}
|
2021-08-22 21:08:25 +00:00
|
|
|
|
2022-01-30 22:47:58 +00:00
|
|
|
VNode::Fragment(self.bump.alloc(VFragment { children, key: None }))
|
2021-12-18 20:49:30 +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>>,
|
2021-12-14 07:27:59 +00:00
|
|
|
) -> Element<'a> {
|
2021-12-18 20:49:30 +00:00
|
|
|
let mut nodes = bumpalo::collections::Vec::new_in(self.bump);
|
2021-11-10 22:09:52 +00:00
|
|
|
|
|
|
|
for node in node_iter {
|
|
|
|
nodes.push(node.into_vnode(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodes.is_empty() {
|
2021-12-21 05:46:10 +00:00
|
|
|
Some(VNode::Placeholder(
|
|
|
|
self.bump.alloc(VPlaceholder { id: empty_cell() }),
|
|
|
|
))
|
2021-12-18 20:49:30 +00:00
|
|
|
} else {
|
|
|
|
let children = nodes.into_bump_slice();
|
2021-11-10 22:09:52 +00:00
|
|
|
|
2022-01-30 22:47:58 +00:00
|
|
|
Some(VNode::Fragment(
|
|
|
|
self.bump.alloc(VFragment { children, key: None }),
|
|
|
|
))
|
2021-12-18 20:49:30 +00:00
|
|
|
}
|
2021-11-10 22:09:52 +00:00
|
|
|
}
|
2022-01-07 16:51:25 +00:00
|
|
|
|
|
|
|
pub fn event_handler<T>(self, f: impl FnMut(T) + 'a) -> EventHandler<'a, T> {
|
|
|
|
let handler: &mut dyn FnMut(T) = self.bump.alloc(f);
|
|
|
|
let caller = unsafe { BumpBox::from_raw(handler as *mut dyn FnMut(T)) };
|
2022-01-16 01:17:48 +00:00
|
|
|
let callback = RefCell::new(Some(caller));
|
2022-01-07 16:51:25 +00:00
|
|
|
EventHandler { callback }
|
|
|
|
}
|
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:
|
2021-11-12 02:34:20 +00:00
|
|
|
/// ```rust, ignore
|
2021-07-15 07:38:09 +00:00
|
|
|
/// 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-12-21 05:46:10 +00:00
|
|
|
None => VNode::Placeholder(cx.bump.alloc(VPlaceholder { id: empty_cell() })),
|
2021-07-20 23:03:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 22:19:27 +00:00
|
|
|
|
2021-12-25 22:18:05 +00:00
|
|
|
impl<'a, 'b> IntoIterator for LazyNodes<'a, 'b> {
|
|
|
|
type Item = LazyNodes<'a, 'b>;
|
|
|
|
type IntoIter = std::iter::Once<Self::Item>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
std::iter::once(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 06:25:38 +00:00
|
|
|
impl<'a, 'b> IntoVNode<'a> for LazyNodes<'a, 'b> {
|
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-12-30 08:14:47 +00:00
|
|
|
impl<'b> IntoVNode<'_> for &'b str {
|
2021-09-21 22:13:09 +00:00
|
|
|
fn into_vnode(self, cx: NodeFactory) -> VNode {
|
2021-12-30 08:14:47 +00:00
|
|
|
cx.text(format_args!("{}", self))
|
2021-07-21 21:05:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 06:22:08 +00:00
|
|
|
|
2021-12-30 02:28:28 +00:00
|
|
|
impl IntoVNode<'_> for String {
|
|
|
|
fn into_vnode(self, cx: NodeFactory) -> VNode {
|
|
|
|
cx.text(format_args!("{}", self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-15 03:48:20 +00:00
|
|
|
|
|
|
|
impl<'a> IntoVNode<'a> for &Option<VNode<'a>> {
|
|
|
|
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
2022-01-05 05:27:22 +00:00
|
|
|
self.as_ref()
|
2022-01-05 21:34:24 +00:00
|
|
|
.map(|f| f.into_vnode(cx))
|
|
|
|
.unwrap_or_else(|| cx.fragment_from_iter(None as Option<VNode>))
|
2021-12-15 03:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoVNode<'a> for &VNode<'a> {
|
|
|
|
fn into_vnode(self, _cx: NodeFactory<'a>) -> VNode<'a> {
|
2022-01-05 21:34:24 +00:00
|
|
|
// borrowed nodes are strange
|
2021-12-15 03:48:20 +00:00
|
|
|
self.decouple()
|
|
|
|
}
|
|
|
|
}
|