use async_trait::async_trait;
use core::future::Future;
#[async_trait(?Send)]
pub trait FutureResult {
type Ok;
type Err;
async fn then_map<U, F, Fut>(self, f: F) -> Result<U, Self::Err>
where
F: FnOnce(Self::Ok) -> Fut,
Fut: Future<Output = U>;
async fn then_map_err<U, F, Fut>(self, f: F) -> Result<Self::Ok, U>
where
F: FnOnce(Self::Err) -> Fut,
Fut: Future<Output = U>;
}
#[async_trait(?Send)]
impl<T: Sync, E: Sync> FutureResult for Result<T, E> {
type Ok = T;
type Err = E;
async fn then_map<U, F, Fut>(self, f: F) -> Result<U, E>
where
Self: Sized,
F: FnOnce(T) -> Fut,
Fut: Future<Output = U>,
{
match self {
Ok(x) => Ok(f(x).await),
Err(e) => Err(e),
}
}
async fn then_map_err<U, F, Fut>(self, f: F) -> Result<T, U>
where
Self: Sized,
F: FnOnce(E) -> Fut,
Fut: Future<Output = U>,
{
match self {
Ok(x) => Ok(x),
Err(e) => Err(f(e).await),
}
}
}