Skip to content

Commit

Permalink
Box VTag to reduce size disparity between VNode variants (yewstack#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgzimmerman authored and llebout committed Jan 20, 2020
1 parent f071274 commit 3aa6198
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/macro/src/html_tree/html_tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl ToTokens for HtmlTag {
#vtag.add_attributes(vec![#(#attr_pairs),*]);
#vtag.add_listeners(vec![#(::std::boxed::Box::new(#listeners)),*]);
#vtag.add_children(vec![#(#children),*]);
::yew::virtual_dom::VNode::VTag(#vtag)
::yew::virtual_dom::VNode::from(#vtag)
}});
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/crm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ The main goals of this demo example to show you how to:

* Add multiple screens with Yew (scenes)
* Use storage service
* Generate VNodes without the html! macro
2 changes: 1 addition & 1 deletion examples/crm/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ where
}

if elems.len() == 1 {
VNode::VTag(elems.pop().unwrap())
VNode::VTag(Box::new(elems.pop().unwrap()))
} else {
html! {
<div>{ for elems.into_iter() }</div>
Expand Down
5 changes: 2 additions & 3 deletions src/virtual_dom/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use std::iter::FromIterator;
use stdweb::web::{Element, INode, Node};

/// Bind virtual element to a DOM reference.
#[allow(clippy::large_enum_variant)] // ISSUE: #571
pub enum VNode<COMP: Component> {
/// A bind between `VTag` and `Element`.
VTag(VTag<COMP>),
VTag(Box<VTag<COMP>>),
/// A bind between `VText` and `TextNode`.
VText(VText<COMP>),
/// A bind between `VComp` and `Element`.
Expand Down Expand Up @@ -93,7 +92,7 @@ impl<COMP: Component> From<VList<COMP>> for VNode<COMP> {

impl<COMP: Component> From<VTag<COMP>> for VNode<COMP> {
fn from(vtag: VTag<COMP>) -> Self {
VNode::VTag(vtag)
VNode::VTag(Box::new(vtag))
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<COMP: Component> VTag<COMP> {
/// Otherwise just add everything.
fn diff_classes<'a>(
&'a self,
ancestor: &'a Option<Self>,
ancestor: &'a Option<Box<Self>>,
) -> impl Iterator<Item = Patch<&'a str, ()>> + 'a {
let to_add = {
let all_or_nothing = not(ancestor)
Expand Down Expand Up @@ -210,7 +210,7 @@ impl<COMP: Component> VTag<COMP> {
/// the values are different.
fn diff_attributes<'a>(
&'a self,
ancestor: &'a Option<Self>,
ancestor: &'a Option<Box<Self>>,
) -> impl Iterator<Item = Patch<&'a str, &'a str>> + 'a {
// Only change what is necessary.
let to_add_or_replace =
Expand All @@ -236,7 +236,7 @@ impl<COMP: Component> VTag<COMP> {
}

/// Similar to `diff_attributers` except there is only a single `kind`.
fn diff_kind<'a>(&'a self, ancestor: &'a Option<Self>) -> Option<Patch<&'a str, ()>> {
fn diff_kind<'a>(&'a self, ancestor: &'a Option<Box<Self>>) -> Option<Patch<&'a str, ()>> {
match (
self.kind.as_ref(),
ancestor.as_ref().and_then(|anc| anc.kind.as_ref()),
Expand All @@ -255,7 +255,7 @@ impl<COMP: Component> VTag<COMP> {
}

/// Almost identical in spirit to `diff_kind`
fn diff_value<'a>(&'a self, ancestor: &'a Option<Self>) -> Option<Patch<&'a str, ()>> {
fn diff_value<'a>(&'a self, ancestor: &'a Option<Box<Self>>) -> Option<Patch<&'a str, ()>> {
match (
self.value.as_ref(),
ancestor.as_ref().and_then(|anc| anc.value.as_ref()),
Expand All @@ -273,7 +273,7 @@ impl<COMP: Component> VTag<COMP> {
}
}

fn apply_diffs(&mut self, element: &Element, ancestor: &Option<Self>) {
fn apply_diffs(&mut self, element: &Element, ancestor: &Option<Box<Self>>) {
// Update parameters
let changes = self.diff_classes(ancestor);
for change in changes {
Expand Down

0 comments on commit 3aa6198

Please sign in to comment.