Movatterモバイル変換


[0]ホーム

URL:


Docs.rs

Cratereplace_with

Source
Expand description

Temporarily take ownership of a value at a mutable location, and replace it with a new valuebased on the old one.

📦  Crates.io  │  📑  GitHub  │  💬  Chat

This crate provides the functionreplace_with(), which is likestd::mem::replace()except it allows the replacement value to be mapped from the original value.

SeeRFC 1736 for a lot of discussion as to itsmerits. It was never merged, and the desired ability to temporarily move out of&mut T doesn’texist yet, so this crate is my interim solution.

It’s very akin totake_mut, though usesDrop instead ofstd::panic::catch_unwind() to react to unwinding, which avoids the optimisation barrier ofcalling theextern "C" __rust_maybe_catch_panic(). As such it’s up to ∞x faster. The API alsoattempts to make slightly more explicit the behavior on panic –replace_with() accepts twoclosures such that aborting in the “standard case” where the mapping closure (FnOnce(T) -> T)panics (astake_mut::take() does) isavoided. If the second closure (FnOnce() -> T) panics, however, then it does indeed abort.The “abort on first panic” behaviour is available withreplace_with_or_abort().

§Example

Consider this motivating example:

enumStates {    A(String),    B(String),}implStates {fnpoll(&mutself) {// error[E0507]: cannot move out of borrowed content*self=match*self{//            ^^^^^ cannot move out of borrowed contentStates::A(a) => States::B(a),            States::B(a) => States::A(a),        };    }}

Depending on context this can be quite tricky to work around. With this crate, however:

enumStates {    A(String),    B(String),}implStates {fnpoll(&mutself) {        replace_with_or_abort(self, |self_|matchself_ {            States::A(a) => States::B(a),            States::B(a) => States::A(a),        });    }}

Huzzah!

§no_std

To usereplace_with withno_std you have to disable thestd feature, which is active bydefault, by specifying your dependency to it like this:

# Cargo.tomlreplace_with = { version = "0.1", default-features = false }

Thenightly feature can be enabled to usecore::intrinsics::abort()instead of triggering an abort viastd::process::abort()orextern "C" fn abort() { panic!() } abort().

Functions§

replace_with
Temporarily takes ownership of a value at a mutable location, and replace it with a new valuebased on the old one.
replace_with_and_return
Temporarily takes ownership of a value at a mutable location, and replace it with a new valuebased on the old one. Lets the closure return a custom value as well.
replace_with_or_abort
Temporarily takes ownership of a value at a mutable location, and replace it with a new valuebased on the old one. Aborts on panic.
replace_with_or_abort_and_return
Temporarily takes ownership of a value at a mutable location, and replace it with a new valuebased on the old one. Aborts on panic. Lets the closure return a custom value as well.
replace_with_or_default
Temporarily takes ownership of a value at a mutable location, and replace it with a new valuebased on the old one. Replaces withDefault::default() on panic.
replace_with_or_default_and_return
Temporarily takes ownership of a value at a mutable location, and replace it with a new valuebased on the old one. Replaces withDefault::default() on panic.Lets the closure return a custom value as well.

[8]ページ先頭

©2009-2025 Movatter.jp