Skip to content

Document field struct alignment guarantees? #1151

Closed
@dureuill

Description

@dureuill

This recently posted article makes that point that, because the default representation makes no guarantees of data layout, one must use write_unaligned to write to the fields of a struct when initializing it field by field.

This runs contrary to the the documentation of MaybeUninit about initializing a struct field by field, and also to what the nomicon has to say on the subject:

By default, composite structures have an alignment equal to the maximum of their fields' alignments. Rust will consequently insert padding where necessary to ensure that all fields are properly aligned and that the overall type's size is a multiple of its alignment.

Should we amend the reference to either link to the nomicon for more details, or to specify that fields that can be accessed in a struct are properly aligned (no write_unaligned necessary)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions