actix_service/
ext.rs

1use crate::{
2    and_then::{AndThenService, AndThenServiceFactory},
3    map::Map,
4    map_err::MapErr,
5    transform_err::TransformMapInitErr,
6    IntoService, IntoServiceFactory, Service, ServiceFactory, Transform,
7};
8
9/// An extension trait for [`Service`]s that provides a variety of convenient adapters.
10pub trait ServiceExt<Req>: Service<Req> {
11    /// Map this service's output to a different type, returning a new service
12    /// of the resulting type.
13    ///
14    /// This function is similar to the `Option::map` or `Iterator::map` where
15    /// it will change the type of the underlying service.
16    ///
17    /// Note that this function consumes the receiving service and returns a
18    /// wrapped version of it, similar to the existing `map` methods in the
19    /// standard library.
20    fn map<F, R>(self, f: F) -> Map<Self, F, Req, R>
21    where
22        Self: Sized,
23        F: FnMut(Self::Response) -> R,
24    {
25        Map::new(self, f)
26    }
27
28    /// Map this service's error to a different error, returning a new service.
29    ///
30    /// This function is similar to the `Result::map_err` where it will change
31    /// the error type of the underlying service. For example, this can be useful to
32    /// ensure that services have the same error type.
33    ///
34    /// Note that this function consumes the receiving service and returns a
35    /// wrapped version of it.
36    fn map_err<F, E>(self, f: F) -> MapErr<Self, Req, F, E>
37    where
38        Self: Sized,
39        F: Fn(Self::Error) -> E,
40    {
41        MapErr::new(self, f)
42    }
43
44    /// Call another service after call to this one has resolved successfully.
45    ///
46    /// This function can be used to chain two services together and ensure that the second service
47    /// isn't called until call to the fist service have finished. Result of the call to the first
48    /// service is used as an input parameter for the second service's call.
49    ///
50    /// Note that this function consumes the receiving service and returns a wrapped version of it.
51    fn and_then<I, S1>(self, service: I) -> AndThenService<Self, S1, Req>
52    where
53        Self: Sized,
54        I: IntoService<S1, Self::Response>,
55        S1: Service<Self::Response, Error = Self::Error>,
56    {
57        AndThenService::new(self, service.into_service())
58    }
59}
60
61impl<S, Req> ServiceExt<Req> for S where S: Service<Req> {}
62
63/// An extension trait for [`ServiceFactory`]s that provides a variety of convenient adapters.
64pub trait ServiceFactoryExt<Req>: ServiceFactory<Req> {
65    /// Map this service's output to a different type, returning a new service
66    /// of the resulting type.
67    fn map<F, R>(self, f: F) -> crate::map::MapServiceFactory<Self, F, Req, R>
68    where
69        Self: Sized,
70        F: FnMut(Self::Response) -> R + Clone,
71    {
72        crate::map::MapServiceFactory::new(self, f)
73    }
74
75    /// Map this service's error to a different error, returning a new service.
76    fn map_err<F, E>(self, f: F) -> crate::map_err::MapErrServiceFactory<Self, Req, F, E>
77    where
78        Self: Sized,
79        F: Fn(Self::Error) -> E + Clone,
80    {
81        crate::map_err::MapErrServiceFactory::new(self, f)
82    }
83
84    /// Map this factory's init error to a different error, returning a new service.
85    fn map_init_err<F, E>(self, f: F) -> crate::map_init_err::MapInitErr<Self, F, Req, E>
86    where
87        Self: Sized,
88        F: Fn(Self::InitError) -> E + Clone,
89    {
90        crate::map_init_err::MapInitErr::new(self, f)
91    }
92
93    /// Call another service after call to this one has resolved successfully.
94    fn and_then<I, SF1>(self, factory: I) -> AndThenServiceFactory<Self, SF1, Req>
95    where
96        Self: Sized,
97        Self::Config: Clone,
98        I: IntoServiceFactory<SF1, Self::Response>,
99        SF1: ServiceFactory<
100            Self::Response,
101            Config = Self::Config,
102            Error = Self::Error,
103            InitError = Self::InitError,
104        >,
105    {
106        AndThenServiceFactory::new(self, factory.into_factory())
107    }
108}
109
110impl<SF, Req> ServiceFactoryExt<Req> for SF where SF: ServiceFactory<Req> {}
111
112/// An extension trait for [`Transform`]s that provides a variety of convenient adapters.
113pub trait TransformExt<S, Req>: Transform<S, Req> {
114    /// Return a new `Transform` whose init error is mapped to to a different type.
115    fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, Req, F, E>
116    where
117        Self: Sized,
118        F: Fn(Self::InitError) -> E + Clone,
119    {
120        TransformMapInitErr::new(self, f)
121    }
122}
123
124impl<T, Req> TransformExt<T, Req> for T where T: Transform<T, Req> {}