tower/util/
oneshot.rs

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