Function std::finally::try_finallyExperimental
[-]
[+]
[src]
pub fn try_finally<T, U, R, F, G>(mutate: &mut T, drop: U, try_fn: F, finally_fn: G) -> R
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: uint } 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: uint } 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 })