2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
2019-03-09 18:01:02 +01:00
|
|
|
use std::marker::PhantomData;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
use crate::cell::Cell;
|
2019-11-18 09:30:04 +01:00
|
|
|
use crate::{Service, ServiceFactory};
|
2019-05-12 15:03:50 +02:00
|
|
|
|
|
|
|
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
2019-11-18 09:30:04 +01:00
|
|
|
pub fn apply_cfg<F, C, T, R, S, E>(srv: T, f: F) -> ApplyConfigService<F, C, T, R, S, E>
|
2019-03-13 00:32:10 +01:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
F: FnMut(&C, &mut T) -> R,
|
|
|
|
T: Service,
|
2019-11-18 09:30:04 +01:00
|
|
|
R: Future<Output = Result<S, E>> + Unpin,
|
2019-05-12 15:03:50 +02:00
|
|
|
S: Service,
|
2019-03-13 00:32:10 +01:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
ApplyConfigService {
|
|
|
|
f: Cell::new(f),
|
2019-11-14 13:38:24 +01:00
|
|
|
srv: Cell::new(srv),
|
2019-05-12 15:03:50 +02:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
|
2019-06-06 10:28:07 +02:00
|
|
|
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
|
|
|
/// Service get constructor from NewService.
|
2019-11-14 13:38:24 +01:00
|
|
|
pub fn apply_cfg_factory<F, C, T, R, S>(
|
2019-06-06 10:28:07 +02:00
|
|
|
srv: T,
|
|
|
|
f: F,
|
2019-11-18 09:30:04 +01:00
|
|
|
) -> ApplyConfigServiceFactory<F, C, T, R, S>
|
2019-06-06 10:28:07 +02:00
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 13:38:24 +01:00
|
|
|
T: ServiceFactory<Config = ()>,
|
2019-08-27 01:28:15 +02:00
|
|
|
T::InitError: From<T::Error>,
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, T::InitError>>,
|
2019-06-06 10:28:07 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
2019-11-18 09:30:04 +01:00
|
|
|
ApplyConfigServiceFactory {
|
2019-06-06 10:28:07 +02:00
|
|
|
f: Cell::new(f),
|
|
|
|
srv: Cell::new(srv),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService\
|
2019-11-18 09:30:04 +01:00
|
|
|
pub struct ApplyConfigService<F, C, T, R, S, E>
|
2019-03-09 18:01:02 +01:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
F: FnMut(&C, &mut T) -> R,
|
|
|
|
T: Service,
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 15:03:50 +02:00
|
|
|
S: Service,
|
2019-03-09 18:01:02 +01:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
f: Cell<F>,
|
|
|
|
srv: Cell<T>,
|
|
|
|
_t: PhantomData<(C, R, S)>,
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<F, C, T, R, S, E> Clone for ApplyConfigService<F, C, T, R, S, E>
|
2019-03-09 18:01:02 +01:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
F: FnMut(&C, &mut T) -> R,
|
|
|
|
T: Service,
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 15:03:50 +02:00
|
|
|
S: Service,
|
2019-03-09 18:01:02 +01:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
2019-05-12 15:03:50 +02:00
|
|
|
ApplyConfigService {
|
2019-03-09 18:01:02 +01:00
|
|
|
f: self.f.clone(),
|
2019-05-12 15:03:50 +02:00
|
|
|
srv: self.srv.clone(),
|
|
|
|
_t: PhantomData,
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<F, C, T, R, S, E> ServiceFactory for ApplyConfigService<F, C, T, R, S, E>
|
2019-03-09 18:01:02 +01:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
F: FnMut(&C, &mut T) -> R,
|
|
|
|
T: Service,
|
2019-11-18 09:30:04 +01:00
|
|
|
T::Future: Unpin,
|
|
|
|
R: Future<Output = Result<S, E>> + Unpin,
|
2019-05-12 15:03:50 +02:00
|
|
|
S: Service,
|
2019-03-09 18:01:02 +01:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = C;
|
2019-03-13 00:32:10 +01:00
|
|
|
type Request = S::Request;
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
2019-05-12 15:03:50 +02:00
|
|
|
type Service = S;
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
type InitError = E;
|
2019-11-18 09:30:04 +01:00
|
|
|
type Future = ApplyConfigServiceResponse<R, S, E>;
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
2019-11-18 09:30:04 +01:00
|
|
|
ApplyConfigServiceResponse {
|
2019-11-14 13:38:24 +01:00
|
|
|
fut: unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) },
|
2019-05-12 15:03:50 +02:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
pub struct ApplyConfigServiceResponse<R, S, E>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 15:03:50 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
fut: R,
|
2019-05-12 15:03:50 +02:00
|
|
|
_t: PhantomData<(S,)>,
|
|
|
|
}
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<R, S, E> Unpin for ApplyConfigServiceResponse<R, S, E>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2019-11-18 09:30:04 +01:00
|
|
|
R: Future<Output = Result<S, E>> + Unpin,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R, S, E> Future for ApplyConfigServiceResponse<R, S, E>
|
|
|
|
where
|
|
|
|
R: Future<Output = Result<S, E>> + Unpin,
|
2019-05-12 15:03:50 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = Result<S, E>;
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-18 09:30:04 +01:00
|
|
|
Pin::new(&mut self.get_mut().fut).poll(cx)
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 10:28:07 +02:00
|
|
|
|
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
2019-11-18 09:30:04 +01:00
|
|
|
pub struct ApplyConfigServiceFactory<F, C, T, R, S>
|
2019-06-06 10:28:07 +02:00
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 13:38:24 +01:00
|
|
|
T: ServiceFactory<Config = ()>,
|
|
|
|
R: Future<Output = Result<S, T::InitError>>,
|
2019-06-06 10:28:07 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
f: Cell<F>,
|
|
|
|
srv: Cell<T>,
|
|
|
|
_t: PhantomData<(C, R, S)>,
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, C, T, R, S> Clone for ApplyConfigServiceFactory<F, C, T, R, S>
|
2019-06-06 10:28:07 +02:00
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 13:38:24 +01:00
|
|
|
T: ServiceFactory<Config = ()>,
|
|
|
|
R: Future<Output = Result<S, T::InitError>>,
|
2019-06-06 10:28:07 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
2019-11-18 09:30:04 +01:00
|
|
|
Self {
|
2019-06-06 10:28:07 +02:00
|
|
|
f: self.f.clone(),
|
|
|
|
srv: self.srv.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, C, T, R, S> ServiceFactory for ApplyConfigServiceFactory<F, C, T, R, S>
|
2019-06-06 10:28:07 +02:00
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 13:38:24 +01:00
|
|
|
T: ServiceFactory<Config = ()>,
|
2019-11-18 09:30:04 +01:00
|
|
|
T::Future: Unpin,
|
2019-08-27 01:28:15 +02:00
|
|
|
T::InitError: From<T::Error>,
|
2019-11-18 09:30:04 +01:00
|
|
|
R: Future<Output = Result<S, T::InitError>> + Unpin,
|
2019-06-06 10:28:07 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
type Config = C;
|
|
|
|
type Request = S::Request;
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Service = S;
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
type InitError = T::InitError;
|
2019-11-18 09:30:04 +01:00
|
|
|
type Future = ApplyConfigServiceFactoryResponse<F, C, T, R, S>;
|
2019-06-06 10:28:07 +02:00
|
|
|
|
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
2019-11-18 09:30:04 +01:00
|
|
|
ApplyConfigServiceFactoryResponse {
|
2019-06-06 10:28:07 +02:00
|
|
|
f: self.f.clone(),
|
|
|
|
cfg: cfg.clone(),
|
|
|
|
fut: None,
|
2019-08-27 01:28:15 +02:00
|
|
|
srv: None,
|
|
|
|
srv_fut: Some(self.srv.get_ref().new_service(&())),
|
2019-06-06 10:28:07 +02:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
pub struct ApplyConfigServiceFactoryResponse<F, C, T, R, S>
|
2019-06-06 10:28:07 +02:00
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 13:38:24 +01:00
|
|
|
T: ServiceFactory<Config = ()>,
|
2019-08-27 01:28:15 +02:00
|
|
|
T::InitError: From<T::Error>,
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, T::InitError>>,
|
2019-06-06 10:28:07 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
cfg: C,
|
|
|
|
f: Cell<F>,
|
2019-08-27 01:28:15 +02:00
|
|
|
srv: Option<T::Service>,
|
|
|
|
srv_fut: Option<T::Future>,
|
2019-11-14 13:38:24 +01:00
|
|
|
fut: Option<R>,
|
2019-06-06 10:28:07 +02:00
|
|
|
_t: PhantomData<(S,)>,
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, C, T, R, S> Unpin for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
|
2019-06-06 10:28:07 +02:00
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
2019-11-14 13:38:24 +01:00
|
|
|
T: ServiceFactory<Config = ()>,
|
2019-11-18 09:30:04 +01:00
|
|
|
T::Future: Unpin,
|
2019-08-27 01:28:15 +02:00
|
|
|
T::InitError: From<T::Error>,
|
2019-11-18 09:30:04 +01:00
|
|
|
R: Future<Output = Result<S, T::InitError>> + Unpin,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, C, T, R, S> Future for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
|
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
|
|
|
T: ServiceFactory<Config = ()>,
|
|
|
|
T::Future: Unpin,
|
|
|
|
T::InitError: From<T::Error>,
|
|
|
|
R: Future<Output = Result<S, T::InitError>> + Unpin,
|
2019-06-06 10:28:07 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = Result<S, T::InitError>;
|
2019-06-06 10:28:07 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-18 09:30:04 +01:00
|
|
|
let this = self.get_mut();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if let Some(ref mut fut) = this.srv_fut {
|
|
|
|
match Pin::new(fut).poll(cx)? {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
Poll::Ready(srv) => {
|
2019-11-18 09:30:04 +01:00
|
|
|
let _ = this.srv_fut.take();
|
|
|
|
this.srv = Some(srv);
|
|
|
|
continue;
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
2019-06-06 10:28:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
if let Some(ref mut fut) = this.fut {
|
|
|
|
return Pin::new(fut).poll(cx);
|
2019-11-14 13:38:24 +01:00
|
|
|
} else if let Some(ref mut srv) = this.srv {
|
|
|
|
match srv.poll_ready(cx)? {
|
|
|
|
Poll::Ready(_) => {
|
2019-11-18 09:30:04 +01:00
|
|
|
this.fut = Some(this.f.get_mut()(&this.cfg, srv));
|
|
|
|
continue;
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
Poll::Pending => return Poll::Pending,
|
2019-08-27 01:28:15 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
} else {
|
|
|
|
return Poll::Pending;
|
2019-08-27 01:28:15 +02:00
|
|
|
}
|
2019-06-06 10:28:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|