2016-10-08 06:51:31 +00:00
|
|
|
|
# `mem::replace` to keep owned values in changed enums
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
|
|
|
|
|
Say we have a `&mut MyEnum` which has (at least) two variants,
|
|
|
|
|
`A { name: String, x: u8 }` and `B { name: String }`. Now we want to change
|
|
|
|
|
`MyEnum::A` to a `B` if `x` is zero, while keeping `MyEnum::B` intact.
|
|
|
|
|
|
|
|
|
|
We can do this without cloning the `name`.
|
|
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
use std::mem;
|
|
|
|
|
|
2016-10-09 17:47:44 +00:00
|
|
|
|
enum MyEnum {
|
|
|
|
|
A { name: String, x: u8 },
|
|
|
|
|
B { name: String }
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-08 06:51:31 +00:00
|
|
|
|
fn a_to_b(e: &mut MyEnum) {
|
2016-10-09 17:47:44 +00:00
|
|
|
|
|
2016-10-08 06:51:31 +00:00
|
|
|
|
// we mutably borrow `e` here. This precludes us from changing it directly
|
2016-10-09 17:47:44 +00:00
|
|
|
|
// as in `*e = ...`, because the borrow checker won't allow it. Therefore
|
2021-01-05 14:57:07 +00:00
|
|
|
|
// the assignment to `e` must be outside the `if let` clause.
|
2016-10-09 17:47:44 +00:00
|
|
|
|
*e = if let MyEnum::A { ref mut name, x: 0 } = *e {
|
2021-01-05 14:57:07 +00:00
|
|
|
|
|
2016-10-09 17:47:44 +00:00
|
|
|
|
// this takes out our `name` and put in an empty String instead
|
|
|
|
|
// (note that empty strings don't allocate).
|
2021-01-05 14:57:07 +00:00
|
|
|
|
// Then, construct the new enum variant (which will
|
2016-10-09 17:47:44 +00:00
|
|
|
|
// be assigned to `*e`, because it is the result of the `if let` expression).
|
2016-10-12 06:50:29 +00:00
|
|
|
|
MyEnum::B { name: mem::replace(name, String::new()) }
|
2021-01-05 14:57:07 +00:00
|
|
|
|
|
2016-10-09 17:47:44 +00:00
|
|
|
|
// In all other cases, we return immediately, thus skipping the assignment
|
|
|
|
|
} else { return }
|
2016-10-08 06:51:31 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-18 20:24:30 +00:00
|
|
|
|
This also works with more variants:
|
|
|
|
|
|
|
|
|
|
```Rust
|
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
|
|
enum MultiVariateEnum {
|
|
|
|
|
A { name: String },
|
|
|
|
|
B { name: String },
|
|
|
|
|
C,
|
|
|
|
|
D
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn swizzle(e: &mut MultiVariateEnum) {
|
|
|
|
|
use self::MultiVariateEnum::*;
|
|
|
|
|
*e = match *e {
|
2017-01-01 04:26:33 +00:00
|
|
|
|
// Ownership rules do not allow taking `name` by value, but we cannot
|
|
|
|
|
// take the value out of a mutable reference, unless we replace it:
|
2016-12-18 20:24:30 +00:00
|
|
|
|
A { ref mut name } => B { name: mem::replace(name, String::new()) },
|
|
|
|
|
B { ref mut name } => A { name: mem::replace(name, String::new()) },
|
|
|
|
|
C => D,
|
|
|
|
|
D => C
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2016-10-08 06:51:31 +00:00
|
|
|
|
|
|
|
|
|
## Motivation
|
|
|
|
|
|
|
|
|
|
When working with enums, we may want to change an enum value in place, perhaps
|
|
|
|
|
to another variant. This is usually done in two phases to keep the borrow
|
|
|
|
|
checker happy. In the first phase, we observe the existing value and look at
|
|
|
|
|
its parts to decide what to do next. In the second phase we may conditionally
|
|
|
|
|
change the value (as in the example above).
|
|
|
|
|
|
|
|
|
|
The borrow checker won't allow us to take out `name` of the enum (because
|
|
|
|
|
*something* must be there. We could of course `.clone()` name and put the clone
|
|
|
|
|
into our `MyEnum::B`, but that would be an instance of the [Clone to satisfy
|
|
|
|
|
the borrow checker] antipattern. Anyway, we can avoid the extra allocation by
|
|
|
|
|
changing `e` with only a mutable borrow.
|
|
|
|
|
|
|
|
|
|
`mem::replace` lets us swap out the value, replacing it with something else. In
|
|
|
|
|
this case, we put in an empty `String`, which does not need to allocate. As a
|
2016-10-09 17:47:44 +00:00
|
|
|
|
result, we get the original `name` *as an owned value*. We can then wrap this in
|
|
|
|
|
another enum.
|
2016-10-08 06:51:31 +00:00
|
|
|
|
|
2016-10-10 08:52:57 +00:00
|
|
|
|
Note, however, that if we are using an `Option` and want to replace its
|
2016-10-09 11:39:26 +00:00
|
|
|
|
value with a `None`, `Option`’s `take()` method provides a shorter and
|
|
|
|
|
more idiomatic alternative.
|
|
|
|
|
|
2016-10-08 06:51:31 +00:00
|
|
|
|
## Advantages
|
|
|
|
|
|
|
|
|
|
Look ma, no allocation! Also you may feel like Indiana Jones while doing it.
|
|
|
|
|
|
|
|
|
|
## Disadvantages
|
|
|
|
|
|
|
|
|
|
This gets a bit wordy. Getting it wrong repeatedly will make you hate the
|
|
|
|
|
borrow checker. The compiler may fail to optimize away the double store,
|
|
|
|
|
resulting in reduced performance as opposed to what you'd do in unsafe
|
|
|
|
|
languages.
|
|
|
|
|
|
|
|
|
|
## Discussion
|
|
|
|
|
|
|
|
|
|
This pattern is only of interest in Rust. In GC'd languages, you'd take the
|
|
|
|
|
reference to the value by default (and the GC would keep track of refs), and in
|
|
|
|
|
other low-level languages like C you'd simply alias the pointer and fix things
|
|
|
|
|
later.
|
|
|
|
|
|
|
|
|
|
However, in Rust, we have to do a little more work to do this. An owned value
|
|
|
|
|
may only have one owner, so to take it out, we need to put something back in –
|
|
|
|
|
like Indiana Jones, replacing the artifact with a bag of sand.
|
|
|
|
|
|
|
|
|
|
## See also
|
|
|
|
|
|
|
|
|
|
This gets rid of the [Clone to satisfy the borrow checker] antipattern in a
|
|
|
|
|
specific case.
|
|
|
|
|
|
2016-10-08 17:14:05 +00:00
|
|
|
|
[Clone to satisfy the borrow checker](TODO: Hinges on PR #23)
|