|
| 1 | +use super::assert_stream; |
| 2 | +use crate::stream::{Fuse, StreamExt}; |
| 3 | +use core::{fmt, pin::Pin}; |
| 4 | +use futures_core::stream::{FusedStream, Stream}; |
| 5 | +use futures_core::task::{Context, Poll}; |
| 6 | +use pin_project_lite::pin_project; |
| 7 | + |
| 8 | +/// Type to tell [`SelectWithStrategy`] which stream to poll next. |
| 9 | +#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)] |
| 10 | +pub enum PollNext { |
| 11 | + /// Poll the first stream. |
| 12 | + Left, |
| 13 | + /// Poll the second stream. |
| 14 | + Right, |
| 15 | +} |
| 16 | + |
| 17 | +impl PollNext { |
| 18 | + /// Toggle the value and return the old one. |
| 19 | + pub fn toggle(&mut self) -> Self { |
| 20 | + let old = *self; |
| 21 | + |
| 22 | + match self { |
| 23 | + PollNext::Left => *self = PollNext::Right, |
| 24 | + PollNext::Right => *self = PollNext::Left, |
| 25 | + } |
| 26 | + |
| 27 | + old |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Default for PollNext { |
| 32 | + fn default() -> Self { |
| 33 | + PollNext::Left |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +pin_project! { |
| 38 | + /// Stream for the [`select_with_strategy()`] function. See function docs for details. |
| 39 | + #[must_use = "streams do nothing unless polled"] |
| 40 | + pub struct SelectWithStrategy<St1, St2, Clos, State> { |
| 41 | + #[pin] |
| 42 | + stream1: Fuse<St1>, |
| 43 | + #[pin] |
| 44 | + stream2: Fuse<St2>, |
| 45 | + state: State, |
| 46 | + clos: Clos, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// This function will attempt to pull items from both streams. You provide a |
| 51 | +/// closure to tell [`SelectWithStrategy`] which stream to poll. The closure can |
| 52 | +/// store state on `SelectWithStrategy` to which it will receive a `&mut` on every |
| 53 | +/// invocation. This allows basing the strategy on prior choices. |
| 54 | +/// |
| 55 | +/// After one of the two input streams completes, the remaining one will be |
| 56 | +/// polled exclusively. The returned stream completes when both input |
| 57 | +/// streams have completed. |
| 58 | +/// |
| 59 | +/// Note that this function consumes both streams and returns a wrapped |
| 60 | +/// version of them. |
| 61 | +/// |
| 62 | +/// ## Examples |
| 63 | +/// |
| 64 | +/// ### Priority |
| 65 | +/// This example shows how to always prioritize the left stream. |
| 66 | +/// |
| 67 | +/// ```rust |
| 68 | +/// # futures::executor::block_on(async { |
| 69 | +/// use futures::stream::{ repeat, select_with_strategy, PollNext, StreamExt }; |
| 70 | +/// |
| 71 | +/// let left = repeat(1); |
| 72 | +/// let right = repeat(2); |
| 73 | +/// |
| 74 | +/// // We don't need any state, so let's make it an empty tuple. |
| 75 | +/// // We must provide some type here, as there is no way for the compiler |
| 76 | +/// // to infer it. As we don't need to capture variables, we can just |
| 77 | +/// // use a function pointer instead of a closure. |
| 78 | +/// fn prio_left(_: &mut ()) -> PollNext { PollNext::Left } |
| 79 | +/// |
| 80 | +/// let mut out = select_with_strategy(left, right, prio_left); |
| 81 | +/// |
| 82 | +/// for _ in 0..100 { |
| 83 | +/// // Whenever we poll out, we will alwas get `1`. |
| 84 | +/// assert_eq!(1, out.select_next_some().await); |
| 85 | +/// } |
| 86 | +/// # }); |
| 87 | +/// ``` |
| 88 | +/// |
| 89 | +/// ### Round Robin |
| 90 | +/// This example shows how to select from both streams round robin. |
| 91 | +/// Note: this special case is provided by [`futures-util::stream::select`]. |
| 92 | +/// |
| 93 | +/// ```rust |
| 94 | +/// # futures::executor::block_on(async { |
| 95 | +/// use futures::stream::{ repeat, select_with_strategy, PollNext, StreamExt }; |
| 96 | +/// |
| 97 | +/// let left = repeat(1); |
| 98 | +/// let right = repeat(2); |
| 99 | +/// |
| 100 | +/// // We don't need any state, so let's make it an empty tuple. |
| 101 | +/// let rrobin = |last: &mut PollNext| last.toggle(); |
| 102 | +/// |
| 103 | +/// let mut out = select_with_strategy(left, right, rrobin); |
| 104 | +/// |
| 105 | +/// for _ in 0..100 { |
| 106 | +/// // We should be alternating now. |
| 107 | +/// assert_eq!(1, out.select_next_some().await); |
| 108 | +/// assert_eq!(2, out.select_next_some().await); |
| 109 | +/// } |
| 110 | +/// # }); |
| 111 | +/// ``` |
| 112 | +pub fn select_with_strategy<St1, St2, Clos, State>( |
| 113 | + stream1: St1, |
| 114 | + stream2: St2, |
| 115 | + which: Clos, |
| 116 | +) -> SelectWithStrategy<St1, St2, Clos, State> |
| 117 | +where |
| 118 | + St1: Stream, |
| 119 | + St2: Stream<Item = St1::Item>, |
| 120 | + Clos: FnMut(&mut State) -> PollNext, |
| 121 | + State: Default, |
| 122 | +{ |
| 123 | + assert_stream::<St1::Item, _>(SelectWithStrategy { |
| 124 | + stream1: stream1.fuse(), |
| 125 | + stream2: stream2.fuse(), |
| 126 | + state: Default::default(), |
| 127 | + clos: which, |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | +impl<St1, St2, Clos, State> SelectWithStrategy<St1, St2, Clos, State> { |
| 132 | + /// Acquires a reference to the underlying streams that this combinator is |
| 133 | + /// pulling from. |
| 134 | + pub fn get_ref(&self) -> (&St1, &St2) { |
| 135 | + (self.stream1.get_ref(), self.stream2.get_ref()) |
| 136 | + } |
| 137 | + |
| 138 | + /// Acquires a mutable reference to the underlying streams that this |
| 139 | + /// combinator is pulling from. |
| 140 | + /// |
| 141 | + /// Note that care must be taken to avoid tampering with the state of the |
| 142 | + /// stream which may otherwise confuse this combinator. |
| 143 | + pub fn get_mut(&mut self) -> (&mut St1, &mut St2) { |
| 144 | + (self.stream1.get_mut(), self.stream2.get_mut()) |
| 145 | + } |
| 146 | + |
| 147 | + /// Acquires a pinned mutable reference to the underlying streams that this |
| 148 | + /// combinator is pulling from. |
| 149 | + /// |
| 150 | + /// Note that care must be taken to avoid tampering with the state of the |
| 151 | + /// stream which may otherwise confuse this combinator. |
| 152 | + pub fn get_pin_mut(self: Pin<&mut Self>) -> (Pin<&mut St1>, Pin<&mut St2>) { |
| 153 | + let this = self.project(); |
| 154 | + (this.stream1.get_pin_mut(), this.stream2.get_pin_mut()) |
| 155 | + } |
| 156 | + |
| 157 | + /// Consumes this combinator, returning the underlying streams. |
| 158 | + /// |
| 159 | + /// Note that this may discard intermediate state of this combinator, so |
| 160 | + /// care should be taken to avoid losing resources when this is called. |
| 161 | + pub fn into_inner(self) -> (St1, St2) { |
| 162 | + (self.stream1.into_inner(), self.stream2.into_inner()) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl<St1, St2, Clos, State> FusedStream for SelectWithStrategy<St1, St2, Clos, State> |
| 167 | +where |
| 168 | + St1: Stream, |
| 169 | + St2: Stream<Item = St1::Item>, |
| 170 | + Clos: FnMut(&mut State) -> PollNext, |
| 171 | +{ |
| 172 | + fn is_terminated(&self) -> bool { |
| 173 | + self.stream1.is_terminated() && self.stream2.is_terminated() |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +impl<St1, St2, Clos, State> Stream for SelectWithStrategy<St1, St2, Clos, State> |
| 178 | +where |
| 179 | + St1: Stream, |
| 180 | + St2: Stream<Item = St1::Item>, |
| 181 | + Clos: FnMut(&mut State) -> PollNext, |
| 182 | +{ |
| 183 | + type Item = St1::Item; |
| 184 | + |
| 185 | + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<St1::Item>> { |
| 186 | + let this = self.project(); |
| 187 | + |
| 188 | + match (this.clos)(this.state) { |
| 189 | + PollNext::Left => poll_inner(this.stream1, this.stream2, cx), |
| 190 | + PollNext::Right => poll_inner(this.stream2, this.stream1, cx), |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +fn poll_inner<St1, St2>( |
| 196 | + a: Pin<&mut St1>, |
| 197 | + b: Pin<&mut St2>, |
| 198 | + cx: &mut Context<'_>, |
| 199 | +) -> Poll<Option<St1::Item>> |
| 200 | +where |
| 201 | + St1: Stream, |
| 202 | + St2: Stream<Item = St1::Item>, |
| 203 | +{ |
| 204 | + let a_done = match a.poll_next(cx) { |
| 205 | + Poll::Ready(Some(item)) => return Poll::Ready(Some(item)), |
| 206 | + Poll::Ready(None) => true, |
| 207 | + Poll::Pending => false, |
| 208 | + }; |
| 209 | + |
| 210 | + match b.poll_next(cx) { |
| 211 | + Poll::Ready(Some(item)) => Poll::Ready(Some(item)), |
| 212 | + Poll::Ready(None) if a_done => Poll::Ready(None), |
| 213 | + Poll::Ready(None) | Poll::Pending => Poll::Pending, |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +impl<St1, St2, Clos, State> fmt::Debug for SelectWithStrategy<St1, St2, Clos, State> |
| 218 | +where |
| 219 | + St1: fmt::Debug, |
| 220 | + St2: fmt::Debug, |
| 221 | + State: fmt::Debug, |
| 222 | +{ |
| 223 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 224 | + f.debug_struct("SelectWithStrategy") |
| 225 | + .field("stream1", &self.stream1) |
| 226 | + .field("stream2", &self.stream2) |
| 227 | + .field("state", &self.state) |
| 228 | + .finish() |
| 229 | + } |
| 230 | +} |
0 commit comments