email/
error.rs

1use std::{any::Any, error, result};
2
3use tokio::task::JoinError;
4
5/// The global any `Result` alias of the library.
6///
7/// The difference with [`Result`] is that it takes a dynamic error
8/// `Box<dyn AnyError>`.
9pub type AnyResult<T> = result::Result<T, AnyBoxedError>;
10
11/// The global, dowcastable any `Error` trait of the library.
12///
13/// This trait is used instead of [`Error`] when an error that is not
14/// known at compilation time cannot be placed in a generic due to
15/// object-safe trait constraint. The main use case is for backend
16/// features.
17pub trait AnyError: error::Error + Any + Send + Sync {
18    fn as_any(&self) -> &dyn Any;
19}
20
21impl AnyError for JoinError {
22    fn as_any(&self) -> &dyn Any {
23        self
24    }
25}
26
27/// The global any boxed `Error` alias of the module.
28pub type AnyBoxedError = Box<dyn AnyError + Send + 'static>;
29
30impl error::Error for AnyBoxedError {
31    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
32        self.as_ref().source()
33    }
34}
35
36impl From<JoinError> for AnyBoxedError {
37    fn from(err: JoinError) -> Self {
38        Box::new(err)
39    }
40}