Skip to content

Commit

Permalink
[type-layout] Document minimum size and alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlf authored Mar 11, 2024
1 parent 5afb503 commit 49aa9bf
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/type-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ as [dynamically sized types]. Since all values of a `Sized` type share the same
size and alignment, we refer to those shared values as the size of the type and
the alignment of the type respectively.

### Minimum Size and Alignment

Every type, including [dynamically sized types], has a "minimum size" and "minimum
alignment." Every value of a type must have a size at least as large as the type's
minimum size and an alignment at least as large as the type's minimum alignment.
Some notable cases of minimum size and alignment are:
- For [`Sized`] types, the type's minimum size and minimum alignment
are always equal to the type's size and alignment
- For a [slice type](#slice-layout), `[T]`, the minimum size is 0 bytes (corresponding
a slice with 0 elements), and the minimum alignment is the alignment of `T`
- For a [trait object](#trait-object-layout), the minimum size is 0 and the minimum
alignment is 1
- For a struct type with a dynamically-sized field, the minimum size is taken to be
the size of the struct when the dynamically-sized field has *its* minimum size.
The struct's minimum alignment is taken to be the alignment of the struct when the
dynamically-sized field has *its* minimum alignment. When relying on this property
of dynamically-sized structs, be careful not to assume too much! For example,
this property provides *no* guarantees about [`repr(Rust)`](#the-rust-representation)
types, as such a type can have arbitrarily large size and alignment regardless of
the sizes and alignments of its fields
- Every type's minimum size is less than or equal to `isize::MAX`

## Primitive Data Layout

The size of most primitives is given in this table.
Expand Down

0 comments on commit 49aa9bf

Please sign in to comment.