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-09-04 18:49:21 +02:00
|
|
|
|
2018-08-25 18:02:14 +02:00
|
|
|
pub struct FnService<F, Req, Resp, E, Fut>
|
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut,
|
|
|
|
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
|
|
|
|
F: Fn(Req) -> Fut,
|
|
|
|
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
|
|
|
|
F: Fn(Req) -> Fut + Clone,
|
|
|
|
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-08-25 18:02:14 +02:00
|
|
|
impl<F, Req, Resp, E, Fut> Service for FnService<F, Req, Resp, E, Fut>
|
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut,
|
|
|
|
Fut: IntoFuture<Item = Resp, Error = E>,
|
|
|
|
{
|
|
|
|
type Request = Req;
|
|
|
|
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-09-04 18:57:47 +02:00
|
|
|
impl<F, Req, Resp, Err, Fut> IntoService<FnService<F, Req, Resp, Err, Fut>> for F
|
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut + 'static,
|
|
|
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
fn into_service(self) -> FnService<F, Req, Resp, Err, Fut> {
|
|
|
|
FnService::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 18:02:14 +02:00
|
|
|
pub struct FnNewService<F, Req, Resp, Err, IErr, Fut>
|
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut,
|
|
|
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
f: F,
|
2018-09-07 03:03:01 +02:00
|
|
|
_t: marker::PhantomData<(Req, Resp, Err, IErr)>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, Req, Resp, Err, IErr, Fut> FnNewService<F, Req, Resp, Err, IErr, Fut>
|
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut + Clone,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, Req, Resp, Err, IErr, Fut> NewService for FnNewService<F, Req, Resp, Err, IErr, Fut>
|
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut + Clone,
|
|
|
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
type Request = Req;
|
|
|
|
type Response = Resp;
|
|
|
|
type Error = Err;
|
|
|
|
type Service = FnService<F, Req, Resp, Err, Fut>;
|
|
|
|
type InitError = IErr;
|
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
ok(FnService::new(self.f.clone()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:49:21 +02:00
|
|
|
impl<F, Req, Resp, Err, IErr, Fut> IntoNewService<FnNewService<F, Req, Resp, Err, IErr, Fut>>
|
|
|
|
for F
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut + Clone + 'static,
|
|
|
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
|
|
|
{
|
2018-09-04 18:49:21 +02:00
|
|
|
fn into_new_service(self) -> FnNewService<F, Req, Resp, Err, IErr, Fut> {
|
|
|
|
FnNewService::new(self)
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, Req, Resp, Err, IErr, Fut> Clone for FnNewService<F, Req, Resp, Err, IErr, Fut>
|
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut + Clone,
|
|
|
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self::new(self.f.clone())
|
|
|
|
}
|
|
|
|
}
|