use std::marker::PhantomData; use futures::{Async, Future, Poll}; use super::{NewService, Service}; /// Service for the `from_err` combinator, changing the error type of a service. /// /// This is created by the `ServiceExt::from_err` method. pub struct FromErr { service: A, f: PhantomData, } impl FromErr { pub(crate) fn new(service: A) -> Self where A: Service, E: From, { FromErr { service, f: PhantomData, } } } impl Clone for FromErr where A: Clone, { fn clone(&self) -> Self { FromErr { service: self.service.clone(), f: PhantomData, } } } impl Service for FromErr where A: Service, E: From, { type Response = A::Response; type Error = E; type Future = FromErrFuture; fn poll_ready(&mut self) -> Poll<(), E> { Ok(self.service.poll_ready().map_err(E::from)?) } fn call(&mut self, req: Request) -> Self::Future { FromErrFuture { fut: self.service.call(req), f: PhantomData, } } } pub struct FromErrFuture, E, Request> { fut: A::Future, f: PhantomData, } impl Future for FromErrFuture where A: Service, E: From, { type Item = A::Response; type Error = E; fn poll(&mut self) -> Poll { self.fut.poll().map_err(E::from) } } /// NewService for the `from_err` combinator, changing the type of a new /// service's error. /// /// This is created by the `NewServiceExt::from_err` method. pub struct FromErrNewService { a: A, e: PhantomData, } impl FromErrNewService { /// Create new `FromErr` new service instance pub fn new(a: A) -> Self where A: NewService, E: From, { Self { a, e: PhantomData } } } impl Clone for FromErrNewService where A: Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), e: PhantomData, } } } impl NewService for FromErrNewService where A: NewService, E: From, { type Response = A::Response; type Error = E; type Service = FromErr; type InitError = A::InitError; type Future = FromErrNewServiceFuture; fn new_service(&self) -> Self::Future { FromErrNewServiceFuture { fut: self.a.new_service(), e: PhantomData, } } } pub struct FromErrNewServiceFuture where A: NewService, E: From, { fut: A::Future, e: PhantomData, } impl Future for FromErrNewServiceFuture where A: NewService, E: From, { type Item = FromErr; type Error = A::InitError; fn poll(&mut self) -> Poll { if let Async::Ready(service) = self.fut.poll()? { Ok(Async::Ready(FromErr::new(service))) } else { Ok(Async::NotReady) } } } #[cfg(test)] mod tests { use futures::future::{err, FutureResult}; use super::*; use service::{IntoNewService, NewServiceExt, Service, ServiceExt}; struct Srv; impl Service for Srv { type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; fn poll_ready(&mut self) -> Poll<(), Self::Error> { Err(()) } fn call(&mut self, _: ()) -> Self::Future { err(()) } } #[derive(Debug, PartialEq)] struct Error; impl From<()> for Error { fn from(_: ()) -> Self { Error } } #[test] fn test_poll_ready() { let mut srv = Srv.from_err::(); let res = srv.poll_ready(); assert!(res.is_err()); assert_eq!(res.err().unwrap(), Error); } #[test] fn test_call() { let mut srv = Srv.from_err::(); let res = srv.call(()).poll(); assert!(res.is_err()); assert_eq!(res.err().unwrap(), Error); } #[test] fn test_new_service() { let blank = || Ok::<_, ()>(Srv); let new_srv = blank.into_new_service().from_err::(); if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { let res = srv.call(()).poll(); assert!(res.is_err()); assert_eq!(res.err().unwrap(), Error); } else { panic!() } } }