Skip to content

Commit

Permalink
Simplify code, restore original with_children() signature, optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
cecton committed Nov 6, 2023
1 parent ab1e138 commit 38d45d9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
9 changes: 1 addition & 8 deletions packages/yew/src/html/component/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,7 @@ impl From<ChildrenRenderer<Html>> for Html {

impl From<ChildrenRenderer<Html>> for VList {
fn from(val: ChildrenRenderer<Html>) -> 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)
}
}

Expand Down
16 changes: 4 additions & 12 deletions packages/yew/src/html/conversion/into_prop_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ impl IntoPropValue<VNode> for ChildrenRenderer<VNode> {
impl IntoPropValue<VNode> for &ChildrenRenderer<VNode> {
#[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())))
}
}

Expand All @@ -195,18 +191,14 @@ impl IntoPropValue<ChildrenRenderer<VNode>> for VText {
impl IntoPropValue<VList> for ChildrenRenderer<VNode> {
#[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<C: BaseComponent> IntoPropValue<VList> for VChild<C> {
#[inline]
fn into_prop_value(self) -> VList {
VList::with_children(vec![self.into()], None)
VList::from_iter([VNode::from(self)].into_iter())
}
}

Expand All @@ -219,7 +211,7 @@ impl IntoPropValue<ChildrenRenderer<VNode>> for AttrValue {
impl IntoPropValue<VNode> for Vec<VNode> {
#[inline]
fn into_prop_value(self) -> VNode {
VNode::VList(Rc::new(VList::with_children(self, None)))
VNode::VList(Rc::new(VList::from_iter(self)))
}
}

Expand Down
55 changes: 47 additions & 8 deletions packages/yew/src/virtual_dom/vlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,37 @@ impl DerefMut for VList {
}
}

impl<A: Into<VNode>> FromIterator<A> for VList {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
let children = iter.into_iter().map(|n| n.into()).collect::<Vec<_>>();
if children.is_empty() {
VList::new()
} else {
VList {
children: Some(Rc::new(children)),
key: None,
fully_keyed: FullyKeyedState::KnownFullyKeyed,
}
}
}
}

impl From<Option<Rc<Vec<VNode>>>> for VList {
fn from(children: Option<Rc<Vec<VNode>>>) -> 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 {
Expand All @@ -72,14 +103,22 @@ impl VList {
}

/// Creates a new [VList] instance with children.
pub fn with_children(children: impl Into<Rc<Vec<VNode>>>, key: Option<Key>) -> 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<VNode>, key: Option<Key>) -> 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.
Expand Down
8 changes: 3 additions & 5 deletions packages/yew/src/virtual_dom/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}
}
}
Expand Down Expand Up @@ -172,9 +171,8 @@ impl<T: ToString> From<T> for VNode {

impl<A: Into<VNode>> FromIterator<A> for VNode {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
VNode::VList(Rc::new(VList::with_children(
iter.into_iter().map(|n| n.into()).collect::<Vec<_>>(),
None,
VNode::VList(Rc::new(VList::from_iter(
iter.into_iter().map(|n| n.into()),
)))
}
}
Expand Down

0 comments on commit 38d45d9

Please sign in to comment.