1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-09-15 17:26:58 +00:00

additional futures extension utils

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-12-31 01:11:58 +00:00
parent a3f9432da8
commit 27328cbc01
6 changed files with 123 additions and 2 deletions

View file

@ -0,0 +1,34 @@
//! Extended external extensions to futures::FutureExt
use std::marker::Unpin;
use futures::{future, future::Select, Future};
/// This interface is not necessarily complete; feel free to add as-needed.
pub trait ExtExt<T>
where
Self: Future<Output = T> + Send,
{
fn until<A, B, F>(self, f: F) -> Select<A, B>
where
Self: Sized,
F: FnOnce() -> B,
A: Future<Output = T> + From<Self> + Send + Unpin,
B: Future<Output = ()> + Send + Unpin;
}
impl<T, Fut> ExtExt<T> for Fut
where
Fut: Future<Output = T> + Send,
{
#[inline]
fn until<A, B, F>(self, f: F) -> Select<A, B>
where
Self: Sized,
F: FnOnce() -> B,
A: Future<Output = T> + From<Self> + Send + Unpin,
B: Future<Output = ()> + Send + Unpin,
{
future::select(self.into(), f())
}
}

View file

@ -1,5 +1,7 @@
mod ext_ext;
mod option_ext;
mod try_ext_ext;
pub use ext_ext::ExtExt;
pub use option_ext::OptionExt;
pub use try_ext_ext::TryExtExt;

View file

@ -4,8 +4,11 @@
// caller only ever caring about result status while discarding all contents.
#![allow(clippy::wrong_self_convention)]
use std::marker::Unpin;
use futures::{
future::{MapOkOrElse, UnwrapOrElse},
future,
future::{MapOkOrElse, TrySelect, UnwrapOrElse},
TryFuture, TryFutureExt,
};
@ -46,6 +49,13 @@ where
where
Self: Sized;
fn try_until<A, B, F>(self, f: F) -> TrySelect<A, B>
where
Self: Sized,
F: FnOnce() -> B,
A: TryFuture<Ok = Self::Ok> + From<Self> + Send + Unpin,
B: TryFuture<Ok = (), Error = Self::Error> + Send + Unpin;
fn unwrap_or(
self,
default: Self::Ok,
@ -110,6 +120,17 @@ where
self.map_ok_or(None, Some)
}
#[inline]
fn try_until<A, B, F>(self, f: F) -> TrySelect<A, B>
where
Self: Sized,
F: FnOnce() -> B,
A: TryFuture<Ok = Self::Ok> + From<Self> + Send + Unpin,
B: TryFuture<Ok = (), Error = Self::Error> + Send + Unpin,
{
future::try_select(self.into(), f())
}
#[inline]
fn unwrap_or(
self,