on master help sign in
Expose the `Node::element` constructor
08ccdbbNick Fitzgerald
src/builder.rs+12−3
@@ -282,13 +282,22 @@ where
282 /// ```
282 /// ```
283 #[inline]
283 #[inline]
284 pub fn finish(self) -> Node<'a> {
284 pub fn finish(self) -> Node<'a> {
285+ let children: &'a Children = self.bump.alloc(self.children);
286+ let children: &'a [Node<'a>] = children.as_ref();
287+
288+ let listeners: &'a Listeners = self.bump.alloc(self.listeners);
289+ let listeners: &'a [Listener<'a>] = listeners.as_ref();
290+
291+ let attributes: &'a Attributes = self.bump.alloc(self.attributes);
292+ let attributes: &'a [Attribute<'a>] = attributes.as_ref();
293+
285 Node::element(
294 Node::element(
286 self.bump,
295 self.bump,
287 self.key,
296 self.key,
288 self.tag_name,
297 self.tag_name,
289− self.listeners,
298+ listeners,
290− self.attributes,
299+ attributes,
291− self.children,
300+ children,
292 self.namespace,
301 self.namespace,
293 )
302 )
294 }
303 }
src/lib.rs+1−1
@@ -98,7 +98,7 @@ pub mod builder;
98
98
99// Re-export items at the top level.
99// Re-export items at the top level.
100pub use self::cached::Cached;
100pub use self::cached::Cached;
101−pub use self::node::{Attribute, Listener, Node};
101+pub use self::node::{Attribute, Listener, Node, NodeKey};
102pub use self::render::{Render, RootRender};
102pub use self::render::{Render, RootRender};
103pub use self::render_context::RenderContext;
103pub use self::render_context::RenderContext;
104pub use self::vdom::{Vdom, VdomWeak};
104pub use self::vdom::{Vdom, VdomWeak};
src/node.rs+35−24
@@ -68,24 +68,44 @@ pub_unstable_internal! {
68 }
68 }
69}
69}
70
70
71−pub_unstable_internal! {
72− /// The key for keyed children.
71+/// The key for keyed children.
72+///
73+/// Keys must be unique among siblings.
74+///
75+/// If any sibling is keyed, then they all must be keyed.
73− #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
76+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
74− pub(crate) struct NodeKey(pub(crate) u32);
77+pub struct NodeKey(pub(crate) u32);
78+
79+impl Default for NodeKey {
80+ fn default() -> NodeKey {
81+ NodeKey::NONE
82+ }
75}
83}
76
84
77impl NodeKey {
85impl NodeKey {
86+ /// The default, lack of a key.
78 pub const NONE: NodeKey = NodeKey(u32::MAX);
87 pub const NONE: NodeKey = NodeKey(u32::MAX);
79
88
89+ /// Is this key `NodeKey::NONE`?
80 #[inline]
90 #[inline]
81 pub fn is_none(&self) -> bool {
91 pub fn is_none(&self) -> bool {
82 *self == Self::NONE
92 *self == Self::NONE
83 }
93 }
84
94
95+ /// Is this key not `NodeKey::NONE`?
85 #[inline]
96 #[inline]
86 pub fn is_some(&self) -> bool {
97 pub fn is_some(&self) -> bool {
87 !self.is_none()
98 !self.is_none()
88 }
99 }
100+
101+ /// Create a new `NodeKey`.
102+ ///
103+ /// `key` must not be `u32::MAX`.
104+ #[inline]
105+ pub fn new(key: u32) -> Self {
106+ debug_assert_ne!(key, u32::MAX);
107+ NodeKey(key)
108+ }
89}
109}
90
110
91/// An event listener callback function.
111/// An event listener callback function.
@@ -162,31 +182,22 @@ impl<'a> Attribute<'a> {
162}
182}
163
183
164impl<'a> Node<'a> {
184impl<'a> Node<'a> {
165− /// Construct a new Node of type element with given tag name and children
185+ /// Low-level constructor for making a new `Node` of type element with given
186+ /// parts.
187+ ///
188+ /// This is primarily intended for JSX and templating proc-macros to compile
189+ /// down into. If you are building nodes by-hand, prefer using the
190+ /// `dodrio::builder::*` APIs.
166 #[inline]
191 #[inline]
167− pub(crate) fn element<Listeners, Attributes, Children>(
192+ pub fn element(
168 bump: &'a Bump,
193 bump: &'a Bump,
169 key: NodeKey,
194 key: NodeKey,
170 tag_name: &'a str,
195 tag_name: &'a str,
171− listeners: Listeners,
196+ listeners: &'a [Listener<'a>],
172− attributes: Attributes,
197+ attributes: &'a [Attribute<'a>],
173− children: Children,
198+ children: &'a [Node<'a>],
174 namespace: Option<&'a str>,
199 namespace: Option<&'a str>,
175− ) -> Node<'a>
200+ ) -> Node<'a> {
176− where
177− Listeners: 'a + AsRef<[Listener<'a>]>,
178− Attributes: 'a + AsRef<[Attribute<'a>]>,
179− Children: 'a + AsRef<[Node<'a>]>,
180− {
181− let children: &'a Children = bump.alloc(children);
182− let children: &'a [Node<'a>] = children.as_ref();
183−
184− let listeners: &'a Listeners = bump.alloc(listeners);
185− let listeners: &'a [Listener<'a>] = listeners.as_ref();
186−
187− let attributes: &'a Attributes = bump.alloc(attributes);
188− let attributes: &'a [Attribute<'a>] = attributes.as_ref();
189−
190 let element = bump.alloc_with(|| ElementNode {
201 let element = bump.alloc_with(|| ElementNode {
191 key,
202 key,
192 tag_name,
203 tag_name,
src/vdom.rs+2−1
@@ -166,7 +166,8 @@ impl Vdom {
166
166
167 // Create a dummy `<div/>` in our container.
167 // Create a dummy `<div/>` in our container.
168 initialize_container(container);
168 initialize_container(container);
169+ let current_root =
169− let current_root = Node::element(&dom_buffers[0], NodeKey::NONE, "div", [], [], [], None);
170+ Node::element(&dom_buffers[0], NodeKey::NONE, "div", &[], &[], &[], None);
170 let current_root = Some(unsafe { extend_node_lifetime(current_root) });
171 let current_root = Some(unsafe { extend_node_lifetime(current_root) });
171
172
172 let container = container.clone();
173 let container = container.clone();