use std::marker::PhantomData; use futures::{Future, Poll}; use super::Transform; /// Transform for the `map_err` combinator, changing the type of a new /// transform's init error. /// /// This is created by the `Transform::map_err` method. pub struct TransformMapInitErr { t: T, f: F, e: PhantomData<(S, E)>, } impl TransformMapInitErr { /// Create new `TransformMapErr` new transform instance pub fn new(t: T, f: F) -> Self where T: Transform, F: Fn(T::InitError) -> E, { Self { t, f, e: PhantomData, } } } impl Clone for TransformMapInitErr where T: Clone, F: Clone, { fn clone(&self) -> Self { Self { t: self.t.clone(), f: self.f.clone(), e: PhantomData, } } } impl Transform for TransformMapInitErr where T: Transform, F: Fn(T::InitError) -> E + Clone, { type Request = T::Request; type Response = T::Response; type Error = T::Error; type Transform = T::Transform; type InitError = E; type Future = TransformMapInitErrFuture; fn new_transform(&self, service: S) -> Self::Future { TransformMapInitErrFuture { fut: self.t.new_transform(service), f: self.f.clone(), } } } pub struct TransformMapInitErrFuture where T: Transform, F: Fn(T::InitError) -> E, { fut: T::Future, f: F, } impl Future for TransformMapInitErrFuture where T: Transform, F: Fn(T::InitError) -> E + Clone, { type Item = T::Transform; type Error = E; fn poll(&mut self) -> Poll { self.fut.poll().map_err(&self.f) } } /// Transform for the `from_err` combinator, changing the type of a new /// transform's init error. /// /// This is created by the `Transform::from_err` method. pub struct TransformFromErr { t: T, e: PhantomData<(S, E)>, } impl TransformFromErr where T: Transform, E: From, { /// Create new `TransformFromErr` new transform instance pub fn new(t: T) -> Self { Self { t, e: PhantomData } } } impl Clone for TransformFromErr where T: Clone, { fn clone(&self) -> Self { Self { t: self.t.clone(), e: PhantomData, } } } impl Transform for TransformFromErr where T: Transform, E: From, { type Request = T::Request; type Response = T::Response; type Error = T::Error; type Transform = T::Transform; type InitError = E; type Future = TransformFromErrFuture; fn new_transform(&self, service: S) -> Self::Future { TransformFromErrFuture { fut: self.t.new_transform(service), _t: PhantomData, } } } pub struct TransformFromErrFuture where T: Transform, E: From, { fut: T::Future, _t: PhantomData, } impl Future for TransformFromErrFuture where T: Transform, E: From, { type Item = T::Transform; type Error = E; fn poll(&mut self) -> Poll { self.fut.poll().map_err(E::from) } }