Function panic_control::chain_hook_ignoring_if [] [src]

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

Augments the panic hook, filtering out panics with a statically typed closure.

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 a value of a particular type. If the panic payload is found to be of the same type and 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

The value types most commonly used in panics are &'string str or String, depending on whether the panic! macro was used in the single parameter form or the formatting form, respectively. The example below filters out either kind if the string message contains a particular substring.

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

const MAGIC: &str = "Move along, nothing to see here";

static HOOK_ONCE: Once = ONCE_INIT;
HOOK_ONCE.call_once(|| {
    chain_hook_ignoring_if(|s: &&'static str| {
        s.contains(MAGIC)
    });
    chain_hook_ignoring_if(|s: &String| {
        s.contains(MAGIC)
    });
});