Re:Why Rust helps even if you have to use a lot of `unsafe`
by flohofwoe on Thursday August 03, 2023 @02:38AM
(#36982177)
The allocator can't just work with a size and alignment, it also needs to know the internal structure of objects so that it can arrange them differently (maybe to remove padding bytes, maybe to turn arrays-of-structs into structs-of-arrays. And in turn the compiler needs to know how the 'allocator' has rearranged things in order to generate the code which accesses the data.
I also wasn't thinking about moving parts of a struct out into heap allocations, that would be worse. Instead think about whether a sum type is actually necessary, even if it is convenient.
Such as ?