use crate::prelude::{AFBoxFuture, AFConcurrent}; use crate::service::{AFPluginServiceFactory, Service}; pub fn factory(factory: SF) -> BoxServiceFactory where SF: AFPluginServiceFactory + 'static + AFConcurrent, Req: 'static, SF::Response: 'static, SF::Service: 'static, SF::Future: 'static, SF::Error: 'static, >::Service: AFConcurrent, <>::Service as Service>::Future: AFConcurrent, >::Future: AFConcurrent, { BoxServiceFactory(Box::new(FactoryWrapper(factory))) } #[cfg(feature = "single_thread")] type Inner = Box< dyn AFPluginServiceFactory< Req, Context = Cfg, Response = Res, Error = Err, Service = BoxService, Future = AFBoxFuture<'static, Result, Err>>, >, >; #[cfg(not(feature = "single_thread"))] type Inner = Box< dyn AFPluginServiceFactory< Req, Context = Cfg, Response = Res, Error = Err, Service = BoxService, Future = AFBoxFuture<'static, Result, Err>>, > + Send + Sync, >; pub struct BoxServiceFactory(Inner); impl AFPluginServiceFactory for BoxServiceFactory where Req: 'static, Res: 'static, Err: 'static, { type Response = Res; type Error = Err; type Service = BoxService; type Context = Cfg; type Future = AFBoxFuture<'static, Result>; fn new_service(&self, cfg: Cfg) -> Self::Future { self.0.new_service(cfg) } } #[cfg(feature = "single_thread")] pub type BoxService = Box< dyn Service>>, >; #[cfg(not(feature = "single_thread"))] pub type BoxService = Box< dyn Service>> + Sync + Send, >; // #[allow(dead_code)] // pub fn service(service: S) -> BoxService // where // S: Service + 'static, // Req: 'static, // S::Future: 'static, // { // Box::new(ServiceWrapper::new(service)) // } impl Service for Box where S: Service + ?Sized, { type Response = S::Response; type Error = S::Error; type Future = S::Future; fn call(&self, request: Req) -> S::Future { (**self).call(request) } } struct ServiceWrapper { inner: S, } impl ServiceWrapper { fn new(inner: S) -> Self { Self { inner } } } impl Service for ServiceWrapper where S: Service, S::Future: 'static + AFConcurrent, { type Response = Res; type Error = Err; type Future = AFBoxFuture<'static, Result>; fn call(&self, req: Req) -> Self::Future { Box::pin(self.inner.call(req)) } } struct FactoryWrapper(SF); impl AFPluginServiceFactory for FactoryWrapper where Req: 'static, Res: 'static, Err: 'static, SF: AFPluginServiceFactory, SF::Future: 'static, SF::Service: 'static + AFConcurrent, <>::Service as Service>::Future: AFConcurrent + 'static, >::Future: AFConcurrent, { type Response = Res; type Error = Err; type Service = BoxService; type Context = Cfg; type Future = AFBoxFuture<'static, Result>; fn new_service(&self, cfg: Cfg) -> Self::Future { let f = self.0.new_service(cfg); Box::pin(async { f.await .map(|s| Box::new(ServiceWrapper::new(s)) as Self::Service) }) } }