2020-12-27 14:15:42 +00:00
|
|
|
use core::{
|
|
|
|
future::Future,
|
|
|
|
marker::PhantomData,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
|
|
|
use pin_project_lite::pin_project;
|
2018-08-25 09:02:14 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
use super::ServiceFactory;
|
2018-08-25 09:02:14 -07:00
|
|
|
|
|
|
|
/// `MapInitErr` service combinator
|
2020-12-27 04:28:00 +00:00
|
|
|
pub struct MapInitErr<A, F, Req, Err> {
|
2018-08-25 09:02:14 -07:00
|
|
|
a: A,
|
|
|
|
f: F,
|
2021-12-05 01:30:04 +03:00
|
|
|
e: PhantomData<fn(Req) -> Err>,
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<A, F, Req, Err> MapInitErr<A, F, Req, Err>
|
2019-11-14 18:38:24 +06:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
A: ServiceFactory<Req>,
|
|
|
|
F: Fn(A::InitError) -> Err,
|
2019-11-14 18:38:24 +06:00
|
|
|
{
|
2018-08-25 09:02:14 -07:00
|
|
|
/// Create new `MapInitErr` combinator
|
2019-11-14 18:38:24 +06:00
|
|
|
pub(crate) fn new(a: A, f: F) -> Self {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<A, F, Req, E> Clone for MapInitErr<A, F, Req, E>
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<A, F, Req, E> ServiceFactory<Req> for MapInitErr<A, F, Req, E>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
A: ServiceFactory<Req>,
|
2019-11-19 14:51:40 +06:00
|
|
|
F: Fn(A::InitError) -> E + Clone,
|
2018-08-25 09:02:14 -07:00
|
|
|
{
|
|
|
|
type Response = A::Response;
|
|
|
|
type Error = A::Error;
|
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
type Config = A::Config;
|
|
|
|
type Service = A::Service;
|
2018-08-25 09:02:14 -07:00
|
|
|
type InitError = E;
|
2020-12-27 04:28:00 +00:00
|
|
|
type Future = MapInitErrFuture<A, F, Req, E>;
|
2018-08-25 09:02:14 -07:00
|
|
|
|
2019-12-02 21:27:48 +06:00
|
|
|
fn new_service(&self, cfg: A::Config) -> 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-11-18 14:30:04 +06:00
|
|
|
|
2020-12-27 14:15:42 +00:00
|
|
|
pin_project! {
|
|
|
|
pub struct MapInitErrFuture<A, F, Req, E>
|
|
|
|
where
|
|
|
|
A: ServiceFactory<Req>,
|
|
|
|
F: Fn(A::InitError) -> E,
|
|
|
|
{
|
|
|
|
f: F,
|
|
|
|
#[pin]
|
|
|
|
fut: A::Future,
|
|
|
|
}
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<A, F, Req, E> MapInitErrFuture<A, F, Req, E>
|
2019-03-09 06:36:23 -08:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
A: ServiceFactory<Req>,
|
2019-03-09 06:36:23 -08:00
|
|
|
F: Fn(A::InitError) -> E,
|
|
|
|
{
|
|
|
|
fn new(fut: A::Future, f: F) -> Self {
|
|
|
|
MapInitErrFuture { f, fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<A, F, Req, E> Future for MapInitErrFuture<A, F, Req, E>
|
2018-08-25 09:02:14 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
A: ServiceFactory<Req>,
|
2019-11-19 14:51:40 +06:00
|
|
|
F: Fn(A::InitError) -> E,
|
2018-08-25 09:02:14 -07:00
|
|
|
{
|
2019-11-14 18:38:24 +06:00
|
|
|
type Output = Result<A::Service, E>;
|
2018-08-25 09:02:14 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-19 14:51:40 +06:00
|
|
|
let this = self.project();
|
|
|
|
this.fut.poll(cx).map_err(this.f)
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
}
|