Rust, alignment, size_of

Suppose we have some struct/enum T, whose std::mem::size_of - Rust is measured as 2^k * odd_number

Then, as long as we read/write T at boundaries of 2^k, there should be no alignment issues right? (regardless of T's repr)

I don’t know if it’s formally guaranteed to remain true in the future, but I believe that’s correct for now (and probably a long time to come):

  • size_of is guaranteed to be a multiple of the alignment, as it’s the stride between successive elements in an array
  • Alignment is currently required to be a power of two; this might change in the future, but it serms unlikely
  • Therefore, any odd factor of size_of cannot be part of the type’s alignment
3 Likes