| 1 |
# replace_with |
| 2 |
|
| 3 |
[](https://crates.io/crates/replace_with) |
| 4 |
[](#License) |
| 5 |
[](https://dev.azure.com/alecmocatta/replace_with/_build?definitionId=11) |
| 6 |
|
| 7 |
[📖 Docs](https://docs.rs/replace_with) | [💬 Chat](https://constellation.zulipchat.com/#narrow/stream/213236-subprojects) |
| 8 |
|
| 9 |
Temporarily take ownership of a value at a mutable location, and replace it with a new value based on the old one. |
| 10 |
|
| 11 |
This crate provides the function [`replace_with()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with.html), which is like [`std::mem::replace()`](https://doc.rust-lang.org/std/mem/fn.replace.html) except it allows the replacement value to be mapped from the original value. |
| 12 |
|
| 13 |
See [RFC 1736](https://github.com/rust-lang/rfcs/pull/1736) for a lot of discussion as to its merits. It was never merged, and the desired ability to temporarily move out of `&mut T` doesn't exist yet, so this crate is my interim solution. |
| 14 |
|
| 15 |
It's very akin to [`take_mut`](https://github.com/Sgeo/take_mut), though uses `Drop` instead of [`std::panic::catch_unwind()`](https://doc.rust-lang.org/std/panic/fn.catch_unwind.html) to react to unwinding, which avoids the optimisation barrier of calling the `extern "C" __rust_maybe_catch_panic()`. As such it's up to ∞x faster. The API also attempts to make slightly more explicit the behavior on panic – [`replace_with()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with.html) accepts two closures such that aborting in the "standard case" where the mapping closure (`FnOnce(T) -> T`) panics (as [`take_mut::take()`](https://docs.rs/take_mut/0.2.2/take_mut/fn.take.html) does) is avoided. If the second closure (`FnOnce() -> T`) panics, however, then it does indeed abort. The "abort on first panic" behaviour is available with [`replace_with_or_abort()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with_or_abort.html). |
| 16 |
|
| 17 |
## Example |
| 18 |
|
| 19 |
Consider this motivating example: |
| 20 |
|
| 21 |
```rust |
| 22 |
enum States { |
| 23 |
A(String), |
| 24 |
B(String), |
| 25 |
} |
| 26 |
|
| 27 |
impl States { |
| 28 |
fn poll(&mut self) { |
| 29 |
// error[E0507]: cannot move out of borrowed content |
| 30 |
*self = match *self { |
| 31 |
// ^^^^^ cannot move out of borrowed content |
| 32 |
States::A(a) => States::B(a), |
| 33 |
States::B(a) => States::A(a), |
| 34 |
}; |
| 35 |
} |
| 36 |
} |
| 37 |
``` |
| 38 |
|
| 39 |
Depending on context this can be quite tricky to work around. With this crate, however: |
| 40 |
|
| 41 |
```rust |
| 42 |
enum States { |
| 43 |
A(String), |
| 44 |
B(String), |
| 45 |
} |
| 46 |
|
| 47 |
impl States { |
| 48 |
fn poll(&mut self) { |
| 49 |
replace_with_or_abort(self, |self_| match self_ { |
| 50 |
States::A(a) => States::B(a), |
| 51 |
States::B(a) => States::A(a), |
| 52 |
}); |
| 53 |
} |
| 54 |
} |
| 55 |
``` |
| 56 |
|
| 57 |
Huzzah! |
| 58 |
|
| 59 |
## `no_std` |
| 60 |
|
| 61 |
To use `replace_with` with `no_std` you have to disable the `std` feature, which is active by default, by specifying your dependency to it like this: |
| 62 |
|
| 63 |
```toml |
| 64 |
# Cargo.toml |
| 65 |
|
| 66 |
[dependencies.replace_with] |
| 67 |
version = ... |
| 68 |
default-features = false |
| 69 |
features = [] |
| 70 |
... |
| 71 |
``` |
| 72 |
|
| 73 |
The [`replace_with()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with.html) & [`replace_with_or_default()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with_or_default.html) functions are available on stable Rust both, with and without `std`. |
| 74 |
|
| 75 |
The [`replace_with_or_abort()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with_or_abort.html) function however by default makes use of [`std::process::abort()`](https://doc.rust-lang.org/std/process/fn.abort.html) which is not available with `no_std`. |
| 76 |
|
| 77 |
As such `replace_with` will by default call [`core::intrinsics::abort()`](https://doc.rust-lang.org/core/intrinsics/fn.abort.html) instead, which in turn requires nightly Rust. |
| 78 |
|
| 79 |
Not everything is lost for stable `no_std` though, `replace_with` has one more trick up its sleeve: |
| 80 |
|
| 81 |
### panic = "abort" |
| 82 |
|
| 83 |
If you define [`panic = abort`](https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/aborting-on-panic.html) in the `[profile]` section of your crate's `Cargo.toml` … |
| 84 |
|
| 85 |
```toml |
| 86 |
# Cargo.toml |
| 87 |
|
| 88 |
[profile.debug] |
| 89 |
panic = "abort" |
| 90 |
|
| 91 |
[profile.release] |
| 92 |
panic = "abort" |
| 93 |
``` |
| 94 |
|
| 95 |
… and add the `"panic_abort"` feature to `replace_with` in the `dependencies` section of your crate's `Cargo.toml` … |
| 96 |
|
| 97 |
```toml |
| 98 |
# Cargo.toml |
| 99 |
|
| 100 |
[dependencies.replace_with] |
| 101 |
features = ["panic_abort"] |
| 102 |
... |
| 103 |
``` |
| 104 |
|
| 105 |
… the `"panic_abort"` feature enables the [`replace_with_or_abort_unchecked()`](https://docs.rs/replace_with/0.1/replace_with/fn.replace_with_or_abort_unchecked.html) function becomes on stable Rust as an `unsafe` function, a simple wrapper around `ptr::write(dest, f(ptr::read(dest)));`. |
| 106 |
|
| 107 |
**Word of caution:** It is crucial to only ever use this function having defined `panic = "abort"`, or else bad things may happen. It's *up to you* to uphold this invariant! |
| 108 |
|
| 109 |
## License |
| 110 |
Licensed under either of |
| 111 |
|
| 112 |
* Apache License, Version 2.0, ([LICENSE-APACHE.txt](LICENSE-APACHE.txt) or http://www.apache.org/licenses/LICENSE-2.0) |
| 113 |
* MIT license ([LICENSE-MIT.txt](LICENSE-MIT.txt) or http://opensource.org/licenses/MIT) |
| 114 |
|
| 115 |
at your option. |
| 116 |
|
| 117 |
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. |