Skip to content

Commit 527a90a

Browse files
handle empty containers on race for vec and array
1 parent ac5203d commit 527a90a

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/future/race/array.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ where
7070
type Future = Race<Fut::IntoFuture, N>;
7171

7272
fn race(self) -> Self::Future {
73+
assert!(N > 0, "race requires at least one future");
7374
Race {
7475
futures: self.map(|fut| fut.into_future()),
7576
indexer: Indexer::new(N),
@@ -83,6 +84,13 @@ mod test {
8384
use super::*;
8485
use core::future;
8586

87+
#[test]
88+
#[should_panic(expected = "race requires at least one future")]
89+
fn empty_array() {
90+
let futs: [future::Ready<()>; 0] = [];
91+
let _ = futs.race();
92+
}
93+
8694
// NOTE: we should probably poll in random order.
8795
#[test]
8896
fn no_fairness() {

src/future/race/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ pub trait Race {
2222
/// other futures are cancelled.
2323
///
2424
/// This function returns a new future which polls all futures concurrently.
25+
///
26+
/// # Panics
27+
///
28+
/// This method will panic if the collection is empty (for `Vec` and array
29+
/// implementations) since there would be no future to race.
2530
fn race(self) -> Self::Future;
2631
}

src/future/race/vec.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ where
7373
type Future = Race<Fut::IntoFuture>;
7474

7575
fn race(self) -> Self::Future {
76+
assert!(!self.is_empty(), "race requires at least one future");
7677
Race {
7778
indexer: Indexer::new(self.len()),
7879
futures: self.into_iter().map(|fut| fut.into_future()).collect(),
@@ -87,6 +88,15 @@ mod test {
8788
use alloc::vec;
8889
use core::future;
8990

91+
#[test]
92+
#[should_panic(expected = "race requires at least one future")]
93+
fn empty_vec() {
94+
futures_lite::future::block_on(async {
95+
let futs: Vec<future::Ready<()>> = vec![];
96+
let _ = futs.race().await;
97+
});
98+
}
99+
90100
// NOTE: we should probably poll in random order.
91101
#[test]
92102
fn no_fairness() {

0 commit comments

Comments
 (0)