4 ms·
It is a nice read! I am not super familiar with the definition of "object-safe" (I read the docs but the definition is not completely trivial for me). Does it
by palata 2y ago
It is a nice read!
I am not super familiar with the definition of "object-safe" (I read the docs but the definition is not completely trivial for me).
Does it work to say that an "object-safe" method mutates the object? That's what happens when you take `&mut self`, right?
But if I understood this correctly, I guess it means that a function without side-effect in Java should not really count as a method? Because in Java I call it a method whether it has a side-effect or not. Which corresponds exactly to the definition of `method` in the Rust docs, I think [1]. In C++, I would think that this is the difference between a const (no side-effect) and a non-const (side-effect) method.
[1]: https://doc.rust-lang.org/reference/items/associated-items.html#associated-functions-and-methods https://doc.rust-lang.org/reference/items/associated-items.h...
- throwup238 2y agoIt has to do with type erasure and knowing how big a type is in memory, not mutation. With traits, you can erase the implementing type and get back a vtable of a trait's method - called a trait object. A trait is object-safe as long as no trait method returns the implementing type (like T::clone() -> T). Once it's a trait object, T is no longer known and the compiler doesn't know how much memory to allocate on the stack or heap, which makes it impossible to use. That's why trait objects have to be boxed or behind references - all pointers have a known size.
- diarrhea 2y ago> A trait is object-safe as long as no trait method returns the implementing type (like T::clone() -> T) That, and much more: https://doc.rust-lang.org/reference/items/traits.html#object-safety https://doc.rust-lang.org/reference/items/traits.html#object... .
- asa400 2y agoThis is the shortest, clearest explanation I've yet seen of object safety. Thanks.
- steveklabnik 2y agoNot every trait can be made into a trait object. Traits that are "object safe" can be made into a trait object. Nothing to do with mutation, or side effects.
- palata 2y agohmm... so there are object-safe methods and non-object-safe methods, and you can make a `Vec<YourType>` as long as `YourType` does not implement any non-object-safe methods?
- xvedejas 2y agoYou might be confusing types and traits (typeclasses) here; it's not about `YourType` but rather when you want to make a Vec of heterogenous types that share a trait. That's when the question of object safety (and needing a vtable) arises.
- sshine 2y ago> there are object-safe methods and non-object-safe methods Yes. In the reference, below the definition, there is a list of examples of: - Traits that are object-safe with object-safe methods - Traits that are object-safe with non-dispatchable methods - Traits that are not object-safe https://doc.rust-lang.org/reference/items/traits.html#object-safety https://doc.rust-lang.org/reference/items/traits.html#object... > you can make a `Vec<YourType>` as long as `YourType` does not implement any non-object-safe methods? It is traits that are object safe, not concrete types. You cannot make a `Vec<YourTrait>`, you have to make a `Vec<T> where T: YourTrait` or a `Vec<Box<dyn YourTrait>>` or similar. You only need object safety if you're passing around values dynamically cast to a trait. That's not a required programming pattern.
- josephg 2y ago> You only need object safety if you're passing around values dynamically cast to a trait. That's not a required programming pattern. Yep. In 3+ years of writing rust full time, I’ve still never used `dyn Trait` in any of its forms. A vec where every item is independently heap allocated? Why? And yikes! That would perform terribly.