add ReadyEq future extension

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-04-06 19:09:07 +00:00
parent 7cf61b5b7b
commit 7c9d3f7e07
4 changed files with 52 additions and 25 deletions

View file

@ -22,30 +22,6 @@ where
Self: Sized + Unpin;
}
pub async fn and<I, F>(args: I) -> impl Future<Output = bool> + Send
where
I: Iterator<Item = F> + Send,
F: Future<Output = bool> + Send,
{
type Result = crate::Result<(), ()>;
let args = args.map(|a| a.map(|a| a.then_some(()).ok_or(Result::Err(()))));
try_join_all(args).map(|result| result.is_ok())
}
pub async fn or<I, F>(args: I) -> impl Future<Output = bool> + Send
where
I: Iterator<Item = F> + Send,
F: Future<Output = bool> + Send + Unpin,
{
type Result = crate::Result<(), ()>;
let args = args.map(|a| a.map(|a| a.then_some(()).ok_or(Result::Err(()))));
select_ok(args).map(|result| result.is_ok())
}
impl<Fut> BoolExt for Fut
where
Fut: Future<Output = bool> + Send,
@ -80,3 +56,27 @@ where
try_select(a, b).map(|result| result.is_ok())
}
}
pub async fn and<I, F>(args: I) -> impl Future<Output = bool> + Send
where
I: Iterator<Item = F> + Send,
F: Future<Output = bool> + Send,
{
type Result = crate::Result<(), ()>;
let args = args.map(|a| a.map(|a| a.then_some(()).ok_or(Result::Err(()))));
try_join_all(args).map(|result| result.is_ok())
}
pub async fn or<I, F>(args: I) -> impl Future<Output = bool> + Send
where
I: Iterator<Item = F> + Send,
F: Future<Output = bool> + Send + Unpin,
{
type Result = crate::Result<(), ()>;
let args = args.map(|a| a.map(|a| a.then_some(()).ok_or(Result::Err(()))));
select_ok(args).map(|result| result.is_ok())
}

View file

@ -2,10 +2,12 @@ mod bool_ext;
mod ext_ext;
mod option_ext;
mod option_stream;
mod ready_eq_ext;
mod try_ext_ext;
pub use bool_ext::{BoolExt, and, or};
pub use ext_ext::ExtExt;
pub use option_ext::OptionExt;
pub use option_stream::OptionStream;
pub use ready_eq_ext::ReadyEqExt;
pub use try_ext_ext::TryExtExt;

View file

@ -0,0 +1,25 @@
//! Future extension for Partial Equality against present value
use futures::{Future, FutureExt};
pub trait ReadyEqExt<T>
where
Self: Future<Output = T> + Send + Sized,
T: PartialEq + Send + Sync,
{
fn eq(self, t: &T) -> impl Future<Output = bool> + Send;
fn ne(self, t: &T) -> impl Future<Output = bool> + Send;
}
impl<Fut, T> ReadyEqExt<T> for Fut
where
Fut: Future<Output = T> + Send + Sized,
T: PartialEq + Send + Sync,
{
#[inline]
fn eq(self, t: &T) -> impl Future<Output = bool> + Send { self.map(move |r| r.eq(t)) }
#[inline]
fn ne(self, t: &T) -> impl Future<Output = bool> + Send { self.map(move |r| r.ne(t)) }
}

View file

@ -28,7 +28,7 @@ pub use self::{
bool::BoolExt,
bytes::{increment, u64_from_bytes, u64_from_u8, u64_from_u8x8},
debug::slice_truncated as debug_slice_truncated,
future::TryExtExt as TryFutureExtExt,
future::{BoolExt as FutureBoolExt, OptionStream, TryExtExt as TryFutureExtExt},
hash::sha256::delimited as calculate_hash,
html::Escape as HtmlEscape,
json::{deserialize_from_str, to_canonical_object},