qubit_executor/task/task_execution_error.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9use std::{
10 error::Error,
11 fmt,
12};
13
14/// Result type used by managed task handles.
15pub type TaskResult<R, E> = Result<R, TaskExecutionError<E>>;
16
17/// Error observed when retrieving the result of an accepted task.
18///
19/// This error is distinct from [`RejectedExecution`](crate::service::RejectedExecution).
20/// Rejection happens before a service accepts a task; `TaskExecutionError`
21/// describes what happened after the task was accepted.
22///
23/// # Type Parameters
24///
25/// * `E` - The error type returned by the task itself.
26///
27/// # Author
28///
29/// Haixing Hu
30#[derive(Debug)]
31pub enum TaskExecutionError<E> {
32 /// The task ran and returned `Err(E)`.
33 Failed(E),
34
35 /// The task panicked while running.
36 Panicked,
37
38 /// The task was cancelled before producing a result.
39 Cancelled,
40}
41
42impl<E> TaskExecutionError<E> {
43 /// Returns true when this error wraps the task's own error value.
44 ///
45 /// # Returns
46 ///
47 /// `true` if the task returned `Err(E)`.
48 #[inline]
49 pub const fn is_failed(&self) -> bool {
50 matches!(self, Self::Failed(_))
51 }
52
53 /// Returns true when the task panicked.
54 ///
55 /// # Returns
56 ///
57 /// `true` if the task panicked while running.
58 #[inline]
59 pub const fn is_panicked(&self) -> bool {
60 matches!(self, Self::Panicked)
61 }
62
63 /// Returns true when the task was cancelled.
64 ///
65 /// # Returns
66 ///
67 /// `true` if the task was cancelled before producing a result.
68 #[inline]
69 pub const fn is_cancelled(&self) -> bool {
70 matches!(self, Self::Cancelled)
71 }
72}
73
74impl<E> fmt::Display for TaskExecutionError<E>
75where
76 E: fmt::Display,
77{
78 /// Formats this task execution error for users.
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 match self {
81 Self::Failed(err) => write!(f, "task failed: {err}"),
82 Self::Panicked => f.write_str("task panicked"),
83 Self::Cancelled => f.write_str("task was cancelled"),
84 }
85 }
86}
87
88impl<E> Error for TaskExecutionError<E> where E: Error + 'static {}