-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Tracking issue for clone_from_slice
stabilization
#27750
Comments
This method is our one API that offers "copy from x to y", i.e. a kind of copy_nonoverlapping, although so general it handles everything that's Clone. This is requested semi-often. The need is relatively larger since your .zip() codegen is nonoptimal, so writing the loop manually ends up worse. Using |
I use this method. I have a large static array (a file header) and a slice (the bytes of the file), and it seems like this is the only way to copy the bytes from the slice to the array without looping. |
@gsingh93 Yeah, there is one more way, see How to “memcpy” bytes in stable rust |
The subteam report this week said this feature was in the stabilization phase, so I'm nominating it for the next beta. |
Nominating for discussion for 1.6. Discuss post. cc @briansmith |
🔔 This issue is now entering its cycle-long final comment period for stabilization 🔔 The libs team was a little up in the air about this function, but we're willing to consider stabilizing it. Some thoughts we had were:
|
I noticed that the So, I propose that:
|
That seems like sound reasoning to me, I may go even further to see if we could drop the |
Without |
FWIW, the proposed |
I identified some problems when trying to implement this:
|
That's a good point about fill not always filling, although I'm not entirely sure what to do about that other than ponder a better name. In terms of renaming that's fine, the |
I like the name On other naming suggestions:
I agree with @briansmith that there is potential for this API to be used incorrectly. If the destination slice has more elements than the source slice, then some elements of the destination slice will not be overwritten. For example, imagine clone_from_slice() being used to erase a sensitive area of memory, but the source slice had fewer elements. In that case, there would be I propose the following two changes:
|
The libs team discussed this at triage today and unfortunately we were unable to reach a conclusion. We think that the name We're going to punt this for another cycle to discuss the panic semantics in more detail, although if they're resolved quickly we can likely also backport a stabilization to beta. |
Some good news, in simple cases, clone_from_slice is replaced by memcpy. (playground link) @dtrebbien It's not the right tool to erase sensitive memory: llvm may choose to remove the whole copy if it can infer that the destination is never read again; there's a volatile intrinsic for this instead. I like this name & signature as a free function best: I don't object to the current behavior of using the minimum length. Is there any way we can use |
🔔 This issue is now entering its final comment period for stabilization 🔔 The primary point to discuss now is the panic semantics of the functions in question. |
A thought occurs—consider the following: let mut a = [0; 4];
let b = [1; 5];
let mut c = [0; 6];
a = b; // what should this do?
c = b; // what should this do? If your answer is "not compile", then I think |
This also seems potentially related to the question of whether slicing should panic or just truncate -- quite similarly to |
Even |
It should work, it requires a mutable rvalue and |
See rust-lang/rfcs/pull/1419, which requires some coordination for consistency due to very similar shape APIs. I do believe both that and this should exist! |
I like the idea of a free function. Both arguments are of equal importance. |
Closing as this was intended to be closed by #30943 @briansmith as @ubsan pointed out I think that rust-lang/rfcs#1419 will be useful for a guaranteed memcpy/memset @bluss we talked about this at the libs team triage yesterday and the conclusion was that we're a little wary about specializing for @ubsan the |
Yeah. As the example in #27750 (comment) shows, even when a composite type like |
@alexcrichton @bluss I’ve filed #31085 and #31086. |
For the record, I think |
@alexcrichton I disagree with the stabilization under the current name. |
I have a weak preference for I also think it would be fine to address this via a |
Another note: it would probably be better if this API returned the number of bytes copied instead of panicking if the src slice is not the same length as the dst slice. |
@reem That’s what |
@SimonSapin yes, except it unnecessarily wraps it in an |
And it can't be used in People keep saying that this is the same as |
@briansmith the issue is that the size of a slice isn't strongly typed. The only way to inform the user that you tried to clone one slice into another and it didn't work is to use panicking. The issue that I see is that we are using a very substandard name for a fairly common operation, just because we already used that name elsewhere for a very, very uncommon operation. I don't think I've ever even seen anyone use |
@ubsan @briansmith It's not true that we can only panic! We can return a value indicating the number of bytes/entries copied, which would be much nicer for the user. |
Either way, it doesn't match |
The fact that these discussions go on forever is what is killing the usability of these things, not an extra 6 characters to type. Somebody just needs to make decisions so they can ship. |
@briansmith This is also a rarely used function. The issue is that it's going to impact a very useful function a lot, because we're either going to have a very substandard name |
Honestly, I don't care that much. I've noticed, though, that even after these months of discussions, we don't have even a concrete plan for a safe |
@briansmith rust-lang/rfcs/pull/1419 is that plan. Sure, it's not completely in safe harbour yet, but it's pretty likely to get there. |
The libs team discussed this is again during triage today, and the conclusion was to stick with the name |
@alexcrichton okay, I can see the argument there. |
@alexcrichton #27750 (comment)
What I tried to say in #27750 (comment) was that we add an explicit documentation item for this function, "reserving the right" to specialize it to use memcpy. We need to document this reservation precisely because of the issues you mentioned, and if we make this clear in the docs, it shouldn't be a problem. I want that we add this guarantee now, so that it's present from the moment the API becomes stable. |
@bluss I don't think we should reserve that right. I think if someone wants to implement clone in a different way from copy, it should be their prerogative, and that they should be able to get their clone function called. Also, I think it is super obvious that this function is not ready to be stabilized. |
I don't think it is reasonable to expect |
I started a thread on the internals message board about the guarantees of |
I'm just talking about this function and a specific documentation for it. |
It feels wrong to me. However, I don't feel too strongly about it. If, however, we are going to use this as a memcpy instead of |
Hey all, just a bump from someone who started using it: I really didn’t care what it was called. I was just happy it existed. |
The
clone_from_slice
method clones a slice into another, using theclone_from
method, which in principle can be more efficient than usingclone
.It's not clear whether this method, or
clone_from
, are seeing any use at all.The text was updated successfully, but these errors were encountered: