File tree 2 files changed +12
-9
lines changed
2 files changed +12
-9
lines changed Original file line number Diff line number Diff line change @@ -39,11 +39,13 @@ let f = u.f1;
39
39
40
40
Unions have no notion of an "active field". Instead, every union access just
41
41
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
42
+ union field reads the bits of the union at the field's type. Fields might have a
43
+ non-zero offset (except when ` #[repr(C)] ` is used); in that case the bits
44
+ starting at the offset of the fields are read. It is the programmer's
45
+ responsibility to make sure that the data is valid at that type. Failing to do
46
+ so results in undefined behavior. For example, reading the value ` 3 ` at type
47
+ ` bool ` is undefined behavior. Effectively, writing to and then reading from a
48
+ ` #[repr(C)] ` union is analogous to a [ ` transmute ` ] from the type used for
47
49
writing to the type used for reading.
48
50
49
51
Consequently, all reads of union fields have to be placed in ` unsafe ` blocks:
Original file line number Diff line number Diff line change 1
1
# Union types
2
2
3
3
A * union type* is a nominal, heterogeneous C-like union, denoted by the name of
4
- a [ ` union ` item] .
4
+ a [ ` union ` item] [ item ] .
5
5
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
7
8
field. Since transmutes can cause unexpected or undefined behaviour, ` unsafe ` is
8
9
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.
10
11
11
12
The memory layout of a ` union ` is undefined by default, but the ` #[repr(...)] `
12
13
attribute can be used to fix a layout.
13
14
14
15
[ `Copy` ] : special-types-and-traits.html#copy
15
- [ `union` item] : items/unions.html
16
+ [ item ] : items/unions.html
You can’t perform that action at this time.
0 commit comments