use std::marker::PhantomData; use super::NewService; pub enum MappedConfig<'a, T> { Ref(&'a T), Owned(T), } /// `MapInitErr` service combinator pub struct MapConfig { a: A, f: F, e: PhantomData, } impl MapConfig { /// Create new `MapConfig` combinator pub fn new(a: A, f: F) -> Self where A: NewService, F: Fn(&C) -> MappedConfig, { 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 NewService for MapConfig where A: NewService, F: Fn(&C) -> MappedConfig, { 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 { match (self.f)(cfg) { MappedConfig::Ref(cfg) => self.a.new_service(cfg), MappedConfig::Owned(cfg) => self.a.new_service(&cfg), } } } /// `MapInitErr` service combinator pub struct UnitConfig { a: A, e: PhantomData, } impl UnitConfig { /// Create new `UnitConfig` combinator pub fn new(a: A) -> Self where A: NewService, { Self { a, e: PhantomData } } } impl Clone for UnitConfig where A: Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), e: PhantomData, } } } impl NewService for UnitConfig where A: NewService, { 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(&()) } }