use crate::{NewService, Service}; use futures::{Future, Poll}; pub type BoxedService = Box< Service< Request = Req, Response = Res, Error = Err, Future = Box>, >, >; /// Create boxed new service pub fn new_service( service: T, ) -> BoxedNewService where C: 'static, T: NewService + 'static, T::Request: 'static, T::Response: 'static, T::Service: 'static, T::Future: 'static, T::Error: 'static, T::InitError: 'static, { BoxedNewService(Box::new(NewServiceWrapper { service, _t: std::marker::PhantomData, })) } /// Create boxed service pub fn service(service: T) -> BoxedService where T: Service + 'static, T::Future: 'static, { Box::new(ServiceWrapper(service)) } type Inner = Box< NewService< C, Request = Req, Response = Res, Error = Err, InitError = InitErr, Service = BoxedService, Future = Box, Error = InitErr>>, >, >; pub struct BoxedNewService(Inner); impl NewService for BoxedNewService where Req: 'static, Res: 'static, Err: 'static, InitErr: 'static, { type Request = Req; type Response = Res; type Error = Err; type InitError = InitErr; type Service = BoxedService; type Future = Box>; fn new_service(&self, cfg: &C) -> Self::Future { self.0.new_service(cfg) } } struct NewServiceWrapper> { service: T, _t: std::marker::PhantomData, } impl NewService for NewServiceWrapper where Req: 'static, Res: 'static, Err: 'static, InitErr: 'static, T: NewService, T::Future: 'static, T::Service: 'static, ::Future: 'static, { type Request = Req; type Response = Res; type Error = Err; type InitError = InitErr; type Service = BoxedService; type Future = Box>; fn new_service(&self, cfg: &C) -> Self::Future { Box::new( self.service .new_service(cfg) .map(|service| ServiceWrapper::boxed(service)), ) } } struct ServiceWrapper(T); impl ServiceWrapper where T: Service + 'static, T::Future: 'static, { fn boxed(service: T) -> BoxedService { Box::new(ServiceWrapper(service)) } } impl Service for ServiceWrapper where T: Service, T::Future: 'static, { type Request = Req; type Response = Res; type Error = Err; type Future = Box>; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.0.poll_ready() } fn call(&mut self, req: Self::Request) -> Self::Future { Box::new(self.0.call(req)) } }