Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upTracking issue for `clone_from_slice` stabilization #27750
Comments
aturon
added
T-libs
B-unstable
labels
Aug 12, 2015
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
@gsingh93 Yeah, there is one more way, see How to “memcpy” bytes in stable rust |
This comment has been minimized.
This comment has been minimized.
|
The subteam report this week said this feature was in the stabilization phase, so I'm nominating it for the next beta. |
This comment has been minimized.
This comment has been minimized.
|
Nominating for discussion for 1.6. Discuss post. cc @briansmith |
aturon
added
the
I-nominated
label
Nov 4, 2015
This comment has been minimized.
This comment has been minimized.
|
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:
|
alexcrichton
added
final-comment-period
and removed
I-nominated
labels
Nov 5, 2015
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Nov 6, 2015
|
I noticed that the So, I propose that:
|
briansmith
referenced this issue
Nov 6, 2015
Closed
Tracking issue for `push_all` stabilization #27744
This comment has been minimized.
This comment has been minimized.
|
That seems like sound reasoning to me, I may go even further to see if we could drop the |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Nov 6, 2015
Without |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Nov 7, 2015
|
FWIW, the proposed |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Nov 11, 2015
|
I identified some problems when trying to implement this:
|
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
ghost
commented
Nov 24, 2015
|
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:
|
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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 |
alexcrichton
added
the
I-nominated
label
Dec 16, 2015
This comment has been minimized.
This comment has been minimized.
|
The primary point to discuss now is the panic semantics of the functions in question. |
alexcrichton
removed
the
I-nominated
label
Dec 17, 2015
This comment has been minimized.
This comment has been minimized.
|
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 comment has been minimized.
This comment has been minimized.
|
This also seems potentially related to the question of whether slicing should panic or just truncate -- quite similarly to |
This comment has been minimized.
This comment has been minimized.
|
Even |
This comment has been minimized.
This comment has been minimized.
|
It should work, it requires a mutable rvalue and |
This comment has been minimized.
This comment has been minimized.
|
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! |
This comment has been minimized.
This comment has been minimized.
|
I like the idea of a free function. Both arguments are of equal importance. |
This comment has been minimized.
This comment has been minimized.
|
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 |
alexcrichton
closed this
Jan 21, 2016
This comment has been minimized.
This comment has been minimized.
|
Yeah. As the example in #27750 (comment) shows, even when a composite type like |
This was referenced Jan 21, 2016
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton @bluss I’ve filed #31085 and #31086. |
This comment has been minimized.
This comment has been minimized.
|
For the record, I think |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton I disagree with the stabilization under the current name. |
This comment has been minimized.
This comment has been minimized.
|
I have a weak preference for I also think it would be fine to address this via a |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
@reem That’s what |
This comment has been minimized.
This comment has been minimized.
|
@SimonSapin yes, except it unnecessarily wraps it in an |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Feb 3, 2016
And it can't be used in People keep saying that this is the same as |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Feb 3, 2016
Either way, it doesn't match |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Feb 3, 2016
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. |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Feb 3, 2016
|
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 |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
The libs team discussed this is again during triage today, and the conclusion was to stick with the name |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton okay, I can see the argument there. |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
I don't think it is reasonable to expect |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Feb 5, 2016
|
I started a thread on the internals message board about the guarantees of |
This comment has been minimized.
This comment has been minimized.
|
I'm just talking about this function and a specific documentation for it. |
This comment has been minimized.
This comment has been minimized.
|
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 |
sfackler
referenced this issue
Apr 8, 2018
Closed
Why do copy_from_slice and clone_from_slice require equal size? #49769
This comment has been minimized.
This comment has been minimized.
RandomInsano
commented
Apr 8, 2018
|
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. |
aturon commentedAug 12, 2015
The
clone_from_slicemethod clones a slice into another, using theclone_frommethod, 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.