/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tower-0.4.13/src/util/oneshot.rs
Line | Count | Source |
1 | | use futures_core::ready; |
2 | | use pin_project_lite::pin_project; |
3 | | use std::{ |
4 | | fmt, |
5 | | future::Future, |
6 | | pin::Pin, |
7 | | task::{Context, Poll}, |
8 | | }; |
9 | | use tower_service::Service; |
10 | | |
11 | | pin_project! { |
12 | | /// A [`Future`] consuming a [`Service`] and request, waiting until the [`Service`] |
13 | | /// is ready, and then calling [`Service::call`] with the request, and |
14 | | /// waiting for that [`Future`]. |
15 | | #[derive(Debug)] |
16 | | pub struct Oneshot<S: Service<Req>, Req> { |
17 | | #[pin] |
18 | | state: State<S, Req>, |
19 | | } |
20 | | } |
21 | | |
22 | | pin_project! { |
23 | | #[project = StateProj] |
24 | | enum State<S: Service<Req>, Req> { |
25 | | NotReady { |
26 | | svc: S, |
27 | | req: Option<Req>, |
28 | | }, |
29 | | Called { |
30 | | #[pin] |
31 | | fut: S::Future, |
32 | | }, |
33 | | Done, |
34 | | } |
35 | | } |
36 | | |
37 | | impl<S: Service<Req>, Req> State<S, Req> { |
38 | 0 | fn not_ready(svc: S, req: Option<Req>) -> Self { |
39 | 0 | Self::NotReady { svc, req } |
40 | 0 | } |
41 | | |
42 | 0 | fn called(fut: S::Future) -> Self { |
43 | 0 | Self::Called { fut } |
44 | 0 | } |
45 | | } |
46 | | |
47 | | impl<S, Req> fmt::Debug for State<S, Req> |
48 | | where |
49 | | S: Service<Req> + fmt::Debug, |
50 | | Req: fmt::Debug, |
51 | | { |
52 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
53 | 0 | match self { |
54 | | State::NotReady { |
55 | 0 | svc, |
56 | 0 | req: Some(req), |
57 | 0 | } => f |
58 | 0 | .debug_tuple("State::NotReady") |
59 | 0 | .field(svc) |
60 | 0 | .field(req) |
61 | 0 | .finish(), |
62 | 0 | State::NotReady { req: None, .. } => unreachable!(), |
63 | 0 | State::Called { .. } => f.debug_tuple("State::Called").field(&"S::Future").finish(), |
64 | 0 | State::Done => f.debug_tuple("State::Done").finish(), |
65 | | } |
66 | 0 | } |
67 | | } |
68 | | |
69 | | impl<S, Req> Oneshot<S, Req> |
70 | | where |
71 | | S: Service<Req>, |
72 | | { |
73 | | #[allow(missing_docs)] |
74 | 0 | pub fn new(svc: S, req: Req) -> Self { |
75 | 0 | Oneshot { |
76 | 0 | state: State::not_ready(svc, Some(req)), |
77 | 0 | } |
78 | 0 | } |
79 | | } |
80 | | |
81 | | impl<S, Req> Future for Oneshot<S, Req> |
82 | | where |
83 | | S: Service<Req>, |
84 | | { |
85 | | type Output = Result<S::Response, S::Error>; |
86 | | |
87 | 0 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
88 | 0 | let mut this = self.project(); |
89 | | loop { |
90 | 0 | match this.state.as_mut().project() { |
91 | 0 | StateProj::NotReady { svc, req } => { |
92 | 0 | let _ = ready!(svc.poll_ready(cx))?; |
93 | 0 | let f = svc.call(req.take().expect("already called")); |
94 | 0 | this.state.set(State::called(f)); |
95 | | } |
96 | 0 | StateProj::Called { fut } => { |
97 | 0 | let res = ready!(fut.poll(cx))?; |
98 | 0 | this.state.set(State::Done); |
99 | 0 | return Poll::Ready(Ok(res)); |
100 | | } |
101 | 0 | StateProj::Done => panic!("polled after complete"), |
102 | | } |
103 | | } |
104 | 0 | } |
105 | | } |