Skip to main content

async_deferred/
error.rs

1//! Types shared by `Deferred` and `StaticDeferred`.
2
3#[cfg(feature = "alloc")]
4use alloc::string::String;
5use core::fmt;
6
7/// The state of a [`Deferred`](crate::Deferred) or [`StaticDeferred`](crate::StaticDeferred) task.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum State {
11    /// No task has been started, or its result was taken.
12    NotStarted,
13    /// The task is running.
14    Pending,
15    /// The task finished and its result is available.
16    Completed,
17    /// The task panicked. See [`Deferred::panic_message`](crate::Deferred::panic_message).
18    ///
19    /// Only reported with the `std` feature; without it, a panic is not caught.
20    TaskPanicked,
21    /// The task finished but its callback panicked. The result is still available.
22    /// See [`Deferred::panic_message`](crate::Deferred::panic_message).
23    ///
24    /// Only reported with the `std` feature; without it, a panic is not caught.
25    CallbackPanicked,
26    /// The runtime dropped the task before it finished, for example on shutdown.
27    Cancelled,
28}
29
30/// Why [`Deferred::join`](crate::Deferred::join) or
31/// [`StaticDeferred::join`](crate::StaticDeferred::join) has no result.
32#[derive(Debug, Clone, PartialEq, Eq)]
33#[non_exhaustive]
34pub enum Error {
35    /// No task has been started, or its result was taken.
36    NotStarted,
37    /// The task panicked. Contains the panic message.
38    #[cfg(feature = "alloc")]
39    Panicked(String),
40    /// The runtime dropped the task before it finished, for example on shutdown. For
41    /// [`StaticDeferred::join`](crate::StaticDeferred::join), also returned when the run
42    /// it waited for was cancelled.
43    Cancelled,
44}
45
46impl fmt::Display for Error {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            Error::NotStarted => f.write_str("no task has been started"),
50            #[cfg(feature = "alloc")]
51            Error::Panicked(msg) => write!(f, "task panicked: {msg}"),
52            Error::Cancelled => f.write_str("task was cancelled"),
53        }
54    }
55}
56
57#[cfg(feature = "std")]
58impl std::error::Error for Error {}
59
60/// Why a runtime could not start a task. Returned by [`Spawner`](crate::Spawner) and
61/// [`LocalSpawner`](crate::LocalSpawner).
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub struct SpawnError {
64    reason: &'static str,
65}
66
67impl SpawnError {
68    /// Creates an error with a short `reason`, such as `"task pool is full"`.
69    pub const fn new(reason: &'static str) -> Self {
70        Self { reason }
71    }
72
73    /// The reason given by the runtime.
74    pub const fn reason(&self) -> &'static str {
75        self.reason
76    }
77}
78
79impl fmt::Display for SpawnError {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        write!(f, "could not spawn the task: {}", self.reason)
82    }
83}
84
85#[cfg(feature = "std")]
86impl std::error::Error for SpawnError {}
87
88/// Why a `begin` method did not start a task.
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90#[non_exhaustive]
91pub enum BeginError {
92    /// A task is running, or its result has not been taken yet.
93    AlreadyStarted,
94    /// The runtime could not start the task.
95    Spawn(SpawnError),
96}
97
98impl From<SpawnError> for BeginError {
99    fn from(err: SpawnError) -> Self {
100        BeginError::Spawn(err)
101    }
102}
103
104impl fmt::Display for BeginError {
105    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106        match self {
107            BeginError::AlreadyStarted => {
108                f.write_str("a task is running or its result has not been taken")
109            }
110            BeginError::Spawn(err) => err.fmt(f),
111        }
112    }
113}
114
115#[cfg(feature = "std")]
116impl std::error::Error for BeginError {}
117
118#[cfg(feature = "alloc")]
119impl BeginError {
120    /// The spawn error from a `begin` on a new `Deferred`, which can't be `AlreadyStarted`.
121    pub(crate) fn into_spawn_error(self) -> SpawnError {
122        match self {
123            BeginError::Spawn(err) => err,
124            BeginError::AlreadyStarted => unreachable!("a new `Deferred` has no task"),
125        }
126    }
127}