actix_service/
map_config.rs

1use core::marker::PhantomData;
2
3use super::{IntoServiceFactory, ServiceFactory};
4
5/// Adapt external config argument to a config for provided service factory
6///
7/// Note that this function consumes the receiving service factory and returns
8/// a wrapped version of it.
9pub fn map_config<I, SF, Req, F, Cfg>(factory: I, f: F) -> MapConfig<SF, Req, F, Cfg>
10where
11    I: IntoServiceFactory<SF, Req>,
12    SF: ServiceFactory<Req>,
13    F: Fn(Cfg) -> SF::Config,
14{
15    MapConfig::new(factory.into_factory(), f)
16}
17
18/// Replace config with unit.
19pub fn unit_config<I, SF, Cfg, Req>(factory: I) -> UnitConfig<SF, Cfg, Req>
20where
21    I: IntoServiceFactory<SF, Req>,
22    SF: ServiceFactory<Req, Config = ()>,
23{
24    UnitConfig::new(factory.into_factory())
25}
26
27/// `map_config()` adapter service factory
28pub struct MapConfig<SF, Req, F, Cfg> {
29    factory: SF,
30    cfg_mapper: F,
31    e: PhantomData<fn(Cfg, Req)>,
32}
33
34impl<SF, Req, F, Cfg> MapConfig<SF, Req, F, Cfg> {
35    /// Create new `MapConfig` combinator
36    pub(crate) fn new(factory: SF, cfg_mapper: F) -> Self
37    where
38        SF: ServiceFactory<Req>,
39        F: Fn(Cfg) -> SF::Config,
40    {
41        Self {
42            factory,
43            cfg_mapper,
44            e: PhantomData,
45        }
46    }
47}
48
49impl<SF, Req, F, Cfg> Clone for MapConfig<SF, Req, F, Cfg>
50where
51    SF: Clone,
52    F: Clone,
53{
54    fn clone(&self) -> Self {
55        Self {
56            factory: self.factory.clone(),
57            cfg_mapper: self.cfg_mapper.clone(),
58            e: PhantomData,
59        }
60    }
61}
62
63impl<SF, Req, F, Cfg> ServiceFactory<Req> for MapConfig<SF, Req, F, Cfg>
64where
65    SF: ServiceFactory<Req>,
66    F: Fn(Cfg) -> SF::Config,
67{
68    type Response = SF::Response;
69    type Error = SF::Error;
70
71    type Config = Cfg;
72    type Service = SF::Service;
73    type InitError = SF::InitError;
74    type Future = SF::Future;
75
76    fn new_service(&self, cfg: Self::Config) -> Self::Future {
77        let mapped_cfg = (self.cfg_mapper)(cfg);
78        self.factory.new_service(mapped_cfg)
79    }
80}
81
82/// `unit_config()` config combinator
83pub struct UnitConfig<SF, Cfg, Req> {
84    factory: SF,
85    _phantom: PhantomData<fn(Cfg, Req)>,
86}
87
88impl<SF, Cfg, Req> UnitConfig<SF, Cfg, Req>
89where
90    SF: ServiceFactory<Req, Config = ()>,
91{
92    /// Create new `UnitConfig` combinator
93    pub(crate) fn new(factory: SF) -> Self {
94        Self {
95            factory,
96            _phantom: PhantomData,
97        }
98    }
99}
100
101impl<SF, Cfg, Req> Clone for UnitConfig<SF, Cfg, Req>
102where
103    SF: Clone,
104{
105    fn clone(&self) -> Self {
106        Self {
107            factory: self.factory.clone(),
108            _phantom: PhantomData,
109        }
110    }
111}
112
113impl<SF, Cfg, Req> ServiceFactory<Req> for UnitConfig<SF, Cfg, Req>
114where
115    SF: ServiceFactory<Req, Config = ()>,
116{
117    type Response = SF::Response;
118    type Error = SF::Error;
119
120    type Config = Cfg;
121    type Service = SF::Service;
122    type InitError = SF::InitError;
123    type Future = SF::Future;
124
125    fn new_service(&self, _: Cfg) -> Self::Future {
126        self.factory.new_service(())
127    }
128}