@@ -29,18 +29,24 @@ struct types, except that it must specify exactly one field:
2929let u = MyUnion { f1 : 1 };
3030```
3131
32- The expression above creates a value of type ` MyUnion ` with active field ` f1 ` .
33- Active field of a union can be accessed using the same syntax as struct fields:
32+ The expression above creates a value of type ` MyUnion ` and initializes the
33+ storage using field ` f1 ` . The union can be accessed using the same syntax as
34+ struct fields:
3435
3536``` rust,ignore
3637let f = u.f1;
3738```
3839
39- Inactive fields can be accessed as well (using the same syntax) if they are
40- sufficiently layout compatible with the current value kept by the union.
41- Reading incompatible fields results in undefined behavior. However, the active
42- field is not generally known statically, so all reads of union fields have to
43- be placed in ` unsafe ` blocks.
40+ Unions have no notion of an "active field". Instead, every union access just
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
47+ writing to the type used for reading.
48+
49+ Consequently, all reads of union fields have to be placed in ` unsafe ` blocks:
4450
4551``` rust
4652# union MyUnion { f1 : u32 , f2 : f32 }
@@ -65,9 +71,9 @@ Commonly, code using unions will provide safe wrappers around unsafe union
6571field accesses.
6672
6773Another way to access union fields is to use pattern matching. Pattern matching
68- on union fields uses the same syntax as struct patterns, except that the
69- pattern must specify exactly one field. Since pattern matching accesses
70- potentially inactive fields it has to be placed in ` unsafe ` blocks as well.
74+ on union fields uses the same syntax as struct patterns, except that the pattern
75+ must specify exactly one field. Since pattern matching is like reading the union
76+ with a particular field, it has to be placed in ` unsafe ` blocks as well.
7177
7278``` rust
7379# union MyUnion { f1 : u32 , f2 : f32 }
@@ -142,10 +148,8 @@ aspects of Rust language (such as privacy, name resolution, type inference,
142148generics, trait implementations, inherent implementations, coherence, pattern
143149checking, etc etc etc).
144150
145- More detailed specification for unions, including unstable bits, can be found
146- in [ RFC 1897 "Unions v1.2"] ( https://github.com/rust-lang/rfcs/pull/1897 ) .
147-
148151[ IDENTIFIER ] : identifiers.html
149152[ _Generics_ ] : items/generics.html
150153[ _WhereClause_ ] : items/generics.html#where-clauses
151154[ _StructFields_ ] : items/structs.html
155+ [ `transmute` ] : ../../std/mem/fn.transmute.html
0 commit comments