Skip to content

Commit 2401701

Browse files
committed
Revert "Allow iterating over ref of ChildrenRenderer"
This reverts commit 71bcd09.
1 parent 71bcd09 commit 2401701

File tree

2 files changed

+31
-74
lines changed

2 files changed

+31
-74
lines changed

packages/yew/src/html/component/children.rs

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
use std::fmt;
44
use std::rc::Rc;
55

6-
use implicit_clone::{unsync::IArray, ImplicitClone};
7-
86
use crate::html::Html;
9-
use crate::utils::RcExt;
107
use crate::virtual_dom::{VChild, VComp, VList, VNode};
118
use crate::{BaseComponent, Properties};
129

@@ -155,27 +152,23 @@ pub type ChildrenWithProps<CHILD> = ChildrenRenderer<VChild<CHILD>>;
155152

156153
/// A type used for rendering children html.
157154
#[derive(Clone)]
158-
pub struct ChildrenRenderer<T: Clone + 'static> {
159-
pub(crate) children: IArray<Rc<T>>,
155+
pub struct ChildrenRenderer<T> {
156+
pub(crate) children: Vec<T>,
160157
}
161158

162-
impl<T: Clone> ImplicitClone for ChildrenRenderer<T> {}
163-
164-
impl<T: Clone + PartialEq> PartialEq for ChildrenRenderer<T> {
159+
impl<T: PartialEq> PartialEq for ChildrenRenderer<T> {
165160
fn eq(&self, other: &Self) -> bool {
166161
self.children == other.children
167162
}
168163
}
169164

170165
impl<T> ChildrenRenderer<T>
171166
where
172-
T: Clone + 'static,
167+
T: Clone,
173168
{
174169
/// Create children
175170
pub fn new(children: Vec<T>) -> Self {
176-
Self {
177-
children: children.into_iter().map(Rc::new).collect(),
178-
}
171+
Self { children }
179172
}
180173

181174
/// Children list is empty
@@ -192,8 +185,7 @@ where
192185
pub fn iter(&self) -> impl Iterator<Item = T> + '_ {
193186
// clone each child lazily.
194187
// This way `self.iter().next()` only has to clone a single node.
195-
// TODO not sure if I shouldnt keep the Rc here
196-
self.children.iter().map(RcExt::unwrap_or_clone)
188+
self.children.iter().cloned()
197189
}
198190

199191
/// Convert the children elements to another object (if there are any).
@@ -205,7 +197,7 @@ where
205197
/// children.map(|children| {
206198
/// html! {
207199
/// <div class={classes!("container")}>
208-
/// {children}
200+
/// {children.clone()}
209201
/// </div>
210202
/// }
211203
/// })
@@ -218,63 +210,37 @@ where
218210
closure(self)
219211
}
220212
}
221-
222-
pub(crate) fn to_vec(&self) -> Vec<T> {
223-
self.iter().collect()
224-
}
225213
}
226214

