2018-11-29 16:56:15 -10:00
|
|
|
use std::marker::PhantomData;
|
2018-08-25 09:02:14 -07:00
|
|
|
|
2019-03-04 21:35:47 -08:00
|
|
|
use futures::{Future, Poll};
|
2018-09-11 09:30:22 -07:00
|
|
|
|
|
|
|
use super::NewService;
|
2018-08-25 09:02:14 -07:00
|
|
|
|
|
|
|
/// `MapInitErr` service combinator
|
2019-02-22 12:44:37 -08:00
|
|
|
pub struct MapInitErr<A, F, E, C> {
|
2018-08-25 09:02:14 -07:00
|
|
|
a: A,
|
|
|
|
f: F,
|
2019-02-22 12:44:37 -08:00
|
|
|
e: PhantomData<(E, C)>,
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
|
2019-02-22 12:44:37 -08:00
|
|
|
impl<A, F, E, C> MapInitErr<A, F, E, C> {
|
2018-08-25 09:02:14 -07:00
|
|
|
/// Create new `MapInitErr` combinator
|
2019-03-09 06:36:23 -08:00
|
|
|
pub fn new(a: A, f: F) -> Self
|
2018-11-29 16:56:15 -10:00
|
|
|
where
|
2019-03-09 06:36:23 -08:00
|
|
|
A: NewService<C>,
|
2018-11-29 16:56:15 -10:00
|
|
|
F: Fn(A::InitError) -> E,
|
|
|
|
{
|
2018-08-25 09:02:14 -07:00
|
|
|
Self {
|
|
|
|
a,
|
|
|
|
f,
|
2018-11-29 16:56:15 -10:00
|
|
|
e: PhantomData,
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:44:37 -08:00
|
|
|
impl<A, F, E, C> Clone for MapInitErr<A, F, E, C>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2018-11-29 16:56:15 -10:00
|
|
|
A: Clone,
|
|
|
|
F: Clone,
|
2018-08-25 09:02:14 -07:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
a: self.a.clone(),
|
|
|
|
f: self.f.clone(),
|
2018-11-29 16:56:15 -10:00
|
|
|
e: PhantomData,
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 06:36:23 -08:00
|
|
|
impl<A, F, E, C> NewService<C> for MapInitErr<A, F, E, C>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2019-03-09 06:36:23 -08:00
|
|
|
A: NewService<C>,
|
2018-08-25 09:02:14 -07:00
|
|
|
F: Fn(A::InitError) -> E + Clone,
|
|
|
|
{
|
2019-03-09 06:36:23 -08:00
|
|
|
type Request = A::Request;
|
2018-08-25 09:02:14 -07:00
|
|
|
type Response = A::Response;
|
|
|
|
type Error = A::Error;
|
|
|
|
type Service = A::Service;
|
|
|
|
|
|
|
|
type InitError = E;
|
2019-03-09 06:36:23 -08:00
|
|
|
type Future = MapInitErrFuture<A, F, E, C>;
|
2018-08-25 09:02:14 -07:00
|
|
|
|
2019-02-22 12:44:37 -08:00
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
2019-03-09 06:36:23 -08:00
|
|
|
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 06:36:23 -08:00
|
|
|
pub struct MapInitErrFuture<A, F, E, C>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2019-03-09 06:36:23 -08:00
|
|
|
A: NewService<C>,
|
2018-08-25 09:02:14 -07:00
|
|
|
F: Fn(A::InitError) -> E,
|
|
|
|
{
|
|
|
|
f: F,
|
2019-03-04 21:35:47 -08:00
|
|
|
fut: A::Future,
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
|
2019-03-09 06:36:23 -08:00
|
|
|
impl<A, F, E, C> MapInitErrFuture<A, F, E, C>
|
|
|
|
where
|
|
|
|
A: NewService<C>,
|
|
|
|
F: Fn(A::InitError) -> E,
|
|
|
|
{
|
|
|
|
fn new(fut: A::Future, f: F) -> Self {
|
|
|
|
MapInitErrFuture { f, fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, F, E, C> Future for MapInitErrFuture<A, F, E, C>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2019-03-09 06:36:23 -08:00
|
|
|
A: NewService<C>,
|
2018-08-25 09:02:14 -07:00
|
|
|
F: Fn(A::InitError) -> E,
|
|
|
|
{
|
|
|
|
type Item = A::Service;
|
|
|
|
type Error = E;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
self.fut.poll().map_err(&self.f)
|
|
|
|
}
|
|
|
|
}
|