use std::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: U, f: F) -> MapConfig where T: ServiceFactory, U: IntoServiceFactory, F: Fn(C) -> T::Config, { MapConfig::new(factory.into_factory(), f) } /// Replace config with unit pub fn unit_config(factory: U) -> UnitConfig where T: ServiceFactory, U: IntoServiceFactory, { UnitConfig::new(factory.into_factory()) } /// `map_config()` adapter service factory pub struct MapConfig { a: A, f: F, e: PhantomData, } impl MapConfig { /// Create new `MapConfig` combinator pub(crate) fn new(a: A, f: F) -> Self where A: ServiceFactory, F: Fn(C) -> A::Config, { Self { a, f, e: PhantomData, } } } impl Clone for MapConfig where A: Clone, F: Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), f: self.f.clone(), e: PhantomData, } } } impl ServiceFactory for MapConfig where A: ServiceFactory, F: Fn(C) -> A::Config, { type Request = A::Request; type Response = A::Response; type Error = A::Error; type Config = C; type Service = A::Service; type InitError = A::InitError; type Future = A::Future; fn new_service(&self, cfg: C) -> Self::Future { self.a.new_service((self.f)(cfg)) } } /// `unit_config()` config combinator pub struct UnitConfig { a: A, e: PhantomData, } impl UnitConfig where A: ServiceFactory, { /// Create new `UnitConfig` combinator pub(crate) fn new(a: A) -> Self { Self { a, e: PhantomData } } } impl Clone for UnitConfig where A: Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), e: PhantomData, } } } impl ServiceFactory for UnitConfig where A: ServiceFactory, { type Request = A::Request; type Response = A::Response; type Error = A::Error; type Config = C; type Service = A::Service; type InitError = A::InitError; type Future = A::Future; fn new_service(&self, _: C) -> Self::Future { self.a.new_service(()) } }