Coverage Report

Created: 2026-01-15 06:51

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/then.rs
Line
Count
Source
1
use futures_util::{future, FutureExt};
2
use std::{
3
    fmt,
4
    future::Future,
5
    task::{Context, Poll},
6
};
7
use tower_layer::Layer;
8
use tower_service::Service;
9
10
/// [`Service`] returned by the [`then`] combinator.
11
///
12
/// [`then`]: crate::util::ServiceExt::then
13
#[derive(Clone)]
14
pub struct Then<S, F> {
15
    inner: S,
16
    f: F,
17
}
18
19
impl<S, F> fmt::Debug for Then<S, F>
20
where
21
    S: fmt::Debug,
22
{
23
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24
0
        f.debug_struct("Then")
25
0
            .field("inner", &self.inner)
26
0
            .field("f", &format_args!("{}", std::any::type_name::<F>()))
27
0
            .finish()
28
0
    }
29
}
30
31
/// A [`Layer`] that produces a [`Then`] service.
32
///
33
/// [`Layer`]: tower_layer::Layer
34
#[derive(Debug, Clone)]
35
pub struct ThenLayer<F> {
36
    f: F,
37
}
38
39
impl<S, F> Then<S, F> {
40
    /// Creates a new `Then` service.
41
0
    pub const fn new(inner: S, f: F) -> Self {
42
0
        Then { f, inner }
43
0
    }
44
45
    /// Returns a new [`Layer`] that produces [`Then`] services.
46
    ///
47
    /// This is a convenience function that simply calls [`ThenLayer::new`].
48
    ///
49
    /// [`Layer`]: tower_layer::Layer
50
0
    pub fn layer(f: F) -> ThenLayer<F> {
51
0
        ThenLayer { f }
52
0
    }
53
}
54
55
opaque_future! {
56
    /// Response future from [`Then`] services.
57
    ///
58
    /// [`Then`]: crate::util::Then
59
    pub type ThenFuture<F1, F2, N> = future::Then<F1, F2, N>;
60
}
61
62
impl<S, F, Request, Response, Error, Fut> Service<Request> for Then<S, F>
63
where
64
    S: Service<Request>,
65
    S::Error: Into<Error>,
66
    F: FnOnce(Result<S::Response, S::Error>) -> Fut + Clone,
67
    Fut: Future<Output = Result<Response, Error>>,
68
{
69
    type Response = Response;
70
    type Error = Error;
71
    type Future = ThenFuture<S::Future, Fut, F>;
72
73
    #[inline]
74
0
    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
75
0
        self.inner.poll_ready(cx).map_err(Into::into)
76
0
    }
77
78
    #[inline]
79
0
    fn call(&mut self, request: Request) -> Self::Future {
80
0
        ThenFuture::new(self.inner.call(request).then(self.f.clone()))
81
0
    }
82
}
83
84
impl<F> ThenLayer<F> {
85
    /// Creates a new [`ThenLayer`] layer.
86
0
    pub const fn new(f: F) -> Self {
87
0
        ThenLayer { f }
88
0
    }
89
}
90
91
impl<S, F> Layer<S> for ThenLayer<F>
92
where
93
    F: Clone,
94
{
95
    type Service = Then<S, F>;
96
97
0
    fn layer(&self, inner: S) -> Self::Service {
98
0
        Then {
99
0
            f: self.f.clone(),
100
0
            inner,
101
0
        }
102
0
    }
103
}