@@ -501,6 +501,104 @@ impl<I> FusedIterator for Rev<I>
501
501
unsafe impl < I > TrustedLen for Rev < I >
502
502
where I : TrustedLen + DoubleEndedIterator { }
503
503
504
+ /// An iterator that copies the elements of an underlying iterator.
505
+ ///
506
+ /// This `struct` is created by the [`copied`] method on [`Iterator`]. See its
507
+ /// documentation for more.
508
+ ///
509
+ /// [`copied`]: trait.Iterator.html#method.copied
510
+ /// [`Iterator`]: trait.Iterator.html
511
+ #[ unstable( feature = "iter_copied" , issue = "0" ) ]
512
+ #[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
513
+ #[ derive( Clone , Debug ) ]
514
+ pub struct Copied < I > {
515
+ it : I ,
516
+ }
517
+
518
+ #[ unstable( feature = "iter_copied" , issue = "0" ) ]
519
+ impl < ' a , I , T : ' a > Iterator for Copied < I >
520
+ where I : Iterator < Item =& ' a T > , T : Copy
521
+ {
522
+ type Item = T ;
523
+
524
+ fn next ( & mut self ) -> Option < T > {
525
+ self . it . next ( ) . copied ( )
526
+ }
527
+
528
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
529
+ self . it . size_hint ( )
530
+ }
531
+
532
+ fn try_fold < B , F , R > ( & mut self , init : B , mut f : F ) -> R where
533
+ Self : Sized , F : FnMut ( B , Self :: Item ) -> R , R : Try < Ok =B >
534
+ {
535
+ self . it . try_fold ( init, move |acc, & elt| f ( acc, elt) )
536
+ }
537
+
538
+ fn fold < Acc , F > ( self , init : Acc , mut f : F ) -> Acc
539
+ where F : FnMut ( Acc , Self :: Item ) -> Acc ,
540
+ {
541
+ self . it . fold ( init, move |acc, & elt| f ( acc, elt) )
542
+ }
543
+ }
544
+
545
+ #[ unstable( feature = "iter_copied" , issue = "0" ) ]
546
+ impl < ' a , I , T : ' a > DoubleEndedIterator for Copied < I >
547
+ where I : DoubleEndedIterator < Item =& ' a T > , T : Copy
548
+ {
549
+ fn next_back ( & mut self ) -> Option < T > {
550
+ self . it . next_back ( ) . cloned ( )
551
+ }
552
+
553
+ fn try_rfold < B , F , R > ( & mut self , init : B , mut f : F ) -> R where
554
+ Self : Sized , F : FnMut ( B , Self :: Item ) -> R , R : Try < Ok =B >
555
+ {
556
+ self . it . try_rfold ( init, move |acc, elt| f ( acc, elt. clone ( ) ) )
557
+ }
558
+
559
+ fn rfold < Acc , F > ( self , init : Acc , mut f : F ) -> Acc
560
+ where F : FnMut ( Acc , Self :: Item ) -> Acc ,
561
+ {
562
+ self . it . rfold ( init, move |acc, elt| f ( acc, elt. clone ( ) ) )
563
+ }
564
+ }
565
+
566
+ #[ unstable( feature = "iter_copied" , issue = "0" ) ]
567
+ impl < ' a , I , T : ' a > ExactSizeIterator for Copied < I >
568
+ where I : ExactSizeIterator < Item =& ' a T > , T : Copy
569
+ {
570
+ fn len ( & self ) -> usize {
571
+ self . it . len ( )
572
+ }
573
+
574
+ fn is_empty ( & self ) -> bool {
575
+ self . it . is_empty ( )
576
+ }
577
+ }
578
+
579
+ #[ unstable( feature = "iter_copied" , issue = "0" ) ]
580
+ impl < ' a , I , T : ' a > FusedIterator for Copied < I >
581
+ where I : FusedIterator < Item =& ' a T > , T : Copy
582
+ { }
583
+
584
+ #[ doc( hidden) ]
585
+ unsafe impl < ' a , I , T : ' a > TrustedRandomAccess for Copied < I >
586
+ where I : TrustedRandomAccess < Item =& ' a T > , T : Copy
587
+ {
588
+ unsafe fn get_unchecked ( & mut self , i : usize ) -> Self :: Item {
589
+ * self . it . get_unchecked ( i)
590
+ }
591
+
592
+ #[ inline]
593
+ fn may_have_side_effect ( ) -> bool { false }
594
+ }
595
+
596
+ #[ unstable( feature = "iter_copied" , issue = "0" ) ]
597
+ unsafe impl < ' a , I , T : ' a > TrustedLen for Copied < I >
598
+ where I : TrustedLen < Item =& ' a T > ,
599
+ T : Copy
600
+ { }
601
+
504
602
/// An iterator that clones the elements of an underlying iterator.
505
603
///
506
604
/// This `struct` is created by the [`cloned`] method on [`Iterator`]. See its
0 commit comments