From 15326986114a386046a5f672c5907b6f7305b4c3 Mon Sep 17 00:00:00 2001 From: Yosh Date: Fri, 25 Aug 2023 13:16:21 +0200 Subject: [PATCH] handle empty lists for `join` --- src/future/join/array.rs | 11 ++++++++++- src/future/join/vec.rs | 11 ++++++++++- src/future/try_join/array.rs | 11 ++++++++++- src/future/try_join/vec.rs | 11 ++++++++++- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/future/join/array.rs b/src/future/join/array.rs index 4db6447..ca628a4 100644 --- a/src/future/join/array.rs +++ b/src/future/join/array.rs @@ -95,7 +95,7 @@ where let mut readiness = this.wakers.readiness().lock().unwrap(); readiness.set_waker(cx.waker()); - if !readiness.any_ready() { + if *this.pending != 0 && !readiness.any_ready() { // Nothing is ready yet return Poll::Pending; } @@ -193,6 +193,15 @@ mod test { }); } + #[test] + fn empty() { + futures_lite::future::block_on(async { + let data: [future::Ready<()>; 0] = []; + let fut = data.join(); + assert_eq!(fut.await, []); + }); + } + #[test] fn debug() { let mut fut = [future::ready("hello"), future::ready("world")].join(); diff --git a/src/future/join/vec.rs b/src/future/join/vec.rs index e847832..15b67bf 100644 --- a/src/future/join/vec.rs +++ b/src/future/join/vec.rs @@ -87,7 +87,7 @@ where let mut readiness = this.wakers.readiness().lock().unwrap(); readiness.set_waker(cx.waker()); - if !readiness.any_ready() { + if *this.pending != 0 && !readiness.any_ready() { // Nothing is ready yet return Poll::Pending; } @@ -187,6 +187,15 @@ mod test { }); } + #[test] + fn empty() { + futures_lite::future::block_on(async { + let data: Vec> = vec![]; + let fut = data.join(); + assert_eq!(fut.await, vec![]); + }); + } + #[test] fn debug() { let mut fut = vec![future::ready("hello"), future::ready("world")].join(); diff --git a/src/future/try_join/array.rs b/src/future/try_join/array.rs index 5406f74..3aee1e2 100644 --- a/src/future/try_join/array.rs +++ b/src/future/try_join/array.rs @@ -95,7 +95,7 @@ where let mut readiness = this.wakers.readiness().lock().unwrap(); readiness.set_waker(cx.waker()); - if !readiness.any_ready() { + if *this.pending != 0 && !readiness.any_ready() { // Nothing is ready yet return Poll::Pending; } @@ -200,6 +200,15 @@ mod test { }) } + #[test] + fn empty() { + futures_lite::future::block_on(async { + let data: [future::Ready>; 0] = []; + let res = data.try_join().await; + assert_eq!(res.unwrap(), []); + }); + } + #[test] fn one_err() { futures_lite::future::block_on(async { diff --git a/src/future/try_join/vec.rs b/src/future/try_join/vec.rs index 9c2f800..203a7ce 100644 --- a/src/future/try_join/vec.rs +++ b/src/future/try_join/vec.rs @@ -96,7 +96,7 @@ where let mut readiness = this.wakers.readiness().lock().unwrap(); readiness.set_waker(cx.waker()); - if !readiness.any_ready() { + if *this.pending != 0 && !readiness.any_ready() { // Nothing is ready yet return Poll::Pending; } @@ -201,6 +201,15 @@ mod test { }) } + #[test] + fn empty() { + futures_lite::future::block_on(async { + let data: Vec>> = vec![]; + let res = data.try_join().await; + assert_eq!(res.unwrap(), vec![]); + }); + } + #[test] fn one_err() { futures_lite::future::block_on(async {