use super::common;
use futures_core::Stream;
use futures_util::stream::FuturesOrdered;
use pin_project_lite::pin_project;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tower_service::Service;
pin_project! {
#[derive(Debug)]
pub struct CallAll<Svc, S>
where
Svc: Service<S::Item>,
S: Stream,
{
#[pin]
inner: common::CallAll<Svc, S, FuturesOrdered<Svc::Future>>,
}
}
impl<Svc, S> CallAll<Svc, S>
where
Svc: Service<S::Item>,
Svc::Error: Into<crate::BoxError>,
S: Stream,
{
pub fn new(service: Svc, stream: S) -> CallAll<Svc, S> {
CallAll {
inner: common::CallAll::new(service, stream, FuturesOrdered::new()),
}
}
pub fn into_inner(self) -> Svc {
self.inner.into_inner()
}
pub fn take_service(self: Pin<&mut Self>) -> Svc {
self.project().inner.take_service()
}
pub fn unordered(self) -> super::CallAllUnordered<Svc, S> {
self.inner.unordered()
}
}
impl<Svc, S> Stream for CallAll<Svc, S>
where
Svc: Service<S::Item>,
Svc::Error: Into<crate::BoxError>,
S: Stream,
{
type Item = Result<Svc::Response, crate::BoxError>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.project().inner.poll_next(cx)
}
}
impl<F: Future> common::Drive<F> for FuturesOrdered<F> {
fn is_empty(&self) -> bool {
FuturesOrdered::is_empty(self)
}
fn push(&mut self, future: F) {
FuturesOrdered::push(self, future)
}
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Option<F::Output>> {
Stream::poll_next(Pin::new(self), cx)
}
}