use std::{
error::Error,
fmt,
};
pub type TaskResult<R, E> = Result<R, TaskExecutionError<E>>;
#[derive(Debug)]
pub enum TaskExecutionError<E> {
Failed(E),
Panicked,
Cancelled,
}
impl<E> TaskExecutionError<E> {
#[inline]
pub const fn is_failed(&self) -> bool {
matches!(self, Self::Failed(_))
}
#[inline]
pub const fn is_panicked(&self) -> bool {
matches!(self, Self::Panicked)
}
#[inline]
pub const fn is_cancelled(&self) -> bool {
matches!(self, Self::Cancelled)
}
}
impl<E> fmt::Display for TaskExecutionError<E>
where
E: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Failed(err) => write!(f, "task failed: {err}"),
Self::Panicked => f.write_str("task panicked"),
Self::Cancelled => f.write_str("task was cancelled"),
}
}
}
impl<E> Error for TaskExecutionError<E> where E: Error + 'static {}