use core::marker::PhantomData; use super::{IntoServiceFactory, ServiceFactory}; /// Adapt external config argument to a config for provided service factory /// /// Note that this function consumes the receiving service factory and returns /// a wrapped version of it. pub fn map_config(factory: I, f: F) -> MapConfig where I: IntoServiceFactory, SF: ServiceFactory, F: Fn(Cfg) -> SF::Config, { MapConfig::new(factory.into_factory(), f) } /// Replace config with unit. pub fn unit_config(factory: I) -> UnitConfig where I: IntoServiceFactory, SF: ServiceFactory, { UnitConfig::new(factory.into_factory()) } /// `map_config()` adapter service factory pub struct MapConfig { factory: SF, cfg_mapper: F, e: PhantomData<(Cfg, Req)>, } impl MapConfig { /// Create new `MapConfig` combinator pub(crate) fn new(factory: SF, cfg_mapper: F) -> Self where SF: ServiceFactory, F: Fn(Cfg) -> SF::Config, { Self { factory, cfg_mapper, e: PhantomData, } } } impl Clone for MapConfig where SF: Clone, F: Clone, { fn clone(&self) -> Self { Self { factory: self.factory.clone(), cfg_mapper: self.cfg_mapper.clone(), e: PhantomData, } } } impl ServiceFactory for MapConfig where SF: ServiceFactory, F: Fn(Cfg) -> SF::Config, { type Response = SF::Response; type Error = SF::Error; type Config = Cfg; type Service = SF::Service; type InitError = SF::InitError; type Future = SF::Future; fn new_service(&self, cfg: Self::Config) -> Self::Future { let mapped_cfg = (self.cfg_mapper)(cfg); self.factory.new_service(mapped_cfg) } } /// `unit_config()` config combinator pub struct UnitConfig { factory: SF, _phantom: PhantomData<(Cfg, Req)>, } impl UnitConfig where SF: ServiceFactory, { /// Create new `UnitConfig` combinator pub(crate) fn new(factory: SF) -> Self { Self { factory, _phantom: PhantomData, } } } impl Clone for UnitConfig where SF: Clone, { fn clone(&self) -> Self { Self { factory: self.factory.clone(), _phantom: PhantomData, } } } impl ServiceFactory for UnitConfig where SF: ServiceFactory, { type Response = SF::Response; type Error = SF::Error; type Config = Cfg; type Service = SF::Service; type InitError = SF::InitError; type Future = SF::Future; fn new_service(&self, _: Cfg) -> Self::Future { self.factory.new_service(()) } }