-
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
Rc/Arc needs a better name than clone
#2588
Comments
clone
clone
Clone is a trait method, not an inherent method on |
I believe the solution to this issue is
See https://doc.rust-lang.org/std/sync/struct.Arc.html#cloning-references Would converting all uses of |
@Diggsey, No, not a direct rename - I'm just trying to see if there's a better solution to this. However, separate traits may not be the worst idea though - You could have one (say, Or perhaps a marker type, that the compiler simply forwards to Clone even. I think it can solved in a nice way if it's acknowledged by the community as a problem. PS: This is not too different from @stefano-pogliani, I think that would would be a good solution. If this is indeed the best solution, perhaps adding to clippy, and/or compiler warnings would help mitigate this. |
A clippy lint for this already exists, it's just not on by default: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#clone_on_ref_ptr |
I strongly support the idea of a new trait. With just a |
See also #1954 |
What about types that themselves contain Arc/Rcs? The There are two possible things that could be added:
The first trait has very limited usefulness: it's a pretty vague description, and even if you go with "heap allocation" as your distinguishing factor, heap allocation is often pretty fast, and in some cases may be faster than cloning an The second trait is potentially more useful, but can easily be implemented in a library. |
We should add a clippy lint warning against cloning
because there is a DoS vector arising from the fact that I believe I suspect lints might be the correct option here too: If deep clones are usually the correct behavior for |
Another possibility would be to allow |
As an aside, I suspect |
IMHO, |
@phlopsi the It would become impossible to |
You can implement |
Regardless of the weight of that argument, it's a massive breaking change to Rust and so it won't be done for that reason alone. |
I'm confident Also Afaik, we've no clear reason for a As an aside, I do still think Rust would benefit from lints against some borderline impls, but for |
IMO it’s clearer to say InnerType::clone(&some_arc) |
It is disabled by default. https://rust-lang.github.io/rust-clippy/master/#clone_on_ref_ptr Motivated by rust-lang/rfcs#2588
Seeing
clone
all over the codebases makes me cringe with no way to know if it's an expensive one or if it's just a reference increment. One has to dig back all the way to type to understand what it actually means.Would it not make more sense to have a method like
ref
or similar that can be used instead ofclone
? So, that's its explicit and clear. Theref
method could just as well callclone
internally. Or perhaps,new_ref
. Or a name that makes more sense. Just notclone
, (though that has become accepted today in the ecosystem) but the actual semantics are rather different.Edit: Make it clear that I'm not suggesting that clone be renamed - That's neither my intent nor a solution to the issue. Just looking for potential way to mitigate this.
The text was updated successfully, but these errors were encountered: