Skip to content

Commit 5133f9e

Browse files
authored
[type-layout] Document bit validity of structs and padding
This intentionally omits discussions of enums and unions. Enum bit validity is well-defined, but it is difficult to articulate concisely. Union bit validity is still not nailed down.
1 parent 9c21bee commit 5133f9e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/type-layout.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Slices have the same layout as the section of the array they slice.
9191
> etc.) to slices.
9292
9393
## `str` Layout
94+
9495
String slices are a UTF-8 representation of characters that have the same layout as slices of type `[u8]`.
9596

9697
## Tuple Layout
@@ -589,6 +590,22 @@ primitive field will have the ABI of the primitive field.
589590
Because this representation delegates type layout to another type, it cannot be
590591
used with any other representation.
591592
593+
## Bit validity of composite types
594+
595+
Each Rust type has "bit validity", which is the set of bit patterns which may
596+
appear in a value of that type. It is [undefined behavior] to produce a value
597+
of a type which violates bit validity (in other words, to initialize a value
598+
to a bit pattern which is not in that type's set of valid bit patterns).
599+
Note that the total set of possible bit patterns for any given byte has 257
600+
elements - 256 initialized bit patterns and one "uninitialized" bit pattern.
601+
602+
The bit validity of a struct type is the composition of the bit validities
603+
of its fields.
604+
605+
Padding bytes have no bit validity requirement: it is always well-defined
606+
to write any byte value - including an uninitialized byte - to any byte of
607+
padding.
608+
592609
[`align_of_val`]: std::mem::align_of_val
593610
[`size_of_val`]: std::mem::size_of_val
594611
[`align_of`]: std::mem::align_of

0 commit comments

Comments
 (0)