If possible, then the safest, most portable solution is to have the memory owned by a Vec<u32>
, and cast slices to [u8]
temporarily instead.
Of course, the [u8]
slices can't be used to grow the vector, but that's important: Growing the vec would cause it to reallocate, and you don't have any control over that new allocation even if the initial one was suitably aligned.
Alternately, if you know which platform or allocator your code is using, you could rely on guarantees made by the specific allocator. For example, the system allocator on both Unix-like platforms and Windows will always return allocations that are aligned for all built-in types. So if you know you'll be using one of these allocators, I believe you can rely on Vec<u8>
being at least 64-bit aligned (higher on some architectures).