runestick/
panic.rs

1use crate::PanicReason;
2use std::fmt;
3
4pub trait BoxedPanic: 'static + fmt::Display + fmt::Debug + Send + Sync {}
5impl<T> BoxedPanic for T where T: 'static + fmt::Display + fmt::Debug + Send + Sync {}
6
7/// A descriptibe panic.
8///
9/// This can be used as an error variant in functions that you want to be able
10/// to panic.
11#[derive(Debug)]
12pub struct Panic {
13    inner: Box<dyn BoxedPanic>,
14}
15
16impl Panic {
17    /// A custom panic reason.
18    pub fn custom<D>(message: D) -> Self
19    where
20        D: BoxedPanic,
21    {
22        Self {
23            inner: Box::new(message),
24        }
25    }
26}
27
28impl fmt::Display for Panic {
29    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
30        write!(fmt, "{}", self.inner)
31    }
32}
33
34impl From<PanicReason> for Panic {
35    fn from(value: PanicReason) -> Self {
36        Self {
37            inner: Box::new(value),
38        }
39    }
40}