227-
impl<T: Clone> Default for ChildrenRenderer<T> {
215+
impl<T> Default for ChildrenRenderer<T> {
228216
fn default() -> Self {
229217
Self {
230-
children: Default::default(),
218+
children: Vec::new(),
231219
}
232220
}
233221
}
234222

235-
impl<T: Clone> fmt::Debug for ChildrenRenderer<T> {
223+
impl<T> fmt::Debug for ChildrenRenderer<T> {
236224
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237225
f.write_str("ChildrenRenderer<_>")
238226
}
239227
}
240228

241-
#[derive(Debug)]
242-
#[doc(hidden)]
243-
pub struct Iter<T: Clone + 'static> {
244-
children: implicit_clone::unsync::Iter<Rc<T>>,
245-
}
246-
247-
impl<T: Clone + 'static> Iterator for Iter<T> {
248-
type Item = T;
249-
250-
fn next(&mut self) -> Option<Self::Item> {
251-
self.children.next().map(|x| RcExt::unwrap_or_clone(x))
252-
}
253-
}
254-
255-
impl<T: ImplicitClone> IntoIterator for ChildrenRenderer<T> {
256-
type IntoIter = Iter<T>;
229+
impl<T> IntoIterator for ChildrenRenderer<T> {
230+
type IntoIter = std::vec::IntoIter<Self::Item>;
257231
type Item = T;
258232

259233
fn into_iter(self) -> Self::IntoIter {
260-
Iter {
261-
children: self.children.iter(),
262-
}
263-
}
264-
}
265-
266-
impl<T: Clone> FromIterator<T> for ChildrenRenderer<T> {
267-
fn from_iter<IT: IntoIterator<Item = T>>(it: IT) -> Self {
268-
Self {
269-
children: it.into_iter().map(Rc::new).collect(),
270-
}
234+
self.children.into_iter()
271235
}
272236
}
273237

274238
impl From<ChildrenRenderer<Html>> for Html {
275-
fn from(val: ChildrenRenderer<Html>) -> Self {
239+
fn from(mut val: ChildrenRenderer<Html>) -> Self {
276240
if val.children.len() == 1 {
277-
return RcExt::unwrap_or_clone(val.children[0].clone());
241+
if let Some(m) = val.children.pop() {
242+
return m;
243+
}
278244
}
279245

280246
Html::VList(Rc::new(val.into()))
@@ -286,7 +252,7 @@ impl From<ChildrenRenderer<Html>> for VList {
286252
if val.is_empty() {
287253
return VList::new();
288254
}
289-
VList::with_children(val.to_vec(), None)
255+
VList::with_children(val.children, None)
290256
}
291257
}
292258

@@ -319,7 +285,7 @@ mod tests {
319285

320286
#[test]
321287
fn children_map() {
322-
let children = Children::new(Default::default());
288+
let children = Children::new(vec![]);
323289
let res = children.map(|children| Some(children.clone()));
324290
assert!(res.is_none());
325291
let children = Children::new(vec![Default::default()]);

packages/yew/src/html/conversion/into_prop_value.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,47 +86,47 @@ where
8686
impl<T, C> IntoPropValue<ChildrenRenderer<C>> for VChild<T>
8787
where
8888
T: BaseComponent,
89-
C: ImplicitClone + Into<VNode>,
89+
C: Clone + Into<VNode>,
9090
VChild<T>: Into<C>,
9191
{
9292
#[inline]
9393
fn into_prop_value(self) -> ChildrenRenderer<C> {
94-
[self.into()].into_iter().collect()
94+
ChildrenRenderer::new(vec![self.into()])
9595
}
9696
}
9797

9898
impl<T, C> IntoPropValue<Option<ChildrenRenderer<C>>> for VChild<T>
9999
where
100100
T: BaseComponent,
101-
C: ImplicitClone + Into<VNode>,
101+
C: Clone + Into<VNode>,
102102
VChild<T>: Into<C>,
103103
{
104104
#[inline]
105105
fn into_prop_value(self) -> Option<ChildrenRenderer<C>> {
106-
Some([self.into()].into_iter().collect())
106+
Some(ChildrenRenderer::new(vec![self.into()]))
107107
}
108108
}
109109

110110
impl<T, C> IntoPropValue<Option<ChildrenRenderer<C>>> for Option<VChild<T>>
111111
where
112112
T: BaseComponent,
113-
C: ImplicitClone + Into<VNode>,
113+
C: Clone + Into<VNode>,
114114
VChild<T>: Into<C>,
115115
{
116116
#[inline]
117117
fn into_prop_value(self) -> Option<ChildrenRenderer<C>> {
118-
self.map(|m| [m.into()].into_iter().collect())
118+
self.map(|m| ChildrenRenderer::new(vec![m.into()]))
119119
}
120120
}
121121

122122
impl<T, R> IntoPropValue<ChildrenRenderer<R>> for Vec<T>
123123
where
124124
T: Into<R>,
125-
R: ImplicitClone + Into<VNode>,
125+
R: Clone + Into<VNode>,
126126
{
127127
#[inline]
128128
fn into_prop_value(self) -> ChildrenRenderer<R> {
129-
self.into_iter().map(|m| m.into()).collect()
129+
ChildrenRenderer::new(self.into_iter().map(|m| m.into()).collect())
130130
}
131131
}
132132

@@ -167,33 +167,24 @@ impl IntoPropValue<VNode> for ChildrenRenderer<VNode> {
167167
}
168168
}
169169

170-
impl IntoPropValue<VNode> for &ChildrenRenderer<VNode> {
171-
#[inline]
172-
fn into_prop_value(self) -> VNode {
173-
VNode::VList(Rc::new(self.clone().into()))
174-
}
175-
}
176-
177170
impl IntoPropValue<ChildrenRenderer<VNode>> for VNode {
178171
#[inline]
179172
fn into_prop_value(self) -> ChildrenRenderer<VNode> {
180-
[self].into_iter().collect()
173+
ChildrenRenderer::new(vec![self])
181174
}
182175
}
183176

184177
impl IntoPropValue<ChildrenRenderer<VNode>> for VText {
185178
#[inline]
186179
fn into_prop_value(self) -> ChildrenRenderer<VNode> {
187-
[self.into()].into_iter().collect()
180+
ChildrenRenderer::new(vec![self.into()])
188181
}
189182
}
190183

191184
impl IntoPropValue<VList> for ChildrenRenderer<VNode> {
192185
#[inline]
193186
fn into_prop_value(self) -> VList {
194-
// TODO converting IArray to Vec doesn't seem optimal, but it's too complicated to change
195-
// VList for now
196-
VList::with_children(self.to_vec(), None)
187+
VList::with_children(self.children, None)
197188
}
198189
}
199190

@@ -206,7 +197,7 @@ impl<C: BaseComponent> IntoPropValue<VList> for VChild<C> {
206197

207198
impl IntoPropValue<ChildrenRenderer<VNode>> for AttrValue {
208199
fn into_prop_value(self) -> ChildrenRenderer<VNode> {
209-
[VNode::VText(VText::new(self))].into_iter().collect()
200+
ChildrenRenderer::new(vec![VNode::VText(VText::new(self))])
210201
}
211202
}
212203

0 commit comments

Comments
 (0)