Files
anyhow
async_stream
async_stream_impl
async_trait
base64
byteorder
bytes
cfg_if
either
firestore_grpc
firestore_grpc_cloudrun
fnv
futures
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
io
lock
sink
stream
task
getrandom
h2
http
http_body
httparse
hyper
indexmap
iovec
itertools
itoa
lazy_static
libc
log
memchr
mio
net2
openssl_probe
percent_encoding
pin_project
pin_project_internal
pin_project_lite
pin_utils
ppv_lite86
proc_macro2
proc_macro_hack
proc_macro_nested
prost
prost_derive
prost_types
quote
rand
rand_chacha
rand_core
rand_pcg
ring
rustls
rustls_native_certs
ryu
sct
serde
serde_derive
serde_json
slab
spin
syn
time
tokio
future
io
loom
macros
net
park
runtime
sync
task
time
util
tokio_rustls
tokio_util
tonic
tower
tower_balance
tower_buffer
tower_discover
tower_layer
tower_limit
tower_load
tower_load_shed
tower_make
tower_ready_cache
tower_retry
tower_service
tower_timeout
tower_util
tracing
tracing_attributes
tracing_core
tracing_futures
try_lock
unicode_xid
untrusted
want
webpki
  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
//! `Stream<Item = Request>` + `Service<Request>` => `Stream<Item = Response>`.

use super::{common, Error};
use futures_core::Stream;
use futures_util::stream::FuturesOrdered;
use pin_project::pin_project;
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};
use tower_service::Service;

/// This is a `futures::Stream` of responses resulting from calling the wrapped `tower::Service`
/// for each request received on the wrapped `Stream`.
///
/// ```rust
/// # use std::task::{Poll, Context};
/// # use std::cell::Cell;
/// # use std::error::Error;
/// # use std::rc::Rc;
/// #
/// use futures_util::future::{ready, Ready};
/// use futures_util::StreamExt;
/// use tower_service::Service;
/// use tower_util::ServiceExt;
/// use tokio::prelude::*;
///
/// // First, we need to have a Service to process our requests.
/// #[derive(Debug, Eq, PartialEq)]
/// struct FirstLetter;
/// impl Service<&'static str> for FirstLetter {
///      type Response = &'static str;
///      type Error = Box<dyn Error + Send + Sync>;
///      type Future = Ready<Result<Self::Response, Self::Error>>;
///
///      fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
///          Poll::Ready(Ok(()))
///      }
///
///      fn call(&mut self, req: &'static str) -> Self::Future {
///          ready(Ok(&req[..1]))
///      }
/// }
///
/// #[tokio::main]
/// async fn main() {
///     // Next, we need a Stream of requests.
///     let (mut reqs, rx) = tokio::sync::mpsc::unbounded_channel();
///     // Note that we have to help Rust out here by telling it what error type to use.
///     // Specifically, it has to be From<Service::Error> + From<Stream::Error>.
///     let mut rsps = FirstLetter.call_all(rx);
///
///     // Now, let's send a few requests and then check that we get the corresponding responses.
///     reqs.send("one");
///     reqs.send("two");
///     reqs.send("three");
///     drop(reqs);
///
///     // We then loop over the response Strem that we get back from call_all.
///     let mut i = 0usize;
///     while let Some(rsp) = rsps.next().await {
///         // Each response is a Result (we could also have used TryStream::try_next)
///         match (i + 1, rsp.unwrap()) {
///             (1, "o") |
///             (2, "t") |
///             (3, "t") => {}
///             (n, i) => {
///                 unreachable!("{}. response was '{}'", n, i);
///             }
///         }
///         i += 1;
///     }
///
///     // And at the end, we can get the Service back when there are no more requests.
///     assert_eq!(rsps.into_inner(), FirstLetter);
/// }
/// ```
#[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<Error>,
    S: Stream,
{
    /// Create new `CallAll` combinator.
    ///
    /// Each request yielded by `stread` is passed to `svc`, and the resulting responses are
    /// yielded in the same order by the implementation of `Stream` for `CallAll`.
    pub fn new(service: Svc, stream: S) -> CallAll<Svc, S> {
        CallAll {
            inner: common::CallAll::new(service, stream, FuturesOrdered::new()),
        }
    }

    /// Extract the wrapped `Service`.
    ///
    /// # Panics
    ///
    /// Panics if `take_service` was already called.
    pub fn into_inner(self) -> Svc {
        self.inner.into_inner()
    }

    /// Extract the wrapped `Service`.
    ///
    /// This `CallAll` can no longer be used after this function has been called.
    ///
    /// # Panics
    ///
    /// Panics if `take_service` was already called.
    pub fn take_service(self: Pin<&mut Self>) -> Svc {
        self.project().inner.take_service()
    }

    /// Return responses as they are ready, regardless of the initial order.
    ///
    /// This function must be called before the stream is polled.
    ///
    /// # Panics
    ///
    /// Panics if `poll` was called.
    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<Error>,
    S: Stream,
{
    type Item = Result<Svc::Response, Error>;

    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)
    }
}