2021-02-03 07:26:04 +00:00
|
|
|
//! Virtual Node Support
|
2021-07-15 08:09:28 +00:00
|
|
|
//! --------------------
|
2021-02-03 07:26: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 insanely quick.
|
2021-03-11 00:42:10 +00:00
|
|
|
use crate::{
|
|
|
|
events::VirtualEvent,
|
2021-07-23 14:27:43 +00:00
|
|
|
innerlude::{empty_cell, Context, DomTree, ElementId, Properties, Scope, ScopeId, FC},
|
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-07-12 22:19:27 +00:00
|
|
|
marker::PhantomData,
|
2021-07-27 15:28:05 +00:00
|
|
|
mem::ManuallyDrop,
|
2021-06-03 14:42:28 +00:00
|
|
|
rc::Rc,
|
|
|
|
};
|
2021-02-21 02:59:16 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
pub struct VNode<'src> {
|
|
|
|
pub kind: VNodeKind<'src>,
|
2021-07-23 14:27:43 +00:00
|
|
|
|
2021-07-23 21:03:51 +00:00
|
|
|
pub(crate) key: Option<&'src str>,
|
2021-07-15 08:09:28 +00:00
|
|
|
}
|
2021-07-29 22:04:09 +00:00
|
|
|
|
|
|
|
impl<'src> VNode<'src> {
|
|
|
|
pub fn key(&self) -> Option<&'src str> {
|
2021-07-15 08:09:28 +00:00
|
|
|
self.key
|
|
|
|
}
|
2021-07-29 22:04:09 +00:00
|
|
|
pub fn direct_id(&self) -> ElementId {
|
|
|
|
self.try_direct_id().unwrap()
|
|
|
|
}
|
|
|
|
pub fn try_direct_id(&self) -> Option<ElementId> {
|
|
|
|
match &self.kind {
|
|
|
|
VNodeKind::Text(el) => el.dom_id.get(),
|
|
|
|
VNodeKind::Element(el) => el.dom_id.get(),
|
|
|
|
VNodeKind::Anchor(el) => el.dom_id.get(),
|
|
|
|
VNodeKind::Fragment(_) => None,
|
|
|
|
VNodeKind::Component(_) => None,
|
|
|
|
VNodeKind::Suspended(_) => None,
|
|
|
|
}
|
2021-07-23 21:03:51 +00:00
|
|
|
}
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 07:26:04 +00:00
|
|
|
/// Tools for the base unit of the virtual dom - the VNode
|
|
|
|
/// VNodes are intended to be quickly-allocated, lightweight enum values.
|
|
|
|
///
|
|
|
|
/// Components will be generating a lot of these very quickly, so we want to
|
|
|
|
/// limit the amount of heap allocations / overly large enum sizes.
|
2021-07-12 22:19:27 +00:00
|
|
|
pub enum VNodeKind<'src> {
|
2021-06-20 05:52:32 +00:00
|
|
|
Text(VText<'src>),
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
Element(&'src VElement<'src>),
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
Fragment(VFragment<'src>),
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-06-01 22:33:15 +00:00
|
|
|
Component(&'src VComponent<'src>),
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-29 22:04:09 +00:00
|
|
|
Suspended(VSuspended),
|
|
|
|
|
|
|
|
Anchor(VAnchor),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct VAnchor {
|
|
|
|
pub dom_id: Cell<Option<ElementId>>,
|
2021-06-01 22:33:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 05:52:32 +00:00
|
|
|
pub struct VText<'src> {
|
|
|
|
pub text: &'src str,
|
2021-07-29 22:04:09 +00:00
|
|
|
pub dom_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-07-12 22:19:27 +00:00
|
|
|
pub struct VFragment<'src> {
|
|
|
|
pub children: &'src [VNode<'src>],
|
2021-07-13 03:44:20 +00:00
|
|
|
pub is_static: bool,
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
2021-06-26 01:15:33 +00:00
|
|
|
|
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
|
|
|
pub struct VElement<'a> {
|
2021-07-12 22:19:27 +00:00
|
|
|
// tag is always static
|
2021-06-26 01:15:33 +00:00
|
|
|
pub tag_name: &'static str,
|
2021-07-13 03:44:20 +00:00
|
|
|
pub namespace: Option<&'static str>,
|
2021-07-29 22:04:09 +00:00
|
|
|
pub dom_id: Cell<Option<ElementId>>,
|
2021-07-12 22:19:27 +00:00
|
|
|
|
|
|
|
pub static_listeners: bool,
|
2021-07-24 04:29:23 +00:00
|
|
|
pub listeners: &'a [Listener<'a>],
|
2021-07-12 22:19:27 +00:00
|
|
|
|
|
|
|
pub static_attrs: bool,
|
2021-03-11 00:42:10 +00:00
|
|
|
pub attributes: &'a [Attribute<'a>],
|
2021-07-12 22:19:27 +00:00
|
|
|
|
|
|
|
pub static_children: bool,
|
2021-03-11 00:42:10 +00:00
|
|
|
pub children: &'a [VNode<'a>],
|
|
|
|
}
|
2021-02-03 07:26:04 +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-07-12 22:19:27 +00:00
|
|
|
// Doesn't exist in the html spec, mostly used to denote "style" tags - could be for any type of group
|
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> {
|
|
|
|
/// The type of event to listen for.
|
|
|
|
pub(crate) event: &'static str,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
pub mounted_node: Cell<Option<ElementId>>,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-26 16:14:48 +00:00
|
|
|
pub(crate) callback: RefCell<Option<BumpBox<'bump, dyn FnMut(VirtualEvent) + 'bump>>>,
|
2021-03-11 00:42:10 +00:00
|
|
|
}
|
2021-02-03 07:26:04 +00:00
|
|
|
|
2021-07-29 01:46:53 +00:00
|
|
|
impl Listener<'_> {
|
|
|
|
// serialize the listener event stuff to a string
|
|
|
|
pub fn serialize(&self) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
pub fn deserialize() {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-15 07:38:09 +00:00
|
|
|
pub ass_scope: Cell<Option<ScopeId>>,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-18 16:39:32 +00:00
|
|
|
pub(crate) caller: Rc<dyn Fn(&Scope) -> DomTree>,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-13 03:44:20 +00:00
|
|
|
pub(crate) children: &'src [VNode<'src>],
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-13 03:44:20 +00:00
|
|
|
pub(crate) comparator: Option<&'src dyn Fn(&VComponent) -> bool>,
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-08-08 19:15:16 +00:00
|
|
|
pub(crate) drop_props: RefCell<Option<BumpBox<'src, dyn FnMut()>>>,
|
2021-07-27 15:28:05 +00:00
|
|
|
|
2021-07-13 03:44:20 +00:00
|
|
|
pub is_static: bool,
|
|
|
|
|
2021-08-08 19:15:16 +00:00
|
|
|
pub can_memoize: bool,
|
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
// a pointer into the bump arena (given by the 'src lifetime)
|
2021-07-13 03:44:20 +00:00
|
|
|
pub(crate) raw_props: *const (),
|
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
// a pointer to the raw fn typ
|
2021-07-13 03:44:20 +00:00
|
|
|
pub(crate) user_fc: *const (),
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
2021-02-03 07:26:04 +00:00
|
|
|
|
2021-07-29 22:04:09 +00:00
|
|
|
pub struct VSuspended {
|
|
|
|
pub node: Rc<Cell<Option<ElementId>>>,
|
|
|
|
}
|
|
|
|
|
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-07-29 22:04:09 +00:00
|
|
|
&self.bump
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn render_directly<F>(&self, lazy_nodes: LazyNodes<'a, F>) -> DomTree<'a>
|
|
|
|
where
|
|
|
|
F: FnOnce(NodeFactory<'a>) -> VNode<'a>,
|
|
|
|
{
|
|
|
|
Some(lazy_nodes.into_vnode(NodeFactory { bump: self.bump }))
|
2021-07-23 14:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unstable_place_holder() -> VNode<'static> {
|
|
|
|
VNode {
|
|
|
|
key: None,
|
|
|
|
kind: VNodeKind::Text(VText {
|
|
|
|
text: "",
|
2021-07-29 22:04:09 +00:00
|
|
|
dom_id: empty_cell(),
|
2021-07-23 14:27:43 +00:00
|
|
|
is_static: true,
|
|
|
|
}),
|
|
|
|
}
|
2021-02-03 07:26:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 08:09:28 +00:00
|
|
|
/// Used in a place or two to make it easier to build vnodes from dummy text
|
2021-07-23 14:27:43 +00:00
|
|
|
pub fn static_text(&self, text: &'static str) -> VNode<'a> {
|
2021-07-12 22:19:27 +00:00
|
|
|
VNode {
|
|
|
|
key: None,
|
|
|
|
kind: VNodeKind::Text(VText {
|
2021-07-29 22:04:09 +00:00
|
|
|
dom_id: empty_cell(),
|
2021-07-12 22:19:27 +00:00
|
|
|
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;
|
|
|
|
let mut s = bumpalo::collections::String::new_in(self.bump());
|
|
|
|
s.write_fmt(args).unwrap();
|
|
|
|
(s.into_bump_str(), false)
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
VNode {
|
|
|
|
key: None,
|
2021-07-29 22:04:09 +00:00
|
|
|
kind: VNodeKind::Text(VText {
|
|
|
|
text,
|
|
|
|
is_static,
|
|
|
|
dom_id: empty_cell(),
|
|
|
|
}),
|
2021-07-12 22:19:27 +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-07-15 08:09:28 +00:00
|
|
|
tag: &'static str,
|
|
|
|
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>]>,
|
|
|
|
{
|
|
|
|
let listeners: &'a L = self.bump().alloc(listeners);
|
|
|
|
let listeners = listeners.as_ref();
|
|
|
|
|
|
|
|
let attributes: &'a A = self.bump().alloc(attributes);
|
|
|
|
let attributes = attributes.as_ref();
|
|
|
|
|
|
|
|
let children: &'a V = self.bump().alloc(children);
|
|
|
|
let children = children.as_ref();
|
|
|
|
|
2021-07-30 14:35:47 +00:00
|
|
|
let key = key.map(|f| self.raw_text(f).0);
|
|
|
|
|
2021-07-13 04:56:39 +00:00
|
|
|
VNode {
|
|
|
|
key,
|
|
|
|
kind: VNodeKind::Element(self.bump().alloc(VElement {
|
2021-07-15 08:09:28 +00:00
|
|
|
tag_name: tag,
|
|
|
|
namespace,
|
2021-07-13 04:56:39 +00:00
|
|
|
listeners,
|
|
|
|
attributes,
|
|
|
|
children,
|
2021-07-29 22:04:09 +00:00
|
|
|
dom_id: empty_cell(),
|
2021-07-15 08:09:28 +00:00
|
|
|
|
|
|
|
// todo: wire up more constization
|
|
|
|
static_listeners: false,
|
|
|
|
static_attrs: false,
|
|
|
|
static_children: false,
|
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 suspended() -> VNode<'static> {
|
|
|
|
VNode {
|
|
|
|
key: None,
|
2021-07-29 22:04:09 +00:00
|
|
|
kind: VNodeKind::Suspended(VSuspended {
|
2021-07-23 14:27:43 +00:00
|
|
|
node: Rc::new(empty_cell()),
|
2021-07-29 22:04:09 +00:00
|
|
|
}),
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-12 19:27:32 +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-07-20 23:03:49 +00:00
|
|
|
pub fn attr_with_alloc_val(
|
|
|
|
&self,
|
|
|
|
name: &'static str,
|
|
|
|
val: &'a str,
|
|
|
|
namespace: Option<&'static str>,
|
|
|
|
is_volatile: bool,
|
|
|
|
) -> Attribute<'a> {
|
|
|
|
Attribute {
|
|
|
|
name,
|
|
|
|
value: val,
|
|
|
|
is_static: false,
|
|
|
|
namespace,
|
|
|
|
is_volatile,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 14:35:47 +00:00
|
|
|
pub fn component<P, V>(
|
2021-07-12 22:19:27 +00:00
|
|
|
&self,
|
2021-06-02 15:07:30 +00:00
|
|
|
component: FC<P>,
|
|
|
|
props: P,
|
2021-07-30 14:35:47 +00:00
|
|
|
key: Option<Arguments>,
|
|
|
|
children: V,
|
2021-07-12 22:19:27 +00:00
|
|
|
) -> VNode<'a>
|
|
|
|
where
|
|
|
|
P: Properties + 'a,
|
2021-07-30 14:35:47 +00:00
|
|
|
V: 'a + AsRef<[VNode<'a>]>,
|
2021-07-12 22:19:27 +00:00
|
|
|
{
|
2021-08-08 19:15:16 +00:00
|
|
|
let bump = self.bump();
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
// We don't want the fat part of the fat pointer
|
|
|
|
// This function does static dispatch so we don't need any VTable stuff
|
2021-08-08 19:15:16 +00:00
|
|
|
let children: &'a V = bump.alloc(children);
|
2021-07-30 14:35:47 +00:00
|
|
|
let children = children.as_ref();
|
|
|
|
|
2021-08-08 19:15:16 +00:00
|
|
|
let props = bump.alloc(props);
|
2021-06-06 03:47:54 +00:00
|
|
|
|
2021-07-27 15:28:05 +00:00
|
|
|
let raw_props = props as *mut P as *mut ();
|
2021-07-12 22:19:27 +00:00
|
|
|
let user_fc = component as *const ();
|
|
|
|
|
2021-08-08 19:15:16 +00:00
|
|
|
let comparator: Option<&dyn Fn(&VComponent) -> bool> = Some(bump.alloc_with(|| {
|
2021-06-30 02:44:21 +00:00
|
|
|
move |other: &VComponent| {
|
2021-06-22 21:20:54 +00:00
|
|
|
if user_fc == other.user_fc {
|
2021-07-27 15:28:05 +00:00
|
|
|
// 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.raw_props as *const _ as *const P);
|
|
|
|
props.memoize(&real_other)
|
|
|
|
};
|
2021-07-27 04:27:07 +00:00
|
|
|
|
|
|
|
// 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
|
2021-06-22 21:20:54 +00:00
|
|
|
match (props_memoized, children.len() == 0) {
|
|
|
|
(true, true) => true,
|
|
|
|
_ => false,
|
2021-06-06 03:47:54 +00:00
|
|
|
}
|
2021-06-22 21:20:54 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2021-06-30 02:44:21 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2021-07-27 15:28:05 +00:00
|
|
|
// create a closure to drop the props
|
2021-08-08 19:15:16 +00:00
|
|
|
let mut has_dropped = false;
|
|
|
|
let drop_props: &mut dyn FnMut() = bump.alloc_with(|| {
|
2021-07-27 15:28:05 +00:00
|
|
|
move || unsafe {
|
2021-08-08 19:15:16 +00:00
|
|
|
if !has_dropped {
|
|
|
|
let real_other = raw_props as *mut _ as *mut P;
|
|
|
|
let b = BumpBox::from_raw(real_other);
|
|
|
|
std::mem::drop(b);
|
|
|
|
|
|
|
|
has_dropped = true;
|
|
|
|
}
|
2021-07-27 15:28:05 +00:00
|
|
|
}
|
2021-08-08 19:15:16 +00:00
|
|
|
});
|
|
|
|
let drop_props = unsafe { BumpBox::from_raw(drop_props) };
|
2021-07-27 15:28:05 +00:00
|
|
|
|
2021-07-15 08:09:28 +00:00
|
|
|
let is_static = children.len() == 0 && P::IS_STATIC && key.is_none();
|
|
|
|
|
2021-07-30 14:35:47 +00:00
|
|
|
let key = key.map(|f| self.raw_text(f).0);
|
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
VNode {
|
|
|
|
key,
|
2021-08-08 19:15:16 +00:00
|
|
|
kind: VNodeKind::Component(bump.alloc_with(|| VComponent {
|
2021-07-12 22:19:27 +00:00
|
|
|
user_fc,
|
|
|
|
comparator,
|
|
|
|
raw_props,
|
|
|
|
children,
|
|
|
|
caller: NodeFactory::create_component_caller(component, raw_props),
|
2021-07-15 08:09:28 +00:00
|
|
|
is_static,
|
2021-08-08 19:15:16 +00:00
|
|
|
drop_props: RefCell::new(Some(drop_props)),
|
|
|
|
can_memoize: P::IS_STATIC,
|
2021-07-12 22:19:27 +00:00
|
|
|
ass_scope: Cell::new(None),
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
2021-06-01 22:33:15 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
pub fn create_component_caller<'g, P: 'g>(
|
|
|
|
component: FC<P>,
|
|
|
|
raw_props: *const (),
|
2021-07-18 16:39:32 +00:00
|
|
|
) -> Rc<dyn for<'r> Fn(&'r Scope) -> DomTree<'r>> {
|
|
|
|
type Captured<'a> = Rc<dyn for<'r> Fn(&'r Scope) -> DomTree<'r> + 'a>;
|
|
|
|
let caller: Captured = Rc::new(move |scp: &Scope| -> DomTree {
|
2021-07-12 22:19:27 +00:00
|
|
|
// cast back into the right lifetime
|
|
|
|
let safe_props: &'_ P = unsafe { &*(raw_props as *const P) };
|
|
|
|
let cx: Context<P> = Context {
|
|
|
|
props: safe_props,
|
|
|
|
scope: scp,
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = component(cx);
|
|
|
|
|
|
|
|
let g2 = unsafe { std::mem::transmute(res) };
|
2021-07-11 18:49:52 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
g2
|
|
|
|
});
|
|
|
|
unsafe { std::mem::transmute::<_, Captured<'static>>(caller) }
|
|
|
|
}
|
|
|
|
|
2021-07-27 04:27:07 +00:00
|
|
|
pub fn fragment_from_iter(self, node_iter: impl IntoVNodeList<'a>) -> VNode<'a> {
|
2021-07-26 16:14:48 +00:00
|
|
|
let children = node_iter.into_vnode_list(self);
|
2021-07-15 08:09:28 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
VNode {
|
|
|
|
key: None,
|
|
|
|
kind: VNodeKind::Fragment(VFragment {
|
2021-07-26 16:14:48 +00:00
|
|
|
children,
|
2021-07-13 03:44:20 +00:00
|
|
|
is_static: false,
|
2021-07-12 22:19:27 +00:00
|
|
|
}),
|
2021-03-12 19:27:32 +00:00
|
|
|
}
|
2021-02-03 07:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-03 14:42:28 +00:00
|
|
|
|
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>>
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// As such, all node creation must go through the factory, which is only availble in the component context.
|
|
|
|
/// These strict requirements make it possible to manage lifetimes and state.
|
|
|
|
pub trait IntoVNode<'a> {
|
|
|
|
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a>;
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:14:48 +00:00
|
|
|
pub trait IntoVNodeList<'a> {
|
|
|
|
fn into_vnode_list(self, cx: NodeFactory<'a>) -> &'a [VNode<'a>];
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T, V> IntoVNodeList<'a> for T
|
|
|
|
where
|
|
|
|
T: IntoIterator<Item = V>,
|
|
|
|
V: IntoVNode<'a>,
|
|
|
|
{
|
|
|
|
fn into_vnode_list(self, cx: NodeFactory<'a>) -> &'a [VNode<'a>] {
|
|
|
|
let mut nodes = bumpalo::collections::Vec::new_in(cx.bump());
|
|
|
|
|
|
|
|
for node in self.into_iter() {
|
|
|
|
nodes.push(node.into_vnode(cx));
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
if nodes.len() > 1 {
|
|
|
|
if nodes.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-07-29 22:04:09 +00:00
|
|
|
if nodes.len() == 0 {
|
|
|
|
nodes.push(VNode {
|
|
|
|
kind: VNodeKind::Anchor(VAnchor {
|
|
|
|
dom_id: empty_cell(),
|
|
|
|
}),
|
|
|
|
key: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:14:48 +00:00
|
|
|
nodes.into_bump_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ScopeChildren<'a>(pub &'a [VNode<'a>]);
|
|
|
|
impl Copy for ScopeChildren<'_> {}
|
|
|
|
impl<'a> Clone for ScopeChildren<'a> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ScopeChildren(self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl ScopeChildren<'_> {
|
|
|
|
pub unsafe fn extend_lifetime(self) -> ScopeChildren<'static> {
|
|
|
|
std::mem::transmute(self)
|
|
|
|
}
|
|
|
|
pub unsafe fn unextend_lfetime<'a>(self) -> ScopeChildren<'a> {
|
|
|
|
std::mem::transmute(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<'a> IntoVNodeList<'a> for ScopeChildren<'a> {
|
|
|
|
fn into_vnode_list(self, _: NodeFactory<'a>) -> &'a [VNode<'a>] {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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> IntoVNode<'a> for VNode<'a> {
|
|
|
|
fn into_vnode(self, _: NodeFactory<'a>) -> VNode<'a> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
/// A concrete type provider for closures that build VNode structures.
|
|
|
|
///
|
|
|
|
/// This struct wraps lazy structs that build VNode trees Normally, we cannot perform a blanket implementation over
|
|
|
|
/// closures, but if we wrap the closure in a concrete type, we can maintain separate implementations of IntoVNode.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// LazyNodes::new(|f| f.element("div", [], [], [] None))
|
|
|
|
/// ```
|
2021-07-12 22:19:27 +00:00
|
|
|
pub struct LazyNodes<'a, G>
|
|
|
|
where
|
|
|
|
G: FnOnce(NodeFactory<'a>) -> VNode<'a>,
|
|
|
|
{
|
|
|
|
inner: G,
|
|
|
|
_p: PhantomData<&'a ()>,
|
|
|
|
}
|
2021-07-09 16:47:41 +00:00
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
impl<'a, G> LazyNodes<'a, G>
|
|
|
|
where
|
|
|
|
G: FnOnce(NodeFactory<'a>) -> VNode<'a>,
|
|
|
|
{
|
|
|
|
pub fn new(f: G) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: f,
|
|
|
|
_p: PhantomData {},
|
2021-07-11 18:49:52 +00:00
|
|
|
}
|
2021-07-12 22:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-09 16:47:41 +00:00
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
// Our blanket impl
|
2021-07-12 22:19:27 +00:00
|
|
|
impl<'a, G> IntoVNode<'a> for LazyNodes<'a, G>
|
|
|
|
where
|
|
|
|
G: FnOnce(NodeFactory<'a>) -> VNode<'a>,
|
|
|
|
{
|
|
|
|
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
|
|
|
(self.inner)(cx)
|
|
|
|
}
|
|
|
|
}
|
2021-07-11 18:49:52 +00:00
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
// Our blanket impl
|
2021-07-12 22:19:27 +00:00
|
|
|
impl<'a, G> IntoIterator for LazyNodes<'a, G>
|
|
|
|
where
|
|
|
|
G: FnOnce(NodeFactory<'a>) -> VNode<'a>,
|
|
|
|
{
|
|
|
|
type Item = Self;
|
|
|
|
type IntoIter = std::iter::Once<Self::Item>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
std::iter::once(self)
|
|
|
|
}
|
2021-06-07 18:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
// Conveniently, we also support "null" (nothing) passed in
|
2021-07-12 22:19:27 +00:00
|
|
|
impl IntoVNode<'_> for () {
|
|
|
|
fn into_vnode<'a>(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
|
|
|
cx.fragment_from_iter(None as Option<VNode>)
|
|
|
|
}
|
2021-06-03 14:42:28 +00:00
|
|
|
}
|
2021-06-08 18:00:29 +00:00
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
// Conveniently, we also support "None"
|
2021-07-12 22:19:27 +00:00
|
|
|
impl IntoVNode<'_> for Option<()> {
|
|
|
|
fn into_vnode<'a>(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
|
|
|
cx.fragment_from_iter(None as Option<VNode>)
|
|
|
|
}
|
|
|
|
}
|
2021-07-20 23:03:49 +00:00
|
|
|
impl<'a> IntoVNode<'a> for Option<VNode<'a>> {
|
|
|
|
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
|
|
|
match self {
|
|
|
|
Some(n) => n,
|
|
|
|
None => cx.fragment_from_iter(None as Option<VNode>),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 22:19:27 +00:00
|
|
|
|
2021-07-21 21:05:48 +00:00
|
|
|
impl IntoVNode<'_> for &'static str {
|
|
|
|
fn into_vnode<'a>(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
2021-07-23 14:27:43 +00:00
|
|
|
cx.static_text(self)
|
2021-07-21 21:05:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl IntoVNode<'_> for Arguments<'_> {
|
|
|
|
fn into_vnode<'a>(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
|
|
|
cx.text(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
impl Debug for NodeFactory<'_> {
|
|
|
|
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for VNode<'_> {
|
|
|
|
fn fmt(&self, s: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
|
|
match &self.kind {
|
2021-07-29 22:04:09 +00:00
|
|
|
VNodeKind::Element(el) => write!(s, "VElement {{ name: {} }}", el.tag_name),
|
|
|
|
VNodeKind::Text(t) => write!(s, "VText {{ text: {} }}", t.text),
|
|
|
|
VNodeKind::Anchor(a) => write!(s, "VAnchor"),
|
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
VNodeKind::Fragment(_) => write!(s, "fragment"),
|
|
|
|
VNodeKind::Suspended { .. } => write!(s, "suspended"),
|
|
|
|
VNodeKind::Component(_) => write!(s, "component"),
|
2021-07-12 06:23:46 +00:00
|
|
|
}
|
2021-06-08 18:00:29 +00:00
|
|
|
}
|
|
|
|
}
|