Skip to content

Commit

Permalink
RangeFrom should have an infinite size_hint
Browse files Browse the repository at this point in the history
This makes the size_hint from things like `take` more precise.
  • Loading branch information
scottmcm committed May 30, 2017
1 parent 920c479 commit 265dce6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
5 changes: 5 additions & 0 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
1 change: 1 addition & 0 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand Down

0 comments on commit 265dce6

Please sign in to comment.