Implement DirectService for ServiceFn (#127)

This commit is contained in:
Jon Gjengset 2018-11-27 14:02:49 -05:00 committed by Carl Lerche
parent 9bae225918
commit 72508ff4ba
3 changed files with 28 additions and 0 deletions

View File

@ -7,3 +7,4 @@ publish = false
[dependencies]
futures = "0.1"
tower-service = { version = "0.1", path = "../tower-service" }
tower-direct-service = { version = "0.1", path = "../tower-direct-service" }

View File

@ -2,6 +2,7 @@
#[macro_use]
extern crate futures;
extern crate tower_direct_service;
extern crate tower_service;
pub mod boxed;

View File

@ -1,4 +1,5 @@
use futures::{IntoFuture, Poll};
use tower_direct_service::DirectService;
use tower_service::Service;
/// A `Service` implemented by a closure.
@ -31,3 +32,28 @@ where T: Fn(Request) -> F,
(self.f)(req).into_future()
}
}
impl<T, F, Request> DirectService<Request> for ServiceFn<T>
where T: Fn(Request) -> F,
F: IntoFuture,
{
type Response = F::Item;
type Error = F::Error;
type Future = F::Future;
fn poll_ready(&mut self) -> Poll<(), F::Error> {
Ok(().into())
}
fn poll_service(&mut self) -> Poll<(), F::Error> {
Ok(().into())
}
fn poll_close(&mut self) -> Poll<(), F::Error> {
Ok(().into())
}
fn call(&mut self, req: Request) -> Self::Future {
(self.f)(req).into_future()
}
}