2020-12-27 15:15:42 +01:00
|
|
|
use core::{
|
|
|
|
future::Future,
|
|
|
|
marker::PhantomData,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
use alloc::rc::Rc;
|
|
|
|
use futures_core::ready;
|
2020-12-27 15:15:42 +01:00
|
|
|
use pin_project_lite::pin_project;
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
use crate::{Service, ServiceFactory};
|
2019-05-12 15:03:50 +02:00
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
/// Convert `Fn(Config, &Service1) -> Future<Service2>` fn to a service factory.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn apply_cfg<S1, Req, F, Cfg, Fut, S2, Err>(
|
|
|
|
srv: S1,
|
2020-01-17 01:58:11 +01:00
|
|
|
f: F,
|
|
|
|
) -> impl ServiceFactory<
|
2020-12-27 05:28:00 +01:00
|
|
|
Req,
|
|
|
|
Config = Cfg,
|
|
|
|
Response = S2::Response,
|
|
|
|
Error = S2::Error,
|
|
|
|
Service = S2,
|
|
|
|
InitError = Err,
|
|
|
|
Future = Fut,
|
2020-01-17 01:58:11 +01:00
|
|
|
> + Clone
|
2019-03-13 00:32:10 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
S1: Service<Req>,
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Cfg, &S1) -> Fut,
|
2020-12-27 05:28:00 +01:00
|
|
|
Fut: Future<Output = Result<S2, Err>>,
|
|
|
|
S2: Service<Req>,
|
2019-03-13 00:32:10 +01:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
ApplyConfigService {
|
2021-01-23 04:06:22 +01:00
|
|
|
srv: Rc::new((srv, f)),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
/// Convert `Fn(Config, &ServiceFactory1) -> Future<ServiceFactory2>` fn to a service factory.
|
2019-12-10 16:34:51 +01:00
|
|
|
///
|
|
|
|
/// Service1 get constructed from `T` factory.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn apply_cfg_factory<SF, Req, F, Cfg, Fut, S>(
|
|
|
|
factory: SF,
|
2019-06-06 10:28:07 +02:00
|
|
|
f: F,
|
2020-01-17 01:58:11 +01:00
|
|
|
) -> impl ServiceFactory<
|
2020-12-27 05:28:00 +01:00
|
|
|
Req,
|
|
|
|
Config = Cfg,
|
2020-01-17 01:58:11 +01:00
|
|
|
Response = S::Response,
|
|
|
|
Error = S::Error,
|
|
|
|
Service = S,
|
2020-12-27 05:28:00 +01:00
|
|
|
InitError = SF::InitError,
|
2020-01-17 01:58:11 +01:00
|
|
|
> + Clone
|
2019-06-06 10:28:07 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: ServiceFactory<Req, Config = ()>,
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Cfg, &SF::Service) -> Fut,
|
2020-12-27 05:28:00 +01:00
|
|
|
SF::InitError: From<SF::Error>,
|
|
|
|
Fut: Future<Output = Result<S, SF::InitError>>,
|
|
|
|
S: Service<Req>,
|
2019-06-06 10:28:07 +02:00
|
|
|
{
|
2019-11-18 09:30:04 +01:00
|
|
|
ApplyConfigServiceFactory {
|
2021-01-23 04:06:22 +01:00
|
|
|
srv: Rc::new((factory, f)),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-06-06 10:28:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
/// Convert `Fn(Config, &Server) -> Future<Service>` fn to NewService\
|
2020-12-27 05:28:00 +01:00
|
|
|
struct ApplyConfigService<S1, Req, F, Cfg, Fut, S2, Err>
|
2019-03-09 18:01:02 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
S1: Service<Req>,
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Cfg, &S1) -> Fut,
|
2020-12-27 05:28:00 +01:00
|
|
|
Fut: Future<Output = Result<S2, Err>>,
|
|
|
|
S2: Service<Req>,
|
2019-03-09 18:01:02 +01:00
|
|
|
{
|
2021-01-23 04:06:22 +01:00
|
|
|
srv: Rc<(S1, F)>,
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData<(Cfg, Req, Fut, S2)>,
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<S1, Req, F, Cfg, Fut, S2, Err> Clone for ApplyConfigService<S1, Req, F, Cfg, Fut, S2, Err>
|
2019-03-09 18:01:02 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
S1: Service<Req>,
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Cfg, &S1) -> Fut,
|
2020-12-27 05:28:00 +01:00
|
|
|
Fut: Future<Output = Result<S2, Err>>,
|
|
|
|
S2: Service<Req>,
|
2019-03-09 18:01:02 +01:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
2019-05-12 15:03:50 +02:00
|
|
|
ApplyConfigService {
|
|
|
|
srv: self.srv.clone(),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<S1, Req, F, Cfg, Fut, S2, Err> ServiceFactory<Req>
|
|
|
|
for ApplyConfigService<S1, Req, F, Cfg, Fut, S2, Err>
|
2019-03-09 18:01:02 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
S1: Service<Req>,
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Cfg, &S1) -> Fut,
|
2020-12-27 05:28:00 +01:00
|
|
|
Fut: Future<Output = Result<S2, Err>>,
|
|
|
|
S2: Service<Req>,
|
2019-03-09 18:01:02 +01:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
type Response = S2::Response;
|
|
|
|
type Error = S2::Error;
|
2021-01-23 04:06:22 +01:00
|
|
|
type Config = Cfg;
|
2020-12-27 05:28:00 +01:00
|
|
|
type Service = S2;
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
type InitError = Err;
|
|
|
|
type Future = Fut;
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
fn new_service(&self, cfg: Cfg) -> Self::Future {
|
2021-01-23 04:06:22 +01:00
|
|
|
let (t, f) = &*self.srv;
|
2020-07-19 23:05:36 +02:00
|
|
|
f(cfg, t)
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 10:28:07 +02:00
|
|
|
|
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
2020-12-27 05:28:00 +01:00
|
|
|
struct ApplyConfigServiceFactory<SF, Req, F, Cfg, Fut, S>
|
2019-06-06 10:28:07 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: ServiceFactory<Req, Config = ()>,
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Cfg, &SF::Service) -> Fut,
|
2020-12-27 05:28:00 +01:00
|
|
|
Fut: Future<Output = Result<S, SF::InitError>>,
|
|
|
|
S: Service<Req>,
|
2019-06-06 10:28:07 +02:00
|
|
|
{
|
2021-01-23 04:06:22 +01:00
|
|
|
srv: Rc<(SF, F)>,
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData<(Cfg, Req, Fut, S)>,
|
2019-06-06 10:28:07 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Req, F, Cfg, Fut, S> Clone for ApplyConfigServiceFactory<SF, Req, F, Cfg, Fut, S>
|
2019-06-06 10:28:07 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: ServiceFactory<Req, Config = ()>,
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Cfg, &SF::Service) -> Fut,
|
2020-12-27 05:28:00 +01:00
|
|
|
Fut: Future<Output = Result<S, SF::InitError>>,
|
|
|
|
S: Service<Req>,
|
2019-06-06 10:28:07 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
2019-11-18 09:30:04 +01:00
|
|
|
Self {
|
2019-06-06 10:28:07 +02:00
|
|
|
srv: self.srv.clone(),
|
2020-12-27 05:28:00 +01:00
|
|
|
_phantom: PhantomData,
|
2019-06-06 10:28:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Req, F, Cfg, Fut, S> ServiceFactory<Req>
|
|
|
|
for ApplyConfigServiceFactory<SF, Req, F, Cfg, Fut, S>
|
2019-06-06 10:28:07 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: ServiceFactory<Req, Config = ()>,
|
|
|
|
SF::InitError: From<SF::Error>,
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Cfg, &SF::Service) -> Fut,
|
2020-12-27 05:28:00 +01:00
|
|
|
Fut: Future<Output = Result<S, SF::InitError>>,
|
|
|
|
S: Service<Req>,
|
2019-06-06 10:28:07 +02:00
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
2021-01-23 04:06:22 +01:00
|
|
|
type Config = Cfg;
|
2019-06-06 10:28:07 +02:00
|
|
|
type Service = S;
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
type InitError = SF::InitError;
|
|
|
|
type Future = ApplyConfigServiceFactoryResponse<SF, Req, F, Cfg, Fut, S>;
|
2019-06-06 10:28:07 +02:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
fn new_service(&self, cfg: Cfg) -> Self::Future {
|
2019-11-18 09:30:04 +01:00
|
|
|
ApplyConfigServiceFactoryResponse {
|
2019-12-02 16:27:48 +01:00
|
|
|
cfg: Some(cfg),
|
2019-12-05 07:37:26 +01:00
|
|
|
store: self.srv.clone(),
|
2020-12-27 15:15:42 +01:00
|
|
|
state: State::A {
|
2021-01-23 04:06:22 +01:00
|
|
|
fut: self.srv.0.new_service(()),
|
2020-12-27 15:15:42 +01:00
|
|
|
},
|
2019-06-06 10:28:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:15:42 +01:00
|
|
|
pin_project! {
|
|
|
|
struct ApplyConfigServiceFactoryResponse<SF, Req, F, Cfg, Fut, S>
|
|
|
|
where
|
|
|
|
SF: ServiceFactory<Req, Config = ()>,
|
|
|
|
SF::InitError: From<SF::Error>,
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Cfg, &SF::Service) -> Fut,
|
2020-12-27 15:15:42 +01:00
|
|
|
Fut: Future<Output = Result<S, SF::InitError>>,
|
|
|
|
S: Service<Req>,
|
|
|
|
{
|
|
|
|
cfg: Option<Cfg>,
|
2021-01-23 04:06:22 +01:00
|
|
|
store: Rc<(SF, F)>,
|
2020-12-27 15:15:42 +01:00
|
|
|
#[pin]
|
|
|
|
state: State<SF, Fut, S, Req>,
|
|
|
|
}
|
2019-12-05 07:37:26 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 15:15:42 +01:00
|
|
|
pin_project! {
|
|
|
|
#[project = StateProj]
|
|
|
|
enum State<SF, Fut, S, Req>
|
|
|
|
where
|
|
|
|
SF: ServiceFactory<Req, Config = ()>,
|
|
|
|
SF::InitError: From<SF::Error>,
|
|
|
|
Fut: Future<Output = Result<S, SF::InitError>>,
|
|
|
|
S: Service<Req>,
|
|
|
|
{
|
|
|
|
A { #[pin] fut: SF::Future },
|
|
|
|
B { svc: SF::Service },
|
|
|
|
C { #[pin] fut: Fut },
|
|
|
|
}
|
2019-06-06 10:28:07 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<SF, Req, F, Cfg, Fut, S> Future
|
|
|
|
for ApplyConfigServiceFactoryResponse<SF, Req, F, Cfg, Fut, S>
|
2019-11-18 09:30:04 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
SF: ServiceFactory<Req, Config = ()>,
|
|
|
|
SF::InitError: From<SF::Error>,
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Cfg, &SF::Service) -> Fut,
|
2020-12-27 05:28:00 +01:00
|
|
|
Fut: Future<Output = Result<S, SF::InitError>>,
|
|
|
|
S: Service<Req>,
|
2019-06-06 10:28:07 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
type Output = Result<S, SF::InitError>;
|
2019-06-06 10:28:07 +02:00
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let mut this = self.as_mut().project();
|
2019-11-18 09:30:04 +01:00
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
match this.state.as_mut().project() {
|
2021-01-23 04:06:22 +01:00
|
|
|
StateProj::A { fut } => {
|
|
|
|
let svc = ready!(fut.poll(cx))?;
|
|
|
|
this.state.set(State::B { svc });
|
|
|
|
self.poll(cx)
|
|
|
|
}
|
|
|
|
StateProj::B { svc } => {
|
|
|
|
ready!(svc.poll_ready(cx))?;
|
|
|
|
{
|
|
|
|
let (_, f) = &**this.store;
|
|
|
|
let fut = f(this.cfg.take().unwrap(), svc);
|
|
|
|
this.state.set(State::C { fut });
|
2019-08-27 01:28:15 +02:00
|
|
|
}
|
2021-01-23 04:06:22 +01:00
|
|
|
self.poll(cx)
|
|
|
|
}
|
2020-12-27 15:15:42 +01:00
|
|
|
StateProj::C { fut } => fut.poll(cx),
|
2019-06-06 10:28:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|