Coverage Report

Created: 2026-03-31 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tower-0.5.2/src/util/ready.rs
Line
Count
Source
1
use std::{fmt, marker::PhantomData};
2
3
use futures_core::ready;
4
use std::{
5
    future::Future,
6
    pin::Pin,
7
    task::{Context, Poll},
8
};
9
use 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`].
14
///
15
/// [`ServiceExt::ready_oneshot`]: crate::util::ServiceExt::ready_oneshot
16
pub struct ReadyOneshot<T, Request> {
17
    inner: Option<T>,
18
    _p: PhantomData<fn() -> Request>,
19
}
20
21
// Safety: This is safe because `Services`'s are always `Unpin`.
22
impl<T, Request> Unpin for ReadyOneshot<T, Request> {}
23
24
impl<T, Request> ReadyOneshot<T, Request>
25
where
26
    T: Service<Request>,
27
{
28
    #[allow(missing_docs)]
29
0
    pub const fn new(service: T) -> Self {
30
0
        Self {
31
0
            inner: Some(service),
32
0
            _p: PhantomData,
33
0
        }
34
0
    }
35
}
36
37
impl<T, Request> Future for ReadyOneshot<T, Request>
38
where
39
    T: Service<Request>,
40
{
41
    type Output = Result<T, T::Error>;
42
43
0
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
44
0
        ready!(self
45
0
            .inner
46
0
            .as_mut()
47
0
            .expect("poll after Poll::Ready")
48
0
            .poll_ready(cx))?;
49
50
0
        Poll::Ready(Ok(self.inner.take().expect("poll after Poll::Ready")))
51
0
    }
52
}
53
54
impl<T, Request> fmt::Debug for ReadyOneshot<T, Request>
55
where
56
    T: fmt::Debug,
57
{
58
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59
0
        f.debug_struct("ReadyOneshot")
60
0
            .field("inner", &self.inner)
61
0
            .finish()
62
0
    }
63
}
64
65
/// A future that yields a mutable reference to the service when it is ready to accept a request.
66
///
67
/// [`Ready`] values are produced by [`ServiceExt::ready`].
68
///
69
/// [`ServiceExt::ready`]: crate::util::ServiceExt::ready
70
pub struct Ready<'a, T, Request>(ReadyOneshot<&'a mut T, Request>);
71
72
// Safety: This is safe for the same reason that the impl for ReadyOneshot is safe.
73
impl<'a, T, Request> Unpin for Ready<'a, T, Request> {}
74
75
impl<'a, T, Request> Ready<'a, T, Request>
76
where
77
    T: Service<Request>,
78
{
79
    #[allow(missing_docs)]
80
0
    pub fn new(service: &'a mut T) -> Self {
81
0
        Self(ReadyOneshot::new(service))
82
0
    }
83
}
84
85
impl<'a, T, Request> Future for Ready<'a, T, Request>
86
where
87
    T: Service<Request>,
88
{
89
    type Output = Result<&'a mut T, T::Error>;
90
91
0
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
92
0
        Pin::new(&mut self.0).poll(cx)
93
0
    }
94
}
95
96
impl<'a, T, Request> fmt::Debug for Ready<'a, T, Request>
97
where
98
    T: fmt::Debug,
99
{
100
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101
0
        f.debug_tuple("Ready").field(&self.0).finish()
102
0
    }
103
}