File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ( ) {
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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 ( ) {
You can’t perform that action at this time.
0 commit comments