use std::marker::PhantomData; use futures::future::{ok, FutureResult}; use futures::{Async, IntoFuture, Poll}; use super::{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()) } }