/rust/registry/src/index.crates.io-6f17d22bba15001f/hyper-util-0.1.10/src/service/oneshot.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use futures_util::ready; |
2 | | use pin_project_lite::pin_project; |
3 | | use std::future::Future; |
4 | | use std::pin::Pin; |
5 | | use std::task::{Context, Poll}; |
6 | | use tower_service::Service; |
7 | | |
8 | | // Vendored from tower::util to reduce dependencies, the code is small enough. |
9 | | |
10 | | // Not really pub, but used in a trait for bounds |
11 | | pin_project! { |
12 | | #[project = OneshotProj] |
13 | | #[derive(Debug)] |
14 | | pub enum Oneshot<S: Service<Req>, Req> { |
15 | | NotReady { |
16 | | svc: S, |
17 | | req: Option<Req>, |
18 | | }, |
19 | | Called { |
20 | | #[pin] |
21 | | fut: S::Future, |
22 | | }, |
23 | | Done, |
24 | | } |
25 | | } |
26 | | |
27 | | impl<S, Req> Oneshot<S, Req> |
28 | | where |
29 | | S: Service<Req>, |
30 | | { |
31 | 0 | pub(crate) const fn new(svc: S, req: Req) -> Self { |
32 | 0 | Oneshot::NotReady { |
33 | 0 | svc, |
34 | 0 | req: Some(req), |
35 | 0 | } |
36 | 0 | } Unexecuted instantiation: <hyper_util::service::oneshot::Oneshot<hyper_rustls::connector::HttpsConnector<hyper_util::client::legacy::connect::http::HttpConnector>, http::uri::Uri>>::new Unexecuted instantiation: <hyper_util::service::oneshot::Oneshot<_, _>>::new |
37 | | } |
38 | | |
39 | | impl<S, Req> Future for Oneshot<S, Req> |
40 | | where |
41 | | S: Service<Req>, |
42 | | { |
43 | | type Output = Result<S::Response, S::Error>; |
44 | | |
45 | 0 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
46 | | loop { |
47 | 0 | let this = self.as_mut().project(); |
48 | 0 | match this { |
49 | 0 | OneshotProj::NotReady { svc, req } => { |
50 | 0 | let _ = ready!(svc.poll_ready(cx))?; |
51 | 0 | let fut = svc.call(req.take().expect("already called")); |
52 | 0 | self.set(Oneshot::Called { fut }); |
53 | | } |
54 | 0 | OneshotProj::Called { fut } => { |
55 | 0 | let res = ready!(fut.poll(cx))?; |
56 | 0 | self.set(Oneshot::Done); |
57 | 0 | return Poll::Ready(Ok(res)); |
58 | | } |
59 | 0 | OneshotProj::Done => panic!("polled after complete"), |
60 | | } |
61 | | } |
62 | 0 | } Unexecuted instantiation: <hyper_util::service::oneshot::Oneshot<hyper_rustls::connector::HttpsConnector<hyper_util::client::legacy::connect::http::HttpConnector>, http::uri::Uri> as core::future::future::Future>::poll Unexecuted instantiation: <hyper_util::service::oneshot::Oneshot<_, _> as core::future::future::Future>::poll |
63 | | } |