use std::marker::PhantomData; use futures::future::{ok, FutureResult}; use futures::{Async, IntoFuture, Poll}; use super::{IntoNewService, IntoService, NewService, Service}; pub struct FnService where F: FnMut(Req) -> Out, Out: IntoFuture, { f: F, _t: PhantomData<(Req,)>, } impl FnService where F: FnMut(Req) -> Out, Out: IntoFuture, { pub fn new(f: F) -> Self { FnService { f, _t: PhantomData } } } impl Clone for FnService where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, { fn clone(&self) -> Self { FnService { f: self.f.clone(), _t: PhantomData, } } } impl Service for FnService where F: FnMut(Req) -> Out, Out: IntoFuture, { type Request = Req; type Response = Out::Item; type Error = Out::Error; type Future = Out::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: FnMut(Req) -> Out + 'static, Out: IntoFuture, { fn into_service(self) -> FnService { FnService::new(self) } } pub struct FnNewService where F: FnMut(Req) -> Out, Out: IntoFuture, { f: F, _t: PhantomData<(Req, Cfg)>, } impl FnNewService where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, { pub fn new(f: F) -> Self { FnNewService { f, _t: PhantomData } } } impl NewService for FnNewService where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, { type Request = Req; type Response = Out::Item; type Error = Out::Error; type Service = FnService; type InitError = (); type Future = FutureResult; fn new_service(&self, _: &Cfg) -> Self::Future { ok(FnService::new(self.f.clone())) } } impl Clone for FnNewService where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, { fn clone(&self) -> Self { Self::new(self.f.clone()) } } impl IntoNewService, ()> for F where F: Fn() -> R + Clone, R: IntoFuture, S: Service, { fn into_new_service(self) -> FnNewServiceNoConfig { FnNewServiceNoConfig::new(self) } } pub struct FnNewServiceNoConfig where F: Fn() -> R + Clone, R: IntoFuture, S: Service, { f: F, } impl FnNewServiceNoConfig where F: Fn() -> R + Clone, R: IntoFuture, S: Service, { pub fn new(f: F) -> Self { FnNewServiceNoConfig { f } } } impl NewService<()> for FnNewServiceNoConfig where F: Fn() -> R + Clone, R: IntoFuture, S: Service, { type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S; type InitError = E; type Future = R::Future; fn new_service(&self, _: &()) -> Self::Future { (self.f)().into_future() } } impl Clone for FnNewServiceNoConfig where F: Fn() -> R + Clone, R: IntoFuture, S: Service, { fn clone(&self) -> Self { Self::new(self.f.clone()) } }