use std::marker::PhantomData; use futures::{Future, Poll}; use super::{NewService, Service}; use std::pin::Pin; use std::task::Context; use pin_project::pin_project; /// Service for the `from_err` combinator, changing the error type of a service. /// /// This is created by the `ServiceExt::from_err` method. #[pin_project] pub struct FromErr { #[pin] 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 Request = A::Request; type Response = A::Response; type Error = E; type Future = FromErrFuture; fn poll_ready( self: Pin<&mut Self>, ctx: &mut Context<'_>, ) -> Poll> { self.project().service.poll_ready(ctx).map_err(E::from) } fn call(&mut self, req: A::Request) -> Self::Future { FromErrFuture { fut: self.service.call(req), f: PhantomData, } } } #[pin_project] pub struct FromErrFuture { #[pin] fut: A::Future, f: PhantomData, } impl Future for FromErrFuture where A: Service, E: From, { type Output = Result; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { self.project().fut.poll(cx).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 Request = A::Request; type Response = A::Response; type Error = E; type Config = A::Config; type Service = FromErr; type InitError = A::InitError; type Future = FromErrNewServiceFuture; fn new_service(&self, cfg: &A::Config) -> Self::Future { FromErrNewServiceFuture { fut: self.a.new_service(cfg), e: PhantomData, } } } #[pin_project] pub struct FromErrNewServiceFuture where A: NewService, E: From, { #[pin] fut: A::Future, e: PhantomData, } impl Future for FromErrNewServiceFuture where A: NewService, E: From, { type Output = Result, A::InitError>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { if let Poll::Ready(svc) = self.project().fut.poll(cx)? { Poll::Ready(Ok(FromErr::new(svc))) } else { Poll::Pending } } /* fn poll(&mut self) -> Poll { if let Poll::Ready(service) = self.fut.poll()? { Ok(Poll::Ready(FromErr::new(service))) } else { Ok(Poll::Pending) } } */ } #[cfg(test)] mod tests { use futures::future::{err, Ready}; use super::*; use crate::{IntoNewService, NewService, Service, ServiceExt}; use tokio::future::ok; struct Srv; impl Service for Srv { type Request = (); type Response = (); type Error = (); type Future = Ready>; fn poll_ready( self: Pin<&mut Self>, ctx: &mut Context<'_>, ) -> Poll> { Poll::Ready(Err(())) } fn call(&mut self, _: ()) -> Self::Future { err(()) } } #[derive(Debug, PartialEq)] struct Error; impl From<()> for Error { fn from(_: ()) -> Self { Error } } #[tokio::test] async fn test_poll_ready() { let mut srv = Srv.from_err::(); let res = srv.poll_once().await; assert_eq!(res, Poll::Ready(Err(Error))); } #[tokio::test] async fn test_call() { let mut srv = Srv.from_err::(); let res = srv.call(()).await; assert!(res.is_err()); assert_eq!(res.err().unwrap(), Error); } #[tokio::test] async fn test_new_service() { let blank = || ok::<_, ()>(Srv); let new_srv = blank.into_new_service().from_err::(); let mut srv = new_srv.new_service(&()).await.unwrap(); let res = srv.call(()).await; assert!(res.is_err()); assert_eq!(res.err().unwrap(), Error); } }