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
|
|
|
|
pub fn apply_cfg<F, C, T, R, S>(srv: T, f: F) -> ApplyConfigService<F, C, T, R, S>
|
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-05-12 15:03:50 +02:00
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
|
|
|
pub 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-05-12 15:03:50 +02:00
|
|
|
pub struct FnNewServiceConfigFut<R, S>
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|