futures_buffered/merge_unbounded.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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
use alloc::vec::Vec;
use core::{
pin::Pin,
task::{Context, Poll},
};
use futures_core::Stream;
use crate::{futures_unordered::MIN_CAPACITY, FuturesUnorderedBounded, MergeBounded};
/// A combined stream that releases values in any order that they come.
///
/// This differs from [`crate::Merge`] in that [`MergeUnbounded`] does not have a fixed capacity
/// but instead grows on demand. It uses [`crate::FuturesUnordered`] under the hood.
///
/// # Example
///
/// ```
/// use std::future::ready;
/// use futures::stream::{self, StreamExt};
/// use futures::executor::block_on;
/// use futures_buffered::MergeUnbounded;
///
/// block_on(async {
/// let a = stream::once(ready(2));
/// let b = stream::once(ready(3));
/// let mut s = MergeUnbounded::from_iter([a, b]);
///
/// let mut counter = 0;
/// while let Some(n) = s.next().await {
/// if n == 3 {
/// s.push(stream::once(ready(4)));
/// }
/// counter += n;
/// }
/// assert_eq!(counter, 2+3+4);
/// })
/// ```
pub struct MergeUnbounded<S> {
pub(crate) groups: Vec<MergeBounded<S>>,
poll_next: usize,
}
impl<S> Default for MergeUnbounded<S> {
fn default() -> Self {
Self::new()
}
}
impl<S> MergeUnbounded<S> {
/// Create a new, empty [`MergeUnbounded`].
///
/// Calling [`poll_next`](futures_core::Stream::poll_next) will return `Poll::Ready(None)`
/// until a stream is added with [`Self::push`].
pub const fn new() -> Self {
Self {
groups: Vec::new(),
poll_next: 0,
}
}
/// Push a stream into the set.
///
/// This method adds the given stream to the set. This method will not
/// call [`poll_next`](futures_core::Stream::poll_next) on the submitted stream. The caller must
/// ensure that [`MergeUnbounded::poll_next`](Stream::poll_next) is called
/// in order to receive wake-up notifications for the given stream.
#[track_caller]
pub fn push(&mut self, stream: S) {
let last = match self.groups.last_mut() {
Some(last) => last,
None => {
self.groups.push(MergeBounded {
streams: FuturesUnorderedBounded::new(MIN_CAPACITY),
});
self.groups.last_mut().unwrap()
}
};
match last.try_push(stream) {
Ok(()) => {}
Err(stream) => {
let mut next = MergeBounded {
streams: FuturesUnorderedBounded::new(last.streams.capacity() * 2),
};
next.push(stream);
self.groups.push(next);
}
}
}
/// Returns `true` if there are no streams in the set.
pub fn is_empty(&self) -> bool {
self.groups.iter().all(|g| g.streams.is_empty())
}
/// Returns the number of streams currently in the set.
pub fn len(&self) -> usize {
self.groups.iter().map(|g| g.streams.len()).sum()
}
}
impl<S: Stream + Unpin> Stream for MergeUnbounded<S> {
type Item = S::Item;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let Self { groups, poll_next } = &mut *self;
if groups.is_empty() {
return Poll::Ready(None);
}
for _ in 0..groups.len() {
if *poll_next >= groups.len() {
*poll_next = 0;
}
let poll = Pin::new(&mut groups[*poll_next]).poll_next(cx);
match poll {
Poll::Ready(Some(x)) => {
return Poll::Ready(Some(x));
}
Poll::Ready(None) => {
let group = groups.remove(*poll_next);
debug_assert!(group.streams.is_empty());
if groups.is_empty() {
// group should contain at least 1 set
groups.push(group);
return Poll::Ready(None);
}
// we do not want to drop the last set as it contains
// the largest allocation that we want to keep a hold of
if *poll_next == groups.len() {
groups.push(group);
*poll_next = 0;
}
}
Poll::Pending => {
*poll_next += 1;
}
}
}
Poll::Pending
}
}
impl<S: Stream + Unpin> FromIterator<S> for MergeUnbounded<S> {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = S>,
{
let iter = iter.into_iter();
// let mut this =
// Self::with_capacity(usize::max(iter.size_hint().0, MIN_CAPACITY));
let mut this = Self::new();
for stream in iter {
this.push(stream);
}
this
}
}
#[cfg(test)]
mod tests {
use core::cell::RefCell;
use core::task::Waker;
use super::*;
use alloc::collections::VecDeque;
use alloc::rc::Rc;
use futures::executor::block_on;
use futures::executor::LocalPool;
use futures::stream;
use futures::task::LocalSpawnExt;
use futures::StreamExt;
#[test]
fn merge_tuple_4() {
block_on(async {
let a = stream::repeat(2).take(2);
let b = stream::repeat(3).take(3);
let c = stream::repeat(5).take(5);
let d = stream::repeat(7).take(7);
let mut s: MergeUnbounded<_> = [a, b, c, d].into_iter().collect();
let mut counter = 0;
while let Some(n) = s.next().await {
counter += n;
}
assert_eq!(counter, 4 + 9 + 25 + 49);
});
}
#[test]
fn add_streams() {
block_on(async {
let a = stream::repeat(2).take(2);
let b = stream::repeat(3).take(3);
let mut s = MergeUnbounded::default();
assert_eq!(s.next().await, None);
assert!(s.is_empty());
assert_eq!(s.len(), 0);
s.push(a);
s.push(b);
assert!(!s.is_empty());
assert_eq!(s.len(), 2);
let mut counter = 0;
while let Some(n) = s.next().await {
counter += n;
assert!(!s.is_empty());
}
assert!(s.is_empty());
assert_eq!(s.len(), 0);
let b = stream::repeat(4).take(4);
s.push(b);
assert!(!s.is_empty());
assert_eq!(s.len(), 1);
while let Some(n) = s.next().await {
counter += n;
}
assert_eq!(counter, 4 + 9 + 16);
assert!(s.is_empty());
assert_eq!(s.len(), 0);
});
}
/// This test case uses channels so we'll have streams that return Pending from time to time.
///
/// The purpose of this test is to make sure we have the waking logic working.
#[test]
fn merge_channels() {
struct LocalChannel<T> {
queue: VecDeque<T>,
waker: Option<Waker>,
closed: bool,
}
struct LocalReceiver<T> {
channel: Rc<RefCell<LocalChannel<T>>>,
}
impl<T> Stream for LocalReceiver<T> {
type Item = T;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut channel = self.channel.borrow_mut();
match channel.queue.pop_front() {
Some(item) => Poll::Ready(Some(item)),
None => {
if channel.closed {
Poll::Ready(None)
} else {
channel.waker = Some(cx.waker().clone());
Poll::Pending
}
}
}
}
}
struct LocalSender<T> {
channel: Rc<RefCell<LocalChannel<T>>>,
}
impl<T> LocalSender<T> {
fn send(&self, item: T) {
let mut channel = self.channel.borrow_mut();
channel.queue.push_back(item);
let _ = channel.waker.take().map(Waker::wake);
}
}
impl<T> Drop for LocalSender<T> {
fn drop(&mut self) {
let mut channel = self.channel.borrow_mut();
channel.closed = true;
let _ = channel.waker.take().map(Waker::wake);
}
}
fn local_channel<T>() -> (LocalSender<T>, LocalReceiver<T>) {
let channel = Rc::new(RefCell::new(LocalChannel {
queue: VecDeque::new(),
waker: None,
closed: false,
}));
(
LocalSender {
channel: channel.clone(),
},
LocalReceiver { channel },
)
}
let mut pool = LocalPool::new();
let done = Rc::new(RefCell::new(false));
let done2 = done.clone();
pool.spawner()
.spawn_local(async move {
let (send1, receive1) = local_channel();
let (send2, receive2) = local_channel();
let (send3, receive3) = local_channel();
let (count, ()) = futures::future::join(
async {
let s: MergeUnbounded<_> =
[receive1, receive2, receive3].into_iter().collect();
s.fold(0, |a, b| async move { a + b }).await
},
async {
for i in 1..=4 {
send1.send(i);
send2.send(i);
send3.send(i);
}
drop(send1);
drop(send2);
drop(send3);
},
)
.await;
assert_eq!(count, 30);
*done2.borrow_mut() = true;
})
.unwrap();
while !*done.borrow() {
pool.run_until_stalled();
}
}
}