-
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
MaybeUninit::copy/clone_from_slice #79607
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cramertj (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
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.
Thanks!
Looks like a useful addition, especially in combination with Vec::spare_capacity_mut
.
I'm wondering if the names of these functions should be different. The new copy_from_slice is mostly equivalent to [T]::copy_from_slice, but [T]::clone_from_slice drops old values whereas this new one doesn't. Maybe it should use write
in the name, to make clear it behaves like MaybeUninit::write
or ptr::write
. What do you think?
library/core/src/mem/maybe_uninit.rs
Outdated
/// | ||
/// [`clone_from_slice`]: MaybeUninit::clone_from_slice | ||
/// [`slice::copy_from_slice`]: ../../std/primitive.slice.html#method.copy_from_slice | ||
#[unstable(feature = "maybe_uninit_write_slice", issue = "none")] |
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.
Feel free to open a tracking issue, and then add its number here.
Maybe |
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.
Welcome and thanks for the PR !
r? @sfackler |
☔ The latest upstream changes (presumably #79621) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
74dbaec
to
125ca5b
Compare
r=me with a tracking issue |
Sounds good. That should make it a bit clearer that they only write, and don't drop old elements like []::clone_from_slice does. Can you squash the commits? |
7024d7f
to
87522c4
Compare
Squashed and renamed. |
Can you open a tracking issue for this, and put its number in the |
122dae8
to
5873f9f
Compare
Added tracking issue of #79995 to the attributes |
Thanks! @bors r+ |
📌 Commit 5873f9f5b00401c50d723374922bb4511e86cc3b has been approved by |
⌛ Testing commit 5873f9f5b00401c50d723374922bb4511e86cc3b with merge b9e30d31972a1f45e6a7e3f6c412e2ac1e6abd61... |
💔 Test failed - checks-actions |
The |
Do panics abort on wasm? Would |
Yes. Looks like other tests are using that same cfg too, so that should be fine. (Some tests just explicitly ignore the wasm target for this reason, but those were written before |
5873f9f
to
4652a13
Compare
@bors r+ |
📌 Commit 4652a13 has been approved by |
…, r=m-ou-se MaybeUninit::copy/clone_from_slice This PR adds 2 new methods to MaybeUninit under the feature of `maybe_uninit_write_slice`: `copy_from_slice` and `clone_from_slice`. These are useful for initializing uninitialized buffers (such as the one returned by `Vec::spare_capacity_mut` for example) with initialized data. The methods behave similarly to the methods on slices, but the destination is uninitialized and they return the destination slice as an initialized slice.
☀️ Test successful - checks-actions |
|
||
drop(src); | ||
|
||
assert_eq!(Rc::strong_count(&rc), 2); |
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 think this test made Miri's run on the test suite fail, because it introduced a memory leak... can the test be adjusted to not leak memory?
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.
If the goal is just to test that Drop
is not called, the better way is to use a type that panics on drop.
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 confirmed that this function is the culprit, so I opened an issue: #80116
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.
@RalfJung Thanks for debugging this! I'll watch for leaks in tests more closely in future reviews. :)
@drmeepster Do you feel like changing it to a panic-on-drop to avoid the leak? Otherwise I can also make some time for this. ^^
This PR adds 2 new methods to MaybeUninit under the feature of
maybe_uninit_write_slice
:copy_from_slice
andclone_from_slice
.These are useful for initializing uninitialized buffers (such as the one returned by
Vec::spare_capacity_mut
for example) with initialized data.The methods behave similarly to the methods on slices, but the destination is uninitialized and they return the destination slice as an initialized slice.