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