use std::marker; use futures::{ future::{ok, FutureResult}, Async, IntoFuture, Poll, }; use super::{IntoNewService, IntoService, NewService, Service}; 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 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, Req> 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)>, } 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 Response = Resp; type Error = Err; type Service = FnService; type InitError = (); type Future = FutureResult; fn new_service(&self) -> Self::Future { ok(FnService::new(self.f.clone())) } } impl IntoNewService, Req> 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()) } }