use std::marker; use futures::{ future::{ok, FutureResult}, Async, IntoFuture, Poll, }; use tower_service::{NewService, Service}; use super::{IntoNewService, IntoService}; pub struct FnService where F: Fn(Req) -> Fut, Fut: IntoFuture, { f: F, _t: marker::PhantomData<(Req, Resp, E)>, } impl FnService where F: Fn(Req) -> Fut, Fut: IntoFuture, { pub fn new(f: F) -> Self { FnService { f, _t: marker::PhantomData, } } } impl Clone for FnService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, { fn clone(&self) -> Self { FnService { f: self.f.clone(), _t: 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() } } impl IntoService> for F where F: Fn(Req) -> Fut + 'static, Fut: IntoFuture, { fn into_service(self) -> FnService { FnService::new(self) } } pub struct FnNewService where F: Fn(Req) -> Fut, Fut: IntoFuture, { f: F, _t: marker::PhantomData<(Req, Resp, Err, IErr)>, } impl FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, { pub fn new(f: F) -> Self { FnNewService { f, _t: 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()) } }