Skip to content

Opt for .cloned() over .map(|foo| foo.clone()) etc. #22287

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 4 commits into from
Feb 19, 2015

Conversation

Ryman
Copy link
Contributor

@Ryman Ryman commented Feb 13, 2015

This overlaps with #22276 (I left make check running overnight) but covers a number of additional cases and has a few rewrites where the clones are not even necessary.

This also implements RandomAccessIterator for iter::Cloned

cc @steveklabnik, you may want to glance at this before #22281 gets the bors treatment

@rust-highfive
Copy link
Contributor

r? @nikomatsakis

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

@nikomatsakis
Copy link
Contributor

Looks good to me!

@nikomatsakis
Copy link
Contributor

@bors r+ b042d10

@bors
Copy link
Collaborator

bors commented Feb 13, 2015

🙀 You have the wrong number! Please try again with 43f0fbf.

@nikomatsakis
Copy link
Contributor

@bors r+ 43f0fbf

@nikomatsakis
Copy link
Contributor

@Ryman does this cover all the cases of #22276?

@Ryman
Copy link
Contributor Author

Ryman commented Feb 13, 2015

@nikomatsakis It missed .map(|&foo| foo), which I just realised after @gankro's comment on #22276. I could add that here, or as a new PR depending on preference? (Although I think @gankro covered the ones in libcollections for that pattern)

@nikomatsakis
Copy link
Contributor

@bors r-

@nikomatsakis
Copy link
Contributor

I'm canceling the r+ until the other PRs land so we can sort this out in a more orderly fashion.

@ruuda ruuda mentioned this pull request Feb 13, 2015
@steveklabnik
Copy link
Member

@bors: r-

@Ryman Ryman force-pushed the purge_carthographers branch from 43f0fbf to d786e10 Compare February 15, 2015 06:45
@Ryman
Copy link
Contributor Author

Ryman commented Feb 15, 2015

Rebased on master and added the remaining cases.

r? @nikomatsakis

@@ -600,7 +600,7 @@ pub fn lit_to_const(lit: &ast::Lit) -> const_val {
match lit.node {
ast::LitStr(ref s, _) => const_str((*s).clone()),
ast::LitBinary(ref data) => {
const_binary(Rc::new(data.iter().map(|x| *x).collect()))
const_binary(data.clone())
Copy link
Contributor

Choose a reason for hiding this comment

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

This has slightly different semantics from the above code. I'm not clear what was intended. Naively this should be a strict improvement? (just bumps a refcount instead of creating a new Rc and new Vec)

librustc is crazy, though...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, assumed safe rust usage which makes this equivalent? Tests passed for a full make check so it should be fine?

Copy link
Contributor

Choose a reason for hiding this comment

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

If an Rc happens to be unique, you can actually move the data out. So there is actually a semantic change in safe Rust to make a new disjoint Rc.

I don't think rustc ever actually does this, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't that just CoW? Or has it changed recently? Wouldn't the underlying Vec be the same and whatever code wants to act on a mutation would Cow or clone the inside? It's not a RefCell so doing anything strange to the underlying data would be pretty dodgy? (Am I overlooking something really terrible?)

Other than this concern, rebase is done :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Note to future: Talked to eddyb, this is just a case of gradual code refactoring making previously sane code insane.

@Gankra
Copy link
Contributor

Gankra commented Feb 15, 2015

Wow, this is way more principled than a lot of bulk refactors we get! Actually reflecting on if the surrounding code makes sense?? 👍

Just a few minor issues.

@Ryman Ryman force-pushed the purge_carthographers branch from d786e10 to a11e900 Compare February 15, 2015 17:54
@Gankra
Copy link
Contributor

Gankra commented Feb 15, 2015

@bors r+ a11e900

@bors
Copy link
Collaborator

bors commented Feb 16, 2015

⌛ Testing commit a11e900 with merge 9c9711b...

@bors
Copy link
Collaborator

bors commented Feb 16, 2015

💔 Test failed - auto-win-32-nopt-t

@Ryman Ryman force-pushed the purge_carthographers branch from a11e900 to fe99ca9 Compare February 16, 2015 10:29
@Ryman
Copy link
Contributor Author

Ryman commented Feb 16, 2015

Windows failed with:

libstd\sys\windows\thread_local.rs:247:42: 247:51 error: the trait `core::clone::Clone` is not implemented for the type `unsafe extern "C" fn(*mut u8)` [E0277]
libstd\sys\windows\thread_local.rs:247                 (*DTORS).iter().cloned().collect()

@gankro re r? Rebased and reverted the above in last added commit as I don't think I can implement Clone for that?

Related: are we ever going to have Copy implements Clone or was that written off for some reason?

@Gankra
Copy link
Contributor

Gankra commented Feb 16, 2015

Something Something Coherence makes the blanket impl hard to do, I think.

@Gankra
Copy link
Contributor

Gankra commented Feb 16, 2015

Can you squash the revert into whatever commit added it?

@Ryman Ryman force-pushed the purge_carthographers branch from fe99ca9 to 20db2c9 Compare February 16, 2015 17:21
@Ryman
Copy link
Contributor Author

Ryman commented Feb 16, 2015

@gankro done.

@Gankra
Copy link
Contributor

Gankra commented Feb 16, 2015

@bors r+ 20db2c

@Gankra
Copy link
Contributor

Gankra commented Feb 16, 2015

Thanks a ton!

@Ryman Ryman force-pushed the purge_carthographers branch from 20db2c9 to d2f54e6 Compare February 18, 2015 00:58
@Ryman
Copy link
Contributor Author

Ryman commented Feb 18, 2015

@gankro rebased as it seems to have dropped off bors's radar according to the build queue.

@Gankra
Copy link
Contributor

Gankra commented Feb 18, 2015

@bors r+ d2f54e

@bors
Copy link
Collaborator

bors commented Feb 18, 2015

⌛ Testing commit d2f54e6 with merge 3d01ccc...

@bors
Copy link
Collaborator

bors commented Feb 18, 2015

💔 Test failed - auto-win-64-opt

@alexcrichton
Copy link
Member

@bors: retry

@bors
Copy link
Collaborator

bors commented Feb 18, 2015

⌛ Testing commit d2f54e6 with merge dfaf453...

@bors
Copy link
Collaborator

bors commented Feb 18, 2015

💔 Test failed - auto-win-32-nopt-t

@Gankra
Copy link
Contributor

Gankra commented Feb 18, 2015

@bors retry

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Feb 19, 2015
This overlaps with rust-lang#22276 (I left make check running overnight) but covers a number of additional cases and has a few rewrites where the clones are not even necessary.

This also implements `RandomAccessIterator` for `iter::Cloned`

cc @steveklabnik, you may want to glance at this before rust-lang#22281 gets the bors treatment
@bors bors merged commit d2f54e6 into rust-lang:master Feb 19, 2015
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.

7 participants