Rust's trait objects are fat pointers that contain 2 regular pointers: to data and to a vtable. The vtable is a structure containing a destructor function pointer, all trait method pointers and finally the size and alignment of the data.
What are the size and alignment fields for?
I couldn't find much:
- Blog post A: it's for deallocating memory, but not used today, may be used by some future, more flexible mechanisms (What could it be? Does any exist yet?)
- Blog post B: it's for deallocating type-erased boxed values, so they know how to release memory (Doesn't
Box
store location, size and alignment of its allocation? Every size variant of every DST can't get its own version of a vtable, can it?)