Skip to content

std: Add Vec::from_iter comment #22394

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

Merged
merged 1 commit into from
Feb 18, 2015

Conversation

alexcrichton
Copy link
Member

Requested by Niko in #22200 (and is good to have anyway)

@rust-highfive
Copy link
Contributor

r? @gankro

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

@brson
Copy link
Contributor

brson commented Feb 16, 2015

@bors: r+ 42053 rollup y'all

// optimization below (eliding bound/growth checks) means that we
// actually run the iterator twice. To ensure the "moral equivalent" we
// do a `fuse()` operation to ensure that the iterator continues to
// return `None` after seeing the first `None`.
let mut i = iterator.fuse();
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, I'm not super keen on this fuse approach. In general it adds a check to every next call, to protect against a case that is basically "unspecified behaviour" anyway (the iterator being shorter than its stated size_hint).

I'd prefer adding a check after the loop:

// the iterator ran out of elements before filling the whole vector, so we're done.
if vector.len() < vector.capacity() { return vector }

since that is guaranteed to be only a single check and outside the inner loop, even if the optimiser can't remove it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea! I have updated and added an appropriate comment as well.

Manishearth added a commit to Manishearth/rust that referenced this pull request Feb 17, 2015
… r=brson

 Requested by Niko in rust-lang#22200 (and is good to have anyway)
Manishearth added a commit to Manishearth/rust that referenced this pull request Feb 17, 2015
… r=brson

Requested by Niko in rust-lang#22200 (and is good to have anyway)
@alexcrichton alexcrichton force-pushed the vec-from-iter-comment branch 2 times, most recently from f8251e7 to bebfa78 Compare February 17, 2015 17:06
@alexcrichton
Copy link
Member Author

It appears this landed in a rollup but then wasn't auto-closed.

re-r? @huonw

@rust-highfive rust-highfive assigned huonw and unassigned Gankra Feb 17, 2015
let mut i = iterator.fuse();
for element in i.by_ref().take(vector.capacity()) {
// This equivalent crucially runs the iterator precisely once. Below we
// actually in theory run the iteartor twice (one without bounds checks
Copy link
Member

Choose a reason for hiding this comment

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

s/ear/era/

@huonw
Copy link
Member

huonw commented Feb 17, 2015

r=me with the speeling fix.

@alexcrichton
Copy link
Member Author

@bors: r+ 95a28c9

Requested by Niko in rust-lang#22200 (and is good to have anyway)
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Feb 18, 2015
@huonw huonw merged commit 95a28c9 into rust-lang:master Feb 18, 2015
@alexcrichton alexcrichton deleted the vec-from-iter-comment branch March 27, 2015 20:43
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.

5 participants