// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//! A wrapper for [`FuturesUnordered`] that wakes the task up once a new future is pushed
//! for it to be polled automatically. It's [`Stream`] never terminates.
use futures::{stream::FuturesUnordered, Future, Stream, StreamExt};
use std::{
pin::Pin,
task::{Context, Poll, Waker},
};
/// Wrapper around [`FuturesUnordered`] that wakes a task up automatically.
pub struct FuturesStream {
futures: FuturesUnordered,
waker: Option,
}
/// Surprizingly, `#[derive(Default)]` doesn't work on [`FuturesStream`].
impl Default for FuturesStream {
fn default() -> FuturesStream {
FuturesStream { futures: Default::default(), waker: None }
}
}
impl FuturesStream {
/// Push a future for processing.
pub fn push(&mut self, future: F) {
self.futures.push(future);
if let Some(waker) = self.waker.take() {
waker.wake();
}
}
/// The number of futures in the stream.
pub fn len(&self) -> usize {
self.futures.len()
}
}
impl Stream for FuturesStream {
type Item = ::Output;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll