mirror of
https://github.com/fafhrd91/actix-net
synced 2025-01-31 07:02:08 +01:00
add .apply_cfg new service combinator
This commit is contained in:
parent
d2b96ff877
commit
629ef23371
@ -1,11 +1,14 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [0.3.2] - 2019-03-xx
|
## [0.3.3] - 2019-03-xx
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
* Add `ApplyTransform` new service for transform and new service.
|
* Add `ApplyTransform` new service for transform and new service.
|
||||||
|
|
||||||
|
* Add `NewService::apply_cfg()` combinator, it allows to use
|
||||||
|
nested `NewService` with different config parameter.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
* Revert IntoFuture change
|
* Revert IntoFuture change
|
||||||
|
163
actix-service/src/apply_cfg.rs
Normal file
163
actix-service/src/apply_cfg.rs
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
|
use crate::and_then::AndThen;
|
||||||
|
use crate::{IntoNewService, NewService};
|
||||||
|
|
||||||
|
/// `ApplyNewService` new service combinator
|
||||||
|
pub struct ApplyConfig<F, A, B, C1, C2> {
|
||||||
|
a: A,
|
||||||
|
b: B,
|
||||||
|
f: F,
|
||||||
|
r: PhantomData<(C1, C2)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, A, B, C1, C2> ApplyConfig<F, A, B, C1, C2>
|
||||||
|
where
|
||||||
|
A: NewService<C1>,
|
||||||
|
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
||||||
|
F: Fn(&C1) -> C2,
|
||||||
|
{
|
||||||
|
/// Create new `ApplyNewService` new service instance
|
||||||
|
pub fn new<A1: IntoNewService<A, C1>, B1: IntoNewService<B, C2>>(
|
||||||
|
a: A1,
|
||||||
|
b: B1,
|
||||||
|
f: F,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
f,
|
||||||
|
a: a.into_new_service(),
|
||||||
|
b: b.into_new_service(),
|
||||||
|
r: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, A, B, C1, C2> Clone for ApplyConfig<F, A, B, C1, C2>
|
||||||
|
where
|
||||||
|
A: Clone,
|
||||||
|
B: Clone,
|
||||||
|
F: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
a: self.a.clone(),
|
||||||
|
b: self.b.clone(),
|
||||||
|
f: self.f.clone(),
|
||||||
|
r: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, A, B, C1, C2> NewService<C1> for ApplyConfig<F, A, B, C1, C2>
|
||||||
|
where
|
||||||
|
A: NewService<C1>,
|
||||||
|
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
||||||
|
F: Fn(&C1) -> C2,
|
||||||
|
{
|
||||||
|
type Request = A::Request;
|
||||||
|
type Response = B::Response;
|
||||||
|
type Error = A::Error;
|
||||||
|
type Service = AndThen<A::Service, B::Service>;
|
||||||
|
|
||||||
|
type InitError = A::InitError;
|
||||||
|
type Future = ApplyConfigResponse<A, B, C1, C2>;
|
||||||
|
|
||||||
|
fn new_service(&self, cfg: &C1) -> Self::Future {
|
||||||
|
let cfg2 = (self.f)(cfg);
|
||||||
|
|
||||||
|
ApplyConfigResponse {
|
||||||
|
a: None,
|
||||||
|
b: None,
|
||||||
|
fut_a: self.a.new_service(cfg),
|
||||||
|
fut_b: self.b.new_service(&cfg2),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ApplyConfigResponse<A, B, C1, C2>
|
||||||
|
where
|
||||||
|
A: NewService<C1>,
|
||||||
|
B: NewService<C2>,
|
||||||
|
{
|
||||||
|
fut_b: B::Future,
|
||||||
|
fut_a: A::Future,
|
||||||
|
a: Option<A::Service>,
|
||||||
|
b: Option<B::Service>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B, C1, C2> Future for ApplyConfigResponse<A, B, C1, C2>
|
||||||
|
where
|
||||||
|
A: NewService<C1>,
|
||||||
|
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
||||||
|
{
|
||||||
|
type Item = AndThen<A::Service, B::Service>;
|
||||||
|
type Error = A::InitError;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
if self.a.is_none() {
|
||||||
|
if let Async::Ready(service) = self.fut_a.poll()? {
|
||||||
|
self.a = Some(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.b.is_none() {
|
||||||
|
if let Async::Ready(service) = self.fut_b.poll()? {
|
||||||
|
self.b = Some(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.a.is_some() && self.b.is_some() {
|
||||||
|
Ok(Async::Ready(AndThen::new(
|
||||||
|
self.a.take().unwrap(),
|
||||||
|
self.b.take().unwrap(),
|
||||||
|
)))
|
||||||
|
} else {
|
||||||
|
Ok(Async::NotReady)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use futures::future::{ok, FutureResult};
|
||||||
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
|
use crate::{fn_cfg_factory, NewService, Service};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct Srv;
|
||||||
|
impl Service for Srv {
|
||||||
|
type Request = ();
|
||||||
|
type Response = ();
|
||||||
|
type Error = ();
|
||||||
|
type Future = FutureResult<(), ()>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, _: ()) -> Self::Future {
|
||||||
|
ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_service() {
|
||||||
|
let new_srv = fn_cfg_factory(|_: &usize| Ok::<_, ()>(Srv)).apply_cfg(
|
||||||
|
fn_cfg_factory(|s: &String| {
|
||||||
|
assert_eq!(s, "test");
|
||||||
|
Ok::<_, ()>(Srv)
|
||||||
|
}),
|
||||||
|
|cfg: &usize| {
|
||||||
|
assert_eq!(*cfg, 1);
|
||||||
|
"test".to_string()
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Async::Ready(mut srv) = new_srv.new_service(&1).poll().unwrap() {
|
||||||
|
assert!(srv.poll_ready().is_ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@ mod and_then;
|
|||||||
mod and_then_apply;
|
mod and_then_apply;
|
||||||
mod and_then_apply_fn;
|
mod and_then_apply_fn;
|
||||||
mod apply;
|
mod apply;
|
||||||
|
mod apply_cfg;
|
||||||
pub mod blank;
|
pub mod blank;
|
||||||
pub mod boxed;
|
pub mod boxed;
|
||||||
mod cell;
|
mod cell;
|
||||||
@ -26,6 +27,7 @@ pub use self::and_then::{AndThen, AndThenNewService};
|
|||||||
use self::and_then_apply::AndThenTransform;
|
use self::and_then_apply::AndThenTransform;
|
||||||
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
||||||
pub use self::apply::{Apply, ApplyNewService};
|
pub use self::apply::{Apply, ApplyNewService};
|
||||||
|
use self::apply_cfg::ApplyConfig;
|
||||||
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
|
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
|
||||||
pub use self::fn_transform::FnTransform;
|
pub use self::fn_transform::FnTransform;
|
||||||
pub use self::from_err::{FromErr, FromErrNewService};
|
pub use self::from_err::{FromErr, FromErrNewService};
|
||||||
@ -204,7 +206,7 @@ pub trait NewService<Config = ()> {
|
|||||||
/// Create and return a new service value asynchronously.
|
/// Create and return a new service value asynchronously.
|
||||||
fn new_service(&self, cfg: &Config) -> Self::Future;
|
fn new_service(&self, cfg: &Config) -> Self::Future;
|
||||||
|
|
||||||
/// Apply function to specified service and use it as a next service in
|
/// Apply transform service to specified service and use it as a next service in
|
||||||
/// chain.
|
/// chain.
|
||||||
fn apply<T, T1, B, B1>(
|
fn apply<T, T1, B, B1>(
|
||||||
self,
|
self,
|
||||||
@ -240,6 +242,23 @@ pub trait NewService<Config = ()> {
|
|||||||
AndThenApplyNewService::new(self, service, f)
|
AndThenApplyNewService::new(self, service, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Map this service's config type to a different config,
|
||||||
|
/// and use for nested service
|
||||||
|
fn apply_cfg<F, C, B, B1>(self, service: B1, f: F) -> ApplyConfig<F, Self, B, Config, C>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: Fn(&Config) -> C,
|
||||||
|
B1: IntoNewService<B, C>,
|
||||||
|
B: NewService<
|
||||||
|
C,
|
||||||
|
Request = Self::Response,
|
||||||
|
Error = Self::Error,
|
||||||
|
InitError = Self::InitError,
|
||||||
|
>,
|
||||||
|
{
|
||||||
|
ApplyConfig::new(self, service, f)
|
||||||
|
}
|
||||||
|
|
||||||
/// Call another service after call to this one has resolved successfully.
|
/// Call another service after call to this one has resolved successfully.
|
||||||
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B, Config>
|
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B, Config>
|
||||||
where
|
where
|
||||||
|
Loading…
x
Reference in New Issue
Block a user