futures_concurrency/future/race/
vec.rs

1use crate::utils::{self, Indexer};
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: Indexer,
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        Race {
77            indexer: Indexer::new(self.len()),
78            futures: self.into_iter().map(|fut| fut.into_future()).collect(),
79            done: false,
80        }
81    }
82}
83
84#[cfg(test)]
85mod test {
86    use super::*;
87    use alloc::vec;
88    use core::future;
89
90    // NOTE: we should probably poll in random order.
91    #[test]
92    fn no_fairness() {
93        futures_lite::future::block_on(async {
94            let res = vec![future::ready("hello"), future::ready("world")]
95                .race()
96                .await;
97            assert!(matches!(res, "hello" | "world"));
98        });
99    }
100}