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
pub type BoxError = std::boxed::Box<dyn std::error::Error>; pub type Result<T> = core::result::Result<T, BoxError>; #[derive(Debug)] pub struct Error { message: String, source: Option<BoxError>, } impl Error { pub fn new(message: impl Into<String>) -> Self { Self { message: message.into(), source: None, } } pub fn boxed(self) -> BoxError { Box::new(self) } } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.message)?; if let Some(error) = &self.source { write!(f, "\nCaused by: {}", error)?; } Ok(()) } } impl std::error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { self.source .as_ref() .map(|error| error.as_ref() as &(dyn std::error::Error + 'static)) } } unsafe impl Sync for Error {} unsafe impl Send for Error {}