Function take_mut::take_or_recover [] [src]

pub fn take_or_recover<T, F, R>(mut_ref: &mut T, recover: R, closure: F) where
    F: FnOnce(T) -> T,
    R: FnOnce() -> T, 

Allows use of a value pointed to by &mut T as though it was owned, as long as a T is made available afterwards.

The closure must return a valid T.

Important

Will replace &mut T with recover if the closure panics, then continues the panic.

Example

struct Foo;
let mut foo = Foo;
take_mut::take_or_recover(&mut foo, || Foo, |foo| {
    // Can now consume the Foo, and provide a new value later
    drop(foo);
    // Do more stuff
    Foo // Return new Foo from closure, which goes back into the &mut Foo
});