Skip to main content

guard

Function guard 

Source
pub fn guard<T, E, F, P>(f: F, on_panic: P) -> Result<T, E>
where F: FnOnce() -> Result<T, E>, P: FnOnce() -> E,
Expand description

Run f, converting an unwinding panic into on_panic().

§Errors

Returns whatever f returns on the ordinary path. If f panics and the panic unwinds, returns on_panic() instead — so a caller can distinguish “this file was refused” from “the parser fell over”, which are different facts about the same input.

AssertUnwindSafe is used because the closure operates on values owned by the caller and nothing observable is shared across the boundary: on the panic path the partially-built value is dropped and an error is returned, so no caller can observe a half-updated state.