Skip to main content

tuwunel_core/utils/future/
ext_ext.rs

1//! Extended external extensions to futures::FutureExt
2
3use futures::{future, future::Select};
4
5/// Adds a stop-future race to ordinary futures.
6///
7/// The returned selector resolves when either future completes. Its output
8/// identifies the winner and retains the unfinished future.
9pub trait ExtExt<T>
10where
11	Self: Future<Output = T> + Send,
12{
13	/// Races the receiver against a unit-output stopping future.
14	///
15	/// `f` constructs the stopping future when the selector is created. The
16	/// returned [`Select`] preserves whichever future has not completed.
17	fn until<A, B, F>(self, f: F) -> Select<A, B>
18	where
19		Self: Sized,
20		F: FnOnce() -> B,
21		A: Future<Output = T> + From<Self> + Send + Unpin,
22		B: Future<Output = ()> + Send + Unpin;
23}
24
25impl<T, Fut> ExtExt<T> for Fut
26where
27	Fut: Future<Output = T> + Send,
28{
29	#[inline]
30	fn until<A, B, F>(self, f: F) -> Select<A, B>
31	where
32		Self: Sized,
33		F: FnOnce() -> B,
34		A: Future<Output = T> + From<Self> + Send + Unpin,
35		B: Future<Output = ()> + Send + Unpin,
36	{
37		future::select(self.into(), f())
38	}
39}