1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-18 13:33:11 +01:00
actix-net/actix-service/src/boxed.rs

152 lines
3.9 KiB
Rust
Raw Normal View History

use futures::future::{err, ok, Either, FutureResult};
use futures::{Async, Future, IntoFuture, Poll};
use crate::{NewService, Service};
2019-02-21 19:41:39 +01:00
pub type BoxedService<Req, Res, Err> = Box<
2019-03-09 15:36:23 +01:00
Service<
Request = Req,
Response = Res,
Error = Err,
2019-04-20 02:43:52 +02:00
Future = BoxedServiceResponse<Res, Err>,
2019-03-09 15:36:23 +01:00
>,
2019-02-21 19:41:39 +01:00
>;
2019-04-20 02:43:52 +02:00
pub type BoxedServiceResponse<Res, Err> =
Either<FutureResult<Res, Err>, Box<Future<Item = Res, Error = Err>>>;
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
2019-02-21 19:41:39 +01:00
/// Create boxed new service
pub fn new_service<T>(
2019-02-21 19:41:39 +01:00
service: T,
) -> BoxedNewService<T::Config, T::Request, T::Response, T::Error, T::InitError>
2019-02-21 19:41:39 +01:00
where
T: NewService + 'static,
2019-03-09 15:36:23 +01:00
T::Request: 'static,
2019-02-22 21:44:37 +01:00
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,
2019-03-09 15:36:23 +01:00
_t: std::marker::PhantomData,
2019-02-22 21:44:37 +01:00
}))
2019-02-21 19:41:39 +01:00
}
/// Create boxed service
2019-03-09 15:36:23 +01:00
pub fn service<T>(service: T) -> BoxedService<T::Request, T::Response, T::Error>
2019-02-21 19:41:39 +01:00
where
2019-03-09 15:36:23 +01:00
T: Service + 'static,
2019-02-21 19:41:39 +01:00
T::Future: 'static,
{
2019-03-09 15:36:23 +01:00
Box::new(ServiceWrapper(service))
2019-02-21 19:41:39 +01:00
}
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<
Config = C,
2019-03-09 15:36:23 +01:00
Request = Req,
2019-02-21 20:19:16 +01:00
Response = Res,
Error = Err,
InitError = InitErr,
Service = BoxedService<Req, Res, Err>,
Future = Box<Future<Item = BoxedService<Req, Res, Err>, Error = InitErr>>,
>,
>;
impl<C, Req, Res, Err, InitErr> NewService for BoxedNewService<C, Req, Res, Err, InitErr>
2019-02-21 20:19:16 +01:00
where
Req: 'static,
Res: 'static,
Err: 'static,
InitErr: 'static,
{
2019-03-09 15:36:23 +01:00
type Request = Req;
2019-02-21 20:19:16 +01:00
type Response = Res;
type Error = Err;
type InitError = InitErr;
type Config = C;
2019-02-21 20:19:16 +01:00
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
}
}
struct NewServiceWrapper<C, T: NewService> {
2019-02-22 21:44:37 +01:00
service: T,
2019-03-09 15:36:23 +01:00
_t: std::marker::PhantomData<C>,
2019-02-22 21:44:37 +01:00
}
2019-02-21 19:41:39 +01:00
impl<C, T, Req, Res, Err, InitErr> NewService for NewServiceWrapper<C, T>
2019-02-21 19:41:39 +01:00
where
Req: 'static,
Res: 'static,
Err: 'static,
InitErr: 'static,
T: NewService<Config = C, Request = Req, Response = Res, Error = Err, InitError = InitErr>,
2019-02-21 19:41:39 +01:00
T::Future: 'static,
T::Service: 'static,
2019-03-09 15:36:23 +01:00
<T::Service as Service>::Future: 'static,
2019-02-21 19:41:39 +01:00
{
2019-03-09 15:36:23 +01:00
type Request = Req;
2019-02-21 19:41:39 +01:00
type Response = Res;
type Error = Err;
type InitError = InitErr;
type Config = C;
2019-02-21 19:41:39 +01:00
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 05:29:35 +01:00
Box::new(
self.service
.new_service(cfg)
.into_future()
.map(ServiceWrapper::boxed),
)
2019-02-21 19:41:39 +01:00
}
}
2019-03-09 15:36:23 +01:00
struct ServiceWrapper<T: Service>(T);
2019-02-21 19:41:39 +01:00
2019-03-09 15:36:23 +01:00
impl<T> ServiceWrapper<T>
2019-02-21 19:41:39 +01:00
where
2019-03-09 15:36:23 +01:00
T: Service + 'static,
2019-02-21 19:41:39 +01:00
T::Future: 'static,
{
2019-03-09 15:36:23 +01:00
fn boxed(service: T) -> BoxedService<T::Request, T::Response, T::Error> {
Box::new(ServiceWrapper(service))
2019-02-21 19:41:39 +01:00
}
}
2019-03-09 15:36:23 +01:00
impl<T, Req, Res, Err> Service for ServiceWrapper<T>
2019-02-21 19:41:39 +01:00
where
2019-03-09 15:36:23 +01:00
T: Service<Request = Req, Response = Res, Error = Err>,
2019-02-21 19:41:39 +01:00
T::Future: 'static,
{
2019-03-09 15:36:23 +01:00
type Request = Req;
2019-02-21 19:41:39 +01:00
type Response = Res;
type Error = Err;
type Future = Either<
FutureResult<Self::Response, Self::Error>,
Box<Future<Item = Self::Response, Error = Self::Error>>,
>;
2019-02-21 19:41:39 +01:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
2019-03-09 15:36:23 +01:00
self.0.poll_ready()
2019-02-21 19:41:39 +01:00
}
2019-03-09 15:36:23 +01:00
fn call(&mut self, req: Self::Request) -> Self::Future {
let mut fut = self.0.call(req);
match fut.poll() {
Ok(Async::Ready(res)) => Either::A(ok(res)),
Err(e) => Either::A(err(e)),
Ok(Async::NotReady) => Either::B(Box::new(fut)),
}
2019-02-21 19:41:39 +01:00
}
}