Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union initialization and Drop #2514

Merged
merged 12 commits into from
Oct 17, 2018
Prev Previous commit
Next Next commit
fix code typo, and make unions more consistent
  • Loading branch information
RalfJung committed Aug 3, 2018
commit d4d6e380f6fe8a113022cd1e616fa37ce7690370
8 changes: 4 additions & 4 deletions text/0000-union-initialization-and-drop.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ u.f1 = ManuallyDrop::new(Vec::new());
{ let _x = &u.f2.0; }

// Equivalently, we can assign the entire union:
u = U { f2: S(42) };
u = U { f2: (S(42), S(23) };
// Now `u` is still initialized.

// Copying does not change anything:
Expand Down Expand Up @@ -144,7 +144,7 @@ and it is not possible to move out of a field. For example:

struct S(i32); // not `Copy`, no drop glue

union U { f1: ManuallyDrop<Vec<i32>>, f2: S, f3: u32 }
union U { f1: ManuallyDrop<Vec<i32>>, f2: (S, S), f3: u32 }
impl Drop for U {
fn drop(&mut self) {
println!("Goodbye!");
Expand All @@ -153,7 +153,7 @@ impl Drop for U {

let mut u: U;
// `u.f1 = ...;` gets rejected: Cannot initialize a union with `Drop` by assigning a field.
u = U { f2: S(42) };
u = U { f2: (S(42), S(1)) };
// Now `u` is initialized.

// `let v = u.f1;` gets rejected: Cannot move out of union that implements `Drop`.
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -166,7 +166,7 @@ When a union implementing `Drop` goes out of scope, its destructor gets called i

```rust
{
let u = U { f2: S(42) };
let u = U { f2: (S(0), S(1)) };
// drop gets called
}
{
Expand Down