1
1
use std:: cell:: { Cell , RefCell } ;
2
+ use std:: ops:: Deref ;
3
+ use std:: rc:: Rc ;
2
4
use alloc:: vec:: { self , Vec } ;
3
5
4
6
/// A trait to unify FnMut for GroupBy with the chunk key in IntoChunks
@@ -334,20 +336,44 @@ impl<K, I, F> GroupBy<K, I, F>
334
336
}
335
337
}
336
338
339
+ impl < K , I , F > IntoIterator for GroupBy < K , I , F >
340
+ where I : Iterator ,
341
+ F : FnMut ( & I :: Item ) -> K ,
342
+ K : PartialEq
343
+ {
344
+ type Item = ( K , Group < K , I , F , Rc < Self > > ) ;
345
+ type IntoIter = Groups < K , I , F , Rc < Self > > ;
346
+
347
+ fn into_iter ( self ) -> Self :: IntoIter {
348
+ Groups { parent : Rc :: new ( self ) }
349
+ }
350
+ }
351
+
337
352
impl < ' a , K , I , F > IntoIterator for & ' a GroupBy < K , I , F >
338
353
where I : Iterator ,
339
- I :: Item : ' a ,
340
354
F : FnMut ( & I :: Item ) -> K ,
341
355
K : PartialEq
342
356
{
343
- type Item = ( K , Group < ' a , K , I , F > ) ;
344
- type IntoIter = Groups < ' a , K , I , F > ;
357
+ type Item = ( K , Group < K , I , F , Self > ) ;
358
+ type IntoIter = Groups < K , I , F , Self > ;
345
359
346
360
fn into_iter ( self ) -> Self :: IntoIter {
347
361
Groups { parent : self }
348
362
}
349
363
}
350
364
365
+ impl < K , I , F > GroupBy < K , I , F >
366
+ where I : Iterator ,
367
+ F : FnMut ( & I :: Item ) -> K ,
368
+ K : PartialEq
369
+ {
370
+ /// This is pretty much the same as `.into_iter()`, except it uses references
371
+ /// in the underlying iterators instead of reference counts, resulting in one less
372
+ /// allocation. You may however hit lifetime errors if you require full ownership.
373
+ pub fn borrowed_iter < ' a > ( & ' a self ) -> <& ' a Self as IntoIterator >:: IntoIter {
374
+ self . into_iter ( )
375
+ }
376
+ }
351
377
352
378
/// An iterator that yields the Group iterators.
353
379
///
@@ -356,20 +382,19 @@ impl<'a, K, I, F> IntoIterator for &'a GroupBy<K, I, F>
356
382
///
357
383
/// See [`.group_by()`](../trait.Itertools.html#method.group_by) for more information.
358
384
#[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
359
- pub struct Groups < ' a , K : ' a , I : ' a , F : ' a >
385
+ pub struct Groups < K , I , F , D : Deref < Target = GroupBy < K , I , F > > >
360
386
where I : Iterator ,
361
- I :: Item : ' a
362
387
{
363
- parent : & ' a GroupBy < K , I , F > ,
388
+ parent : D ,
364
389
}
365
390
366
- impl < ' a , K , I , F > Iterator for Groups < ' a , K , I , F >
391
+ impl < K , I , F , D > Iterator for Groups < K , I , F , D >
367
392
where I : Iterator ,
368
- I :: Item : ' a ,
369
393
F : FnMut ( & I :: Item ) -> K ,
370
- K : PartialEq
394
+ K : PartialEq ,
395
+ D : Deref < Target =GroupBy < K , I , F > > + Clone ,
371
396
{
372
- type Item = ( K , Group < ' a , K , I , F > ) ;
397
+ type Item = ( K , Group < K , I , F , D > ) ;
373
398
374
399
#[ inline]
375
400
fn next ( & mut self ) -> Option < Self :: Item > {
@@ -379,7 +404,7 @@ impl<'a, K, I, F> Iterator for Groups<'a, K, I, F>
379
404
inner. step ( index) . map ( |elt| {
380
405
let key = inner. group_key ( index) ;
381
406
( key, Group {
382
- parent : self . parent ,
407
+ parent : self . parent . clone ( ) ,
383
408
index,
384
409
first : Some ( elt) ,
385
410
} )
@@ -390,29 +415,29 @@ impl<'a, K, I, F> Iterator for Groups<'a, K, I, F>
390
415
/// An iterator for the elements in a single group.
391
416
///
392
417
/// Iterator element type is `I::Item`.
393
- pub struct Group < ' a , K : ' a , I : ' a , F : ' a >
418
+ pub struct Group < K , I , F , D >
394
419
where I : Iterator ,
395
- I :: Item : ' a ,
420
+ D : Deref < Target = GroupBy < K , I , F > >
396
421
{
397
- parent : & ' a GroupBy < K , I , F > ,
422
+ parent : D ,
398
423
index : usize ,
399
424
first : Option < I :: Item > ,
400
425
}
401
426
402
- impl < ' a , K , I , F > Drop for Group < ' a , K , I , F >
427
+ impl < K , I , F , D > Drop for Group < K , I , F , D >
403
428
where I : Iterator ,
404
- I :: Item : ' a ,
429
+ D : Deref < Target = GroupBy < K , I , F > >
405
430
{
406
431
fn drop ( & mut self ) {
407
432
self . parent . drop_group ( self . index ) ;
408
433
}
409
434
}
410
435
411
- impl < ' a , K , I , F > Iterator for Group < ' a , K , I , F >
436
+ impl < K , I , F , D > Iterator for Group < K , I , F , D >
412
437
where I : Iterator ,
413
- I :: Item : ' a ,
414
438
F : FnMut ( & I :: Item ) -> K ,
415
439
K : PartialEq ,
440
+ D : Deref < Target =GroupBy < K , I , F > >
416
441
{
417
442
type Item = I :: Item ;
418
443
#[ inline]
@@ -486,12 +511,24 @@ impl<I> IntoChunks<I>
486
511
}
487
512
}
488
513
514
+ impl < I > IntoIterator for IntoChunks < I >
515
+ where I : Iterator ,
516
+ {
517
+ type Item = Chunk < I , Rc < Self > > ;
518
+ type IntoIter = Chunks < I , Rc < Self > > ;
519
+
520
+ fn into_iter ( self ) -> Self :: IntoIter {
521
+ Chunks {
522
+ parent : Rc :: new ( self ) ,
523
+ }
524
+ }
525
+ }
526
+
489
527
impl < ' a , I > IntoIterator for & ' a IntoChunks < I >
490
528
where I : Iterator ,
491
- I :: Item : ' a ,
492
529
{
493
- type Item = Chunk < ' a , I > ;
494
- type IntoIter = Chunks < ' a , I > ;
530
+ type Item = Chunk < I , Self > ;
531
+ type IntoIter = Chunks < I , Self > ;
495
532
496
533
fn into_iter ( self ) -> Self :: IntoIter {
497
534
Chunks {
@@ -500,25 +537,36 @@ impl<'a, I> IntoIterator for &'a IntoChunks<I>
500
537
}
501
538
}
502
539
540
+ impl < I > IntoChunks < I >
541
+ where I : Iterator ,
542
+ {
543
+ /// This is pretty much the same as `.into_iter()`, except it uses references
544
+ /// in the underlying iterators instead of reference counts, resulting in one less
545
+ /// allocation. You may however hit lifetime errors if you require full ownership.
546
+ pub fn borrowed_iter < ' a > ( & ' a self ) -> <& ' a Self as IntoIterator >:: IntoIter {
547
+ self . into_iter ( )
548
+ }
549
+ }
550
+
503
551
504
552
/// An iterator that yields the Chunk iterators.
505
553
///
506
554
/// Iterator element type is `Chunk`.
507
555
///
508
556
/// See [`.chunks()`](../trait.Itertools.html#method.chunks) for more information.
509
557
#[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
510
- pub struct Chunks < ' a , I : ' a >
558
+ pub struct Chunks < I , D >
511
559
where I : Iterator ,
512
- I :: Item : ' a ,
560
+ D : Deref < Target = IntoChunks < I > >
513
561
{
514
- parent : & ' a IntoChunks < I > ,
562
+ parent : D ,
515
563
}
516
564
517
- impl < ' a , I > Iterator for Chunks < ' a , I >
565
+ impl < I , D > Iterator for Chunks < I , D >
518
566
where I : Iterator ,
519
- I :: Item : ' a ,
567
+ D : Deref < Target = IntoChunks < I > > + Clone ,
520
568
{
521
- type Item = Chunk < ' a , I > ;
569
+ type Item = Chunk < I , D > ;
522
570
523
571
#[ inline]
524
572
fn next ( & mut self ) -> Option < Self :: Item > {
@@ -527,7 +575,7 @@ impl<'a, I> Iterator for Chunks<'a, I>
527
575
let inner = & mut * self . parent . inner . borrow_mut ( ) ;
528
576
inner. step ( index) . map ( |elt| {
529
577
Chunk {
530
- parent : self . parent ,
578
+ parent : self . parent . clone ( ) ,
531
579
index,
532
580
first : Some ( elt) ,
533
581
}
@@ -538,27 +586,27 @@ impl<'a, I> Iterator for Chunks<'a, I>
538
586
/// An iterator for the elements in a single chunk.
539
587
///
540
588
/// Iterator element type is `I::Item`.
541
- pub struct Chunk < ' a , I : ' a >
589
+ pub struct Chunk < I , D >
542
590
where I : Iterator ,
543
- I :: Item : ' a ,
591
+ D : Deref < Target = IntoChunks < I > >
544
592
{
545
- parent : & ' a IntoChunks < I > ,
593
+ parent : D ,
546
594
index : usize ,
547
595
first : Option < I :: Item > ,
548
596
}
549
597
550
- impl < ' a , I > Drop for Chunk < ' a , I >
598
+ impl < I , D > Drop for Chunk < I , D >
551
599
where I : Iterator ,
552
- I :: Item : ' a ,
600
+ D : Deref < Target = IntoChunks < I > >
553
601
{
554
602
fn drop ( & mut self ) {
555
603
self . parent . drop_group ( self . index ) ;
556
604
}
557
605
}
558
606
559
- impl < ' a , I > Iterator for Chunk < ' a , I >
607
+ impl < I , D > Iterator for Chunk < I , D >
560
608
where I : Iterator ,
561
- I :: Item : ' a ,
609
+ D : Deref < Target = IntoChunks < I > >
562
610
{
563
611
type Item = I :: Item ;
564
612
#[ inline]
0 commit comments