use super::super::{
get_interface as get_base_interface, get_mut_interface as get_mut_base_interface,
};
use crate::extensions::unwind_internal::{
default_context::DefaultContext, Signal, UnwindInternalAPI, UnwindInternalContextAPI,
UnwindInternalContextRef, UnwindInternalInterface,
};
use std::mem::MaybeUninit;
use std::panic::UnwindSafe;
static mut INTERFACE: MaybeUninit<UnwindInternalInterface<'static>> = MaybeUninit::uninit();
#[inline]
pub fn initialize() {
unsafe {
INTERFACE = MaybeUninit::new(UnwindInternalInterface::from_interface(get_base_interface()))
}
}
#[inline]
pub fn get_interface<'a>() -> &'a UnwindInternalInterface<'static> {
unsafe { &*INTERFACE.as_ptr() }
}
#[inline]
pub fn get_mut_interface<'a>() -> &'a mut UnwindInternalInterface<'static> {
unsafe { &mut *INTERFACE.as_mut_ptr() }
}
#[inline]
pub fn get_context() -> Option<UnwindInternalContextRef> {
get_interface().get_context(get_base_interface())
}
#[inline]
pub fn set_context(context: Option<UnwindInternalContextRef>) {
get_mut_interface().set_context(get_mut_base_interface(), context)
}
#[inline]
pub fn setup_unwind<T>(f: impl FnOnce() -> T + UnwindSafe) -> T {
DefaultContext::default().setup_unwind(
get_mut_interface(),
get_mut_base_interface(),
move |_interface| f(),
)
}
#[inline]
pub fn catch_unwind<T>(f: impl FnOnce() -> T + UnwindSafe) -> Result<T, Signal> {
DefaultContext::default().catch_unwind(
get_mut_interface(),
get_mut_base_interface(),
move |_interface| f(),
)
}