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 upUnsafeCell should implement the Copy trait #25053
Comments
This comment has been minimized.
This comment has been minimized.
|
This seems like it would require an RFC: |
steveklabnik
added
the
A-libs
label
May 2, 2015
This comment has been minimized.
This comment has been minimized.
|
@mattico Making |
This comment has been minimized.
This comment has been minimized.
|
If adding a trait impl to a type required an RFC we would never get anything done. |
This comment has been minimized.
This comment has been minimized.
|
There is currently no stable and safe method to read the value in an |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton Could you be more specific about the "undefined behavior in some situations"? And is this a problem with |
This comment has been minimized.
This comment has been minimized.
|
Yes each type which contains an |
This comment has been minimized.
This comment has been minimized.
|
I was thinking about this again today and I'm wondering if it would be better to remove the Then people could implement their own cells with what ever degree of unsafety they want. |
This comment has been minimized.
This comment has been minimized.
|
We need this for FFI with C and C++ too, where it would be nice to be able to tell that some struct fields may change through unknown means. |
This comment has been minimized.
This comment has been minimized.
|
+1 |
aturon
added
I-nominated
T-libs
labels
May 5, 2016
This comment has been minimized.
This comment has been minimized.
|
Nominating for libs team discussion. |
This comment has been minimized.
This comment has been minimized.
|
This libs team discussed this issue a few days ago and we unfortunately didn't reach a firm consensus. The case I mentioned above means that guaranteeing the safety of an One idea brought up by @sfackler was perhaps something like One other concern here is that if |
alexcrichton
removed
the
I-nominated
label
May 11, 2016
This comment has been minimized.
This comment has been minimized.
|
Just ran into this. @alexcrichton Can you think of any other cases in which it would make sense to If there aren't any, then special syntax seems unwarranted; if |
This comment has been minimized.
This comment has been minimized.
|
Another possible use case for Though it probably wouldn't be the best approach in that case. In short, |
This comment has been minimized.
This comment has been minimized.
|
If it makes things easier, we could make an additional type As additional bonus, our regular |
This comment has been minimized.
This comment has been minimized.
|
cc @rust-lang/lang, anybody on the lang team have an opinion on this? |
This comment has been minimized.
This comment has been minimized.
|
I am in favor. |
This comment has been minimized.
This comment has been minimized.
|
Discussed this a bit with @aturon -- I agree with the consensus that it would be easy to write broken code if This reminds me of the discussion around a Still, I feel like while having implicit copies might be dangerous from a race condition POV, it's also easy to imagine types that wouldn't have to worry about that. If my type is not |
This comment has been minimized.
This comment has been minimized.
|
This issue seems to be at a bit of a standstill.
If we want to deal with both of these perspectives, we can rule out changing
The former is clearly the easier approach, and personally I'm hesitant to add special casing to the handling of Does anyone strongly object to adding some additional version of |
This comment has been minimized.
This comment has been minimized.
|
Doesn't |
This comment has been minimized.
This comment has been minimized.
|
I thought @nikomatsakis had some more usecases for |
This comment has been minimized.
This comment has been minimized.
|
@nox, if |
This comment has been minimized.
This comment has been minimized.
|
For name bikeshedding, how about |
This comment has been minimized.
This comment has been minimized.
|
@aturon I feel that adding a new type wouldn't actually solve the problem the problem I raised earlier. That'd still be a problem for anyone using In that sense then if we must solve this specific issue then I'd be in favor of just adding the impl to |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton Which types do you think would want to move over? As I see it, the only reason to use In any case, |
This comment has been minimized.
This comment has been minimized.
|
Well presumably there's some motivation for some type that wants to move over, right? Otherwise this issue wouldn't exist? We may not have cases in the standard library itself but I would hate to have a convention of "use |
This comment has been minimized.
This comment has been minimized.
|
IMO, When it comes to motivations, I'm not totally sure it's wrong to just mark both (sidenote: if |
This comment has been minimized.
This comment has been minimized.
To be clear, it was mentioned as a joke :)
The problem is with the potential for implicit reads leading to data races.
I agree that it's still a problem for the new type, but I'm not sure how often it'd be used (and we can warn strongly against it). Also, despite there still being that potential footgun, the problem in my mind is that you simply cannot get the desired functionality today. That is, if you want a structure that is both |
This comment has been minimized.
This comment has been minimized.
|
You can't use it as a generic |
This comment has been minimized.
This comment has been minimized.
|
@aturon Er, thinko on my part. What I meant to write is not that it shouldn't literally be called Regarding making existing types |
This comment has been minimized.
This comment has been minimized.
|
I agree that extending the language just for this case seems odd, but it also seems odd that you can "opt out" of the safety for |
steveklabnik
removed
the
A-libs
label
Mar 24, 2017
This comment has been minimized.
This comment has been minimized.
|
This seems strange. I don't understand why y'all are so cautious to implement Copy for UnsafeCell. There's no safe way to do reads or writes on an UnsafeCell, and it isn't Sync, so there are no issues with unsynchronized reads. If you're doing unsynchronized writes to an Basically, I don't understand the footgun here - there is none without writing code that is already broken without |
This comment has been minimized.
This comment has been minimized.
|
(Also, you know, UnsafeCell is an unsafe building block with |
This comment has been minimized.
This comment has been minimized.
|
@ubsan The problem is that the copy is done implicitly, making it easy for there to be a read you don't realize is happening. |
This comment has been minimized.
This comment has been minimized.
|
@withoutboats The problem is that the Either way, as long as we're trying to make |
This comment has been minimized.
This comment has been minimized.
|
@aturon If the usual synchronization primitives (including atomics) opt out, how likely is it that people will actually be trying to race on a straight-up I think this set of requirements (explicitly opt into (Worth noting that this even applies to pointers, thanks to |
This comment has been minimized.
This comment has been minimized.
|
Also! Being able to byte-copy something when it's not shared really is a different use case from being able to byte-copy something when it's shared, and there should probably be a way to differentiate them. It is annoying that one can't easily allocate fixed-size arrays of atomics, since (1) that's a very common thing to want to do and (2) the copies are perfectly safe, since there's no sharing until the array initialization is finished. (In general, I guess the analogy here would be I guess my followup question would be: can anyone think of a reason they'd want to use |
This comment has been minimized.
This comment has been minimized.
Wow. That is an excellent point. @alexcrichton, thoughts? |
This comment has been minimized.
This comment has been minimized.
|
@pythonesque you'd have to implement
|
This comment has been minimized.
This comment has been minimized.
|
@RalfJung, I'd be curious to get your thoughts here as well. |
This comment has been minimized.
This comment has been minimized.
|
Thanks for the comments @pythonesque! I don't think I quite understand the significance of the section @aturon quoted, but I think it makes sense to look at these concerns in real-world scenarios. For example I've used One point, reading over this thread though, which was brought up earlier is that an implementation of |
This comment has been minimized.
This comment has been minimized.
|
@aturon here you go: First of all, I like @pythonesque's point about
From what I understand, The second guarantee, however, is a problem. Clearly many usecases of I like However, |
This comment has been minimized.
This comment has been minimized.
|
Maybe we can get the best of all worlds like this, then:
It's late so I may be missing some stuff here, but I feel like this resolves pretty much all the |
This comment has been minimized.
This comment has been minimized.
|
I think there's still footgun potential. Imagine code like let c = Cell::new(0);
let closure = || { /* using c */ }If this used |
This comment has been minimized.
This comment has been minimized.
|
Might it only do that if you explicitly said (Actually, I think there's an argument that there should be an explicit copy required for |
This comment has been minimized.
This comment has been minimized.
|
Well, maybe, but this is exactly the kind of problem due to which |
This comment has been minimized.
This comment has been minimized.
|
I think |
This comment has been minimized.
This comment has been minimized.
|
I think ergonomics also played a rule, see my question from 2 years ago: https://users.rust-lang.org/t/why-is-cell-not-copy/2208 |
This comment has been minimized.
This comment has been minimized.
|
I find those examples very unconvincing, but why not make sure people are super explicit, then, and make the |
Mark-Simulacrum
added
the
C-feature-request
label
Jul 22, 2017
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
Could you explain how/why the second does not imply the first? Intuitively it seems to me that if you are given the ability to dereference an |
This comment has been minimized.
This comment has been minimized.
|
Maybe that is an artifact of the framework we use for formalization. The reason is that (1) is an implication, saying that every possible way to justify the assertion "we own these bytes at type Off the top of my head, I am not sure if we ever rely on (1) not making such changes. It certainly makes working with this much simpler though... |
diwic commentedMay 2, 2015
Currently
UnsafeCelldoes not implementCopy. It would be beneficial if it could, for at least two reasons:[UnsafeCell::new(0i32); 75]Copy.AFAIK, there are no disadvantages for
UnsafeCellto implementCopy.Note: the reason people can't just copy-and-paste the code for
UnsafeCellto make their own variant with copy semantics, is thatUnsafeCellis a#[lang="unsafe_cell"]: "TheUnsafeCell<T>type is the only legal way to obtain aliasable data that is considered mutable. In general, transmuting an&Ttype into an&mut Tis considered undefined behavior."