Skip to content

Commit

Permalink
Elaborate on how union constructors produce union values
Browse files Browse the repository at this point in the history
  • Loading branch information
chorman0773 committed Dec 11, 2024
1 parent 695d517 commit 5bc13a0
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/types/union.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ The memory layout of a `union` is undefined by default (in particular, fields do
fix a layout.

r[type.union.value]
A value of a union type consists of a sequence of bytes, corresponding to each [value byte][type.struct.value.value-bytes]. The value bytes of a union are represented exactly. Each [padding byte][type.struct.value.padding] is set to uninit.
A value of a union type consists of a sequence of bytes, corresponding to each [value byte][type.struct.value.value-bytes]. The value bytes of a union are represented exactly. Each [padding byte][type.struct.value.padding] is set to uninit when encoded.

> [!NOTE]
> When a union value is constructed or a field is read/written to, the value of that field is encoded or decoded appropriately.
> A given value byte is guaranteed allowed to be uninit if it is padding in any field, recursively expanding union fields. Whether a byte of a union is allowed to be uninit in any other case is not yet decided.
r[type.union.constructor]
The constructor of a union type encodes the initialized field value into the corresponding bytes of the union, and sets all bytes that are not used by the field to uninit.

r[type.union.field-access]
When a field is written to by a field access expression, the value written is encoded into the corresponding bytes of the union. When a field is read, the value of that field is decoded from the corresponding bytes.

[`Copy`]: ../special-types-and-traits.md#copy
[item]: ../items/unions.md

0 comments on commit 5bc13a0

Please sign in to comment.