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
use std::{any::Any, error, result};
use tokio::task::JoinError;
/// The global any `Result` alias of the library.
///
/// The difference with [`Result`] is that it takes a dynamic error
/// `Box<dyn AnyError>`.
pub type AnyResult<T> = result::Result<T, AnyBoxedError>;
/// The global, dowcastable any `Error` trait of the library.
///
/// This trait is used instead of [`Error`] when an error that is not
/// known at compilation time cannot be placed in a generic due to
/// object-safe trait constraint. The main use case is for backend
/// features.
pub trait AnyError: error::Error + Any + Send + Sync {
fn as_any(&self) -> &dyn Any;
}
impl AnyError for JoinError {
fn as_any(&self) -> &dyn Any {
self
}
}
/// The global any boxed `Error` alias of the module.
pub type AnyBoxedError = Box<dyn AnyError + 'static>;
impl error::Error for AnyBoxedError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
self.as_ref().source()
}
}
impl From<JoinError> for AnyBoxedError {
fn from(err: JoinError) -> Self {
Box::new(err)
}
}