tower/util/call_all/
unordered.rs1use super::common;
7use futures_core::Stream;
8use futures_util::stream::FuturesUnordered;
9use pin_project_lite::pin_project;
10use std::{
11    future::Future,
12    pin::Pin,
13    task::{Context, Poll},
14};
15use tower_service::Service;
16
17pin_project! {
18    #[derive(Debug)]
25    pub struct CallAllUnordered<Svc, S>
26    where
27        Svc: Service<S::Item>,
28        S: Stream,
29    {
30        #[pin]
31        inner: common::CallAll<Svc, S, FuturesUnordered<Svc::Future>>,
32    }
33}
34
35impl<Svc, S> CallAllUnordered<Svc, S>
36where
37    Svc: Service<S::Item>,
38    S: Stream,
39{
40    pub fn new(service: Svc, stream: S) -> CallAllUnordered<Svc, S> {
44        CallAllUnordered {
45            inner: common::CallAll::new(service, stream, FuturesUnordered::new()),
46        }
47    }
48
49    pub fn into_inner(self) -> Svc {
57        self.inner.into_inner()
58    }
59
60    pub fn take_service(self: Pin<&mut Self>) -> Svc {
70        self.project().inner.take_service()
71    }
72}
73
74impl<Svc, S> Stream for CallAllUnordered<Svc, S>
75where
76    Svc: Service<S::Item>,
77    S: Stream,
78{
79    type Item = Result<Svc::Response, Svc::Error>;
80
81    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
82        self.project().inner.poll_next(cx)
83    }
84}
85
86impl<F: Future> common::Drive<F> for FuturesUnordered<F> {
87    fn is_empty(&self) -> bool {
88        FuturesUnordered::is_empty(self)
89    }
90
91    fn push(&mut self, future: F) {
92        FuturesUnordered::push(self, future)
93    }
94
95    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Option<F::Output>> {
96        Stream::poll_next(Pin::new(self), cx)
97    }
98}