Skip to content

Commit 9aceafe

Browse files
committed
Update unions for safe ManuallyDrop assignment.
1 parent a8afdca commit 9aceafe

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/items/unions.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@ unsafe {
6666
}
6767
```
6868

69-
Writes to `Copy` union fields do not require reads for running destructors, so
70-
these writes don't have to be placed in `unsafe` blocks
69+
Writes to [`Copy`] or [`ManuallyDrop`][ManuallyDrop] union fields do not
70+
require reads for running destructors, so these writes don't have to be placed
71+
in `unsafe` blocks
7172

7273
```rust
73-
# union MyUnion { f1: u32, f2: f32 }
74-
# let mut u = MyUnion { f1: 1 };
75-
#
74+
# use std::mem::ManuallyDrop;
75+
union MyUnion { f1: u32, f2: ManuallyDrop<String> }
76+
let mut u = MyUnion { f1: 1 };
77+
78+
// These do not require `unsafe`.
7679
u.f1 = 2;
80+
u.f2 = ManuallyDrop::new(String::from("example"));
7781
```
7882

7983
Commonly, code using unions will provide safe wrappers around unsafe union
@@ -82,9 +86,9 @@ field accesses.
8286
## Unions and `Drop`
8387

8488
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.
89+
For this reason, all union fields must either be of a [`Copy`] type or of the
90+
shape [`ManuallyDrop<_>`][ManuallyDrop]. This ensures that a union does not
91+
need to drop anything when it goes out of scope.
8892

8993
Like for structs and enums, it is possible to `impl Drop` for a union to
9094
manually define what happens when it gets dropped.
@@ -177,4 +181,5 @@ checking, etc etc etc).
177181
[_WhereClause_]: generics.md#where-clauses
178182
[_StructFields_]: structs.md
179183
[`transmute`]: ../../std/mem/fn.transmute.html
180-
[`ManuallyDrop<_>`]: ../../std/mem/struct.ManuallyDrop.html
184+
[`Copy`]: ../../std/marker/trait.Copy.html
185+
[ManuallyDrop]: ../../std/mem/struct.ManuallyDrop.html

src/types/union.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ a [`union` item][item].
55

66
Unions have no notion of an "active field". Instead, every union access
77
transmutes parts of the content of the union to the type of the accessed
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
10-
implement [`Copy`]. See the [item] documentation for further details.
8+
field. Since transmutes can cause unexpected or undefined behaviour, `unsafe`
9+
is required to read from a union field, or to write to a field that doesn't
10+
implement [`Copy`] or is a [`ManuallyDrop`] type. See the [item] documentation
11+
for further details.
1112

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

1516
[`Copy`]: ../special-types-and-traits.md#copy
17+
[`ManuallyDrop`]: ../../std/mem/struct.ManuallyDrop.html
1618
[item]: ../items/unions.md

0 commit comments

Comments
 (0)