futures_rx/stream_ext/
race.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use std::{
    pin::Pin,
    task::{Context, Poll},
};

use futures::{
    stream::{Fuse, FusedStream},
    Stream, StreamExt,
};
use pin_project_lite::pin_project;

enum Winner {
    Left,
    Right,
    Undecided,
}

pin_project! {
    /// Stream for the [`race`](RxStreamExt::race) method.
    #[must_use = "streams do nothing unless polled"]
    pub struct Race<S1: Stream<Item = T>, S2: Stream<Item = T>, T> {
        #[pin]
        left: Fuse<S1>,
        #[pin]
        right: Fuse<S2>,
        winner: Winner,
    }
}

impl<S1: Stream<Item = T>, S2: Stream<Item = T>, T> Race<S1, S2, T> {
    pub(crate) fn new(left: S1, right: S2) -> Self {
        Self {
            left: left.fuse(),
            right: right.fuse(),
            winner: Winner::Undecided,
        }
    }
}

impl<S1: Stream<Item = T>, S2: Stream<Item = T>, T> FusedStream for Race<S1, S2, T>
where
    S1: FusedStream,
    S2: FusedStream,
{
    fn is_terminated(&self) -> bool {
        match &self.winner {
            Winner::Left => self.left.is_terminated(),
            Winner::Right => self.right.is_terminated(),
            Winner::Undecided => self.left.is_terminated() && self.right.is_terminated(),
        }
    }
}

impl<S1: Stream<Item = T>, S2: Stream<Item = T>, T> Stream for Race<S1, S2, T> {
    type Item = T;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.project();

        match &this.winner {
            Winner::Left => this.left.poll_next(cx),
            Winner::Right => this.right.poll_next(cx),
            Winner::Undecided => {
                let left = this.left.poll_next(cx);
                let right = this.right.poll_next(cx);

                if left.is_ready() {
                    *this.winner = Winner::Left;
                    left
                } else if right.is_ready() {
                    *this.winner = Winner::Right;
                    right
                } else {
                    Poll::Pending
                }
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (lower_left, upper_left) = self.left.size_hint();
        let (lower_right, upper_right) = self.right.size_hint();

        match &self.winner {
            Winner::Left => (lower_left, upper_left),
            Winner::Right => (lower_right, upper_right),
            Winner::Undecided => (lower_left.min(lower_right), None),
        }
    }
}

#[cfg(test)]
mod test {
    use std::task::Poll;

    use futures::{executor::block_on, stream, StreamExt};

    use crate::RxExt;

    #[test]
    fn smoke() {
        let mut phase = 0usize;
        let fast_stream = stream::iter(["fast"]);
        let slow_stream = stream::poll_fn(move |_| {
            phase += 1;

            match phase {
                1 => Poll::Pending,
                2 => Poll::Ready(Some("slow")),
                3 => Poll::Ready(None),
                _ => unreachable!(),
            }
        });

        block_on(async {
            let all_events = slow_stream.race(fast_stream).collect::<Vec<_>>().await;

            assert_eq!(all_events, ["fast"]);
        });
    }
}