use std::marker::PhantomData; use futures::{Future, Poll}; use super::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 where A: Service, { service: A, f: PhantomData, } impl> FromErr { pub(crate) fn new(service: A) -> Self { FromErr { service, f: PhantomData, } } } impl Clone for FromErr where A: Service + Clone, E: From, { fn clone(&self) -> Self { FromErr { service: self.service.clone(), f: PhantomData, } } } impl Service for FromErr where A: Service, E: From, { type Request = A::Request; 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: Self::Request) -> Self::Future { FromErrFuture { fut: self.service.call(req), f: PhantomData, } } } pub struct FromErrFuture { 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) } } #[cfg(test)] mod tests { use futures::future::{err, FutureResult}; use super::*; use service::{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); } }