1#[cfg(feature = "alloc")]
4use alloc::string::String;
5use core::fmt;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum State {
11 NotStarted,
13 Pending,
15 Completed,
17 TaskPanicked,
21 CallbackPanicked,
26 Cancelled,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq)]
33#[non_exhaustive]
34pub enum Error {
35 NotStarted,
37 #[cfg(feature = "alloc")]
39 Panicked(String),
40 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub struct SpawnError {
64 reason: &'static str,
65}
66
67impl SpawnError {
68 pub const fn new(reason: &'static str) -> Self {
70 Self { reason }
71 }
72
73 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90#[non_exhaustive]
91pub enum BeginError {
92 AlreadyStarted,
94 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 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}