110 lines
2.9 KiB
Rust
Raw Normal View History

2021-06-27 15:11:41 +08:00
use crate::service::{Service, ServiceFactory};
2021-06-24 23:37:45 +08:00
use futures_core::future::LocalBoxFuture;
2021-06-25 23:53:13 +08:00
pub fn factory<SF, Req>(factory: SF) -> BoxServiceFactory<SF::Config, Req, SF::Response, SF::Error>
2021-06-24 23:37:45 +08:00
where
SF: ServiceFactory<Req> + 'static,
Req: 'static,
SF::Response: 'static,
SF::Service: 'static,
SF::Future: 'static,
SF::Error: 'static,
{
BoxServiceFactory(Box::new(FactoryWrapper(factory)))
}
2021-06-24 16:32:36 +08:00
2021-06-25 23:53:13 +08:00
pub struct BoxServiceFactory<Cfg, Req, Res, Err>(Inner<Cfg, Req, Res, Err>);
impl<Cfg, Req, Res, Err> ServiceFactory<Req> for BoxServiceFactory<Cfg, Req, Res, Err>
2021-06-24 16:32:36 +08:00
where
Req: 'static,
Res: 'static,
Err: 'static,
{
type Response = Res;
type Error = Err;
type Service = BoxService<Req, Res, Err>;
2021-06-25 23:53:13 +08:00
type Config = Cfg;
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::Error>>;
2021-06-24 16:32:36 +08:00
2021-06-25 23:53:13 +08:00
fn new_service(&self, cfg: Cfg) -> Self::Future { self.0.new_service(cfg) }
2021-06-24 16:32:36 +08:00
}
2021-06-25 23:53:13 +08:00
type Inner<Cfg, Req, Res, Err> = Box<
2021-06-24 23:37:45 +08:00
dyn ServiceFactory<
Req,
2021-06-25 23:53:13 +08:00
Config = Cfg,
2021-06-24 23:37:45 +08:00
Response = Res,
Error = Err,
Service = BoxService<Req, Res, Err>,
2021-06-25 23:53:13 +08:00
Future = LocalBoxFuture<'static, Result<BoxService<Req, Res, Err>, Err>>,
2021-06-24 23:37:45 +08:00
>,
>;
2021-06-24 16:32:36 +08:00
pub type BoxService<Req, Res, Err> =
2021-06-24 23:37:45 +08:00
Box<dyn Service<Req, Response = Res, Error = Err, Future = LocalBoxFuture<'static, Result<Res, Err>>>>;
2021-06-24 16:32:36 +08:00
2021-06-27 15:11:41 +08:00
#[allow(dead_code)]
2021-06-24 16:32:36 +08:00
pub fn service<S, Req>(service: S) -> BoxService<Req, S::Response, S::Error>
where
S: Service<Req> + 'static,
Req: 'static,
S::Future: 'static,
{
Box::new(ServiceWrapper::new(service))
}
impl<S, Req> Service<Req> for Box<S>
where
S: Service<Req> + ?Sized,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
2021-06-24 23:37:45 +08:00
fn call(&self, request: Req) -> S::Future { (**self).call(request) }
2021-06-24 16:32:36 +08:00
}
struct ServiceWrapper<S> {
inner: S,
}
impl<S> ServiceWrapper<S> {
2021-06-24 23:37:45 +08:00
fn new(inner: S) -> Self { Self { inner } }
2021-06-24 16:32:36 +08:00
}
impl<S, Req, Res, Err> Service<Req> for ServiceWrapper<S>
where
S: Service<Req, Response = Res, Error = Err>,
S::Future: 'static,
{
type Response = Res;
type Error = Err;
2021-06-24 23:37:45 +08:00
type Future = LocalBoxFuture<'static, Result<Res, Err>>;
2021-06-24 16:32:36 +08:00
2021-06-24 23:37:45 +08:00
fn call(&self, req: Req) -> Self::Future { Box::pin(self.inner.call(req)) }
2021-06-24 16:32:36 +08:00
}
struct FactoryWrapper<SF>(SF);
2021-06-25 23:53:13 +08:00
impl<SF, Req, Cfg, Res, Err> ServiceFactory<Req> for FactoryWrapper<SF>
2021-06-24 16:32:36 +08:00
where
Req: 'static,
Res: 'static,
Err: 'static,
2021-06-25 23:53:13 +08:00
SF: ServiceFactory<Req, Config = Cfg, Response = Res, Error = Err>,
2021-06-24 16:32:36 +08:00
SF::Future: 'static,
SF::Service: 'static,
<SF::Service as Service<Req>>::Future: 'static,
{
type Response = Res;
type Error = Err;
type Service = BoxService<Req, Res, Err>;
type Config = Cfg;
2021-06-25 23:53:13 +08:00
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::Error>>;
2021-06-24 16:32:36 +08:00
fn new_service(&self, cfg: Cfg) -> Self::Future {
let f = self.0.new_service(cfg);
2021-06-25 23:53:13 +08:00
Box::pin(async { f.await.map(|s| Box::new(ServiceWrapper::new(s)) as Self::Service) })
2021-06-24 16:32:36 +08:00
}
}