Skip to content

Commit

Permalink
Merge pull request #154 from yoshuawuyts/empty-lists
Browse files Browse the repository at this point in the history
handle empty lists for `{try,}join`
  • Loading branch information
yoshuawuyts authored Aug 25, 2023
2 parents 524a7ce + 1532698 commit a43c735
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/future/join/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 10 additions & 1 deletion src/future/join/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -187,6 +187,15 @@ mod test {
});
}

#[test]
fn empty() {
futures_lite::future::block_on(async {
let data: Vec<future::Ready<()>> = vec![];
let fut = data.join();
assert_eq!(fut.await, vec![]);
});
}

#[test]
fn debug() {
let mut fut = vec![future::ready("hello"), future::ready("world")].join();
Expand Down
11 changes: 10 additions & 1 deletion src/future/try_join/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -200,6 +200,15 @@ mod test {
})
}

#[test]
fn empty() {
futures_lite::future::block_on(async {
let data: [future::Ready<io::Result<()>>; 0] = [];
let res = data.try_join().await;
assert_eq!(res.unwrap(), []);
});
}

#[test]
fn one_err() {
futures_lite::future::block_on(async {
Expand Down
11 changes: 10 additions & 1 deletion src/future/try_join/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -201,6 +201,15 @@ mod test {
})
}

#[test]
fn empty() {
futures_lite::future::block_on(async {
let data: Vec<future::Ready<io::Result<()>>> = vec![];
let res = data.try_join().await;
assert_eq!(res.unwrap(), vec![]);
});
}

#[test]
fn one_err() {
futures_lite::future::block_on(async {
Expand Down

0 comments on commit a43c735

Please sign in to comment.