RangeFrom should have an infinite size_hint

This makes the size_hint from things like `take` more precise.
This commit is contained in:
Scott McMurray 2017-05-30 09:15:25 -07:00
parent 920c4799be
commit 265dce66b6
3 changed files with 8 additions and 1 deletions

View File

@ -130,9 +130,10 @@ pub trait Iterator {
///
/// ```
/// // an infinite iterator has no upper bound
/// // and the maximum possible lower bound
/// let iter = 0..;
///
/// assert_eq!((0, None), iter.size_hint());
/// assert_eq!((usize::max_value(), None), iter.size_hint());
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]

View File

@ -543,6 +543,11 @@ impl<A: Step> Iterator for ops::RangeFrom<A> where
mem::swap(&mut n, &mut self.start);
Some(n)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(usize::MAX, None)
}
}
#[unstable(feature = "fused", issue = "35602")]

View File

@ -764,6 +764,7 @@ fn test_iterator_size_hint() {
let v2 = &[10, 11, 12];
let vi = v.iter();
assert_eq!((0..).size_hint(), (usize::MAX, None));
assert_eq!(c.size_hint(), (usize::MAX, None));
assert_eq!(vi.clone().size_hint(), (10, Some(10)));