mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-12-06 07:12:23 +00:00
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
This commit is contained in:
1 file changed
+79
-15
@@ -1,10 +1,8 @@
|
|||||||
//! Extended external extensions to futures::FutureExt
|
//! Extended external extensions to futures::FutureExt
|
||||||
|
|
||||||
use std::marker::Unpin;
|
|
||||||
|
|
||||||
use futures::{
|
use futures::{
|
||||||
Future, FutureExt,
|
Future, FutureExt,
|
||||||
future::{select_ok, try_join, try_join_all, try_select},
|
future::{try_join, try_join_all},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait BoolExt
|
pub trait BoolExt
|
||||||
@@ -18,8 +16,8 @@ where
|
|||||||
|
|
||||||
fn or<B>(self, b: B) -> impl Future<Output = bool> + Send
|
fn or<B>(self, b: B) -> impl Future<Output = bool> + Send
|
||||||
where
|
where
|
||||||
B: Future<Output = bool> + Send + Unpin,
|
B: Future<Output = bool> + Send,
|
||||||
Self: Sized + Unpin;
|
Self: Sized;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Fut> BoolExt for Fut
|
impl<Fut> BoolExt for Fut
|
||||||
@@ -44,20 +42,20 @@ where
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn or<B>(self, b: B) -> impl Future<Output = bool> + Send
|
fn or<B>(self, b: B) -> impl Future<Output = bool> + Send
|
||||||
where
|
where
|
||||||
B: Future<Output = bool> + Send + Unpin,
|
B: Future<Output = bool> + Send,
|
||||||
Self: Sized + Unpin,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
type Result = crate::Result<(), ()>;
|
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<I, F>(args: I) -> impl Future<Output = bool> + Send
|
pub fn and<I, F>(args: I) -> impl Future<Output = bool> + Send
|
||||||
where
|
where
|
||||||
I: Iterator<Item = F> + Send,
|
I: Iterator<Item = F> + Send,
|
||||||
F: Future<Output = bool> + Send,
|
F: Future<Output = bool> + Send,
|
||||||
@@ -69,14 +67,80 @@ where
|
|||||||
try_join_all(args).map(|result| result.is_ok())
|
try_join_all(args).map(|result| result.is_ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn or<I, F>(args: I) -> impl Future<Output = bool> + Send
|
pub fn or<I, F>(args: I) -> impl Future<Output = bool> + Send
|
||||||
where
|
where
|
||||||
I: Iterator<Item = F> + Send,
|
I: Iterator<Item = F> + Send,
|
||||||
F: Future<Output = bool> + Send + Unpin,
|
F: Future<Output = bool> + Send,
|
||||||
{
|
{
|
||||||
type Result = crate::Result<(), ()>;
|
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::<bool>());
|
||||||
|
assert!(
|
||||||
|
future::ready(true).or(pend).await,
|
||||||
|
"or must short-circuit on true without polling pending"
|
||||||
|
);
|
||||||
|
|
||||||
|
let pend = pin!(future::pending::<bool>());
|
||||||
|
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::<future::Ready<bool>>()).await, "empty and is true");
|
||||||
|
assert!(!or(std::iter::empty::<future::Ready<bool>>()).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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user