use crate::service::{Service, ServiceFactory}; use futures_core::future::{BoxFuture}; pub fn factory(factory: SF) -> BoxServiceFactory where SF: ServiceFactory + 'static + Sync + Send, Req: 'static, SF::Response: 'static, SF::Service: 'static, SF::Future: 'static, SF::Error: 'static + Send + Sync, >::Service: Sync + Send, <>::Service as Service>::Future: Send + Sync, >::Future: Send + Sync, { BoxServiceFactory(Box::new(FactoryWrapper(factory))) } type Inner = Box< dyn ServiceFactory< Req, Context = Cfg, Response = Res, Error = Err, Service = BoxService, Future = BoxFuture<'static, Result, Err>>, > + Sync + Send, >; pub struct BoxServiceFactory(Inner); impl ServiceFactory for BoxServiceFactory where Req: 'static, Res: 'static, Err: 'static, { type Response = Res; type Error = Err; type Service = BoxService; type Context = Cfg; type Future = BoxFuture<'static, Result>; fn new_service(&self, cfg: Cfg) -> Self::Future { self.0.new_service(cfg) } } 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 + Send + Sync, { type Response = Res; type Error = Err; type Future = BoxFuture<'static, Result>; fn call(&self, req: Req) -> Self::Future { Box::pin(self.inner.call(req)) } } struct FactoryWrapper(SF); impl ServiceFactory for FactoryWrapper where Req: 'static, Res: 'static, Err: 'static, SF: ServiceFactory, SF::Future: 'static, SF::Service: 'static + Send + Sync, <>::Service as Service>::Future: Send + Sync + 'static, >::Future: Send + Sync, { type Response = Res; type Error = Err; type Service = BoxService; type Context = Cfg; type Future = BoxFuture<'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) }) } }