2019-02-23 03:20:54 +01:00
|
|
|
use crate::{NewService, Service};
|
2019-02-21 19:41:39 +01:00
|
|
|
use futures::{Future, Poll};
|
|
|
|
|
|
|
|
pub type BoxedService<Req, Res, Err> = Box<
|
|
|
|
Service<
|
|
|
|
Request = Req,
|
|
|
|
Response = Res,
|
|
|
|
Error = Err,
|
|
|
|
Future = Box<Future<Item = Res, Error = Err>>,
|
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
|
|
|
/// Create boxed new service
|
2019-02-22 21:44:37 +01:00
|
|
|
pub fn new_service<T, C>(
|
2019-02-21 19:41:39 +01:00
|
|
|
service: T,
|
2019-02-22 21:44:37 +01:00
|
|
|
) -> BoxedNewService<C, T::Request, T::Response, T::Error, T::InitError>
|
2019-02-21 19:41:39 +01:00
|
|
|
where
|
2019-02-22 21:44:37 +01:00
|
|
|
C: 'static,
|
|
|
|
T: NewService<C> + 'static,
|
|
|
|
T::Request: 'static,
|
|
|
|
T::Response: 'static,
|
2019-02-21 19:41:39 +01:00
|
|
|
T::Service: 'static,
|
2019-02-22 21:44:37 +01:00
|
|
|
T::Future: 'static,
|
|
|
|
T::Error: 'static,
|
|
|
|
T::InitError: 'static,
|
2019-02-21 19:41:39 +01:00
|
|
|
{
|
2019-02-22 21:44:37 +01:00
|
|
|
BoxedNewService(Box::new(NewServiceWrapper {
|
|
|
|
service,
|
|
|
|
_t: std::marker::PhantomData,
|
|
|
|
}))
|
2019-02-21 19:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create boxed service
|
|
|
|
pub fn service<T>(service: T) -> BoxedService<T::Request, T::Response, T::Error>
|
|
|
|
where
|
|
|
|
T: Service + 'static,
|
|
|
|
T::Future: 'static,
|
|
|
|
{
|
|
|
|
Box::new(ServiceWrapper(service))
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
type Inner<C, Req, Res, Err, InitErr> = Box<
|
2019-02-21 20:19:16 +01:00
|
|
|
NewService<
|
2019-02-22 21:44:37 +01:00
|
|
|
C,
|
2019-02-21 20:19:16 +01:00
|
|
|
Request = Req,
|
|
|
|
Response = Res,
|
|
|
|
Error = Err,
|
|
|
|
InitError = InitErr,
|
|
|
|
Service = BoxedService<Req, Res, Err>,
|
|
|
|
Future = Box<Future<Item = BoxedService<Req, Res, Err>, Error = InitErr>>,
|
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
|
2019-02-21 20:19:16 +01:00
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
impl<C, Req, Res, Err, InitErr> NewService<C> for BoxedNewService<C, Req, Res, Err, InitErr>
|
2019-02-21 20:19:16 +01:00
|
|
|
where
|
|
|
|
Req: 'static,
|
|
|
|
Res: 'static,
|
|
|
|
Err: 'static,
|
|
|
|
InitErr: 'static,
|
|
|
|
{
|
|
|
|
type Request = Req;
|
|
|
|
type Response = Res;
|
|
|
|
type Error = Err;
|
|
|
|
type InitError = InitErr;
|
|
|
|
type Service = BoxedService<Req, Res, Err>;
|
|
|
|
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
|
|
|
self.0.new_service(cfg)
|
2019-02-21 20:19:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
struct NewServiceWrapper<C, T: NewService<C>> {
|
|
|
|
service: T,
|
|
|
|
_t: std::marker::PhantomData<C>,
|
|
|
|
}
|
2019-02-21 19:41:39 +01:00
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
impl<C, T, Req, Res, Err, InitErr> NewService<C> for NewServiceWrapper<C, T>
|
2019-02-21 19:41:39 +01:00
|
|
|
where
|
|
|
|
Req: 'static,
|
|
|
|
Res: 'static,
|
|
|
|
Err: 'static,
|
|
|
|
InitErr: 'static,
|
2019-02-22 21:44:37 +01:00
|
|
|
T: NewService<C, Request = Req, Response = Res, Error = Err, InitError = InitErr>,
|
2019-02-21 19:41:39 +01:00
|
|
|
T::Future: 'static,
|
|
|
|
T::Service: 'static,
|
|
|
|
<T::Service as Service>::Future: 'static,
|
|
|
|
{
|
|
|
|
type Request = Req;
|
|
|
|
type Response = Res;
|
|
|
|
type Error = Err;
|
|
|
|
type InitError = InitErr;
|
|
|
|
type Service = BoxedService<Req, Res, Err>;
|
|
|
|
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
2019-03-05 04:38:11 +01:00
|
|
|
Box::new(self.service.new_service(cfg).map(ServiceWrapper::boxed))
|
2019-02-21 19:41:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ServiceWrapper<T: Service>(T);
|
|
|
|
|
|
|
|
impl<T> ServiceWrapper<T>
|
|
|
|
where
|
|
|
|
T: Service + 'static,
|
|
|
|
T::Future: 'static,
|
|
|
|
{
|
|
|
|
fn boxed(service: T) -> BoxedService<T::Request, T::Response, T::Error> {
|
|
|
|
Box::new(ServiceWrapper(service))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, Req, Res, Err> Service for ServiceWrapper<T>
|
|
|
|
where
|
|
|
|
T: Service<Request = Req, Response = Res, Error = Err>,
|
|
|
|
T::Future: 'static,
|
|
|
|
{
|
|
|
|
type Request = Req;
|
|
|
|
type Response = Res;
|
|
|
|
type Error = Err;
|
|
|
|
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|