-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Types implementing Deref{,Mut} shouldn't have any other methods or public fields #13126
Comments
This is not a problem isolated to Because of the auto(de)ref behavior of dot, you can get in this situation in plenty different ways. Prominent example: While implementors of Future example: With DST, Vec will implement |
This is something that we're inevitably buying into with the "smart pointer" concept. If we go down this road, all of these need to be removed:
This is what I think are just the start of the |
Surely removing those makes the types essentially useless? |
That's what I was getting at, I don't think that this is an actionable issue. |
It's totally actionable, potentially with a slightly more verbose API: impl<T> Rc<T> {
pub fn downgrade(&mut self) { ... }
pub fn get(&mut self) -> &'a T { ... }
} Maybe |
Alternatively we can keep the Deref impls and move the other stuff. For example: let foo = Rc<Foo>;
foo.downgrade(); // calls Foo's downgrade
let weak = Rc::downgrade(foo); // the only way to call Rc's downgrade |
I was assuming we would move these operations to functions, which seems like best practice. You could keep them---users can always explicitly dereference---but you couldn't add new such operations backwards compatibly. |
I guess we could establish a convention that you write "smart pointer.methods().foo()" or something -- that is, there is one designated name (
But it all seems awkward. |
This was discussed previously in rust-lang/rust#13126.
This was discussed previously in rust-lang/rust#13126.
This was discussed previously in rust-lang/rust#13126.
cc me |
@alexcrichton is shifting the convention to static methods over free functions. I think it seems like a great idea. Also, practice has been that some essential methods (comparison, clone etc) are shadowed by the smart pointer, while more specialized methods are not. (For example |
I think this is now as done (enough?) in the standard library, and is probably not really actionable anyway, due to stability. Closing. (Don't miss rust-lang/rfcs#279 either.) |
Fix [`redundant_slicing`] when the slice is behind a mutable reference Fixes rust-lang#12751 changelog: Fix [`redundant_slicing`] when the slice is behind a mutable reference and a immutable reference is expected.
For example,
sync::RwLockWriteGuard
implementsDeref
andDerefMut
, implements thedowngrade
method, and has a publiccond
field. Since Rust doesn't have distinct.
and->
operators, this makes for an API that can be confusing and ambiguous.Adding an extra layer of indirection seems like a reasonable solution. It feels a bit strange to call methods of the wrapped type on an RAII lock guard anyways:
The text was updated successfully, but these errors were encountered: