301
301
302
302
use clone:: Clone ;
303
303
use cmp;
304
+ use default:: Default ;
304
305
use fmt;
305
306
use iter_private:: TrustedRandomAccess ;
306
307
use ops:: FnMut ;
@@ -624,8 +625,7 @@ impl<A, B> DoubleEndedIterator for Chain<A, B> where
624
625
pub struct Zip < A , B > {
625
626
a : A ,
626
627
b : B ,
627
- index : usize ,
628
- len : usize ,
628
+ spec : <( A , B ) as ZipImplData >:: Data ,
629
629
}
630
630
631
631
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -667,6 +667,17 @@ trait ZipImpl<A, B> {
667
667
B : DoubleEndedIterator + ExactSizeIterator ;
668
668
}
669
669
670
+ // Zip specialization data members
671
+ #[ doc( hidden) ]
672
+ trait ZipImplData {
673
+ type Data : ' static + Clone + Default + fmt:: Debug ;
674
+ }
675
+
676
+ #[ doc( hidden) ]
677
+ impl < T > ZipImplData for T {
678
+ default type Data = ( ) ;
679
+ }
680
+
670
681
// General Zip impl
671
682
#[ doc( hidden) ]
672
683
impl < A , B > ZipImpl < A , B > for Zip < A , B >
@@ -677,8 +688,7 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
677
688
Zip {
678
689
a : a,
679
690
b : b,
680
- index : 0 , // not used in general case
681
- len : 0 ,
691
+ spec : Default :: default ( ) , // unused
682
692
}
683
693
}
684
694
@@ -731,6 +741,20 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
731
741
}
732
742
}
733
743
744
+ #[ doc( hidden) ]
745
+ #[ derive( Default , Debug , Clone ) ]
746
+ struct ZipImplFields {
747
+ index : usize ,
748
+ len : usize ,
749
+ }
750
+
751
+ #[ doc( hidden) ]
752
+ impl < A , B > ZipImplData for ( A , B )
753
+ where A : TrustedRandomAccess , B : TrustedRandomAccess
754
+ {
755
+ type Data = ZipImplFields ;
756
+ }
757
+
734
758
#[ doc( hidden) ]
735
759
impl < A , B > ZipImpl < A , B > for Zip < A , B >
736
760
where A : TrustedRandomAccess , B : TrustedRandomAccess
@@ -740,16 +764,18 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
740
764
Zip {
741
765
a : a,
742
766
b : b,
743
- index : 0 ,
744
- len : len,
767
+ spec : ZipImplFields {
768
+ index : 0 ,
769
+ len : len,
770
+ }
745
771
}
746
772
}
747
773
748
774
#[ inline]
749
775
fn next ( & mut self ) -> Option < ( A :: Item , B :: Item ) > {
750
- if self . index < self . len {
751
- let i = self . index ;
752
- self . index += 1 ;
776
+ if self . spec . index < self . spec . len {
777
+ let i = self . spec . index ;
778
+ self . spec . index += 1 ;
753
779
unsafe {
754
780
Some ( ( self . a . get_unchecked ( i) , self . b . get_unchecked ( i) ) )
755
781
}
@@ -760,7 +786,7 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
760
786
761
787
#[ inline]
762
788
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
763
- let len = self . len - self . index ;
789
+ let len = self . spec . len - self . spec . index ;
764
790
( len, Some ( len) )
765
791
}
766
792
@@ -769,9 +795,9 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
769
795
where A : DoubleEndedIterator + ExactSizeIterator ,
770
796
B : DoubleEndedIterator + ExactSizeIterator
771
797
{
772
- if self . index < self . len {
773
- self . len -= 1 ;
774
- let i = self . len ;
798
+ if self . spec . index < self . spec . len {
799
+ self . spec . len -= 1 ;
800
+ let i = self . spec . len ;
775
801
unsafe {
776
802
Some ( ( self . a . get_unchecked ( i) , self . b . get_unchecked ( i) ) )
777
803
}
0 commit comments