File tree 2 files changed +12
-1
lines changed
2 files changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -79,6 +79,16 @@ u.f1 = 2;
79
79
Commonly, code using unions will provide safe wrappers around unsafe union
80
80
field accesses.
81
81
82
+ ## Unions and ` Drop `
83
+
84
+ When a union is dropped, it cannot know which of its fields needs to be dropped.
85
+ For this reason, all union fields must either be of a ` Copy ` type or of the
86
+ shape [ ` ManuallyDrop<_> ` ] . This ensures that a union does not need to drop
87
+ anything when it goes out of scope.
88
+
89
+ Like for structs and enums, it is possible to ` impl Drop ` for a union to
90
+ manually define what happens when it gets dropped.
91
+
82
92
## Pattern matching on unions
83
93
84
94
Another way to access union fields is to use pattern matching. Pattern matching
@@ -167,3 +177,4 @@ checking, etc etc etc).
167
177
[ _WhereClause_ ] : generics.md#where-clauses
168
178
[ _StructFields_ ] : structs.md
169
179
[ `transmute` ] : ../../std/mem/fn.transmute.html
180
+ [ `ManuallyDrop<_>` ] : ../../std/mem/struct.ManuallyDrop.html
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ a [`union` item][item].
6
6
Unions have no notion of an "active field". Instead, every union access
7
7
transmutes parts of the content of the union to the type of the accessed
8
8
field. Since transmutes can cause unexpected or undefined behaviour, ` unsafe ` is
9
- required to read from a union field or to write to a field that doesn't
9
+ required to read from a union field, or to write to a field that doesn't
10
10
implement [ ` Copy ` ] . See the [ item] documentation for further details.
11
11
12
12
The memory layout of a ` union ` is undefined by default, but the ` #[repr(...)] `
You can’t perform that action at this time.
0 commit comments