use std::marker::PhantomData; use futures::future::{ok, FutureResult}; use futures::{Async, IntoFuture, Poll}; use super::{IntoNewTransform, IntoTransform, NewTransform, Transform}; pub struct FnTransform where F: FnMut(Req, &mut S) -> Res, Res: IntoFuture, { f: F, _t: PhantomData<(S, Req, Res)>, } impl FnTransform where F: FnMut(Req, &mut S) -> Res, Res: IntoFuture, { pub fn new(f: F) -> Self { FnTransform { f, _t: PhantomData } } } impl Clone for FnTransform where F: FnMut(Req, &mut S) -> Res + Clone, Res: IntoFuture, { fn clone(&self) -> Self { FnTransform { f: self.f.clone(), _t: PhantomData, } } } impl Transform for FnTransform where F: FnMut(Req, &mut S) -> Res, Res: IntoFuture, { type Request = Req; type Response = Res::Item; type Error = Res::Error; type Future = Res::Future; fn poll_ready(&mut self) -> Poll<(), Self::Error> { Ok(Async::Ready(())) } fn call(&mut self, request: Req, service: &mut S) -> Self::Future { (self.f)(request, service).into_future() } } impl IntoTransform, S> for F where F: FnMut(Req, &mut S) -> Res, Res: IntoFuture, { fn into_transform(self) -> FnTransform { FnTransform::new(self) } } pub struct FnNewTransform where F: FnMut(Req, &mut S) -> Out + Clone, Out: IntoFuture, { f: F, _t: PhantomData<(S, Req, Out, Err)>, } impl FnNewTransform where F: FnMut(Req, &mut S) -> Res + Clone, Res: IntoFuture, { pub fn new(f: F) -> Self { FnNewTransform { f, _t: PhantomData } } } impl NewTransform for FnNewTransform where F: FnMut(Req, &mut S) -> Res + Clone, Res: IntoFuture, { type Request = Req; type Response = Res::Item; type Error = Res::Error; type Transform = FnTransform; type InitError = Err; type Future = FutureResult; fn new_transform(&self) -> Self::Future { ok(FnTransform::new(self.f.clone())) } } impl IntoNewTransform, S> for F where F: FnMut(Req, &mut S) -> Res + Clone, Res: IntoFuture, { fn into_new_transform(self) -> FnNewTransform { FnNewTransform::new(self) } } impl Clone for FnNewTransform where F: FnMut(Req, &mut S) -> Res + Clone, Res: IntoFuture, { fn clone(&self) -> Self { Self::new(self.f.clone()) } }