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

Make Vec::clone_from and slice::clone_into share the same code #107424

Merged
merged 2 commits into from
Jan 30, 2023

Conversation

bpeel
Copy link
Contributor

@bpeel bpeel commented Jan 28, 2023

In the past, Vec::clone_from was implemented using slice::clone_into. The code from clone_into was later duplicated into clone_from in 8725e4c, which is the commit that adds custom allocator support to Vec. Presumably this was done because the slice::clone_into method only works for vecs with the default allocator so it would have the wrong type to clone into Vec<T, A>.

Later on in 3613980 the code for the two methods diverged because the Vec::clone_from version gained a specialization to optimize the case when T is Copy. In order to reduce code duplication and make them both be able to take advantage of this specialization, this PR moves the specialization into the slice module and makes vec use it again.

The implementation for the ToOwned::clone_into method on [T] is a copy
of the code for vec::clone_from. In 3613980 the code for
vec::clone_from gained a specialization for when T is Copy. This commit
copies that specialization over to the clone_into implementation.
In the past, Vec::clone_from was implemented using slice::clone_into.
The code from clone_into was later duplicated into clone_from in
8725e4c, which is the commit that adds custom allocator support to
Vec. Presumably this was done because the slice::clone_into only works
for vecs with the default allocator so it would have the wrong type to
clone into Vec<T, A>.

Now that the clone_into implementation is moved out into a specializable
trait anyway we might as well use that to share the code between the two
methods.
@rustbot
Copy link
Collaborator

rustbot commented Jan 28, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (or someone else) soon.

Please see the contribution instructions for more information.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 28, 2023
@rustbot

This comment was marked as resolved.

Copy link
Member

@scottmcm scottmcm left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Great to remove the duplication.

I did have on suggestion though; let me know what you think about it.

r? @scottmcm
@rustbot author

library/alloc/src/vec/mod.rs Show resolved Hide resolved
library/alloc/src/slice.rs Show resolved Hide resolved
@rustbot rustbot assigned scottmcm and unassigned joshtriplett Jan 28, 2023
@rustbot rustbot 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 Jan 28, 2023
@scottmcm
Copy link
Member

scottmcm commented Jan 30, 2023

@bors r+

Thanks for spotting and fixing this!

(And sorry for totally missing your note in the PR description about the custom allocators.)

@bors
Copy link
Contributor

bors commented Jan 30, 2023

📌 Commit a34f11c has been approved by scottmcm

It is now in the queue for this repository.

@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 Jan 30, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 30, 2023
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#107125 (Add and use expect methods to hir.)
 - rust-lang#107172 (Reimplement NormalizeArrayLen based on SsaLocals)
 - rust-lang#107177 (Keep all theme-updating logic together)
 - rust-lang#107424 (Make Vec::clone_from and slice::clone_into share the same code)
 - rust-lang#107455 (use a more descriptive name)
 - rust-lang#107465 (`has_allow_dead_code_or_lang_attr` micro refactor)
 - rust-lang#107469 (Change turbofish context link to an archive link)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b3b9383 into rust-lang:master Jan 30, 2023
@rustbot rustbot added this to the 1.69.0 milestone Jan 30, 2023
@bpeel bpeel deleted the clone-into-from-share-code branch January 30, 2023 19:45
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 Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants