use dope_sys::{engine, io::reactor::event::fault};
use std::{convert::Infallible, mem};
use crate::{
Errno,
driver::{life, result::poll, schedule::ready},
};
use crate::executor::drive::{clock, regress::Regression};
#[derive(Debug)]
pub enum Error {
Boot(life::Busy),
Capacity(ready::Capacity),
Poll(poll::Error),
Completion(fault::Dispatch<crate::Fault>),
Deadline(clock::Range),
ClockMovedBackwards(Regression),
TimerDisarm(Errno),
Shutdown,
QuiescenceTimeout,
Timeout,
}
impl Error {
pub(crate) const fn timeout(clock::Elapsed: clock::Elapsed) -> Self {
Self::Timeout
}
pub(crate) const fn quiesce(clock::Elapsed: clock::Elapsed) -> Self {
Self::QuiescenceTimeout
}
}
impl From<Regression> for Error {
fn from(error: Regression) -> Self {
Self::ClockMovedBackwards(error)
}
}
impl From<Infallible> for Error {
fn from(error: Infallible) -> Self {
match error {}
}
}
impl From<life::Busy> for Error {
fn from(error: life::Busy) -> Self {
Self::Boot(error)
}
}
impl From<ready::Capacity> for Error {
fn from(error: ready::Capacity) -> Self {
Self::Capacity(error)
}
}
impl From<poll::Error> for Error {
fn from(error: poll::Error) -> Self {
Self::Poll(error)
}
}
impl From<fault::Dispatch<crate::Fault>> for Error {
fn from(failure: fault::Dispatch<crate::Fault>) -> Self {
Self::Completion(failure)
}
}
impl<P> From<engine::Failure<P, crate::Fault>> for Error
where
crate::Engine: engine::raw::Poll<Error = P>,
{
fn from(failure: engine::Failure<P, crate::Fault>) -> Self {
match failure {
engine::Failure::Native(error) => Self::Poll(poll::Error::from_backend(error)),
engine::Failure::Completion(fault) => Self::Completion(fault),
}
}
}
impl From<clock::Range> for Error {
fn from(range: clock::Range) -> Self {
Self::Deadline(range)
}
}
const _: () = {
assert!(!mem::needs_drop::<Error>());
assert!(
mem::size_of::<Error>()
<= mem::size_of::<fault::Dispatch<crate::Fault>>() + mem::align_of::<Error>()
);
};