Skip to content

Padding Bytes Guarantees? #174

Closed
@jswrenn

Description

@jswrenn

The context of this issue is determining when it is safe for a struct to implement FromZeros (à la zerocopy::FromBytes), a marker trait implemented for a type T iff any sequence of initialized zeroed bytes of length size_of::<T>() is a valid instance of T. (For such types, mem::zeroed is safe!)

Per the reference:

The representation of a type can change the padding between fields, but does not change the layout of the fields themselves.

Therefore: for a struct T to be FromZeros, the fields of T must also be FromZeros.

I'm trying to determine whether this requirement alone is sufficient.

My understanding is that the padding bytes between fields is expressly undefined (i.e., it can be anything, including uninitialized). Will Rust ever rely on padding having a particular value?

If so, for a struct T to be FromZeros, it must have no padding, as a 0u8 might not correspond to a valid padding byte for T. This poses a very severe limitation on what structs could be FromZeros (and, by extension, FromBytes).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-paddingTopic: Related to paddingC-open-questionCategory: An open question that we should revisit

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions