Skip to content

Commit

Permalink
Rollup merge of rust-lang#42315 - scottmcm:rangefrom-sizehint, r=alex…
Browse files Browse the repository at this point in the history
…crichton

RangeFrom should have an infinite size_hint

Before,
```rust
(0..).take(4).size_hint() == (0, Some(4))
```

With this change,
```rust
(0..).take(4).size_hint() == (4, Some(4))
```
  • Loading branch information
Mark-Simulacrum authored May 31, 2017
2 parents 0daa27f + 265dce6 commit b62eb7f
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 b62eb7f

Please sign in to comment.