1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-28 03:02:58 +01:00
actix-net/actix-service/src/apply_cfg.rs

105 lines
2.3 KiB
Rust
Raw Normal View History

2019-03-09 18:01:02 +01:00
use std::marker::PhantomData;
use futures::future::Future;
use futures::{try_ready, Async, IntoFuture, Poll};
2019-03-09 18:01:02 +01: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
F: FnMut(&C, &mut T) -> R,
T: Service,
R: IntoFuture,
R::Item: IntoService<S>,
S: Service,
2019-03-13 00:32:10 +01:00
{
ApplyConfigService {
f: Cell::new(f),
srv: Cell::new(srv.into_service()),
_t: PhantomData,
}
2019-03-09 18:01:02 +01: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
F: FnMut(&C, &mut T) -> R,
T: Service,
R: IntoFuture,
R::Item: IntoService<S>,
S: Service,
2019-03-09 18:01:02 +01:00
{
f: Cell<F>,
srv: Cell<T>,
_t: PhantomData<(C, R, S)>,
2019-03-09 18:01:02 +01:00
}
impl<F, C, T, R, S> Clone for ApplyConfigService<F, C, T, R, S>
2019-03-09 18:01:02 +01:00
where
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 {
ApplyConfigService {
2019-03-09 18:01:02 +01:00
f: self.f.clone(),
srv: self.srv.clone(),
_t: PhantomData,
2019-03-09 18:01:02 +01:00
}
}
}
impl<F, C, T, R, S> NewService for ApplyConfigService<F, C, T, R, S>
2019-03-09 18:01:02 +01:00
where
F: FnMut(&C, &mut T) -> R,
T: Service,
R: IntoFuture,
R::Item: IntoService<S>,
S: Service,
2019-03-09 18:01:02 +01:00
{
type Config = C;
2019-03-13 00:32:10 +01:00
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S;
2019-03-09 18:01:02 +01:00
type InitError = R::Error;
type Future = FnNewServiceConfigFut<R, S>;
2019-03-09 18:01:02 +01: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
}
}
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
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
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
}
}