2019-03-09 18:01:02 +01:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
use futures::future::Future;
|
|
|
|
use futures::{try_ready, Async, IntoFuture, Poll};
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
use crate::cell::Cell;
|
|
|
|
use crate::{IntoService, NewService, Service};
|
|
|
|
|
|
|
|
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
2019-06-06 10:28:07 +02:00
|
|
|
pub fn apply_cfg<F, C, T, R, S>(
|
|
|
|
srv: T,
|
|
|
|
f: F,
|
|
|
|
) -> impl NewService<
|
|
|
|
Config = C,
|
|
|
|
Request = S::Request,
|
|
|
|
Response = S::Response,
|
|
|
|
Error = S::Error,
|
|
|
|
Service = S,
|
|
|
|
InitError = R::Error,
|
|
|
|
> + Clone
|
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,
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
2019-03-13 00:32:10 +01:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
ApplyConfigService {
|
|
|
|
f: Cell::new(f),
|
|
|
|
srv: Cell::new(srv.into_service()),
|
|
|
|
_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.
|
|
|
|
pub fn new_apply_cfg<F, C, T, R, S>(
|
|
|
|
srv: T,
|
|
|
|
f: F,
|
|
|
|
) -> impl NewService<
|
|
|
|
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,
|
|
|
|
T: NewService<Config = ()>,
|
2019-08-27 01:28:15 +02:00
|
|
|
T::InitError: From<T::Error>,
|
2019-06-06 10:28:07 +02:00
|
|
|
R: IntoFuture<Error = T::InitError>,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
ApplyConfigNewService {
|
|
|
|
f: Cell::new(f),
|
|
|
|
srv: Cell::new(srv),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
2019-06-06 10:28:07 +02:00
|
|
|
struct ApplyConfigService<F, C, T, R, S>
|
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,
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
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-05-12 15:03:50 +02:00
|
|
|
impl<F, C, T, R, S> Clone for ApplyConfigService<F, C, T, R, S>
|
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,
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
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-05-12 15:03:50 +02:00
|
|
|
impl<F, C, T, R, S> NewService for ApplyConfigService<F, C, T, R, S>
|
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,
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
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-05-12 15:03:50 +02:00
|
|
|
type InitError = R::Error;
|
|
|
|
type Future = FnNewServiceConfigFut<R, S>;
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
|
|
|
FnNewServiceConfigFut {
|
|
|
|
fut: unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) }
|
|
|
|
.into_future(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 10:28:07 +02:00
|
|
|
struct FnNewServiceConfigFut<R, S>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
fut: R::Future,
|
|
|
|
_t: PhantomData<(S,)>,
|
|
|
|
}
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<R, S> Future for FnNewServiceConfigFut<R, S>
|
|
|
|
where
|
|
|
|
R: IntoFuture,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
type Item = S;
|
|
|
|
type Error = R::Error;
|
2019-03-09 18:01:02 +01:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
Ok(Async::Ready(try_ready!(self.fut.poll()).into_service()))
|
2019-03-09 18:01:02 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 10:28:07 +02: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,
|
|
|
|
T: NewService<Config = ()>,
|
|
|
|
R: IntoFuture<Error = T::InitError>,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
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,
|
|
|
|
T: NewService<Config = ()>,
|
|
|
|
R: IntoFuture<Error = T::InitError>,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ApplyConfigNewService {
|
|
|
|
f: self.f.clone(),
|
|
|
|
srv: self.srv.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, C, T, R, S> NewService for ApplyConfigNewService<F, C, T, R, S>
|
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
|
|
|
T: NewService<Config = ()>,
|
2019-08-27 01:28:15 +02:00
|
|
|
T::InitError: From<T::Error>,
|
2019-06-06 10:28:07 +02:00
|
|
|
R: IntoFuture<Error = T::InitError>,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
type Config = C;
|
|
|
|
type Request = S::Request;
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Service = S;
|
|
|
|
|
|
|
|
type InitError = R::Error;
|
|
|
|
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 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ApplyConfigNewServiceFut<F, C, T, R, S>
|
|
|
|
where
|
|
|
|
C: Clone,
|
|
|
|
F: FnMut(&C, &mut T::Service) -> R,
|
|
|
|
T: NewService<Config = ()>,
|
2019-08-27 01:28:15 +02:00
|
|
|
T::InitError: From<T::Error>,
|
2019-06-06 10:28:07 +02:00
|
|
|
R: IntoFuture<Error = T::InitError>,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
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-06-06 10:28:07 +02:00
|
|
|
fut: Option<R::Future>,
|
|
|
|
_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,
|
|
|
|
T: NewService<Config = ()>,
|
2019-08-27 01:28:15 +02:00
|
|
|
T::InitError: From<T::Error>,
|
2019-06-06 10:28:07 +02:00
|
|
|
R: IntoFuture<Error = T::InitError>,
|
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
type Item = S;
|
|
|
|
type Error = R::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-08-27 01:28:15 +02:00
|
|
|
if let Some(ref mut fut) = self.srv_fut {
|
2019-06-06 10:28:07 +02:00
|
|
|
match fut.poll()? {
|
|
|
|
Async::NotReady => return Ok(Async::NotReady),
|
2019-08-27 01:28:15 +02:00
|
|
|
Async::Ready(srv) => {
|
|
|
|
let _ = self.srv_fut.take();
|
|
|
|
self.srv = Some(srv);
|
2019-06-06 10:28:07 +02:00
|
|
|
return self.poll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref mut fut) = self.fut {
|
|
|
|
Ok(Async::Ready(try_ready!(fut.poll()).into_service()))
|
2019-08-27 01:28:15 +02:00
|
|
|
} else if let Some(ref mut srv) = self.srv {
|
|
|
|
match srv.poll_ready()? {
|
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
Async::Ready(_) => {
|
|
|
|
self.fut = Some(self.f.get_mut()(&self.cfg, srv).into_future());
|
|
|
|
return self.poll();
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 10:28:07 +02:00
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|