Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What guarantees are provided by repr(packed)? #318

Open
joshlf opened this issue Feb 8, 2022 · 4 comments
Open

What guarantees are provided by repr(packed)? #318

joshlf opened this issue Feb 8, 2022 · 4 comments

Comments

@joshlf
Copy link

joshlf commented Feb 8, 2022

Per the nomicon, repr(packed) guarantees that all inter-field padding is removed. However, the reference merely says that "packed may also alter the padding between fields" (emphasis added).

What guarantees does repr(packed) provide? In particular:

  • Does repr(packed) guarantee that all inter-field padding is removed?
  • Does repr(packed) guarantee that a type's layout is fixed in any way other than removing padding (e.g., field ordering)?
@RalfJung
Copy link
Member

RalfJung commented Feb 9, 2022

Does repr(packed) guarantee that all inter-field padding is removed?

I would strongly think so, that is the entire point after all. Things become a bit more tricky to describe with packed(N) though. Still I think this can be just fixed via a PR against the reference.

As for field ordering, I don't know if it has any effect.

@Diggsey
Copy link

Diggsey commented Feb 9, 2022

#[repr(packed)] means the struct won't add any padding as part of its own layout, but there might still effectively be padding between fields, eg. if the type of a field alignment=8 but only actually uses 4 bytes to store data, then its my understanding that there would still be 4 bytes of padding because that padding belongs to the field itself, and is not added by the struct.

@RalfJung
Copy link
Member

RalfJung commented Feb 9, 2022

Yes, the padding contained inside field types (including trailing padding to make the size a multiple of the alignment) is unaffected.

@joshlf
Copy link
Author

joshlf commented Feb 9, 2022

Submitted: rust-lang/reference#1163

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants