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 Svc::Error: Into<crate::BoxError>,
39 S: Stream,
40{
41 pub fn new(service: Svc, stream: S) -> CallAllUnordered<Svc, S> {
45 CallAllUnordered {
46 inner: common::CallAll::new(service, stream, FuturesUnordered::new()),
47 }
48 }
49
50 pub fn into_inner(self) -> Svc {
58 self.inner.into_inner()
59 }
60
61 pub fn take_service(self: Pin<&mut Self>) -> Svc {
71 self.project().inner.take_service()
72 }
73}
74
75impl<Svc, S> Stream for CallAllUnordered<Svc, S>
76where
77 Svc: Service<S::Item>,
78 Svc::Error: Into<crate::BoxError>,
79 S: Stream,
80{
81 type Item = Result<Svc::Response, crate::BoxError>;
82
83 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
84 self.project().inner.poll_next(cx)
85 }
86}
87
88impl<F: Future> common::Drive<F> for FuturesUnordered<F> {
89 fn is_empty(&self) -> bool {
90 FuturesUnordered::is_empty(self)
91 }
92
93 fn push(&mut self, future: F) {
94 FuturesUnordered::push(self, future)
95 }
96
97 fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Option<F::Output>> {
98 Stream::poll_next(Pin::new(self), cx)
99 }
100}