min32 0.1.2

Minimal boilerplate code for targeting win32 with no-std
Documentation
use core::panic::PanicInfo;
use spin::Mutex;
use windows_sys::Win32::System::Threading::{ExitProcess, GetCurrentProcess, TerminateProcess};

/// Defines a panic hook, which is just a function that takes a [`PanicInfo`].
pub type PanicHook = unsafe fn(&PanicInfo) -> ();

static PANIC_HOOK: Mutex<Option<PanicHook>> = Mutex::new(None);

/// Set or clear the panic hook.
///
/// This function will be called before the process terminates upon panicking.
pub fn set_hook(hook: Option<PanicHook>) {
    *PANIC_HOOK.lock() = hook;
}

#[panic_handler]
unsafe fn on_panic(panic_info: &PanicInfo) -> ! {
    if let Some(hook) = { PANIC_HOOK.lock().clone() } {
        // Safety: Hopefully the caller passed something that isn't bad.
        unsafe { hook(panic_info) };
    }

    // Safety: These functions are generally safe to call.
    unsafe {
        TerminateProcess(GetCurrentProcess(), 197);
        // in case TerminateProcess fails
        ExitProcess(197);
    }
}

#[cfg(not(test))]
#[unsafe(no_mangle)]
fn rust_eh_personality() {}