Description
The Swift ABI defines an interesting struct layout which defines the size of a type separately from its stride:
Note that this differs from C or LLVM's normal layout rules in that size and stride are distinct; whereas C layout requires that an embedded struct's size be padded out to its alignment and that nothing be laid out there, Swift layout allows an outer struct to lay out fields in the inner struct's tail padding, alignment permitting.
While rust doesn't define a stable ABI yet, I think using such a layout would be a breaking change due to assumptions that unsafe code make about mem::size_of
.
In particular, unsafe code might assume that, when given a pointer let x: *mut T
, all bytes from x
to x + mem::size_of::<T>()
can safely be overwritten. This would be incorrect when the padding bytes at the end of T
contain fields of an outer struct.
Is such an optimization worth considering? From a quick look at the rust source, it seems that all uses of mem::size_of
will continue to work fine if we define it as returning the stride (aligned size). I haven't looked at any external crates yet but I think they would continue to work fine as well. In any case this would be a breaking change and should be considered carefully.