diff --git a/packages/yew/src/html/component/children.rs b/packages/yew/src/html/component/children.rs index c428217c116..94205309bb5 100644 --- a/packages/yew/src/html/component/children.rs +++ b/packages/yew/src/html/component/children.rs @@ -278,14 +278,7 @@ impl From> for Html { impl From> for VList { fn from(val: ChildrenRenderer) -> Self { - if let Some(children) = val.children { - if children.is_empty() { - return VList::new(); - } - VList::with_children(children, None) - } else { - VList::new() - } + VList::from(val.children) } } diff --git a/packages/yew/src/html/conversion/into_prop_value.rs b/packages/yew/src/html/conversion/into_prop_value.rs index f7f653396be..3a75f6b42d2 100644 --- a/packages/yew/src/html/conversion/into_prop_value.rs +++ b/packages/yew/src/html/conversion/into_prop_value.rs @@ -170,11 +170,7 @@ impl IntoPropValue for ChildrenRenderer { impl IntoPropValue for &ChildrenRenderer { #[inline] fn into_prop_value(self) -> VNode { - if let Some(children) = self.children.clone() { - VNode::VList(Rc::new(VList::with_children(children, None))) - } else { - VNode::VList(Rc::new(VList::new())) - } + VNode::VList(Rc::new(VList::from(self.children.clone()))) } } @@ -195,18 +191,14 @@ impl IntoPropValue> for VText { impl IntoPropValue for ChildrenRenderer { #[inline] fn into_prop_value(self) -> VList { - if let Some(children) = self.children { - VList::with_children(children, None) - } else { - VList::new() - } + VList::from(self.children) } } impl IntoPropValue for VChild { #[inline] fn into_prop_value(self) -> VList { - VList::with_children(vec![self.into()], None) + VList::from_iter([VNode::from(self)].into_iter()) } } @@ -219,7 +211,7 @@ impl IntoPropValue> for AttrValue { impl IntoPropValue for Vec { #[inline] fn into_prop_value(self) -> VNode { - VNode::VList(Rc::new(VList::with_children(self, None))) + VNode::VList(Rc::new(VList::from_iter(self))) } } diff --git a/packages/yew/src/virtual_dom/vlist.rs b/packages/yew/src/virtual_dom/vlist.rs index 0d14a483cb2..bd366204eb4 100644 --- a/packages/yew/src/virtual_dom/vlist.rs +++ b/packages/yew/src/virtual_dom/vlist.rs @@ -61,6 +61,37 @@ impl DerefMut for VList { } } +impl> FromIterator for VList { + fn from_iter>(iter: T) -> Self { + let children = iter.into_iter().map(|n| n.into()).collect::>(); + if children.is_empty() { + VList::new() + } else { + VList { + children: Some(Rc::new(children)), + key: None, + fully_keyed: FullyKeyedState::KnownFullyKeyed, + } + } + } +} + +impl From>>> for VList { + fn from(children: Option>>) -> Self { + if children.as_ref().map(|x| x.is_empty()).unwrap_or(true) { + VList::new() + } else { + let mut vlist = VList { + fully_keyed: FullyKeyedState::Unknown, + children, + key: None, + }; + vlist.recheck_fully_keyed(); + vlist + } + } +} + impl VList { /// Creates a new empty [VList] instance. pub const fn new() -> Self { @@ -72,14 +103,22 @@ impl VList { } /// Creates a new [VList] instance with children. - pub fn with_children(children: impl Into>>, key: Option) -> Self { - let mut vlist = VList { - fully_keyed: FullyKeyedState::Unknown, - children: Some(children.into()), - key, - }; - vlist.recheck_fully_keyed(); - vlist + pub fn with_children(children: Vec, key: Option) -> Self { + if children.is_empty() { + VList { + fully_keyed: FullyKeyedState::KnownFullyKeyed, + children: None, + key, + } + } else { + let mut vlist = VList { + fully_keyed: FullyKeyedState::Unknown, + children: Some(Rc::new(children)), + key, + }; + vlist.recheck_fully_keyed(); + vlist + } } // Returns a mutable reference to children, allocates the children if it hasn't been done. diff --git a/packages/yew/src/virtual_dom/vnode.rs b/packages/yew/src/virtual_dom/vnode.rs index 2579188d1f2..8ae86098284 100644 --- a/packages/yew/src/virtual_dom/vnode.rs +++ b/packages/yew/src/virtual_dom/vnode.rs @@ -65,8 +65,7 @@ impl VNode { match *self { Self::VList(ref mut m) => return Rc::make_mut(m), _ => { - *self = - VNode::VList(Rc::new(VList::with_children(vec![mem::take(self)], None))); + *self = VNode::VList(Rc::new(VList::from_iter([mem::take(self)].into_iter()))); } } } @@ -172,9 +171,8 @@ impl From for VNode { impl> FromIterator for VNode { fn from_iter>(iter: T) -> Self { - VNode::VList(Rc::new(VList::with_children( - iter.into_iter().map(|n| n.into()).collect::>(), - None, + VNode::VList(Rc::new(VList::from_iter( + iter.into_iter().map(|n| n.into()), ))) } }