Skip to content

Layout of homogeneous structs #36

Open
@nikomatsakis

Description

@nikomatsakis

From #31: If you have homogeneous structs, where all the N fields are of a single type T, can we guarantee a mapping to the memory layout of [T; N]? How do we map between the field names and the indices? What about zero-sized types?

A specific proposal:

  • If all fields have the same type T (ignoring zero-sized types), then the layout will be the same as [T; N] where N is the number of fields
  • If the struct is defined with named fields, the mapping from fields to their indices is undefined (so foo.bar maps an undefined index)
  • If the struct is defined as a tuple struct (or a tuple), then the indices are derived from the definition (so foo.0 maps to [0])

This is basically because it's convenient but also because it's about the only sensible thing to do (unless you imagine we might want to start inserting padding between random fields or something, which connects to #35).

Note that for tuple structs (and tuples), the ordering of (public) fields is also significant for semver and other purposes -- changing the ordering will affect your clients. The same is not true for named fields and so this proposal attempts to avoid making it true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layoutTopic: Related to data structure layout (`#[repr]`)S-not-opsemDespite being in this repo, this is not primarily a T-opsem questionT-lang

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions