1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-08-02 05:08:31 +00:00

additional stream tools

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-09-30 06:46:54 +00:00 committed by strawberry
parent a8d5cf9651
commit 6b80361c31
10 changed files with 242 additions and 46 deletions

View file

@ -0,0 +1,3 @@
mod try_ext_ext;
pub use try_ext_ext::TryExtExt;

View file

@ -0,0 +1,48 @@
//! Extended external extensions to futures::TryFutureExt
use futures::{future::MapOkOrElse, TryFuture, TryFutureExt};
/// This interface is not necessarily complete; feel free to add as-needed.
pub trait TryExtExt<T, E>
where
Self: TryFuture<Ok = T, Error = E> + Send,
{
fn map_ok_or<U, F>(
self, default: U, f: F,
) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> U, impl FnOnce(Self::Error) -> U>
where
F: FnOnce(Self::Ok) -> U,
Self: Send + Sized;
fn ok(
self,
) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> Option<Self::Ok>, impl FnOnce(Self::Error) -> Option<Self::Ok>>
where
Self: Sized;
}
impl<T, E, Fut> TryExtExt<T, E> for Fut
where
Fut: TryFuture<Ok = T, Error = E> + Send,
{
#[inline]
fn map_ok_or<U, F>(
self, default: U, f: F,
) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> U, impl FnOnce(Self::Error) -> U>
where
F: FnOnce(Self::Ok) -> U,
Self: Send + Sized,
{
self.map_ok_or_else(|_| default, f)
}
#[inline]
fn ok(
self,
) -> MapOkOrElse<Self, impl FnOnce(Self::Ok) -> Option<Self::Ok>, impl FnOnce(Self::Error) -> Option<Self::Ok>>
where
Self: Sized,
{
self.map_ok_or(None, Some)
}
}