Skip to main content

tuwunel_core/utils/future/
ready_eq_ext.rs

1//! Future extension for Partial Equality against present value
2
3use futures::FutureExt;
4
5/// Compares a future's output with a borrowed value.
6///
7/// The comparison is attached to the future and runs when it resolves. Both
8/// equality and inequality forms preserve asynchronous composition.
9pub trait ReadyEqExt<T>
10where
11	Self: Future<Output = T> + Send + Sized,
12	T: PartialEq + Send + Sync,
13{
14	/// Tests whether the future's output equals `t`.
15	///
16	/// The comparison borrows the supplied value until the returned future
17	/// completes. The output value is consumed after comparison.
18	fn eq(self, t: &T) -> impl Future<Output = bool> + Send;
19
20	/// Tests whether the future's output differs from `t`.
21	///
22	/// The comparison borrows the supplied value until the returned future
23	/// completes. The output value is consumed after comparison.
24	fn ne(self, t: &T) -> impl Future<Output = bool> + Send;
25}
26
27impl<Fut, T> ReadyEqExt<T> for Fut
28where
29	Fut: Future<Output = T> + Send + Sized,
30	T: PartialEq + Send + Sync,
31{
32	#[inline]
33	fn eq(self, t: &T) -> impl Future<Output = bool> + Send { self.map(move |r| r.eq(t)) }
34
35	#[inline]
36	fn ne(self, t: &T) -> impl Future<Output = bool> + Send { self.map(move |r| r.ne(t)) }
37}