Files
continuwuity/src/core/error/panic.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.0 KiB
Rust
Raw Normal View History

2024-07-13 02:15:15 +00:00
use std::{
any::Any,
panic::{RefUnwindSafe, UnwindSafe, panic_any},
2024-07-13 02:15:15 +00:00
};
use super::Error;
use crate::debug;
impl UnwindSafe for Error {}
impl RefUnwindSafe for Error {}
impl Error {
#[inline]
2024-07-13 02:15:15 +00:00
pub fn panic(self) -> ! { panic_any(self.into_panic()) }
#[must_use]
#[inline]
2025-11-25 06:43:18 +00:00
pub fn from_panic(e: Box<dyn Any + Send + 'static>) -> Self {
Self::Panic(debug::panic_str(&e), e.into())
}
2024-07-13 02:15:15 +00:00
#[inline]
2025-11-25 06:43:18 +00:00
pub fn into_panic(self) -> Box<dyn Any + Send> {
2024-07-13 02:15:15 +00:00
match self {
| Self::JoinError(e) => e.into_panic(),
2025-11-25 06:43:18 +00:00
| Self::Panic(_, mut e) | Self::PanicAny(mut e) =>
e.take().expect("Error contained panic"),
| _ => Box::new(self),
2024-07-13 02:15:15 +00:00
}
}
/// Get the panic message string.
#[inline]
2024-07-13 02:15:15 +00:00
pub fn panic_str(self) -> Option<&'static str> {
2025-11-25 06:43:18 +00:00
self.is_panic().then(|| {
let panic = self.into_panic();
debug::panic_str(&panic)
})
2024-07-13 02:15:15 +00:00
}
/// Check if the Error is trafficking a panic object.
#[inline]
pub fn is_panic(&self) -> bool {
match &self {
| Self::JoinError(e) => e.is_panic(),
2025-11-25 06:43:18 +00:00
| Self::Panic(..) | Self::PanicAny(..) => true,
| _ => false,
2024-07-13 02:15:15 +00:00
}
}
}