Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeFrom should have an infinite size_hint #42315

Merged
merged 1 commit into from
May 31, 2017

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented May 30, 2017

Before,

(0..).take(4).size_hint() == (0, Some(4))

With this change,

(0..).take(4).size_hint() == (4, Some(4))

This makes the size_hint from things like `take` more precise.
@rust-highfive
Copy link
Collaborator

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

@ollie27
Copy link
Member

ollie27 commented May 30, 2017

RangeFrom:

Note: Currently, no overflow checking is done for the iterator implementation; if you use an integer range and the integer overflows, it might panic in debug mode or create an endless loop in release mode. This overflow behavior might change in the future.

I'm not sure we can say RangeFrom is really infinite with it's current implementation. With debug assertions enabled for example 0u8.. certainly isn't: #25708.

@scottmcm
Copy link
Member Author

RangeFrom::next() never returns None, so it's "infinite".

panic! doesn't impact size_hint. For example, this passes:

let it = (-5..5).map(|x| 100 / x);
assert_eq!(it.size_hint(), (10, Some(10)));

Even though iterating it will panic with "attempt to divide by zero" before returning 10 items.

A lower-bound of 0 is inappropriate with other adapters. I mentioned Take above. it.zip(0..) is another example -- that should have the same size_hint as it, which will only happen with RangeFrom::size_hint returning (usize::MAX, None).

@alexcrichton
Copy link
Member

Ah yeah I think what @scottmcm said makes sense to me, especially because it's impossible to return None from these iterators. Thanks for the PR @scottmcm!

@bors: r+

@bors
Copy link
Contributor

bors commented May 30, 2017

📌 Commit 265dce6 has been approved by alexcrichton

@@ -130,9 +130,10 @@ pub trait Iterator {
///
/// ```
/// // an infinite iterator has no upper bound
/// // and the maximum possible lower bound
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Infinite iterators can have any lower bound so I don't think this line should have been added.

@ollie27
Copy link
Member

ollie27 commented May 31, 2017

Well I guess if RangeFrom ever gets a sane Iterator implementation this can be changed if needed.

@Mark-Simulacrum
Copy link
Member

@bors rollup

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 31, 2017
…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))
```
bors added a commit that referenced this pull request May 31, 2017
Rollup of 7 pull requests

- Successful merges: #42126, #42196, #42252, #42277, #42315, #42329, #42330
- Failed merges:
@bors bors merged commit 265dce6 into rust-lang:master May 31, 2017
@scottmcm scottmcm deleted the rangefrom-sizehint branch June 1, 2017 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants