pub fn replace_with_or_abort<T, F: FnOnce(T) -> T>(dest: &mut T, f: F)
Expand description
Temporarily takes ownership of a value at a mutable location, and replace it with a new value based on the old one. Aborts on panic.
We move out of the reference temporarily, to apply a closure f
, returning a new value, which
is then placed at the original value’s location.
§An important note
On panic (or to be more precise, unwinding) of the closure f
, the process will abort to
avoid returning control while dest
is in a potentially invalid state.
If this behaviour is undesirable, use replace_with
or replace_with_or_default
.
Equivalent to replace_with(dest, || process::abort(), f)
.
§Example
enum States {
A(String),
B(String),
}
impl States {
fn poll(&mut self) {
replace_with_or_abort(self, |self_| match self_ {
States::A(a) => States::B(a),
States::B(a) => States::A(a),
});
}
}