Function panic_control::chain_hook_ignoring_full [] [src]

pub fn chain_hook_ignoring_full<F>(predicate: F) where
    F: Fn(&PanicInfo) -> bool,
    F: Send,
    F: Sync,
    F: 'static, 

Augments the panic hook, filtering out panics with a free-form check.

The current panic hook, either installed with std::panic::set_hook() or the standard library default, gets chained, again using std::panic::set_hook(), behind the boolean predicate closure passed as the parameter, testing the std::panic::PanicInfo structure passed to the panic hook. If the predicate returns true, the chained hook is not called.

Caveats

Every call to this function allocates state data and increases the filtering chain for the process-global panic hook, so it should not be called repeatedly unless necessary. Other code within the program that modifies the panic hook, concurrently to, or after, the call to this function, may cause the hook chain to stop working as intended. This function interoperates in a predictable way only with the other functions and methods of this crate that modify the panic hook, and only when used in strictly serialized order with those functions, unless noted otherwise in those functions' documentation. This function is only intended to be used in tests or initialization code of a program; libraries other than those designed for test purposes should avoid using it.

Examples

This example filters out any non-string panics:

use panic_control::chain_hook_ignoring_full;
use std::sync::{Once, ONCE_INIT};

static HOOK_ONCE: Once = ONCE_INIT;
HOOK_ONCE.call_once(|| {
    chain_hook_ignoring_full(|info| {
        let payload = info.payload();
        !(payload.is::<&'static str>() ||
          payload.is::<String>())
    });
});