1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::error::Error;
use std::fmt::Debug;

#[derive(Debug)]
pub struct RuntimeError(pub Box<dyn Debug + Send>);

impl RuntimeError {
    #[inline]
    pub fn expect<T>(message: &'static str) -> Result<T, Self> {
        Err(Self(Box::new(message)))
    }

    #[inline]
    pub fn expect_os<T>(message: std::ffi::OsString) -> Result<T, Self> {
        Err(Self(Box::new(message)))
    }

    #[inline]
    pub fn message<T>(message: String) -> Result<T, Self> {
        Err(Self(Box::new(message)))
    }

    #[inline]
    pub fn unexpected<T>() -> Result<T, Self> {
        Self::expect("Unexpected error")
    }

    #[inline]
    pub fn unreachable<T>() -> Result<T, Self> {
        Self::expect("Unreachable code")
    }

    #[inline]
    pub fn unimplemented<T>() -> Result<T, Self> {
        Self::expect("Unimplemented yet")
    }
}

impl<E> From<E> for RuntimeError
where
    E: Error + Send + 'static,
{
    #[inline]
    fn from(error: E) -> Self {
        Self(Box::new(error))
    }
}