use std::marker; use futures::{ future::{ok, FutureResult}, Async, IntoFuture, Poll, }; use tower_service::{NewService, Service}; use super::IntoNewService; pub struct FnService where F: Fn(Req) -> Fut, Fut: IntoFuture, { f: F, req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, } impl FnService where F: Fn(Req) -> Fut, Fut: IntoFuture, { pub fn new(f: F) -> Self { FnService { f, req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, } } } impl Service for FnService where F: Fn(Req) -> Fut, Fut: IntoFuture, { 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 where F: Fn(Req) -> Fut, Fut: IntoFuture, { f: F, req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, ierr: marker::PhantomData, } impl FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, { fn new(f: F) -> Self { FnNewService { f, req: marker::PhantomData, resp: marker::PhantomData, err: marker::PhantomData, ierr: marker::PhantomData, } } } impl NewService for FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, { type Request = Req; type Response = Resp; type Error = Err; type Service = FnService; type InitError = IErr; type Future = FutureResult; fn new_service(&self) -> Self::Future { ok(FnService::new(self.f.clone())) } } impl IntoNewService> for F where F: Fn(Req) -> Fut + Clone + 'static, Fut: IntoFuture, { fn into_new_service(self) -> FnNewService { FnNewService::new(self) } } impl Clone for FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, { fn clone(&self) -> Self { Self::new(self.f.clone()) } }