use std::future::Future;
#[allow(
async_fn_in_trait,
opaque_hidden_inferred_bound,
clippy::wrong_self_convention
)]
pub trait AsyncResult: Send {
type OkValue;
type ErrValue;
async fn ais_ok(&self) -> bool;
async fn ais_ok_and<F>(self, f: impl FnOnce(Self::OkValue) -> F) -> bool
where
F: Future<Output = bool>;
async fn ais_err(&self) -> bool;
async fn ais_err_and<F>(self, f: impl FnOnce(Self::ErrValue) -> F) -> bool
where
F: Future<Output = bool>;
async fn ais_ok_or<F>(self, f: impl FnOnce(Self::ErrValue) -> F) -> bool
where
F: Future<Output = bool>;
async fn ais_err_or<F>(self, f: impl FnOnce(Self::OkValue) -> F) -> bool
where
F: Future<Output = bool>;
async fn amap<U, F>(self, op: impl FnOnce(Self::OkValue) -> F) -> Result<U, Self::ErrValue>
where
F: Future<Output = U>;
async fn amap_or<U, F>(
self,
default: impl Future<Output = U>,
f: impl FnOnce(Self::OkValue) -> F,
) -> U
where
F: Future<Output = U>;
async fn amap_or_else<U, D, F>(
self,
default: impl FnOnce(Self::ErrValue) -> D,
f: impl FnOnce(Self::OkValue) -> F,
) -> U
where
D: Future<Output = U>,
F: Future<Output = U>;
async fn amap_err<F, O>(self, op: impl FnOnce(Self::ErrValue) -> F) -> Result<Self::OkValue, O>
where
F: Future<Output = O>;
async fn ainspect<F>(self, f: impl FnOnce(&Self::OkValue) -> F) -> Self
where
F: Future<Output = ()>;
async fn ainspect_mut<F>(self, f: impl FnOnce(&mut Self::OkValue) -> F) -> Self
where
F: Future<Output = ()>;
async fn ainspect_err<F>(self, f: impl FnOnce(&Self::ErrValue) -> F) -> Self
where
F: Future<Output = ()>;
async fn ainspect_err_mut<F>(self, f: impl FnOnce(&mut Self::ErrValue) -> F) -> Self
where
F: Future<Output = ()>;
async fn aand<U>(
self,
res: impl Future<Output = Result<U, Self::ErrValue>>,
) -> Result<U, Self::ErrValue>;
async fn aand_then<U, F>(
self,
op: impl FnOnce(Self::OkValue) -> F,
) -> Result<U, Self::ErrValue>
where
F: Future<Output = Result<U, Self::ErrValue>>;
async fn aor<O>(
self,
res: impl Future<Output = Result<Self::OkValue, O>>,
) -> Result<Self::OkValue, O>;
async fn aor_else<O, F>(self, op: impl FnOnce(Self::ErrValue) -> F) -> Result<Self::OkValue, O>
where
F: Future<Output = Result<Self::OkValue, O>>;
async fn unwrap_or(self, default: impl Future<Output = Self::OkValue>) -> Self::OkValue;
async fn unwrap_or_else<F>(self, default: impl FnOnce(Self::ErrValue) -> F) -> Self::OkValue
where
F: Future<Output = Self::OkValue>;
}
impl<T, E> AsyncResult for core::result::Result<T, E>
where
T: std::marker::Send,
E: std::marker::Send,
{
type OkValue = T;
type ErrValue = E;
async fn ais_ok(&self) -> bool {
self.is_ok()
}
async fn ais_ok_and<F>(self, f: impl FnOnce(Self::OkValue) -> F) -> bool
where
F: Future<Output = bool>,
{
match self {
Self::Ok(v) => f(v).await,
Self::Err(_) => false,
}
}
async fn ais_ok_or<F>(self, f: impl FnOnce(Self::ErrValue) -> F) -> bool
where
F: Future<Output = bool>,
{
match self {
Self::Ok(_) => true,
Self::Err(e) => f(e).await,
}
}
async fn ais_err(&self) -> bool {
self.is_err()
}
async fn ais_err_and<F>(self, f: impl FnOnce(Self::ErrValue) -> F) -> bool
where
F: Future<Output = bool>,
{
match self {
Self::Ok(_) => false,
Self::Err(e) => f(e).await,
}
}
async fn ais_err_or<F>(self, f: impl FnOnce(Self::OkValue) -> F) -> bool
where
F: Future<Output = bool>,
{
match self {
Self::Ok(v) => f(v).await,
Self::Err(_) => true,
}
}
async fn amap<U, F>(self, op: impl FnOnce(Self::OkValue) -> F) -> Result<U, Self::ErrValue>
where
F: Future<Output = U>,
{
match self {
Self::Ok(v) => Ok(op(v).await),
Self::Err(e) => Err(e),
}
}
async fn amap_or<U, F>(
self,
default: impl Future<Output = U>,
f: impl FnOnce(Self::OkValue) -> F,
) -> U
where
F: Future<Output = U>,
{
match self {
Self::Ok(val) => f(val).await,
Self::Err(_) => default.await,
}
}
async fn amap_or_else<U, D, F>(
self,
default: impl FnOnce(Self::ErrValue) -> D,
f: impl FnOnce(Self::OkValue) -> F,
) -> U
where
D: Future<Output = U>,
F: Future<Output = U>,
{
match self {
Self::Ok(val) => f(val).await,
Self::Err(e) => default(e).await,
}
}
async fn amap_err<F, O>(self, op: impl FnOnce(Self::ErrValue) -> F) -> Result<Self::OkValue, O>
where
F: Future<Output = O>,
{
match self {
Self::Ok(t) => Ok(t),
Self::Err(e) => Err(op(e).await),
}
}
async fn ainspect<F>(self, f: impl FnOnce(&Self::OkValue) -> F) -> Self
where
F: Future<Output = ()>,
{
if let Ok(t) = &self {
f(t).await
}
self
}
async fn ainspect_mut<F>(mut self, f: impl FnOnce(&mut Self::OkValue) -> F) -> Self
where
F: Future<Output = ()>,
{
if let Ok(ref mut t) = self {
f(t).await
}
self
}
async fn ainspect_err<F>(self, f: impl FnOnce(&Self::ErrValue) -> F) -> Self
where
F: Future<Output = ()>,
{
if let Err(t) = &self {
f(t).await
}
self
}
async fn ainspect_err_mut<F>(mut self, f: impl FnOnce(&mut Self::ErrValue) -> F) -> Self
where
F: Future<Output = ()>,
{
if let Err(ref mut t) = self {
f(t).await
}
self
}
async fn aand<U>(
self,
res: impl Future<Output = Result<U, Self::ErrValue>>,
) -> Result<U, Self::ErrValue> {
self.and(res.await)
}
async fn aand_then<U, F>(self, op: impl FnOnce(Self::OkValue) -> F) -> Result<U, Self::ErrValue>
where
F: Future<Output = Result<U, Self::ErrValue>>,
{
match self {
Self::Ok(val) => op(val).await,
Self::Err(err) => Err(err),
}
}
async fn aor<O>(
self,
res: impl Future<Output = Result<Self::OkValue, O>>,
) -> Result<Self::OkValue, O> {
self.or(res.await)
}
async fn aor_else<O, F>(self, op: impl FnOnce(Self::ErrValue) -> F) -> Result<Self::OkValue, O>
where
F: Future<Output = Result<Self::OkValue, O>>,
{
match self {
Self::Ok(val) => Ok(val),
Self::Err(err) => op(err).await,
}
}
async fn unwrap_or(self, default: impl Future<Output = Self::OkValue>) -> Self::OkValue {
self.unwrap_or(default.await)
}
async fn unwrap_or_else<F>(self, default: impl FnOnce(Self::ErrValue) -> F) -> Self::OkValue
where
F: Future<Output = Self::OkValue>,
{
match self {
Self::Ok(val) => val,
Self::Err(err) => default(err).await,
}
}
}