add tail-efficient logic extension
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
d8e94ee965
commit
5428526120
2 changed files with 84 additions and 0 deletions
82
src/core/utils/future/bool_ext.rs
Normal file
82
src/core/utils/future/bool_ext.rs
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
//! Extended external extensions to futures::FutureExt
|
||||||
|
|
||||||
|
use std::marker::Unpin;
|
||||||
|
|
||||||
|
use futures::{
|
||||||
|
future::{select_ok, try_join, try_join_all, try_select},
|
||||||
|
Future, FutureExt,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub trait BoolExt
|
||||||
|
where
|
||||||
|
Self: Future<Output = bool> + Send,
|
||||||
|
{
|
||||||
|
fn and<B>(self, b: B) -> impl Future<Output = bool> + Send
|
||||||
|
where
|
||||||
|
B: Future<Output = bool> + Send,
|
||||||
|
Self: Sized;
|
||||||
|
|
||||||
|
fn or<B>(self, b: B) -> impl Future<Output = bool> + Send
|
||||||
|
where
|
||||||
|
B: Future<Output = bool> + Send + Unpin,
|
||||||
|
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,
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn and<B>(self, b: B) -> impl Future<Output = bool> + Send
|
||||||
|
where
|
||||||
|
B: Future<Output = bool> + Send,
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
type Result = crate::Result<(), ()>;
|
||||||
|
|
||||||
|
let a = self.map(|a| a.then_some(()).ok_or(Result::Err(())));
|
||||||
|
|
||||||
|
let b = b.map(|b| b.then_some(()).ok_or(Result::Err(())));
|
||||||
|
|
||||||
|
try_join(a, b).map(|result| result.is_ok())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn or<B>(self, b: B) -> impl Future<Output = bool> + Send
|
||||||
|
where
|
||||||
|
B: Future<Output = bool> + Send + Unpin,
|
||||||
|
Self: Sized + Unpin,
|
||||||
|
{
|
||||||
|
type Result = crate::Result<(), ()>;
|
||||||
|
|
||||||
|
let a = self.map(|a| a.then_some(()).ok_or(Result::Err(())));
|
||||||
|
|
||||||
|
let b = b.map(|b| b.then_some(()).ok_or(Result::Err(())));
|
||||||
|
|
||||||
|
try_select(a, b).map(|result| result.is_ok())
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
|
mod bool_ext;
|
||||||
mod ext_ext;
|
mod ext_ext;
|
||||||
mod option_ext;
|
mod option_ext;
|
||||||
mod try_ext_ext;
|
mod try_ext_ext;
|
||||||
|
|
||||||
|
pub use bool_ext::{and, or, BoolExt};
|
||||||
pub use ext_ext::ExtExt;
|
pub use ext_ext::ExtExt;
|
||||||
pub use option_ext::OptionExt;
|
pub use option_ext::OptionExt;
|
||||||
pub use try_ext_ext::TryExtExt;
|
pub use try_ext_ext::TryExtExt;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue