Skip to main content

tuwunel_core/error/
panic.rs

1use std::{
2	any::Any,
3	panic::{RefUnwindSafe, UnwindSafe, panic_any},
4};
5
6use super::Error;
7use crate::debug;
8
9impl UnwindSafe for Error {}
10impl RefUnwindSafe for Error {}
11
12impl Error {
13	#[inline]
14	pub fn panic(self) -> ! { panic_any(self.into_panic()) }
15
16	#[must_use]
17	#[inline]
18	pub fn from_panic(e: Box<dyn Any + Send + 'static>) -> Self {
19		Self::Panic(debug::panic_str(&e), e.into())
20	}
21
22	#[inline]
23	pub fn into_panic(self) -> Box<dyn Any + Send> {
24		match self {
25			| Self::JoinError(e) => e.into_panic(),
26			| Self::Panic(_, e) | Self::PanicAny(e) =>
27				e.into_inner().expect("Error contained panic"),
28			| _ => Box::new(self),
29		}
30	}
31
32	/// Get the panic message string.
33	#[inline]
34	pub fn panic_str(self) -> Option<&'static str> {
35		self.is_panic().then(|| {
36			let panic = self.into_panic();
37			debug::panic_str(&panic)
38		})
39	}
40
41	/// Check if the Error is trafficking a panic object.
42	#[inline]
43	pub fn is_panic(&self) -> bool {
44		match &self {
45			| Self::JoinError(e) => e.is_panic(),
46			| Self::Panic(..) | Self::PanicAny(..) => true,
47			| _ => false,
48		}
49	}
50}