2018-08-25 18:02:14 +02:00
|
|
|
use std::marker;
|
|
|
|
|
|
|
|
use futures::{
|
|
|
|
future::{ok, FutureResult},
|
|
|
|
Async, IntoFuture, Poll,
|
|
|
|
};
|
|
|
|
use tower_service::{NewService, Service};
|
|
|
|
|
2018-09-04 18:49:21 +02:00
|
|
|
use super::IntoNewService;
|
|
|
|
|
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,
|
|
|
|
req: marker::PhantomData<Req>,
|
|
|
|
resp: marker::PhantomData<Resp>,
|
|
|
|
err: marker::PhantomData<E>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
req: marker::PhantomData,
|
|
|
|
resp: marker::PhantomData,
|
|
|
|
err: marker::PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
req: marker::PhantomData,
|
|
|
|
resp: marker::PhantomData,
|
|
|
|
err: marker::PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FnNewService<F, Req, Resp, Err, IErr, Fut>
|
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut,
|
|
|
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
f: F,
|
|
|
|
req: marker::PhantomData<Req>,
|
|
|
|
resp: marker::PhantomData<Resp>,
|
|
|
|
err: marker::PhantomData<Err>,
|
|
|
|
ierr: marker::PhantomData<IErr>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
req: marker::PhantomData,
|
|
|
|
resp: marker::PhantomData,
|
|
|
|
err: marker::PhantomData,
|
|
|
|
ierr: marker::PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|