@@ -20,12 +20,6 @@ use std::collections::VecDeque;
2020// We can only access the type if all requirements for the `ContainerBuilder` implementation are
2121// satisfied.
2222pub trait Container : Default {
23- /// The type of elements when reading non-destructively from the container.
24- type ItemRef < ' a > where Self : ' a ;
25-
26- /// The type of elements when draining the container.
27- type Item < ' a > where Self : ' a ;
28-
2923 /// The number of elements in this container
3024 ///
3125 /// This number is used in progress tracking to confirm the receipt of some number
@@ -38,16 +32,24 @@ pub trait Container: Default {
3832 fn is_empty ( & self ) -> bool {
3933 self . len ( ) == 0
4034 }
35+ }
4136
37+ /// TODO
38+ pub trait IterContainer {
39+ /// The type of elements when reading non-destructively from the container.
40+ type ItemRef < ' a > where Self : ' a ;
4241 /// Iterator type when reading from the container.
4342 type Iter < ' a > : Iterator < Item =Self :: ItemRef < ' a > > where Self : ' a ;
44-
4543 /// Returns an iterator that reads the contents of this container.
4644 fn iter ( & self ) -> Self :: Iter < ' _ > ;
45+ }
4746
47+ /// TODO
48+ pub trait DrainContainer {
49+ /// The type of elements when draining the container.
50+ type Item < ' a > where Self : ' a ;
4851 /// Iterator type when draining the container.
4952 type DrainIter < ' a > : Iterator < Item =Self :: Item < ' a > > where Self : ' a ;
50-
5153 /// Returns an iterator that drains the contents of this container.
5254 /// Drain leaves the container in an undefined state.
5355 fn drain ( & mut self ) -> Self :: DrainIter < ' _ > ;
@@ -112,8 +114,9 @@ pub trait ContainerBuilder: Default + 'static {
112114 /// Partitions `container` among `builders`, using the function `index` to direct items.
113115 fn partition < I > ( container : & mut Self :: Container , builders : & mut [ Self ] , mut index : I )
114116 where
115- Self : for < ' a > PushInto < <Self :: Container as Container >:: Item < ' a > > ,
116- I : for < ' a > FnMut ( & <Self :: Container as Container >:: Item < ' a > ) -> usize ,
117+ Self :: Container : DrainContainer ,
118+ Self : for < ' a > PushInto < <Self :: Container as DrainContainer >:: Item < ' a > > ,
119+ I : for < ' a > FnMut ( & <Self :: Container as DrainContainer >:: Item < ' a > ) -> usize ,
117120 {
118121 for datum in container. drain ( ) {
119122 let index = index ( & datum) ;
@@ -195,25 +198,25 @@ impl<C: Container + Clone + 'static> ContainerBuilder for CapacityContainerBuild
195198impl < C : Container + Clone + ' static > LengthPreservingContainerBuilder for CapacityContainerBuilder < C > { }
196199
197200impl < T > Container for Vec < T > {
198- type ItemRef < ' a > = & ' a T where T : ' a ;
199- type Item < ' a > = T where T : ' a ;
200-
201201 fn len ( & self ) -> usize {
202202 Vec :: len ( self )
203203 }
204-
205204 fn is_empty ( & self ) -> bool {
206205 Vec :: is_empty ( self )
207206 }
207+ }
208208
209+ impl < T > IterContainer for Vec < T > {
210+ type ItemRef < ' a > = & ' a T where T : ' a ;
209211 type Iter < ' a > = std:: slice:: Iter < ' a , T > where Self : ' a ;
210-
211212 fn iter ( & self ) -> Self :: Iter < ' _ > {
212213 self . as_slice ( ) . iter ( )
213214 }
215+ }
214216
217+ impl < T > DrainContainer for Vec < T > {
218+ type Item < ' a > = T where T : ' a ;
215219 type DrainIter < ' a > = std:: vec:: Drain < ' a , T > where Self : ' a ;
216-
217220 fn drain ( & mut self ) -> Self :: DrainIter < ' _ > {
218221 self . drain ( ..)
219222 }
@@ -261,63 +264,47 @@ mod rc {
261264 use std:: ops:: Deref ;
262265 use std:: rc:: Rc ;
263266
264- use crate :: Container ;
267+ use crate :: { Container , IterContainer , DrainContainer } ;
265268
266269 impl < T : Container > Container for Rc < T > {
267- type ItemRef < ' a > = T :: ItemRef < ' a > where Self : ' a ;
268- type Item < ' a > = T :: ItemRef < ' a > where Self : ' a ;
269-
270270 fn len ( & self ) -> usize {
271271 std:: ops:: Deref :: deref ( self ) . len ( )
272272 }
273-
274273 fn is_empty ( & self ) -> bool {
275274 std:: ops:: Deref :: deref ( self ) . is_empty ( )
276275 }
277-
276+ }
277+ impl < T : IterContainer > IterContainer for Rc < T > {
278+ type ItemRef < ' a > = T :: ItemRef < ' a > where Self : ' a ;
278279 type Iter < ' a > = T :: Iter < ' a > where Self : ' a ;
279-
280- fn iter ( & self ) -> Self :: Iter < ' _ > {
281- self . deref ( ) . iter ( )
282- }
283-
280+ fn iter ( & self ) -> Self :: Iter < ' _ > { self . deref ( ) . iter ( ) }
281+ }
282+ impl < T : IterContainer > DrainContainer for Rc < T > {
283+ type Item < ' a > = T :: ItemRef < ' a > where Self : ' a ;
284284 type DrainIter < ' a > = T :: Iter < ' a > where Self : ' a ;
285-
286- fn drain ( & mut self ) -> Self :: DrainIter < ' _ > {
287- self . iter ( )
288- }
285+ fn drain ( & mut self ) -> Self :: DrainIter < ' _ > { self . iter ( ) }
289286 }
290287}
291288
292289mod arc {
293290 use std:: ops:: Deref ;
294291 use std:: sync:: Arc ;
295292
296- use crate :: Container ;
293+ use crate :: { Container , IterContainer , DrainContainer } ;
297294
298295 impl < T : Container > Container for Arc < T > {
296+ fn len ( & self ) -> usize { std:: ops:: Deref :: deref ( self ) . len ( ) }
297+ fn is_empty ( & self ) -> bool { std:: ops:: Deref :: deref ( self ) . is_empty ( ) }
298+ }
299+ impl < T : IterContainer > IterContainer for Arc < T > {
299300 type ItemRef < ' a > = T :: ItemRef < ' a > where Self : ' a ;
300- type Item < ' a > = T :: ItemRef < ' a > where Self : ' a ;
301-
302- fn len ( & self ) -> usize {
303- std:: ops:: Deref :: deref ( self ) . len ( )
304- }
305-
306- fn is_empty ( & self ) -> bool {
307- std:: ops:: Deref :: deref ( self ) . is_empty ( )
308- }
309-
310301 type Iter < ' a > = T :: Iter < ' a > where Self : ' a ;
311-
312- fn iter ( & self ) -> Self :: Iter < ' _ > {
313- self . deref ( ) . iter ( )
314- }
315-
302+ fn iter ( & self ) -> Self :: Iter < ' _ > { self . deref ( ) . iter ( ) }
303+ }
304+ impl < T : IterContainer > DrainContainer for Arc < T > {
305+ type Item < ' a > = T :: ItemRef < ' a > where Self : ' a ;
316306 type DrainIter < ' a > = T :: Iter < ' a > where Self : ' a ;
317-
318- fn drain ( & mut self ) -> Self :: DrainIter < ' _ > {
319- self . iter ( )
320- }
307+ fn drain ( & mut self ) -> Self :: DrainIter < ' _ > { self . iter ( ) }
321308 }
322309}
323310
0 commit comments