@@ -17,6 +17,8 @@ implementing the `Iterator` trait.
17
17
18
18
*/
19
19
20
+ #[ allow( default_methods) ] ; // solid enough for the use case here
21
+
20
22
use cmp;
21
23
use iter:: { FromIter , Times } ;
22
24
use num:: { Zero , One } ;
@@ -31,6 +33,12 @@ use clone::Clone;
31
33
pub trait Iterator < A > {
32
34
/// Advance the iterator and return the next value. Return `None` when the end is reached.
33
35
fn next ( & mut self ) -> Option < A > ;
36
+
37
+ /// Return a lower bound and upper bound on the remaining length of the iterator.
38
+ ///
39
+ /// The common use case for the estimate is pre-allocating space to store the results.
40
+ #[ cfg( not( stage0) ) ]
41
+ fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) { ( None , None ) }
34
42
}
35
43
36
44
/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
@@ -594,6 +602,27 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<A, T, U> {
594
602
self . b . next ( )
595
603
}
596
604
}
605
+
606
+ #[ inline]
607
+ #[ cfg( not( stage0) ) ]
608
+ fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
609
+ let ( a_lower, a_upper) = self . a . size_hint ( ) ;
610
+ let ( b_lower, b_upper) = self . b . size_hint ( ) ;
611
+
612
+ let lower = match ( a_lower, b_lower) {
613
+ ( Some ( x) , Some ( y) ) => Some ( x + y) ,
614
+ ( Some ( x) , None ) => Some ( x) ,
615
+ ( None , Some ( y) ) => Some ( y) ,
616
+ ( None , None ) => None
617
+ } ;
618
+
619
+ let upper = match ( a_upper, b_upper) {
620
+ ( Some ( x) , Some ( y) ) => Some ( x + y) ,
621
+ _ => None
622
+ } ;
623
+
624
+ ( lower, upper)
625
+ }
597
626
}
598
627
599
628
/// An iterator which iterates two other iterators simultaneously
@@ -627,6 +656,12 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
627
656
_ => None
628
657
}
629
658
}
659
+
660
+ #[ inline]
661
+ #[ cfg( not( stage0) ) ]
662
+ fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
663
+ self . iter . size_hint ( )
664
+ }
630
665
}
631
666
632
667
/// An iterator which filters the elements of `iter` with `predicate`
@@ -647,6 +682,13 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
647
682
}
648
683
None
649
684
}
685
+
686
+ #[ inline]
687
+ #[ cfg( not( stage0) ) ]
688
+ fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
689
+ let ( _, upper) = self . iter . size_hint ( ) ;
690
+ ( None , upper) // can't know a lower bound, due to the predicate
691
+ }
650
692
}
651
693
652
694
/// An iterator which uses `f` to both filter and map elements from `iter`
@@ -666,6 +708,13 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B,
666
708
}
667
709
None
668
710
}
711
+
712
+ #[ inline]
713
+ #[ cfg( not( stage0) ) ]
714
+ fn size_hint ( & self ) -> ( Option < uint > , Option < uint > ) {
715
+ let ( _, upper) = self . iter . size_hint ( ) ;
716
+ ( None , upper) // can't know a lower bound, due to the predicate
717
+ }
669
718
}
670
719
671
720
/// An iterator which yields the current count and the element during iteration
0 commit comments