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;
|
|
|
|
use std::task::{Context, Poll};
|
2019-03-09 09:01:02 -08:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
use futures::ready;
|
|
|
|
use pin_project::pin_project;
|
2019-03-09 09:01:02 -08:00
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
use crate::cell::Cell;
|
2019-11-14 18:38:24 +06:00
|
|
|
use crate::{IntoService, Service, ServiceFactory};
|
2019-05-12 06:03:50 -07:00
|
|
|
|
|
|
|
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
2019-11-14 18:38:24 +06:00
|
|
|
pub fn apply_cfg<F, C, T, R, S, E>(
|
2019-06-06 14:28:07 +06:00
|
|
|
srv: T,
|
|
|
|
f: F,
|
2019-11-14 18:38:24 +06:00
|
|
|
) -> impl ServiceFactory<
|
2019-06-06 14:28:07 +06:00
|
|
|
Config = C,
|
|
|
|
Request = S::Request,
|
|
|
|
Response = S::Response,
|
|
|
|
Error = S::Error,
|
|
|
|
Service = S,
|
2019-11-14 18:38:24 +06:00
|
|
|
InitError = E,
|
2019-06-06 14:28:07 +06:00
|
|
|
> + Clone
|
2019-03-12 16:32:10 -07:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
F: FnMut(&C, &mut T) -> R,
|
|
|
|
T: Service,
|
2019-11-14 18:38:24 +06:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 06:03:50 -07:00
|
|
|
S: Service,
|
2019-03-12 16:32:10 -07:00
|
|
|
{
|
2019-05-12 06:03:50 -07:00
|
|
|
ApplyConfigService {
|
|
|
|
f: Cell::new(f),
|
2019-11-14 18:38:24 +06:00
|
|
|
srv: Cell::new(srv),
|
2019-05-12 06:03:50 -07:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2019-03-09 09:01:02 -08:00
|
|
|
}
|
|
|
|
|
2019-06-06 14:28:07 +06:00
|
|
|
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
|
|
|
/// Service get constructor from NewService.
|
2019-11-14 18:38:24 +06:00
|
|
|
pub fn apply_cfg_factory<F, C, T, R, S>(
|
2019-06-06 14:28:07 +06:00
|
|
|
srv: T,
|
|
|
|
f: F,
|
2019-11-14 18:38:24 +06:00
|
|
|
) -> impl ServiceFactory<
|
2019-06-06 14:28:07 +06:00
|
|
|
Config = C,
|
|
|
|
Request = S::Request,
|
|
|
|
Response = S::Response,
|
|
|
|
Error = S::Error,
|
|
|
|
Service = S,
|
|
|
|
InitError = T::InitError,
|
|
|
|
> + Clone
|
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 18:38:24 +06:00
|
|
|
T: ServiceFactory<Config = ()>,
|
2019-08-27 05:28:15 +06:00
|
|
|
T::InitError: From<T::Error>,
|
2019-11-14 18:38:24 +06:00
|
|
|
R: Future<Output = Result<S, T::InitError>>,
|
2019-06-06 14:28:07 +06:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
ApplyConfigNewService {
|
|
|
|
f: Cell::new(f),
|
|
|
|
srv: Cell::new(srv),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService\
|
|
|
|
#[pin_project]
|
|
|
|
struct ApplyConfigService<F, C, T, R, S, E>
|
2019-03-09 09:01:02 -08:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
F: FnMut(&C, &mut T) -> R,
|
|
|
|
T: Service,
|
2019-11-14 18:38:24 +06:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 06:03:50 -07:00
|
|
|
S: Service,
|
2019-03-09 09:01:02 -08:00
|
|
|
{
|
2019-05-12 06:03:50 -07:00
|
|
|
f: Cell<F>,
|
2019-11-14 18:38:24 +06:00
|
|
|
#[pin]
|
2019-05-12 06:03:50 -07:00
|
|
|
srv: Cell<T>,
|
|
|
|
_t: PhantomData<(C, R, S)>,
|
2019-03-09 09:01:02 -08:00
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<F, C, T, R, S, E> Clone for ApplyConfigService<F, C, T, R, S, E>
|
2019-03-09 09:01:02 -08:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
F: FnMut(&C, &mut T) -> R,
|
|
|
|
T: Service,
|
2019-11-14 18:38:24 +06:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 06:03:50 -07:00
|
|
|
S: Service,
|
2019-03-09 09:01:02 -08:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
2019-05-12 06:03:50 -07:00
|
|
|
ApplyConfigService {
|
2019-03-09 09:01:02 -08:00
|
|
|
f: self.f.clone(),
|
2019-05-12 06:03:50 -07:00
|
|
|
srv: self.srv.clone(),
|
|
|
|
_t: PhantomData,
|
2019-03-09 09:01:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<F, C, T, R, S, E> ServiceFactory for ApplyConfigService<F, C, T, R, S, E>
|
2019-03-09 09:01:02 -08:00
|
|
|
where
|
2019-05-12 06:03:50 -07:00
|
|
|
F: FnMut(&C, &mut T) -> R,
|
|
|
|
T: Service,
|
2019-11-14 18:38:24 +06:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 06:03:50 -07:00
|
|
|
S: Service,
|
2019-03-09 09:01:02 -08:00
|
|
|
{
|
2019-05-12 06:03:50 -07:00
|
|
|
type Config = C;
|
2019-03-12 16:32:10 -07:00
|
|
|
type Request = S::Request;
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
2019-05-12 06:03:50 -07:00
|
|
|
type Service = S;
|
2019-03-09 09:01:02 -08:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
type InitError = E;
|
|
|
|
type Future = FnNewServiceConfigFut<R, S, E>;
|
2019-03-09 09:01:02 -08:00
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
|
|
|
FnNewServiceConfigFut {
|
2019-11-14 18:38:24 +06:00
|
|
|
fut: unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) },
|
2019-05-12 06:03:50 -07:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2019-03-09 09:01:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
#[pin_project]
|
|
|
|
struct FnNewServiceConfigFut<R, S, E>
|
2019-05-12 06:03:50 -07:00
|
|
|
where
|
2019-11-14 18:38:24 +06:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 06:03:50 -07:00
|
|
|
S: Service,
|
|
|
|
{
|
2019-11-14 18:38:24 +06:00
|
|
|
#[pin]
|
|
|
|
fut: R,
|
2019-05-12 06:03:50 -07:00
|
|
|
_t: PhantomData<(S,)>,
|
|
|
|
}
|
2019-03-09 09:01:02 -08:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<R, S, E> Future for FnNewServiceConfigFut<R, S, E>
|
2019-05-12 06:03:50 -07:00
|
|
|
where
|
2019-11-14 18:38:24 +06:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 06:03:50 -07:00
|
|
|
S: Service,
|
|
|
|
{
|
2019-11-14 18:38:24 +06:00
|
|
|
type Output = Result<S, E>;
|
2019-03-09 09:01:02 -08:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
Poll::Ready(Ok(ready!(self.project().fut.poll(cx))?.into_service()))
|
2019-03-09 09:01:02 -08:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 14:28:07 +06:00
|
|
|
|
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
|
|
|
struct ApplyConfigNewService<F, C, T, R, S>
|
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 18:38:24 +06:00
|
|
|
T: ServiceFactory<Config = ()>,
|
|
|
|
R: Future<Output = Result<S, T::InitError>>,
|
2019-06-06 14:28:07 +06:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
f: Cell<F>,
|
|
|
|
srv: Cell<T>,
|
|
|
|
_t: PhantomData<(C, R, S)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, C, T, R, S> Clone for ApplyConfigNewService<F, C, T, R, S>
|
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 18:38:24 +06:00
|
|
|
T: ServiceFactory<Config = ()>,
|
|
|
|
R: Future<Output = Result<S, T::InitError>>,
|
2019-06-06 14:28:07 +06:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ApplyConfigNewService {
|
|
|
|
f: self.f.clone(),
|
|
|
|
srv: self.srv.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<F, C, T, R, S> ServiceFactory for ApplyConfigNewService<F, C, T, R, S>
|
2019-06-06 14:28:07 +06:00
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 18:38:24 +06:00
|
|
|
T: ServiceFactory<Config = ()>,
|
2019-08-27 05:28:15 +06:00
|
|
|
T::InitError: From<T::Error>,
|
2019-11-14 18:38:24 +06:00
|
|
|
R: Future<Output = Result<S, T::InitError>>,
|
2019-06-06 14:28:07 +06:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
type Config = C;
|
|
|
|
type Request = S::Request;
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Service = S;
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
type InitError = T::InitError;
|
2019-06-06 14:28:07 +06:00
|
|
|
type Future = ApplyConfigNewServiceFut<F, C, T, R, S>;
|
|
|
|
|
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
|
|
|
ApplyConfigNewServiceFut {
|
|
|
|
f: self.f.clone(),
|
|
|
|
cfg: cfg.clone(),
|
|
|
|
fut: None,
|
2019-08-27 05:28:15 +06:00
|
|
|
srv: None,
|
|
|
|
srv_fut: Some(self.srv.get_ref().new_service(&())),
|
2019-06-06 14:28:07 +06:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
#[pin_project]
|
2019-06-06 14:28:07 +06:00
|
|
|
struct ApplyConfigNewServiceFut<F, C, T, R, S>
|
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 18:38:24 +06:00
|
|
|
T: ServiceFactory<Config = ()>,
|
2019-08-27 05:28:15 +06:00
|
|
|
T::InitError: From<T::Error>,
|
2019-11-14 18:38:24 +06:00
|
|
|
R: Future<Output = Result<S, T::InitError>>,
|
2019-06-06 14:28:07 +06:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
cfg: C,
|
|
|
|
f: Cell<F>,
|
2019-08-27 05:28:15 +06:00
|
|
|
srv: Option<T::Service>,
|
2019-11-14 18:38:24 +06:00
|
|
|
#[pin]
|
2019-08-27 05:28:15 +06:00
|
|
|
srv_fut: Option<T::Future>,
|
2019-11-14 18:38:24 +06:00
|
|
|
#[pin]
|
|
|
|
fut: Option<R>,
|
2019-06-06 14:28:07 +06:00
|
|
|
_t: PhantomData<(S,)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, C, T, R, S> Future for ApplyConfigNewServiceFut<F, C, T, R, S>
|
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 18:38:24 +06:00
|
|
|
T: ServiceFactory<Config = ()>,
|
2019-08-27 05:28:15 +06:00
|
|
|
T::InitError: From<T::Error>,
|
2019-11-14 18:38:24 +06:00
|
|
|
R: Future<Output = Result<S, T::InitError>>,
|
2019-06-06 14:28:07 +06:00
|
|
|
S: Service,
|
|
|
|
{
|
2019-11-14 18:38:24 +06:00
|
|
|
type Output = Result<S, T::InitError>;
|
2019-06-06 14:28:07 +06:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let mut this = self.project();
|
|
|
|
'poll: loop {
|
|
|
|
if let Some(fut) = this.srv_fut.as_mut().as_pin_mut() {
|
|
|
|
match fut.poll(cx)? {
|
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
Poll::Ready(srv) => {
|
|
|
|
this.srv_fut.set(None);
|
|
|
|
*this.srv = Some(srv);
|
|
|
|
continue 'poll;
|
|
|
|
}
|
2019-06-06 14:28:07 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
if let Some(fut) = this.fut.as_mut().as_pin_mut() {
|
|
|
|
return Poll::Ready(Ok(ready!(fut.poll(cx))?.into_service()));
|
|
|
|
} else if let Some(ref mut srv) = this.srv {
|
|
|
|
match srv.poll_ready(cx)? {
|
|
|
|
Poll::Ready(_) => {
|
|
|
|
this.fut.set(Some(this.f.get_mut()(&this.cfg, srv)));
|
|
|
|
continue 'poll;
|
|
|
|
}
|
|
|
|
Poll::Pending => return Poll::Pending,
|
2019-08-27 05:28:15 +06:00
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
} else {
|
|
|
|
return Poll::Pending;
|
2019-08-27 05:28:15 +06:00
|
|
|
}
|
2019-06-06 14:28:07 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|