@@ -7,6 +7,11 @@ use intrinsics;
77use super :: { Iterator , DoubleEndedIterator , ExactSizeIterator , FusedIterator , TrustedLen } ;
88use super :: LoopState ;
99
10+ mod zip;
11+
12+ pub use self :: zip:: Zip ;
13+ pub ( super ) use self :: zip:: ZipImpl ;
14+
1015/// A double-ended iterator with the direction inverted.
1116///
1217/// This `struct` is created by the [`rev`] method on [`Iterator`]. See its
@@ -703,264 +708,6 @@ unsafe impl<A, B> TrustedLen for Chain<A, B>
703708 where A : TrustedLen , B : TrustedLen < Item =A :: Item > ,
704709{ }
705710
706- /// An iterator that iterates two other iterators simultaneously.
707- ///
708- /// This `struct` is created by the [`zip`] method on [`Iterator`]. See its
709- /// documentation for more.
710- ///
711- /// [`zip`]: trait.Iterator.html#method.zip
712- /// [`Iterator`]: trait.Iterator.html
713- #[ derive( Clone , Debug ) ]
714- #[ must_use = "iterators are lazy and do nothing unless consumed" ]
715- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
716- pub struct Zip < A , B > {
717- a : A ,
718- b : B ,
719- // index and len are only used by the specialized version of zip
720- index : usize ,
721- len : usize ,
722- }
723-
724- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
725- impl < A , B > Iterator for Zip < A , B > where A : Iterator , B : Iterator
726- {
727- type Item = ( A :: Item , B :: Item ) ;
728-
729- #[ inline]
730- fn next ( & mut self ) -> Option < Self :: Item > {
731- ZipImpl :: next ( self )
732- }
733-
734- #[ inline]
735- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
736- ZipImpl :: size_hint ( self )
737- }
738-
739- #[ inline]
740- fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
741- ZipImpl :: nth ( self , n)
742- }
743- }
744-
745- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
746- impl < A , B > DoubleEndedIterator for Zip < A , B > where
747- A : DoubleEndedIterator + ExactSizeIterator ,
748- B : DoubleEndedIterator + ExactSizeIterator ,
749- {
750- #[ inline]
751- fn next_back ( & mut self ) -> Option < ( A :: Item , B :: Item ) > {
752- ZipImpl :: next_back ( self )
753- }
754- }
755-
756- // Zip specialization trait
757- #[ doc( hidden) ]
758- pub ( super ) trait ZipImpl < A , B > {
759- type Item ;
760- fn new ( a : A , b : B ) -> Self ;
761- fn next ( & mut self ) -> Option < Self :: Item > ;
762- fn size_hint ( & self ) -> ( usize , Option < usize > ) ;
763- fn nth ( & mut self , n : usize ) -> Option < Self :: Item > ;
764- fn super_nth ( & mut self , mut n : usize ) -> Option < Self :: Item > {
765- while let Some ( x) = self . next ( ) {
766- if n == 0 { return Some ( x) }
767- n -= 1 ;
768- }
769- None
770- }
771- fn next_back ( & mut self ) -> Option < Self :: Item >
772- where A : DoubleEndedIterator + ExactSizeIterator ,
773- B : DoubleEndedIterator + ExactSizeIterator ;
774- }
775-
776- // General Zip impl
777- #[ doc( hidden) ]
778- impl < A , B > ZipImpl < A , B > for Zip < A , B >
779- where A : Iterator , B : Iterator
780- {
781- type Item = ( A :: Item , B :: Item ) ;
782- default fn new ( a : A , b : B ) -> Self {
783- Zip {
784- a,
785- b,
786- index : 0 , // unused
787- len : 0 , // unused
788- }
789- }
790-
791- #[ inline]
792- default fn next ( & mut self ) -> Option < ( A :: Item , B :: Item ) > {
793- self . a . next ( ) . and_then ( |x| {
794- self . b . next ( ) . and_then ( |y| {
795- Some ( ( x, y) )
796- } )
797- } )
798- }
799-
800- #[ inline]
801- default fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
802- self . super_nth ( n)
803- }
804-
805- #[ inline]
806- default fn next_back ( & mut self ) -> Option < ( A :: Item , B :: Item ) >
807- where A : DoubleEndedIterator + ExactSizeIterator ,
808- B : DoubleEndedIterator + ExactSizeIterator
809- {
810- let a_sz = self . a . len ( ) ;
811- let b_sz = self . b . len ( ) ;
812- if a_sz != b_sz {
813- // Adjust a, b to equal length
814- if a_sz > b_sz {
815- for _ in 0 ..a_sz - b_sz { self . a . next_back ( ) ; }
816- } else {
817- for _ in 0 ..b_sz - a_sz { self . b . next_back ( ) ; }
818- }
819- }
820- match ( self . a . next_back ( ) , self . b . next_back ( ) ) {
821- ( Some ( x) , Some ( y) ) => Some ( ( x, y) ) ,
822- ( None , None ) => None ,
823- _ => unreachable ! ( ) ,
824- }
825- }
826-
827- #[ inline]
828- default fn size_hint ( & self ) -> ( usize , Option < usize > ) {
829- let ( a_lower, a_upper) = self . a . size_hint ( ) ;
830- let ( b_lower, b_upper) = self . b . size_hint ( ) ;
831-
832- let lower = cmp:: min ( a_lower, b_lower) ;
833-
834- let upper = match ( a_upper, b_upper) {
835- ( Some ( x) , Some ( y) ) => Some ( cmp:: min ( x, y) ) ,
836- ( Some ( x) , None ) => Some ( x) ,
837- ( None , Some ( y) ) => Some ( y) ,
838- ( None , None ) => None
839- } ;
840-
841- ( lower, upper)
842- }
843- }
844-
845- #[ doc( hidden) ]
846- impl < A , B > ZipImpl < A , B > for Zip < A , B >
847- where A : TrustedRandomAccess , B : TrustedRandomAccess
848- {
849- fn new ( a : A , b : B ) -> Self {
850- let len = cmp:: min ( a. len ( ) , b. len ( ) ) ;
851- Zip {
852- a,
853- b,
854- index : 0 ,
855- len,
856- }
857- }
858-
859- #[ inline]
860- fn next ( & mut self ) -> Option < ( A :: Item , B :: Item ) > {
861- if self . index < self . len {
862- let i = self . index ;
863- self . index += 1 ;
864- unsafe {
865- Some ( ( self . a . get_unchecked ( i) , self . b . get_unchecked ( i) ) )
866- }
867- } else if A :: may_have_side_effect ( ) && self . index < self . a . len ( ) {
868- // match the base implementation's potential side effects
869- unsafe {
870- self . a . get_unchecked ( self . index ) ;
871- }
872- self . index += 1 ;
873- None
874- } else {
875- None
876- }
877- }
878-
879- #[ inline]
880- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
881- let len = self . len - self . index ;
882- ( len, Some ( len) )
883- }
884-
885- #[ inline]
886- fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
887- let delta = cmp:: min ( n, self . len - self . index ) ;
888- let end = self . index + delta;
889- while self . index < end {
890- let i = self . index ;
891- self . index += 1 ;
892- if A :: may_have_side_effect ( ) {
893- unsafe { self . a . get_unchecked ( i) ; }
894- }
895- if B :: may_have_side_effect ( ) {
896- unsafe { self . b . get_unchecked ( i) ; }
897- }
898- }
899-
900- self . super_nth ( n - delta)
901- }
902-
903- #[ inline]
904- fn next_back ( & mut self ) -> Option < ( A :: Item , B :: Item ) >
905- where A : DoubleEndedIterator + ExactSizeIterator ,
906- B : DoubleEndedIterator + ExactSizeIterator
907- {
908- // Adjust a, b to equal length
909- if A :: may_have_side_effect ( ) {
910- let sz = self . a . len ( ) ;
911- if sz > self . len {
912- for _ in 0 ..sz - cmp:: max ( self . len , self . index ) {
913- self . a . next_back ( ) ;
914- }
915- }
916- }
917- if B :: may_have_side_effect ( ) {
918- let sz = self . b . len ( ) ;
919- if sz > self . len {
920- for _ in 0 ..sz - self . len {
921- self . b . next_back ( ) ;
922- }
923- }
924- }
925- if self . index < self . len {
926- self . len -= 1 ;
927- let i = self . len ;
928- unsafe {
929- Some ( ( self . a . get_unchecked ( i) , self . b . get_unchecked ( i) ) )
930- }
931- } else {
932- None
933- }
934- }
935- }
936-
937- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
938- impl < A , B > ExactSizeIterator for Zip < A , B >
939- where A : ExactSizeIterator , B : ExactSizeIterator { }
940-
941- #[ doc( hidden) ]
942- unsafe impl < A , B > TrustedRandomAccess for Zip < A , B >
943- where A : TrustedRandomAccess ,
944- B : TrustedRandomAccess ,
945- {
946- unsafe fn get_unchecked ( & mut self , i : usize ) -> ( A :: Item , B :: Item ) {
947- ( self . a . get_unchecked ( i) , self . b . get_unchecked ( i) )
948- }
949-
950- fn may_have_side_effect ( ) -> bool {
951- A :: may_have_side_effect ( ) || B :: may_have_side_effect ( )
952- }
953- }
954-
955- #[ stable( feature = "fused" , since = "1.26.0" ) ]
956- impl < A , B > FusedIterator for Zip < A , B >
957- where A : FusedIterator , B : FusedIterator , { }
958-
959- #[ unstable( feature = "trusted_len" , issue = "37572" ) ]
960- unsafe impl < A , B > TrustedLen for Zip < A , B >
961- where A : TrustedLen , B : TrustedLen ,
962- { }
963-
964711/// An iterator that maps the values of `iter` with `f`.
965712///
966713/// This `struct` is created by the [`map`] method on [`Iterator`]. See its
0 commit comments