1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 17:52:40 +01:00
actix-extras/actix-framed/src/helpers.rs

91 lines
2.4 KiB
Rust
Raw Normal View History

2019-04-11 00:06:27 +02:00
use actix_http::Error;
use actix_service::{NewService, Service};
use futures::{Future, Poll};
pub(crate) type BoxedHttpService<Req> = Box<
2019-07-17 11:48:37 +02:00
dyn Service<
2019-04-11 00:06:27 +02:00
Request = Req,
Response = (),
Error = Error,
Future = Box<dyn Future<Item = (), Error = Error>>,
2019-04-11 00:06:27 +02:00
>,
>;
pub(crate) type BoxedHttpNewService<Req> = Box<
2019-07-17 11:48:37 +02:00
dyn NewService<
2019-05-12 17:34:51 +02:00
Config = (),
2019-04-11 00:06:27 +02:00
Request = Req,
Response = (),
Error = Error,
InitError = (),
Service = BoxedHttpService<Req>,
Future = Box<dyn Future<Item = BoxedHttpService<Req>, Error = ()>>,
2019-04-11 00:06:27 +02:00
>,
>;
pub(crate) struct HttpNewService<T: NewService>(T);
impl<T> HttpNewService<T>
where
T: NewService<Response = (), Error = Error>,
T::Response: 'static,
T::Future: 'static,
T::Service: Service<Future = Box<dyn Future<Item = (), Error = Error>>> + 'static,
2019-04-11 00:06:27 +02:00
<T::Service as Service>::Future: 'static,
{
pub fn new(service: T) -> Self {
HttpNewService(service)
}
}
impl<T> NewService for HttpNewService<T>
where
2019-05-12 17:34:51 +02:00
T: NewService<Config = (), Response = (), Error = Error>,
2019-04-11 00:06:27 +02:00
T::Request: 'static,
T::Future: 'static,
T::Service: Service<Future = Box<dyn Future<Item = (), Error = Error>>> + 'static,
2019-04-11 00:06:27 +02:00
<T::Service as Service>::Future: 'static,
{
2019-05-12 17:34:51 +02:00
type Config = ();
2019-04-11 00:06:27 +02:00
type Request = T::Request;
type Response = ();
type Error = Error;
type InitError = ();
type Service = BoxedHttpService<T::Request>;
type Future = Box<dyn Future<Item = Self::Service, Error = ()>>;
2019-04-11 00:06:27 +02:00
fn new_service(&self, _: &()) -> Self::Future {
Box::new(self.0.new_service(&()).map_err(|_| ()).and_then(|service| {
let service: BoxedHttpService<_> = Box::new(HttpServiceWrapper { service });
Ok(service)
}))
}
}
struct HttpServiceWrapper<T: Service> {
service: T,
}
impl<T> Service for HttpServiceWrapper<T>
where
T: Service<
Response = (),
Future = Box<dyn Future<Item = (), Error = Error>>,
2019-04-11 00:06:27 +02:00
Error = Error,
>,
T::Request: 'static,
{
type Request = T::Request;
type Response = ();
type Error = Error;
type Future = Box<dyn Future<Item = (), Error = Error>>;
2019-04-11 00:06:27 +02:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready()
}
fn call(&mut self, req: Self::Request) -> Self::Future {
self.service.call(req)
}
}