From cc28a01e49df260da89683e8d720e9cb9766ae2e Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Sat, 21 Nov 2026 21:38:55 +0000 Subject: [PATCH] fix(core): Fix BoolExt::or short-circuiting This would mean that or would return false when one of the inputs was true. Also fix double futures in type signatures --- src/core/utils/future/bool_ext.rs | 94 ++++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 15 deletions(-) diff --git a/src/core/utils/future/bool_ext.rs b/src/core/utils/future/bool_ext.rs index 24f239ff8..16b4b5678 100644 --- a/src/core/utils/future/bool_ext.rs +++ b/src/core/utils/future/bool_ext.rs @@ -1,10 +1,8 @@ //! Extended external extensions to futures::FutureExt -use std::marker::Unpin; - use futures::{ Future, FutureExt, - future::{select_ok, try_join, try_join_all, try_select}, + future::{try_join, try_join_all}, }; pub trait BoolExt @@ -18,8 +16,8 @@ where fn or(self, b: B) -> impl Future + Send where - B: Future + Send + Unpin, - Self: Sized + Unpin; + B: Future + Send, + Self: Sized; } impl BoolExt for Fut @@ -44,20 +42,20 @@ where #[inline] fn or(self, b: B) -> impl Future + Send where - B: Future + Send + Unpin, - Self: Sized + Unpin, + B: Future + Send, + Self: Sized, { type Result = crate::Result<(), ()>; - let a = self.map(|a| a.then_some(()).ok_or(Result::Err(()))); + let a = self.map(|a| (!a).then_some(()).ok_or(Result::Err(()))); - let b = b.map(|b| b.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()) + try_join(a, b).map(|result| result.is_err()) } } -pub async fn and(args: I) -> impl Future + Send +pub fn and(args: I) -> impl Future + Send where I: Iterator + Send, F: Future + Send, @@ -69,14 +67,80 @@ where try_join_all(args).map(|result| result.is_ok()) } -pub async fn or(args: I) -> impl Future + Send +pub fn or(args: I) -> impl Future + Send where I: Iterator + Send, - F: Future + Send + Unpin, + F: Future + Send, { 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).then_some(()).ok_or(Result::Err(())))); - select_ok(args).map(|result| result.is_ok()) + try_join_all(args).map(|result| result.is_err()) +} + +#[cfg(test)] +mod tests { + use std::pin::pin; + + use futures::future; + + use super::{BoolExt, and, or}; + + #[tokio::test] + async fn bool_ext_truth_tables() { + for a in [false, true] { + for b in [false, true] { + assert_eq!(future::ready(a).and(future::ready(b)).await, a && b, "and({a}, {b})"); + assert_eq!(future::ready(a).or(future::ready(b)).await, a || b, "or({a}, {b})"); + } + } + + assert!( + future::ready(false) + .or(future::ready(false)) + .or(future::ready(true)) + .await, + "chained or with trailing true must return true" + ); + assert!( + !future::ready(false) + .or(future::ready(false)) + .or(future::ready(false)) + .await, + "chained or with all false must return false" + ); + } + + #[tokio::test] + async fn bool_ext_short_circuits() { + let pend = pin!(future::pending::()); + assert!( + future::ready(true).or(pend).await, + "or must short-circuit on true without polling pending" + ); + + let pend = pin!(future::pending::()); + assert!( + !future::ready(false).and(pend).await, + "and must short-circuit on false without polling pending" + ); + } + + #[tokio::test] + async fn free_and_or() { + assert!(and(std::iter::empty::>()).await, "empty and is true"); + assert!(!or(std::iter::empty::>()).await, "empty or is false"); + + assert!(and([future::ready(true), future::ready(true)].into_iter()).await, "all true"); + assert!( + !and([future::ready(true), future::ready(false)].into_iter()).await, + "and with false" + ); + assert!( + or([future::ready(false), future::ready(true)].into_iter()).await, + "or with true" + ); + assert!(!or([future::ready(false), future::ready(false)].into_iter()).await, "all false"); + } }