#[cfg(feature = "alloc")]
use alloc::string::String;
use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum State {
NotStarted,
Pending,
Completed,
TaskPanicked,
CallbackPanicked,
Cancelled,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
NotStarted,
#[cfg(feature = "alloc")]
Panicked(String),
Cancelled,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::NotStarted => f.write_str("no task has been started"),
#[cfg(feature = "alloc")]
Error::Panicked(msg) => write!(f, "task panicked: {msg}"),
Error::Cancelled => f.write_str("task was cancelled"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SpawnError {
reason: &'static str,
}
impl SpawnError {
pub const fn new(reason: &'static str) -> Self {
Self { reason }
}
pub const fn reason(&self) -> &'static str {
self.reason
}
}
impl fmt::Display for SpawnError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "could not spawn the task: {}", self.reason)
}
}
#[cfg(feature = "std")]
impl std::error::Error for SpawnError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BeginError {
AlreadyStarted,
Spawn(SpawnError),
}
impl From<SpawnError> for BeginError {
fn from(err: SpawnError) -> Self {
BeginError::Spawn(err)
}
}
impl fmt::Display for BeginError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BeginError::AlreadyStarted => {
f.write_str("a task is running or its result has not been taken")
}
BeginError::Spawn(err) => err.fmt(f),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for BeginError {}
#[cfg(feature = "alloc")]
impl BeginError {
pub(crate) fn into_spawn_error(self) -> SpawnError {
match self {
BeginError::Spawn(err) => err,
BeginError::AlreadyStarted => unreachable!("a new `Deferred` has no task"),
}
}
}