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

Add unstable Iterator::copied() #56534

Merged
merged 10 commits into from
Dec 26, 2018
Merged

Add unstable Iterator::copied() #56534

merged 10 commits into from
Dec 26, 2018

Conversation

KamilaBorowska
Copy link
Contributor

@KamilaBorowska KamilaBorowska commented Dec 5, 2018

Initially suggested at rust-itertools/itertools#289, however the maintainers of itertools suggested this may be better of in a standard library.

The intent of copied is to avoid accidentally cloning iterator elements after doing a code refactoring which causes a structure to be no longer Copy. This is a relatively common pattern, as it can be seen by calling rg --pcre2 '[.]map[(][|](?:(\w+)[|] [*]\1|&(\w+)[|] \2)[)]' on Rust main repository. Additionally, many uses of cloned actually want to simply Copy, and changing something to be no longer copyable may introduce unnoticeable performance penalty.

Also, this makes sense because the standard library includes [T].copy_from_slice to pair with [T].clone_from_slice.

This also adds Option::copied, because it makes sense to pair it with Iterator::copied. I don't think this feature is particularly important, but it makes sense to update Option along with Iterator for consistency.

@rust-highfive
Copy link
Collaborator

r? @shepmaster

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

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 5, 2018
src/libcore/iter/mod.rs Outdated Show resolved Hide resolved
src/libcore/iter/mod.rs Outdated Show resolved Hide resolved
}

fn fold<Acc, F>(self, init: Acc, mut f: F) -> Acc
where F: FnMut(Acc, Self::Item) -> Acc,
Copy link
Member

Choose a reason for hiding this comment

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

the indentation style for where is different in these two methods, we need to use one (whatever this file prefers) 🙂

Copy link
Contributor Author

@KamilaBorowska KamilaBorowska Dec 5, 2018

Choose a reason for hiding this comment

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

It's consistently so in this file, with different indentation for try_fold and fold. It probably should be fixed, but it seems out of scope for this change.

Copy link
Member

Choose a reason for hiding this comment

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

uh that makes sense I guess 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking that it might be good to (at some point) run rustfmt on the whole standard library

Copy link
Member

Choose a reason for hiding this comment

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

That's coming

src/libcore/iter/mod.rs Outdated Show resolved Hide resolved
src/libcore/iter/mod.rs Outdated Show resolved Hide resolved
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Dec 5, 2018
Use inner iterator may_have_side_effect for Cloned

Previous implementation wasn't correct, as an inner iterator could have had side effects. Noticed by @bluss in rust-lang#56534.
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Dec 5, 2018
Use inner iterator may_have_side_effect for Cloned

Previous implementation wasn't correct, as an inner iterator could have had side effects. Noticed by @bluss in rust-lang#56534.
@Lucretiel
Copy link
Contributor

Quick note: there's currently a clippy lint that suggests replacing iter.map(|x| *x) with iter.cloned(). Would probably be worth updating that lint once this API stabilizes.

@kennytm kennytm added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Dec 6, 2018
@SimonSapin
Copy link
Contributor

For what it’s worth copy_from_slice is not "just" clone_from_slice with *x instead of x.clone(), it is implemented as a single call to ptr::copy_nonoverlapping.

@KamilaBorowska
Copy link
Contributor Author

KamilaBorowska commented Dec 9, 2018

While true, a similar argument applies to Cloned, as it actually has a specialization for T: Copy for TrustedRandomAccess trait.

That said, the intent of this feature isn't as much about performance, but rather making the invariants clear while refactoring to prevent accidentally cloning, which could be tricky to detect.

@shepmaster
Copy link
Member

The feature makes sense to me. @bluss has done the existing superb job of review, so I think it's better if they get control of the big button ;-)

r? @bluss

@rust-highfive rust-highfive assigned bluss and unassigned shepmaster Dec 16, 2018
@bors
Copy link
Contributor

bors commented Dec 23, 2018

☔ The latest upstream changes (presumably #57063) made this pull request unmergeable. Please resolve the merge conflicts.

@bluss
Copy link
Member

bluss commented Dec 23, 2018

Technically the PR is fine. Thoughts on including this in std?

@Centril
Copy link
Contributor

Centril commented Dec 24, 2018

(My personal opinion is that this would be useful as a means of ensuring "cheapness")

Randomly re-assigning to someone on T-libs for final triage.

r? @SimonSapin

@rust-highfive rust-highfive assigned SimonSapin and unassigned bluss Dec 24, 2018
@SimonSapin
Copy link
Contributor

Diff looks good. r=me with a tracking issue filed and referenced in #[unstable] attributes.

I believe we tend to default to accepting new unstable APIs, and revisit them when stabilization is proposed.

@SimonSapin SimonSapin added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 25, 2018
@KamilaBorowska
Copy link
Contributor Author

KamilaBorowska commented Dec 26, 2018

@SimonSapin Created tracking issues.

@Centril
Copy link
Contributor

Centril commented Dec 26, 2018

@bors r=@SimonSapin

@bors
Copy link
Contributor

bors commented Dec 26, 2018

📌 Commit 315401d has been approved by @SimonSapin

@bors
Copy link
Contributor

bors commented Dec 26, 2018

🌲 The tree is currently closed for pull requests below priority 500, this pull request will be tested once the tree is reopened

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 26, 2018
@bors
Copy link
Contributor

bors commented Dec 26, 2018

⌛ Testing commit 315401d with merge a7be40c...

bors added a commit that referenced this pull request Dec 26, 2018
Add unstable Iterator::copied()

Initially suggested at rust-itertools/itertools#289, however the maintainers of itertools suggested this may be better of in a standard library.

The intent of `copied` is to avoid accidentally cloning iterator elements after doing a code refactoring which causes a structure to be no longer `Copy`. This is a relatively common pattern, as it can be seen by calling `rg --pcre2 '[.]map[(][|](?:(\w+)[|] [*]\1|&(\w+)[|] \2)[)]'` on Rust main repository. Additionally, many uses of `cloned` actually want to simply `Copy`, and changing something to be no longer copyable may introduce unnoticeable performance penalty.

Also, this makes sense because the standard library includes `[T].copy_from_slice` to pair with `[T].clone_from_slice`.

This also adds `Option::copied`, because it makes sense to pair it with `Iterator::copied`. I don't think this feature is particularly important, but it makes sense to update `Option` along with `Iterator` for consistency.
@bors
Copy link
Contributor

bors commented Dec 26, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: @SimonSapin
Pushing a7be40c to master...

@bors bors merged commit 315401d into rust-lang:master Dec 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants