Struct CallAll
pub struct CallAll<Svc, S>
where
Svc: Service<S::Item>,
S: Stream, { /* private fields */ }
This is a Stream of responses resulting from calling the wrapped Service for each
request received on the wrapped Stream.
# use std::task::{Poll, Context};
# use std::cell::Cell;
# use std::error::Error;
# use std::rc::Rc;
#
use std::future::{ready, Ready};
use futures::StreamExt;
use futures::channel::mpsc;
use tower_service::Service;
use tower::util::ServiceExt;
// 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) = mpsc::unbounded();
// 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.unbounded_send("one").unwrap();
reqs.unbounded_send("two").unwrap();
reqs.unbounded_send("three").unwrap();
drop(reqs);
// We then loop over the response `Stream` 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);
}
Implementations
impl<Svc, S> CallAll<Svc, S>
where
Svc: Service<S::Item>,
S: Stream,
fn new(service: Svc, stream: S) -> CallAll<Svc, S>Create new
CallAllcombinator.Each request yielded by
streamis passed tosvc, and the resulting responses are yielded in the same order by the implementation ofStreamforCallAll.fn into_inner(self) -> SvcExtract the wrapped
Service.Panics
Panics if
take_servicewas already called.fn take_service(self: Pin<&mut Self>) -> SvcExtract the wrapped
Service.This
CallAllcan no longer be used after this function has been called.Panics
Panics if
take_servicewas already called.fn unordered(self) -> CallAllUnordered<Svc, S>Return responses as they are ready, regardless of the initial order.
This function must be called before the stream is polled.
Panics
Panics if
pollwas called.
Trait Implementations
impl<'__pin, Svc, S> Unpin for CallAll<Svc, S>
where
PinnedFieldsOf<__Origin<'__pin, Svc, S>>: Unpin,
Svc: Service<S::Item>,
S: Stream,
impl<Svc, S> Debug for CallAll<Svc, S>
where
Svc: Service<S::Item> + Debug,
S: Stream + Debug,
Svc::Future: Debug,
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<Svc, S> Stream for CallAll<Svc, S>
where
Svc: Service<S::Item>,
S: Stream,
type Item = Result<<Svc as Service<<S as Stream>::Item>>::Response, <Svc as Service<<S as Stream>::Item>>::Error>;fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>