-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Conversation
r? @shepmaster (rust_highfive has picked a reviewer for you, use r? to override) |
97efb25
to
0800e67
Compare
0800e67
to
fe45e9a
Compare
} | ||
|
||
fn fold<Acc, F>(self, init: Acc, mut f: F) -> Acc | ||
where F: FnMut(Acc, Self::Item) -> Acc, |
There was a problem hiding this comment.
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) 🙂
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🙂
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's coming
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.
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.
Quick note: there's currently a clippy lint that suggests replacing |
For what it’s worth |
While true, a similar argument applies to 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. |
☔ The latest upstream changes (presumably #57063) made this pull request unmergeable. Please resolve the merge conflicts. |
Technically the PR is fine. Thoughts on including this in std? |
(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 |
Diff looks good. r=me with a tracking issue filed and referenced in I believe we tend to default to accepting new unstable APIs, and revisit them when stabilization is proposed. |
@SimonSapin Created tracking issues. |
📌 Commit 315401d has been approved by |
🌲 The tree is currently closed for pull requests below priority 500, this pull request will be tested once the tree is reopened |
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.
☀️ Test successful - status-appveyor, status-travis |
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 longerCopy
. This is a relatively common pattern, as it can be seen by callingrg --pcre2 '[.]map[(][|](?:(\w+)[|] [*]\1|&(\w+)[|] \2)[)]'
on Rust main repository. Additionally, many uses ofcloned
actually want to simplyCopy
, 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 withIterator::copied
. I don't think this feature is particularly important, but it makes sense to updateOption
along withIterator
for consistency.