Skip to main content

catch_user_panic_result_with_cleanup

Function catch_user_panic_result_with_cleanup 

Source
pub fn catch_user_panic_result_with_cleanup<S, R, F, C>(
    site: &str,
    state: S,
    f: F,
    cleanup: C,
) -> Option<R>
where F: FnMut(&mut S) -> R, C: FnMut(&mut S),
Expand description

Run a callback and an explicit library-owned cleanup phase.

state, f, and cleanup are retained in this function’s frame while each closure body is invoked by mutable reference. The cleanup body runs immediately after the callback boundary and before any attempt to destroy the callback closure or state. The callback closure, cleanup closure, and state are then destroyed one at a time under best-effort boundaries.

Returns Some(result) only when the callback body, callback-closure destruction, cleanup body, cleanup-closure destruction, and state destruction all complete normally. If a later phase fails after the callback produced a result, that result is also destroyed under a separate boundary before this function returns None.

This sequencing does not make arbitrary user destruction safe. F, C, S, R, their fields, and body-local values must uphold the standard destructor invariant: one opaque aggregate destruction must not produce multiple panics. A second destructor panic during the same drop glue aborts before catch_unwind can recover.