use crate::task::{AsyncTask, NotResult};
use tokio::sync::oneshot;
pub trait FutureExt<T>: Sized {
fn map<U>(self, f: impl FnOnce(T) -> U + Send + 'static) -> AsyncTask<U>
where
U: Send + 'static + NotResult;
fn on_ok<U>(self, f: impl FnOnce(T) -> U + Send + 'static) -> AsyncTask<U>
where
U: Send + 'static + NotResult;
fn on_error<U>(
self,
f: impl FnOnce(oneshot::error::RecvError) -> U + Send + 'static,
) -> AsyncTask<U>
where
U: Send + 'static + NotResult,
T: Into<U>;
fn on_result<U>(
self,
f: impl FnOnce(Result<T, oneshot::error::RecvError>) -> U + Send + 'static,
) -> AsyncTask<U>
where
U: Send + 'static + NotResult;
fn map_ok<U>(self, f: impl FnOnce(T) -> U + Send + 'static) -> AsyncTask<U>
where
U: Send + 'static + NotResult;
fn tap_ok(self, f: impl FnOnce(&T) + Send + 'static) -> AsyncTask<T>
where
T: NotResult;
fn tap_err(self, f: impl FnOnce(&oneshot::error::RecvError) + Send + 'static) -> AsyncTask<T>
where
T: NotResult;
}
impl<T: Send + 'static + NotResult> FutureExt<T> for AsyncTask<T> {
fn map<U>(self, f: impl FnOnce(T) -> U + Send + 'static) -> AsyncTask<U>
where
U: Send + 'static + NotResult,
{
self.on_ok(f)
}
fn on_ok<U>(self, f: impl FnOnce(T) -> U + Send + 'static) -> AsyncTask<U>
where
U: Send + 'static + NotResult,
{
AsyncTask::from_future(async move {
let value = self.await;
f(value)
})
}
fn on_error<U>(
self,
f: impl FnOnce(oneshot::error::RecvError) -> U + Send + 'static,
) -> AsyncTask<U>
where
U: Send + 'static + NotResult,
T: Into<U>,
{
self.on_result(move |result| match result {
Ok(value) => value.into(),
Err(e) => f(e),
})
}
fn on_result<U>(
self,
f: impl FnOnce(Result<T, oneshot::error::RecvError>) -> U + Send + 'static,
) -> AsyncTask<U>
where
U: Send + 'static + NotResult,
{
AsyncTask::from_future(async move {
let result = self.receiver.await;
f(result)
})
}
fn map_ok<U>(self, f: impl FnOnce(T) -> U + Send + 'static) -> AsyncTask<U>
where
U: Send + 'static + NotResult,
{
self.on_ok(f)
}
fn tap_ok(self, f: impl FnOnce(&T) + Send + 'static) -> AsyncTask<T> {
AsyncTask::from_future(async move {
let value = self.await;
f(&value);
value
})
}
fn tap_err(self, f: impl FnOnce(&oneshot::error::RecvError) + Send + 'static) -> AsyncTask<T> {
AsyncTask::from_future(async move {
match self.receiver.await {
Ok(value) => value,
Err(e) => {
f(&e);
panic!("AsyncTask channel error: {e:?}");
}
}
})
}
}