diff --git a/src/core/utils/future/bool_ext.rs b/src/core/utils/future/bool_ext.rs index 899806d2..c24a4a16 100644 --- a/src/core/utils/future/bool_ext.rs +++ b/src/core/utils/future/bool_ext.rs @@ -6,57 +6,89 @@ use futures::{ Future, FutureExt, future::{ Either::{Left, Right}, - select_ok, try_join, try_join_all, + select_ok, try_join, try_join_all, try_join3, try_join4, }, }; +use crate::utils::BoolExt as _; + pub trait BoolExt where Self: Future + Send, { - fn and(self, b: B) -> impl Future + Send - where - B: Future + Send, - Self: Sized; + type Result; fn or(self, b: B) -> impl Future + Send where B: Future + Send + Unpin, Self: Sized + Unpin; + + fn and(self, b: B) -> impl Future + Send + where + B: Future + Send, + Self: Sized; + + fn and2(self, b: B, c: C) -> impl Future + Send + where + B: Future + Send, + C: Future + Send, + Self: Sized; + + fn and3(self, b: B, c: C, d: D) -> impl Future + Send + where + B: Future + Send, + C: Future + Send, + D: Future + Send, + Self: Sized; } impl BoolExt for Fut where Fut: Future + Send, { - #[inline] - fn and(self, b: B) -> impl Future + Send - where - B: Future + Send, - Self: Sized, - { - type Result = crate::Result<(), ()>; + 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(self, b: B) -> impl Future + Send where B: Future + Send + Unpin, Self: Sized + Unpin, { - type Result = crate::Result<(), ()>; + let test = |test: bool| test.ok_or(Self::Result::Err(())); - let a = self.map(|a| a.then_some(()).ok_or(Result::Err(()))); + select_ok([Left(self.map(test)), Right(b.map(test))]).map(|res| res.is_ok()) + } - let b = b.map(|b| b.then_some(()).ok_or(Result::Err(()))); + fn and(self, b: B) -> impl Future + Send + where + B: Future + Send, + Self: Sized, + { + let test = |test: bool| test.ok_or(Self::Result::Err(())); - select_ok([Left(a), Right(b)]).map(|result| result.is_ok()) + try_join(self.map(test), b.map(test)).map(|res| res.is_ok()) + } + + fn and2(self, b: B, c: C) -> impl Future + Send + where + B: Future + Send, + C: Future + Send, + Self: Sized, + { + let test = |test: bool| test.ok_or(Self::Result::Err(())); + + try_join3(self.map(test), b.map(test), c.map(test)).map(|res| res.is_ok()) + } + + fn and3(self, b: B, c: C, d: D) -> impl Future + Send + where + B: Future + Send, + C: Future + Send, + D: Future + Send, + Self: Sized, + { + let test = |test: bool| test.ok_or(Self::Result::Err(())); + + try_join4(self.map(test), b.map(test), c.map(test), d.map(test)).map(|res| res.is_ok()) } } @@ -67,9 +99,9 @@ where { type Result = crate::Result<(), ()>; - let args = args.map(|a| a.map(|a| a.then_some(()).ok_or(Result::Err(())))); + let args = args.map(|a| a.map(|a| a.ok_or(Result::Err(())))); - try_join_all(args).map(|result| result.is_ok()) + try_join_all(args).map(|res| res.is_ok()) } pub fn or(args: I) -> impl Future + Send @@ -79,7 +111,7 @@ where { type Result = crate::Result<(), ()>; - let args = args.map(|a| a.map(|a| a.then_some(()).ok_or(Result::Err(())))); + let args = args.map(|a| a.map(|a| a.ok_or(Result::Err(())))); - select_ok(args).map(|result| result.is_ok()) + select_ok(args).map(|res| res.is_ok()) }