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

Open
wants to merge 7 commits into
base: master
from

Conversation

Projects
None yet
7 participants
@xfix
Contributor

xfix commented Dec 5, 2018

Initially suggested at bluss/rust-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

This comment has been minimized.

Collaborator

rust-highfive commented Dec 5, 2018

r? @shepmaster

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

@xfix xfix force-pushed the xfix:copied branch 4 times, most recently from 97efb25 to 0800e67 Dec 5, 2018

@xfix xfix force-pushed the xfix:copied branch from 0800e67 to fe45e9a Dec 5, 2018

Show resolved Hide resolved src/libcore/iter/mod.rs Outdated
Show resolved Hide resolved src/libcore/iter/mod.rs Outdated
}
fn fold<Acc, F>(self, init: Acc, mut f: F) -> Acc
where F: FnMut(Acc, Self::Item) -> Acc,

This comment has been minimized.

@bluss

bluss Dec 5, 2018

Contributor

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

This comment has been minimized.

@xfix

xfix Dec 5, 2018

Contributor

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.

This comment has been minimized.

@bluss

bluss Dec 5, 2018

Contributor

uh that makes sense I guess 🙂

This comment has been minimized.

@Lucretiel

Lucretiel Dec 6, 2018

Contributor

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

This comment has been minimized.

@bluss

bluss Dec 6, 2018

Contributor

That's coming

Show resolved Hide resolved src/libcore/iter/mod.rs Outdated
Show resolved Hide resolved src/libcore/iter/mod.rs Outdated

pietroalbini added a commit to pietroalbini/rust that referenced this pull request Dec 5, 2018

Rollup merge of rust-lang#56538 - xfix:patch-13, r=bluss
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

Rollup merge of rust-lang#56538 - xfix:patch-13, r=bluss
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

This comment has been minimized.

Contributor

Lucretiel commented Dec 6, 2018

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 label Dec 6, 2018

@SimonSapin

This comment has been minimized.

Contributor

SimonSapin commented Dec 9, 2018

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.

@xfix

This comment has been minimized.

Contributor

xfix 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

This comment has been minimized.

Member

shepmaster commented Dec 16, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment