Function std::finally::try_finallyDeprecated [-] [+] [src]

pub fn try_finally<T, U, R, F, G>(mutate: &mut T, drop: U, try_fn: F, finally_fn: G) -> R where F: FnOnce(&mut T, U), <F as FnOnce(&mut T, U)>::Output == R, G: FnMut(&mut T), <G as FnMut(&mut T)>::Output == ()

The most general form of the finally functions. The function try_fn will be invoked first; whether or not it panics, the function finally_fn will be invoked next. The two parameters mutate and drop are used to thread state through the two closures. mutate is used for any shared, mutable state that both closures require access to; drop is used for any state that the try_fn requires ownership of.

WARNING: While shared, mutable state between the try and finally function is often necessary, one must be very careful; the try function could have panicked at any point, so the values of the shared state may be inconsistent.

Example

fn main() { use std::finally::try_finally; struct State<'a> { buffer: &'a mut [u8], len: usize } let mut buf = []; let mut state = State { buffer: &mut buf, len: 0 }; try_finally( &mut state, (), |state, ()| { // use state.buffer, state.len }, |state| { // use state.buffer, state.len to cleanup }) }
use std::finally::try_finally;

struct State<'a> { buffer: &'a mut [u8], len: usize }
let mut state = State { buffer: &mut buf, len: 0 };
try_finally(
    &mut state, (),
    |state, ()| {
        // use state.buffer, state.len
    },
    |state| {
        // use state.buffer, state.len to cleanup
    })