google / google/zerocopy

Documentation: Warn that `byteorder` types, having alignment 1, don't cause struct padding

Open
#2,324 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2.6k
Forks
179
Avg merge
1d 19h
Merged PRs (30d)
29

Description

The multi-byte types in `zerocopy::byteorder` have alignment 1, which is very useful for reading from unaligned buffers. And the documentation also shows an example of using them to assemble a larger `#[repr(C)]` struct, which can also be read from unaligned buffers.

However, a less-obvious consequence of alignment 1 is that a struct built from these types doesn't have the internal/trailing padding that would be present when declaring a `#[repr(C)]` struct made up of ordinary types like `u32` or `u64`, which have alignment requirements.

This means that when declaring a struct to read struct data dumped by another C/Rust program, it may be necessary to manually insert padding between fields, so that the alignment 1 structure matches the actual layout of the original data.

---

The current behaviour is *correct*, of course, and is a natural consequence of the documented alignment, but could perhaps benefit from an explicit warning in the `byteorder` module docs.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.