33use std:: fmt;
44use std:: rc:: Rc ;
55
6- use implicit_clone:: { unsync:: IArray , ImplicitClone } ;
7-
86use crate :: html:: Html ;
9- use crate :: utils:: RcExt ;
107use crate :: virtual_dom:: { VChild , VComp , VList , VNode } ;
118use 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
170165impl < T > ChildrenRenderer < T >
171166where
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
274238impl 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 ( ) ] ) ;
0 commit comments