use std::future::Future;
use std::ops::DerefMut;
use crate::raw::BoxedFuture;
use crate::raw::MaybeSend;
use crate::raw::OpDelete;
use crate::*;
pub type Deleter = Box<dyn DeleteDyn>;
pub trait Delete: Unpin + Send + Sync {
fn delete<'a>(
&'a mut self,
path: &'a str,
args: OpDelete,
) -> impl Future<Output = Result<()>> + MaybeSend + 'a;
fn close(&mut self) -> impl Future<Output = Result<()>> + MaybeSend;
}
impl Delete for () {
async fn delete(&mut self, _: &str, _: OpDelete) -> Result<()> {
Err(Error::new(
ErrorKind::Unsupported,
"output deleter doesn't support delete",
))
}
async fn close(&mut self) -> Result<()> {
Err(Error::new(
ErrorKind::Unsupported,
"output deleter doesn't support close",
))
}
}
pub trait DeleteDyn: Unpin + Send + Sync {
fn delete_dyn<'a>(&'a mut self, path: &'a str, args: OpDelete) -> BoxedFuture<'a, Result<()>>;
fn close_dyn(&mut self) -> BoxedFuture<'_, Result<()>>;
}
impl<T: Delete + ?Sized> DeleteDyn for T {
fn delete_dyn<'a>(&'a mut self, path: &'a str, args: OpDelete) -> BoxedFuture<'a, Result<()>> {
Box::pin(Delete::delete(self, path, args))
}
fn close_dyn(&mut self) -> BoxedFuture<'_, Result<()>> {
Box::pin(self.close())
}
}
impl<T: DeleteDyn + ?Sized> Delete for Box<T> {
fn delete<'a>(
&'a mut self,
path: &'a str,
args: OpDelete,
) -> impl Future<Output = Result<()>> + MaybeSend + 'a {
self.deref_mut().delete_dyn(path, args)
}
async fn close(&mut self) -> Result<()> {
self.deref_mut().close_dyn().await
}
}