boluo_core/service/
then.rs

1use super::Service;
2
3/// [`then`] 返回的服务。
4///
5/// [`then`]: crate::service::ServiceExt::then
6#[derive(Clone, Copy)]
7pub struct Then<S, F> {
8    service: S,
9    f: F,
10}
11
12impl<S, F> Then<S, F> {
13    /// 创建一个新的 [`Then`] 服务。
14    pub fn new(service: S, f: F) -> Self {
15        Self { service, f }
16    }
17}
18
19impl<S, F, Fut, Req, Res, Err> Service<Req> for Then<S, F>
20where
21    S: Service<Req>,
22    F: Fn(Result<S::Response, S::Error>) -> Fut + Send + Sync,
23    Fut: Future<Output = Result<Res, Err>> + Send,
24{
25    type Response = Res;
26    type Error = Err;
27
28    fn call(&self, req: Req) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send {
29        let fut = self.service.call(req);
30        async move { (self.f)(fut.await).await }
31    }
32}
33
34impl<S, F> std::fmt::Debug for Then<S, F>
35where
36    S: std::fmt::Debug,
37{
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        f.debug_struct("Then")
40            .field("service", &self.service)
41            .field("f", &std::any::type_name::<F>())
42            .finish()
43    }
44}