1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 00:01:11 +01:00

Add new_apply_cfg function

This commit is contained in:
Nikolay Kim 2019-06-06 14:28:07 +06:00
parent 7051888289
commit 158482cd2f
5 changed files with 163 additions and 5 deletions

View File

@ -1,5 +1,11 @@
# Changes # Changes
## [0.4.1] - 2019-06-06
### Added
* Add `new_apply_cfg` function
## [0.4.0] - 2019-05-12 ## [0.4.0] - 2019-05-12
### Changed ### Changed

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-service" name = "actix-service"
version = "0.4.0" version = "0.4.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix Service" description = "Actix Service"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]

View File

@ -7,7 +7,17 @@ use crate::cell::Cell;
use crate::{IntoService, NewService, Service}; use crate::{IntoService, NewService, Service};
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService /// 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> 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
where where
F: FnMut(&C, &mut T) -> R, F: FnMut(&C, &mut T) -> R,
T: Service, T: Service,
@ -22,8 +32,36 @@ where
} }
} }
/// 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 = ()>,
R: IntoFuture<Error = T::InitError>,
R::Item: IntoService<S>,
S: Service,
{
ApplyConfigNewService {
f: Cell::new(f),
srv: Cell::new(srv),
_t: PhantomData,
}
}
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService /// Convert `Fn(&Config) -> Future<Service>` fn to NewService
pub struct ApplyConfigService<F, C, T, R, S> struct ApplyConfigService<F, C, T, R, S>
where where
F: FnMut(&C, &mut T) -> R, F: FnMut(&C, &mut T) -> R,
T: Service, T: Service,
@ -79,7 +117,7 @@ where
} }
} }
pub struct FnNewServiceConfigFut<R, S> struct FnNewServiceConfigFut<R, S>
where where
R: IntoFuture, R: IntoFuture,
R::Item: IntoService<S>, R::Item: IntoService<S>,
@ -102,3 +140,113 @@ where
Ok(Async::Ready(try_ready!(self.fut.poll()).into_service())) Ok(Async::Ready(try_ready!(self.fut.poll()).into_service()))
} }
} }
/// 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 = ()>,
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(),
srv: Some(self.srv.get_ref().new_service(&())),
fut: None,
_t: PhantomData,
}
}
}
struct ApplyConfigNewServiceFut<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,
{
cfg: C,
f: Cell<F>,
srv: Option<T::Future>,
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 = ()>,
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> {
if let Some(ref mut fut) = self.srv {
match fut.poll()? {
Async::NotReady => return Ok(Async::NotReady),
Async::Ready(mut srv) => {
let _ = self.srv.take();
self.fut = Some(self.f.get_mut()(&self.cfg, &mut srv).into_future());
return self.poll();
}
}
}
if let Some(ref mut fut) = self.fut {
Ok(Async::Ready(try_ready!(fut.poll()).into_service()))
} else {
Ok(Async::NotReady)
}
}
}

View File

@ -26,6 +26,10 @@ impl<T> Cell<T> {
} }
} }
pub(crate) fn get_ref(&self) -> &T {
unsafe { &*self.inner.as_ref().get() }
}
pub(crate) fn get_mut(&mut self) -> &mut T { pub(crate) fn get_mut(&mut self) -> &mut T {
unsafe { &mut *self.inner.as_ref().get() } unsafe { &mut *self.inner.as_ref().get() }
} }

View File

@ -25,7 +25,7 @@ mod transform_err;
pub use self::and_then::{AndThen, AndThenNewService}; pub use self::and_then::{AndThen, AndThenNewService};
pub use self::apply::{apply_fn, new_apply_fn, Apply, ApplyNewService}; pub use self::apply::{apply_fn, new_apply_fn, Apply, ApplyNewService};
pub use self::apply_cfg::apply_cfg; pub use self::apply_cfg::{apply_cfg, new_apply_cfg};
pub use self::fn_service::{new_service_cfg, new_service_fn, service_fn, ServiceFn}; pub use self::fn_service::{new_service_cfg, new_service_fn, service_fn, ServiceFn};
pub use self::fn_transform::transform_fn; pub use self::fn_transform::transform_fn;
pub use self::from_err::{FromErr, FromErrNewService}; pub use self::from_err::{FromErr, FromErrNewService};