futures_concurrency/future/race/
vec.rs

1use crate::utils::{self, DynIndexer};
2
3use super::Race as RaceTrait;
4
5#[cfg(all(feature = "alloc", not(feature = "std")))]
6use alloc::vec::Vec;
7
8use core::fmt;
9use core::future::{Future, IntoFuture};
10use core::pin::Pin;
11use core::task::{Context, Poll};
12
13use pin_project::pin_project;
14
15/// A future which waits for the first future to complete.
16///
17/// This `struct` is created by the [`race`] method on the [`Race`] trait. See
18/// its documentation for more.
19///
20/// [`race`]: crate::future::Race::race
21/// [`Race`]: crate::future::Race
22#[must_use = "futures do nothing unless you `.await` or poll them"]
23#[pin_project]
24pub struct Race<Fut>
25where
26    Fut: Future,
27{
28    #[pin]
29    futures: Vec<Fut>,
30    indexer: DynIndexer,
31    done: bool,
32}
33
34impl<Fut> fmt::Debug for Race<Fut>
35where
36    Fut: Future + fmt::Debug,
37    Fut::Output: fmt::Debug,
38{
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        f.debug_list().entries(self.futures.iter()).finish()
41    }
42}
43
44impl<Fut> Future for Race<Fut>
45where
46    Fut: Future,
47{
48    type Output = Fut::Output;
49
50    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
51        let mut this = self.project();
52        assert!(!*this.done, "Futures must not be polled after completing");
53
54        for index in this.indexer.iter() {
55            let fut = utils::get_pin_mut_from_vec(this.futures.as_mut(), index).unwrap();
56            match fut.poll(cx) {
57                Poll::Ready(item) => {
58                    *this.done = true;
59                    return Poll::Ready(item);
60                }
61                Poll::Pending => continue,
62            }
63        }
64        Poll::Pending
65    }
66}
67
68impl<Fut> RaceTrait for Vec<Fut>
69where
70    Fut: IntoFuture,
71{
72    type Output = Fut::Output;
73    type Future = Race<Fut::IntoFuture>;
74
75    fn race(self) -> Self::Future {
76        assert!(!self.is_empty(), "race requires at least one future");
77        Race {
78            indexer: DynIndexer::new(self.len()),
79            futures: self.into_iter().map(|fut| fut.into_future()).collect(),
80            done: false,
81        }
82    }
83}
84
85#[cfg(test)]
86mod test {
87    use super::*;
88    use alloc::vec;
89    use core::future;
90
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
100    // NOTE: we should probably poll in random order.
101    #[test]
102    fn no_fairness() {
103        futures_lite::future::block_on(async {
104            let res = vec![future::ready("hello"), future::ready("world")]
105                .race()
106                .await;
107            assert!(matches!(res, "hello" | "world"));
108        });
109    }
110}