@@ -604,26 +604,42 @@ impl<'self, A, T: DoubleEndedIterator<&'self mut A>> MutableDoubleEndedIterator
604
604
}
605
605
}
606
606
607
- /// A double-ended iterator with known size
608
- pub trait ExactSizeDoubleEndedIterator < A > {
607
+
608
+ /// An object implementing random access indexing by `uint`
609
+ ///
610
+ /// A `RandomAccessIterator` should be either infinite or a `DoubleEndedIterator`.
611
+ pub trait RandomAccessIterator < A > : Iterator < A > {
612
+ /// Return the number of indexable elements. At most `std::uint::max_value`
613
+ /// elements are indexable, even if the iterator represents a longer range.
614
+ fn indexable ( & self ) -> uint ;
615
+
616
+ /// Return an element at an index
617
+ fn idx ( & self , index : uint ) -> Option < A > ;
618
+ }
619
+
620
+ /// An iterator that knows its exact length
621
+ ///
622
+ /// This trait is a helper for iterators like the vector iterator, so that
623
+ /// it can support double-ended enumeration.
624
+ ///
625
+ /// `Iterator::size_hint` *must* return the exact size of the iterator.
626
+ /// Note that the size must fit in `uint`.
627
+ pub trait ExactSize < A > : DoubleEndedIterator < A > {
609
628
/// Return the index of the last element satisfying the specified predicate
610
629
///
611
630
/// If no element matches, None is returned.
612
631
#[ inline]
613
- fn rposition ( & mut self , predicate : & fn ( A ) -> bool ) -> Option < uint > ;
614
- }
615
-
616
- impl < A , T : DoubleEndedIterator < A > + ExactSizeHint > ExactSizeDoubleEndedIterator < A > for T {
617
632
fn rposition ( & mut self , predicate : & fn ( A ) -> bool ) -> Option < uint > {
618
- let ( size, _) = self . size_hint ( ) ;
619
- let mut i = size;
633
+ let ( lower, upper) = self . size_hint ( ) ;
634
+ assert ! ( upper == Some ( lower) ) ;
635
+ let mut i = lower;
620
636
loop {
621
637
match self . next_back ( ) {
622
638
None => break ,
623
639
Some ( x) => {
624
640
i = match i. checked_sub ( & 1 ) {
625
641
Some ( x) => x,
626
- None => fail ! ( "rposition: incorrect ExactSizeHint " )
642
+ None => fail ! ( "rposition: incorrect ExactSize " )
627
643
} ;
628
644
if predicate ( x) {
629
645
return Some ( i)
@@ -635,37 +651,13 @@ impl<A, T: DoubleEndedIterator<A> + ExactSizeHint> ExactSizeDoubleEndedIterator<
635
651
}
636
652
}
637
653
638
- /// An object implementing random access indexing by `uint`
639
- ///
640
- /// A `RandomAccessIterator` should be either infinite or a `DoubleEndedIterator`.
641
- pub trait RandomAccessIterator < A > : Iterator < A > {
642
- /// Return the number of indexable elements. At most `std::uint::max_value`
643
- /// elements are indexable, even if the iterator represents a longer range.
644
- fn indexable ( & self ) -> uint ;
645
-
646
- /// Return an element at an index
647
- fn idx ( & self , index : uint ) -> Option < A > ;
648
- }
649
-
650
- /// An iterator that knows its exact length
651
- ///
652
- /// This trait is a helper for iterators like the vector iterator, so that
653
- /// it can support double-ended enumeration.
654
- ///
655
- /// `Iterator::size_hint` *must* return the exact size of the iterator.
656
- /// Note that the size must fit in `uint`.
657
- pub trait ExactSizeHint { }
658
-
659
654
// All adaptors that preserve the size of the wrapped iterator are fine
660
655
// Adaptors that may overflow in `size_hint` are not, i.e. `Chain`.
661
- impl < T : ExactSizeHint > ExactSizeHint for Enumerate < T > { }
662
- impl < ' self , A , T : ExactSizeHint > ExactSizeHint for Inspect < ' self , A , T > { }
663
- impl < T : ExactSizeHint > ExactSizeHint for Invert < T > { }
664
- impl < ' self , A , B , T : ExactSizeHint > ExactSizeHint for Map < ' self , A , B , T > { }
665
- impl < A , T : ExactSizeHint > ExactSizeHint for Peekable < A , T > { }
666
- impl < T : ExactSizeHint > ExactSizeHint for Skip < T > { }
667
- impl < T : ExactSizeHint > ExactSizeHint for Take < T > { }
668
- impl < T : ExactSizeHint , U : ExactSizeHint > ExactSizeHint for Zip < T , U > { }
656
+ impl < A , T : ExactSize < A > > ExactSize < ( uint , A ) > for Enumerate < T > { }
657
+ impl < ' self , A , T : ExactSize < A > > ExactSize < A > for Inspect < ' self , A , T > { }
658
+ impl < A , T : ExactSize < A > > ExactSize < A > for Invert < T > { }
659
+ impl < ' self , A , B , T : ExactSize < A > > ExactSize < B > for Map < ' self , A , B , T > { }
660
+ impl < A , B , T : ExactSize < A > , U : ExactSize < B > > ExactSize < ( A , B ) > for Zip < T , U > { }
669
661
670
662
/// An double-ended iterator with the direction inverted
671
663
#[ deriving( Clone ) ]
@@ -967,14 +959,14 @@ impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for Zip<T, U> {
967
959
}
968
960
}
969
961
970
- impl < A , B ,
971
- T : DoubleEndedIterator < A > + ExactSizeHint ,
972
- U : DoubleEndedIterator < B > + ExactSizeHint > DoubleEndedIterator < ( A , B ) >
962
+ impl < A , B , T : ExactSize < A > , U : ExactSize < B > > DoubleEndedIterator < ( A , B ) >
973
963
for Zip < T , U > {
974
964
#[ inline]
975
965
fn next_back ( & mut self ) -> Option < ( A , B ) > {
976
- let ( a_sz, _) = self . a . size_hint ( ) ;
977
- let ( b_sz, _) = self . b . size_hint ( ) ;
966
+ let ( a_sz, a_upper) = self . a . size_hint ( ) ;
967
+ let ( b_sz, b_upper) = self . b . size_hint ( ) ;
968
+ assert ! ( a_upper == Some ( a_sz) ) ;
969
+ assert ! ( b_upper == Some ( b_sz) ) ;
978
970
if a_sz < b_sz {
979
971
for _ in range ( 0 , b_sz - a_sz) { self . b . next_back ( ) ; }
980
972
} else if a_sz > b_sz {
@@ -1168,15 +1160,14 @@ impl<A, T: Iterator<A>> Iterator<(uint, A)> for Enumerate<T> {
1168
1160
}
1169
1161
}
1170
1162
1171
- impl < A , T : DoubleEndedIterator < A > + ExactSizeHint > DoubleEndedIterator < ( uint , A ) >
1172
- for Enumerate < T > {
1163
+ impl < A , T : ExactSize < A > > DoubleEndedIterator < ( uint , A ) > for Enumerate < T > {
1173
1164
#[ inline]
1174
1165
fn next_back ( & mut self ) -> Option < ( uint , A ) > {
1175
1166
match self . iter . next_back ( ) {
1176
1167
Some ( a) => {
1177
- let ( len , _ ) = self . iter . size_hint ( ) ;
1178
- let ret = Some ( ( self . count + len , a ) ) ;
1179
- ret
1168
+ let ( lower , upper ) = self . iter . size_hint ( ) ;
1169
+ assert ! ( upper == Some ( lower ) ) ;
1170
+ Some ( ( self . count + lower , a ) )
1180
1171
}
1181
1172
_ => None
1182
1173
}
0 commit comments