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