tower_util/
ready.rs

1use std::{fmt, marker::PhantomData};
2
3use futures_core::ready;
4use std::{
5    future::Future,
6    pin::Pin,
7    task::{Context, Poll},
8};
9use tower_service::Service;
10
11/// A future that yields the service when it is ready to accept a request.
12///
13/// `ReadyOneshot` values are produced by `ServiceExt::ready_oneshot`.
14pub struct ReadyOneshot<T, Request> {
15    inner: Option<T>,
16    _p: PhantomData<fn() -> Request>,
17}
18
19// Safety: This is safe because `Services`'s are always `Unpin`.
20impl<T, Request> Unpin for ReadyOneshot<T, Request> {}
21
22impl<T, Request> ReadyOneshot<T, Request>
23where
24    T: Service<Request>,
25{
26    #[allow(missing_docs)]
27    pub fn new(service: T) -> Self {
28        Self {
29            inner: Some(service),
30            _p: PhantomData,
31        }
32    }
33}
34
35impl<T, Request> Future for ReadyOneshot<T, Request>
36where
37    T: Service<Request>,
38{
39    type Output = Result<T, T::Error>;
40
41    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
42        ready!(self
43            .inner
44            .as_mut()
45            .expect("poll after Poll::Ready")
46            .poll_ready(cx))?;
47
48        Poll::Ready(Ok(self.inner.take().expect("poll after Poll::Ready")))
49    }
50}
51
52impl<T, Request> fmt::Debug for ReadyOneshot<T, Request>
53where
54    T: fmt::Debug,
55{
56    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57        f.debug_struct("ReadyOneshot")
58            .field("inner", &self.inner)
59            .finish()
60    }
61}
62
63/// A future that yields a mutable reference to the service when it is ready to accept a request.
64///
65/// `ReadyAnd` values are produced by `ServiceExt::ready_and`.
66pub struct ReadyAnd<'a, T, Request>(ReadyOneshot<&'a mut T, Request>);
67
68// Safety: This is safe for the same reason that the impl for ReadyOneshot is safe.
69impl<'a, T, Request> Unpin for ReadyAnd<'a, T, Request> {}
70
71impl<'a, T, Request> ReadyAnd<'a, T, Request>
72where
73    T: Service<Request>,
74{
75    #[allow(missing_docs)]
76    pub fn new(service: &'a mut T) -> Self {
77        Self(ReadyOneshot::new(service))
78    }
79}
80
81impl<'a, T, Request> Future for ReadyAnd<'a, T, Request>
82where
83    T: Service<Request>,
84{
85    type Output = Result<&'a mut T, T::Error>;
86
87    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
88        Pin::new(&mut self.0).poll(cx)
89    }
90}
91
92impl<'a, T, Request> fmt::Debug for ReadyAnd<'a, T, Request>
93where
94    T: fmt::Debug,
95{
96    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97        f.debug_tuple("ReadyAnd").field(&self.0).finish()
98    }
99}
100
101// ==== deprecated `Ready` that is just `ReadyAnd` with a unit return type.
102
103/// A future that resolves when a `Service` is ready to accept a request.
104///
105/// `Ready` values are produced by `ServiceExt::ready`.
106pub struct Ready<'a, T, Request>(ReadyAnd<'a, T, Request>);
107
108// Safety: This is safe for the same reason that the impl for ReadyOneshot is safe.
109impl<'a, T, Request> Unpin for Ready<'a, T, Request> {}
110
111impl<'a, T, Request> Ready<'a, T, Request>
112where
113    T: Service<Request>,
114{
115    #[allow(missing_docs)]
116    #[deprecated(since = "0.3.1", note = "prefer `ReadyAnd` which yields the service")]
117    pub fn new(service: &'a mut T) -> Self {
118        Self(ReadyAnd::new(service))
119    }
120}
121
122impl<'a, T, Request> Future for Ready<'a, T, Request>
123where
124    T: Service<Request>,
125{
126    type Output = Result<(), T::Error>;
127
128    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
129        let _ = ready!(Pin::new(&mut self.0).poll(cx));
130        Poll::Ready(Ok(()))
131    }
132}
133
134impl<'a, T, Request> fmt::Debug for Ready<'a, T, Request>
135where
136    T: fmt::Debug,
137{
138    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139        f.debug_tuple("Ready").field(&self.0).finish()
140    }
141}