use std::marker::PhantomData;
use futures::{Future, Poll};
use super::NewService;
/// `MapInitErr` service combinator
pub struct MapInitErr {
a: A,
f: F,
e: PhantomData<(E, C)>,
}
impl MapInitErr {
/// Create new `MapInitErr` combinator
pub fn new(a: A, f: F) -> Self
where
A: NewService,
F: Fn(A::InitError) -> E,
{
Self {
a,
f,
e: PhantomData,
}
}
}
impl Clone for MapInitErr
where
A: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
f: self.f.clone(),
e: PhantomData,
}
}
}
impl NewService for MapInitErr
where
A: NewService,
F: Fn(A::InitError) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type Service = A::Service;
type InitError = E;
type Future = MapInitErrFuture;
fn new_service(&self, cfg: &C) -> Self::Future {
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
}
}
pub struct MapInitErrFuture
where
A: NewService,
F: Fn(A::InitError) -> E,
{
f: F,
fut: A::Future,
}
impl MapInitErrFuture
where
A: NewService,
F: Fn(A::InitError) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
MapInitErrFuture { f, fut }
}
}
impl Future for MapInitErrFuture
where
A: NewService,
F: Fn(A::InitError) -> E,
{
type Item = A::Service;
type Error = E;
fn poll(&mut self) -> Poll {
self.fut.poll().map_err(&self.f)
}
}