Skip to content

Commit bccb2c7

Browse files
authored
Merge pull request rust-lang#627 from RalfJung/union
unions: call out field offset issues
2 parents 7a5aab5 + f9945c0 commit bccb2c7

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/items/unions.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ The key property of unions is that all fields of a union share common storage.
2020
As a result writes to one field of a union can overwrite its other fields, and
2121
size of a union is determined by the size of its largest field.
2222

23+
## Initialization of a union
24+
2325
A value of a union type can be created using the same syntax that is used for
2426
struct types, except that it must specify exactly one field:
2527

@@ -37,13 +39,17 @@ struct fields:
3739
let f = u.f1;
3840
```
3941

42+
## Reading and writing union fields
43+
4044
Unions have no notion of an "active field". Instead, every union access just
4145
interprets the storage at the type of the field used for the access. Reading a
42-
union field reads the bits of the union at the field's type. It is the
43-
programmer's responsibility to make sure that the data is valid at that
44-
type. Failing to do so results in undefined behavior. For example, reading the
45-
value `3` at type `bool` is undefined behavior. Effectively, writing to and then
46-
reading from a union is analogous to a [`transmute`] from the type used for
46+
union field reads the bits of the union at the field's type. Fields might have a
47+
non-zero offset (except when `#[repr(C)]` is used); in that case the bits
48+
starting at the offset of the fields are read. It is the programmer's
49+
responsibility to make sure that the data is valid at the field's type. Failing
50+
to do so results in undefined behavior. For example, reading the value `3` at
51+
type `bool` is undefined behavior. Effectively, writing to and then reading from
52+
a `#[repr(C)]` union is analogous to a [`transmute`] from the type used for
4753
writing to the type used for reading.
4854

4955
Consequently, all reads of union fields have to be placed in `unsafe` blocks:
@@ -70,6 +76,8 @@ u.f1 = 2;
7076
Commonly, code using unions will provide safe wrappers around unsafe union
7177
field accesses.
7278

79+
## Pattern matching on unions
80+
7381
Another way to access union fields is to use pattern matching. Pattern matching
7482
on union fields uses the same syntax as struct patterns, except that the pattern
7583
must specify exactly one field. Since pattern matching is like reading the union
@@ -119,6 +127,8 @@ fn is_zero(v: Value) -> bool {
119127
}
120128
```
121129

130+
## References to union fields
131+
122132
Since union fields share common storage, gaining write access to one field of a
123133
union can give write access to all its remaining fields. Borrow checking rules
124134
have to be adjusted to account for this fact. As a result, if one field of a

src/types/union.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Union types
22

33
A *union type* is a nominal, heterogeneous C-like union, denoted by the name of
4-
a [`union` item].
4+
a [`union` item][item].
55

6-
A union access transmutes the content of the union to the type of the accessed
6+
Unions have no notion of an "active field". Instead, every union access
7+
transmutes parts of the content of the union to the type of the accessed
78
field. Since transmutes can cause unexpected or undefined behaviour, `unsafe` is
89
required to read from a union field or to write to a field that doesn't
9-
implement [`Copy`].
10+
implement [`Copy`]. See the [item] documentation for further details.
1011

1112
The memory layout of a `union` is undefined by default, but the `#[repr(...)]`
1213
attribute can be used to fix a layout.
1314

1415
[`Copy`]: special-types-and-traits.html#copy
15-
[`union` item]: items/unions.html
16+
[item]: items/unions.html

0 commit comments

Comments
 (0)