1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 15:03:00 +01:00
actix-net/actix-service/src/fn_service.rs

130 lines
3.0 KiB
Rust
Raw Normal View History

2018-08-25 18:02:14 +02:00
use std::marker;
use futures::{
future::{ok, FutureResult},
Async, IntoFuture, Poll,
};
2018-09-11 18:30:22 +02:00
use super::{IntoNewService, IntoService, NewService, Service};
2018-08-25 18:02:14 +02:00
pub struct FnService<F, Req, Resp, E, Fut>
where
2019-01-12 06:39:58 +01:00
F: FnMut(Req) -> Fut,
2018-08-25 18:02:14 +02:00
Fut: IntoFuture<Item = Resp, Error = E>,
{
f: F,
2018-09-07 03:03:01 +02:00
_t: marker::PhantomData<(Req, Resp, E)>,
2018-08-25 18:02:14 +02:00
}
impl<F, Req, Resp, E, Fut> FnService<F, Req, Resp, E, Fut>
where
2019-01-12 06:39:58 +01:00
F: FnMut(Req) -> Fut,
2018-08-25 18:02:14 +02:00
Fut: IntoFuture<Item = Resp, Error = E>,
{
pub fn new(f: F) -> Self {
FnService {
f,
2018-09-07 03:03:01 +02:00
_t: marker::PhantomData,
2018-08-25 18:02:14 +02:00
}
}
}
2018-08-28 05:32:49 +02:00
impl<F, Req, Resp, E, Fut> Clone for FnService<F, Req, Resp, E, Fut>
where
2019-01-12 06:39:58 +01:00
F: FnMut(Req) -> Fut + Clone,
2018-08-28 05:32:49 +02:00
Fut: IntoFuture<Item = Resp, Error = E>,
{
fn clone(&self) -> Self {
FnService {
f: self.f.clone(),
2018-09-07 03:03:01 +02:00
_t: marker::PhantomData,
2018-08-28 05:32:49 +02:00
}
}
}
2018-11-30 03:56:15 +01:00
impl<F, Req, Resp, E, Fut> Service<Req> for FnService<F, Req, Resp, E, Fut>
2018-08-25 18:02:14 +02:00
where
2019-01-12 06:39:58 +01:00
F: FnMut(Req) -> Fut,
2018-08-25 18:02:14 +02:00
Fut: IntoFuture<Item = Resp, Error = E>,
{
type Response = Resp;
type Error = E;
type Future = Fut::Future;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, req: Req) -> Self::Future {
(self.f)(req).into_future()
}
}
2018-11-30 03:56:15 +01:00
impl<F, Req, Resp, Err, Fut> IntoService<FnService<F, Req, Resp, Err, Fut>, Req> for F
2018-09-04 18:57:47 +02:00
where
2019-01-12 06:39:58 +01:00
F: FnMut(Req) -> Fut + 'static,
2018-09-04 18:57:47 +02:00
Fut: IntoFuture<Item = Resp, Error = Err>,
{
fn into_service(self) -> FnService<F, Req, Resp, Err, Fut> {
FnService::new(self)
}
}
2018-10-03 06:31:11 +02:00
pub struct FnNewService<F, Req, Resp, Err, Fut>
2018-08-25 18:02:14 +02:00
where
2019-01-12 06:39:58 +01:00
F: FnMut(Req) -> Fut,
2018-08-25 18:02:14 +02:00
Fut: IntoFuture<Item = Resp, Error = Err>,
{
f: F,
2018-10-03 06:31:11 +02:00
_t: marker::PhantomData<(Req, Resp, Err)>,
2018-08-25 18:02:14 +02:00
}
2018-10-03 06:31:11 +02:00
impl<F, Req, Resp, Err, Fut> FnNewService<F, Req, Resp, Err, Fut>
2018-08-25 18:02:14 +02:00
where
2019-01-12 06:39:58 +01:00
F: FnMut(Req) -> Fut + Clone,
2018-08-25 18:02:14 +02:00
Fut: IntoFuture<Item = Resp, Error = Err>,
{
2018-08-29 05:32:01 +02:00
pub fn new(f: F) -> Self {
2018-08-25 18:02:14 +02:00
FnNewService {
f,
2018-09-07 03:03:01 +02:00
_t: marker::PhantomData,
2018-08-25 18:02:14 +02:00
}
}
}
2018-11-30 03:56:15 +01:00
impl<F, Req, Resp, Err, Fut> NewService<Req> for FnNewService<F, Req, Resp, Err, Fut>
2018-08-25 18:02:14 +02:00
where
2019-01-12 06:39:58 +01:00
F: FnMut(Req) -> Fut + Clone,
2018-08-25 18:02:14 +02:00
Fut: IntoFuture<Item = Resp, Error = Err>,
{
type Response = Resp;
type Error = Err;
type Service = FnService<F, Req, Resp, Err, Fut>;
2018-10-03 06:31:11 +02:00
type InitError = ();
2018-08-25 18:02:14 +02:00
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
ok(FnService::new(self.f.clone()))
}
}
2018-11-30 03:56:15 +01:00
impl<F, Req, Resp, Err, Fut> IntoNewService<FnNewService<F, Req, Resp, Err, Fut>, Req> for F
2018-08-25 18:02:14 +02:00
where
2019-01-12 06:39:58 +01:00
F: FnMut(Req) -> Fut + Clone + 'static,
2018-08-25 18:02:14 +02:00
Fut: IntoFuture<Item = Resp, Error = Err>,
{
2018-10-03 06:31:11 +02:00
fn into_new_service(self) -> FnNewService<F, Req, Resp, Err, Fut> {
FnNewService::new(self)
2018-08-25 18:02:14 +02:00
}
}
2018-10-03 06:31:11 +02:00
impl<F, Req, Resp, Err, Fut> Clone for FnNewService<F, Req, Resp, Err, Fut>
2018-08-25 18:02:14 +02:00
where
2019-01-12 06:39:58 +01:00
F: FnMut(Req) -> Fut + Clone,
2018-08-25 18:02:14 +02:00
Fut: IntoFuture<Item = Resp, Error = Err>,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
}
}