on master help sign in
Refactor node representation and use log crate for logging
commit55618b184a35fb2859f4c3844d2c90a486d6afec authorNick Fitzgerald <fitzgen@gmail.com> dateTue 15 Jan 2019, 15:23 (8 years ago) parent08b0abf
6 files changed+100−107
Cargo.toml+2 src/change_list.rs+1 src/lib.rs+2−5 src/macros.rs−7 src/node.rs+58−65 src/vdom.rs+37−30
Cargo.toml+2
@@ -11,6 +11,7 @@ default = []
1111bumpalo = "1.0.2"
1212wasm-bindgen = "0.2.25"
1313js-sys = "0.3.2"
14+log = "0.4.6"
1415
1516[dependencies.web-sys]
1617version = "0.3.8"
@@ -24,6 +25,7 @@ features = [
2425
2526[dev-dependencies]
2627wasm-bindgen-test = "0.2"
28+console_log = "0.1.2"
2729
2830[profile.release]
2931# Tell `rustc` to optimize for small code size.
src/change_list.rs+1
@@ -1,4 +1,5 @@
11use bumpalo::{Bump, BumpAllocSafe};
2+use log::*;
23use std::sync::Once;
34use wasm_bindgen::prelude::*;
45
src/lib.rs+2−5
@@ -1,17 +1,14 @@
11use bumpalo::Bump;
22
3−#[macro_use]
4−mod macros;
5−
63pub mod change_list;
74pub mod node;
85pub mod vdom;
96
10−pub use self::node::{Attribute, Node, NodeRef};
7+pub use self::node::{Attribute, ElementNode, Node, TextNode};
118pub use self::vdom::Vdom;
129
1310pub trait Render {
14− fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> NodeRef<'bump>
11+ fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
1512 where
1613 'a: 'bump;
1714}
src/macros.rsdeleted−7
@@ -1,7 +0,0 @@
1−macro_rules! debug {
2− ( $fmt:expr $(, $x:expr )* $(,)* ) => {{
3− if cfg!(debug_assertions) {
4− web_sys::console::log_1(&format!($fmt, $($x),*).into());
5− }
6− }}
7−}
src/node.rs+58−65
@@ -1,23 +1,29 @@
1−use bumpalo::BumpAllocSafe;
1+use bumpalo::{Bump, BumpAllocSafe};
22
3−#[derive(Clone, Debug)]
4−pub struct Node<'a, Attributes, Children> {
5− data: NodeData<'a>,
6− attributes: Attributes,
7− children: Children,
3+/// A node is either a text node or an element.
4+#[derive(Debug, Clone)]
5+pub enum Node<'a> {
6+ /// A text node.
7+ Text(TextNode<'a>),
8+
9+ /// An element potentially with attributes and children.
10+ Element(ElementNode<'a>),
811}
912
10−#[derive(Clone, Copy, Debug)]
11−pub enum NodeData<'a> {
12− Element { tag_name: &'a str },
13− Text { text: &'a str },
13+/// Text nodes are just a string of text. They cannot have attributes or
14+/// children.
15+#[derive(Debug, Clone)]
16+pub struct TextNode<'a> {
17+ pub(crate) text: &'a str,
1418}
1519
16−#[derive(Clone, Debug)]
17−pub struct NodeRef<'a> {
18− pub(crate) data: NodeData<'a>,
20+/// Elements have a tag name, zero or more attributes, and zero or more
21+/// children.
22+#[derive(Debug, Clone)]
23+pub struct ElementNode<'a> {
24+ pub(crate) tag_name: &'a str,
1925 pub(crate) attributes: &'a [Attribute<'a>],
20− pub(crate) children: &'a [NodeRef<'a>],
26+ pub(crate) children: &'a [Node<'a>],
2127}
2228
2329#[derive(Clone, Debug)]
@@ -26,69 +32,56 @@ pub struct Attribute<'a> {
2632 pub value: &'a str,
2733}
2834
29−impl<'a, Attributes, Children> BumpAllocSafe for Node<'a, Attributes, Children>
30−where
31− Attributes: BumpAllocSafe,
32− Children: BumpAllocSafe,
33−{
34−}
35−
36−impl<'a, Attributes, Children> From<&'a Node<'a, Attributes, Children>> for NodeRef<'a>
37−where
38− Attributes: AsRef<[Attribute<'a>]>,
39− Children: AsRef<[NodeRef<'a>]>,
40−{
41− fn from(node: &'a Node<'a, Attributes, Children>) -> NodeRef<'a> {
42− NodeRef {
43− data: node.data,
44− attributes: node.attributes.as_ref(),
45− children: node.children.as_ref(),
35+impl<'a> Node<'a> {
36+ /// Is this node a text node?
37+ pub fn is_text(&self) -> bool {
38+ match self {
39+ Node::Text(_) => true,
40+ _ => false,
4641 }
4742 }
48−}
4943
50−impl<'a, Attributes, Children> From<&'a mut Node<'a, Attributes, Children>> for NodeRef<'a>
51−where
52− Attributes: AsRef<[Attribute<'a>]>,
53− Children: AsRef<[NodeRef<'a>]>,
54−{
55− fn from(node: &'a mut Node<'a, Attributes, Children>) -> NodeRef<'a> {
56− NodeRef {
57− data: node.data,
58− attributes: node.attributes.as_ref(),
59− children: node.children.as_ref(),
44+ /// Is this node an element?
45+ pub fn is_element(&self) -> bool {
46+ match self {
47+ Node::Element { .. } => true,
48+ _ => false,
6049 }
6150 }
6251}
6352
64−impl<'a, Attributes, Children> Node<'a, Attributes, Children>
65−where
66− Attributes: AsRef<[Attribute<'a>]>,
67− Children: AsRef<[NodeRef<'a>]>,
68−{
69− pub fn element(
53+impl<'a> BumpAllocSafe for Node<'a> {}
54+impl<'a> BumpAllocSafe for Attribute<'a> {}
55+
56+impl<'a> Node<'a> {
57+ /// Construct a new text node with the given text.
58+ #[inline]
59+ pub fn text(text: &'a str) -> Node<'a> {
60+ Node::Text(TextNode { text })
61+ }
62+
63+ /// Construct a new element node with the given tag name and children.
64+ #[inline]
65+ pub fn element<Attributes, Children>(
66+ bump: &'a Bump,
7067 tag_name: &'a str,
7168 attributes: Attributes,
7269 children: Children,
73− ) -> Node<'a, Attributes, Children> {
74− let data = NodeData::Element { tag_name };
75− Node {
76− data,
77− attributes,
78− children,
79− }
80− }
81−}
70+ ) -> Node<'a>
71+ where
72+ Attributes: 'a + BumpAllocSafe + AsRef<[Attribute<'a>]>,
73+ Children: 'a + BumpAllocSafe + AsRef<[Node<'a>]>,
74+ {
75+ let children: &'a Children = bump.alloc(children);
76+ let children: &'a [Node<'a>] = children.as_ref();
8277
83−impl<'a> Node<'a, [Attribute<'a>; 0], [NodeRef<'a>; 0]> {
84− pub fn text(text: &'a str) -> Node<'a, [Attribute<'a>; 0], [NodeRef<'a>; 0]> {
85− let data = NodeData::Text { text };
86− let attributes = [];
87− let children = [];
88− Node {
89− data,
78+ let attributes: &'a Attributes = bump.alloc(attributes);
79+ let attributes: &'a [Attribute<'a>] = attributes.as_ref();
80+
81+ Node::Element(ElementNode {
82+ tag_name,
9083 attributes,
9184 children,
92− }
85+ })
9386 }
9487}
src/vdom.rs+37−30
@@ -1,7 +1,8 @@
11use super::change_list::ChangeList;
2−use super::node::{Attribute, Node, NodeData, NodeRef};
2+use super::node::{Attribute, ElementNode, Node, TextNode};
33use super::Render;
44use bumpalo::Bump;
5+use log::*;
56use std::cmp;
67use std::mem;
78
@@ -11,10 +12,10 @@ pub struct Vdom {
1112 container: web_sys::Element,
1213
1314 // Actually a reference into `self.dom_buffers[0]`.
14− current_root: Option<NodeRef<'static>>,
15+ current_root: Option<Node<'static>>,
1516}
1617
17−unsafe fn extend_node_lifetime<'a>(node: NodeRef<'a>) -> NodeRef<'static> {
18+unsafe fn extend_node_lifetime<'a>(node: Node<'a>) -> Node<'static> {
1819 mem::transmute(node)
1920}
2021
@@ -30,7 +31,7 @@ impl Vdom {
3031 container.set_inner_html("");
3132
3233 // Create a dummy `<div/>` in our container.
33− let current_root: NodeRef = dom_buffers[0].alloc(Node::element("div", [], [])).into();
34+ let current_root = Node::element(&dom_buffers[0], "div", [], []);
3435 let current_root = Some(unsafe { extend_node_lifetime(current_root) });
3536 let window = web_sys::window().expect("should have acess to the Window");
3637 let document = window
@@ -85,53 +86,55 @@ impl Vdom {
8586 mem::swap(&mut first[0], &mut second[0]);
8687 }
8788
88− unsafe fn set_current_root(&mut self, current: NodeRef<'static>) {
89+ unsafe fn set_current_root(&mut self, current: Node<'static>) {
8990 debug_assert!(self.current_root.is_none());
9091 self.current_root = Some(current);
9192 }
9293
93− fn diff(&self, old: NodeRef, new: NodeRef) {
94− debug!("---------------------------------------------------------");
95− debug!("dodrio::Vdom::diff");
96− debug!(" old = {:#?}", old);
97− debug!(" new = {:#?}", new);
98− match (new.data, old.data) {
99− (NodeData::Text { text: new_text }, NodeData::Text { text: old_text }) => {
94+ fn diff(&self, old: Node, new: Node) {
95+ // debug!("---------------------------------------------------------");
96+ // debug!("dodrio::Vdom::diff");
97+ // debug!(" old = {:#?}", old);
98+ // debug!(" new = {:#?}", new);
99+ match (&new, old) {
100+ (&Node::Text(TextNode { text: new_text }), Node::Text(TextNode { text: old_text })) => {
100101 debug!(" both are text nodes");
101102 if new_text != old_text {
102103 debug!(" text needs updating");
103104 self.change_list.emit_set_text(new_text);
104105 }
105106 }
106− (NodeData::Text { .. }, NodeData::Element { .. }) => {
107+ (&Node::Text(TextNode { .. }), Node::Element(ElementNode { .. })) => {
107108 debug!(" replacing a text node with an element");
108109 self.create(new);
109110 self.change_list.emit_replace_with();
110111 }
111− (NodeData::Element { .. }, NodeData::Text { .. }) => {
112+ (&Node::Element(ElementNode { .. }), Node::Text(TextNode { .. })) => {
112113 debug!(" replacing an element with a text node");
113114 self.create(new);
114115 self.change_list.emit_replace_with();
115116 }
116117 (
117− NodeData::Element {
118+ &Node::Element(ElementNode {
118119 tag_name: new_tag_name,
119− },
120− NodeData::Element {
120+ attributes: new_attributes,
121+ children: new_children,
122+ }),
123+ Node::Element(ElementNode {
121124 tag_name: old_tag_name,
122− },
125+ attributes: old_attributes,
126+ children: old_children,
127+ }),
123128 ) => {
124129 debug!(" updating an element");
125−
126130 if new_tag_name != old_tag_name {
127131 debug!(" different tag names; creating new element and replacing old element");
128132 self.create(new);
129133 self.change_list.emit_replace_with();
130134 return;
131135 }
132−
133− self.diff_attributes(old.attributes, new.attributes);
134− self.diff_children(old.children, new.children);
136+ self.diff_attributes(old_attributes, new_attributes);
137+ self.diff_children(old_children, new_children);
135138 }
136139 }
137140 }
@@ -165,7 +168,7 @@ impl Vdom {
165168 }
166169 }
167170
168− fn diff_children(&self, old: &[NodeRef], new: &[NodeRef]) {
171+ fn diff_children(&self, old: &[Node], new: &[Node]) {
169172 debug!(" updating children shared by old and new");
170173
171174 let num_children_to_diff = cmp::min(new.len(), old.len());
@@ -217,18 +220,22 @@ impl Vdom {
217220 }
218221 }
219222
220− fn create(&self, node: NodeRef) {
221− debug!("dodrio::Vdom::create({:#?})", node);
222− match node.data {
223− NodeData::Text { text } => {
223+ fn create(&self, node: Node) {
224+ // debug!("dodrio::Vdom::create({:#?})", node);
225+ match node {
226+ Node::Text(TextNode { text }) => {
224227 self.change_list.emit_create_text_node(text);
225228 }
226− NodeData::Element { tag_name } => {
229+ Node::Element(ElementNode {
230+ tag_name,
231+ attributes,
232+ children,
233+ }) => {
227234 self.change_list.emit_create_element(tag_name);
228− for attr in node.attributes {
235+ for attr in attributes {
229236 self.change_list.emit_set_attribute(&attr.name, &attr.value);
230237 }
231− for child in node.children {
238+ for child in children {
232239 self.create(child.clone());
233240 self.change_list.emit_append_child();
234241 }