In a Rust tutorial about memory layout of different types, it talks about trait objects. However, as it shows, the part of trait object that lives on the stack has constant size: one word
for pointer to the value, and another word
for the pointer to its vtable = 16 bytes on a 64-bit machine.
My question is that why then do we require a reference to the trait object, if it has a fixed size? Does this "fat pointer" consisting of two words actually the reference, because that's inconsistent with how references work in all of other Rust, where it's just a thin pointer to some data. And I don't believe Rust would be unnecessarily hiding this detail and being inconsistent.
As I learned in the comments, Rust is, in-fact inconsistent in how it treats &
, which depends on the context. For example, &dyn Trait
and &[T]
are fat pointers, but in general, &T
will be a thin pointer.