-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 cloned values indistinguishable #1544
Comments
That's because it does not. For example, it is totally fine to put an interior address into some data structure. |
Yes but it shouldn't be exposed. That's why I said indistinguishable, not identical. That is, if I flip a fair coin and give you either |
cc #1521 |
in case of So you can only guarantee that it's not exposed which one is the original and which one is the clone, but only if you always clone before you try to figure out if a value is a clone. |
@oli-obk that's why I did it that way in the example (I wasn't thinking about Rc but Clone side effects in general). However, I somehow didn't notice that this would make this restriction useless in solving the linked issue. Regardless, I still think a restriction like this would be useful. |
It's very hard to enforce this rule. You'd need to ensure that clone doesn't modify any fields that are accessible outside the module. I'll evaluate if it's possible to write a lint for that. |
@oli-obk, I don't think @Stebalien propose to enforce it, but rather add a "rule", like the hash rule:
So just state that this is the expected behavior of clone. |
I think a rule like:
|
@ticki in other words, tying |
@Stebalien what is your rule? The "reasonable definition of indistinguishable" needs to be decided. |
@durka My original thinking was "indistinguishable using the provided API". That is, you can't time things, read raw memory, use unsafe shenanigans, etc. However, you'd also have to ban looking at locations of objects in memory (casting pointers to ints) so I'm no longer sure this proposal is workable. |
They aren't indistiguishable in the current API. See |
@ticki see #1544 (comment). That's why my rule doesn't imply your rule. |
Using this rule can only increase the cost of cloning a The cloned vec is indistinguishable in terms of equality, hash, iteration. But differs in .capacity() (and of course in .as_ptr()). |
I think |
I cannot think of any counter-examples to |
Why not just |
@glaebhoerl, I guess you mean |
Stepanov (the C++ STL author) used several concepts to describe types which are "well-behaved" and can be reasoned about, when used in algorithms and collections. His terminology is also adopted for the current C++ concepts proposals. |
I don't think |
|
I should have guessed. |
Define "Returns a copy of the value" to mean
a == b -> a == b.clone()
.Original unworkable proposal (see discussion below):
Currently, clone explicitly doesn't guarantee that the following are always indistinguishable:
This is unfortunate. However, the trait does say that
Clone::clone()
"Returns a copy of the value." which could imply indistinguishably (i.e., a copy of a value is indistinguishable from the original). It would be nice to make this explicit (so that, e.g.,a == b
impliesa == b.clone()
)./cc #1203
The text was updated successfully, but these errors were encountered: