use std::convert::Infallible;
use crate::{
engine::Buffer,
io::{self, Retry},
memory,
ring::map,
};
pub trait Shutdown: Buffer + Sized {
type State: Cleanup;
fn try_shutdown(self, context: Self::Context) -> Result<(), Self::State>;
}
pub trait Cleanup: Retry<Failure: Into<Failure<Self::Error>>> {
type Error;
fn failure(&self) -> Failure<Self::Error>;
}
#[derive(Debug, Eq, PartialEq)]
pub enum Failure<E> {
Backend(map::Storage<E>),
Context(memory::Error),
}
impl<E, B: Into<map::Storage<E>>, S: Into<memory::Error>> From<io::Cleanup<B, S>> for Failure<E> {
fn from(value: io::Cleanup<B, S>) -> Self {
match value {
io::Cleanup::Owner(cause) => Self::Backend(cause.into()),
io::Cleanup::Storage(cause) => Self::Context(cause.into()),
}
}
}
impl Cleanup for Infallible {
type Error = Infallible;
fn failure(&self) -> Failure<Self::Error> {
match *self {}
}
}
impl<E> From<Infallible> for Failure<E> {
fn from(value: Infallible) -> Self {
match value {}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Cause<F, P> {
Completion(F),
Reactor { cause: P, completion: Option<F> },
File(P),
Tuning,
}