#![allow(clippy::module_name_repetitions)]
use std::process::abort;
use crate::{Disposable, Dispose};
#[derive(Debug)]
pub struct AbortCanary(());
impl AbortCanary {
#[must_use = "Dropping this value immediately will abort the process."]
pub fn new() -> Disposable<Self> { Disposable::new(Self(())) }
pub fn release(canary: Disposable<Self>) { unsafe { Disposable::leak(canary) }; }
}
impl Dispose for AbortCanary {
fn dispose(self) { abort(); }
}
pub fn abort_on_panic<T>(f: impl FnOnce() -> T) -> T {
let canary = AbortCanary::new();
let ret = f();
AbortCanary::release(canary);
ret
}
#[cfg(test)]
mod test {
use std::panic::catch_unwind;
use super::*;
#[test]
fn safe_canary() {
let canary = AbortCanary::new();
AbortCanary::release(canary);
}
#[test]
fn safe_aop() { abort_on_panic(|| ()); }
#[test]
fn sanity_check_aop() { catch_unwind(|| panic!()); }
}