Sha256: 7bcaf8ea2d249b5c1acb3c1b769cbe479834630dc71b3ce1114b8b46277f44da

Contents?: true

Size: 1.05 KB

Versions: 37

Compression:

Stored size: 1.05 KB

Contents

use std::panic;
use std::sync::{Arc, Mutex};

pub fn test_panic<Func: FnOnce() + panic::UnwindSafe>(func: Func) -> Option<String> {
    static PANIC_MUTEX: Mutex<()> = Mutex::new(());

    {
        let _guard = PANIC_MUTEX.lock();
        let panic_file: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));

        let prev_hook = panic::take_hook();
        {
            let panic_file = panic_file.clone();
            panic::set_hook(Box::new(move |panic_info| {
                let panic_location = panic_info.location().unwrap();
                panic_file
                    .lock()
                    .unwrap()
                    .clone_from(&Some(panic_location.file().to_string()));
            }));
        }

        let result = panic::catch_unwind(func);
        // Return to the previously set panic hook (maybe default) so that we get nice error
        // messages in the tests.
        panic::set_hook(prev_hook);

        if result.is_err() {
            panic_file.lock().unwrap().clone()
        } else {
            None
        }
    }
}

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/tokio-1.43.0/tests/support/panic.rs
wasmtime-29.0.0 ./ext/cargo-vendor/tokio-1.43.0/tests/support/panic.rs
wasmtime-28.0.0 ./ext/cargo-vendor/tokio-1.43.0/tests/support/panic.rs
wasmtime-27.0.0 ./ext/cargo-vendor/tokio-1.41.1/tests/support/panic.rs
wasmtime-26.0.0 ./ext/cargo-vendor/tokio-1.41.0/tests/support/panic.rs
wasmtime-25.0.2 ./ext/cargo-vendor/tokio-1.40.0/tests/support/panic.rs
wasmtime-25.0.1 ./ext/cargo-vendor/tokio-1.39.3/tests/support/panic.rs
wasmtime-25.0.0 ./ext/cargo-vendor/tokio-1.39.3/tests/support/panic.rs
wasmtime-24.0.0 ./ext/cargo-vendor/tokio-1.39.3/tests/support/panic.rs
wasmtime-23.0.2 ./ext/cargo-vendor/tokio-1.36.0/tests/support/panic.rs
wasmtime-22.0.0 ./ext/cargo-vendor/tokio-1.36.0/tests/support/panic.rs
wasmtime-21.0.1 ./ext/cargo-vendor/tokio-1.36.0/tests/support/panic.rs
wasmtime-20.0.2 ./ext/cargo-vendor/tokio-1.36.0/tests/support/panic.rs
wasmtime-20.0.0 ./ext/cargo-vendor/tokio-1.36.0/tests/support/panic.rs
wasmtime-18.0.3 ./ext/cargo-vendor/tokio-1.36.0/tests/support/panic.rs
wasmtime-17.0.1 ./ext/cargo-vendor/tokio-1.35.1/tests/support/panic.rs
wasmtime-17.0.0 ./ext/cargo-vendor/tokio-1.35.1/tests/support/panic.rs
wasmtime-16.0.0 ./ext/cargo-vendor/tokio-1.35.1/tests/support/panic.rs
wasmtime-15.0.1 ./ext/cargo-vendor/tokio-1.35.1/tests/support/panic.rs
wasmtime-15.0.0 ./ext/cargo-vendor/tokio-1.35.1/tests/support/panic.rs