mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-27 19:12:56 +01:00
refactor ApplyConfig combinator
This commit is contained in:
parent
21c289d7e4
commit
ae9bc5ae78
@ -6,10 +6,14 @@
|
|||||||
|
|
||||||
* Add `Transform::from_err()` combinator
|
* Add `Transform::from_err()` combinator
|
||||||
|
|
||||||
* Add `apply_fn` and `apply_fn_factory` helpers
|
* Add `apply_fn` helper
|
||||||
|
|
||||||
|
* Add `apply_fn_factory` helper
|
||||||
|
|
||||||
* Add `apply_transform` helper
|
* Add `apply_transform` helper
|
||||||
|
|
||||||
|
* Add `apply_cfg` helper
|
||||||
|
|
||||||
|
|
||||||
## [0.3.3] - 2019-03-09
|
## [0.3.3] - 2019-03-09
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-service"
|
name = "actix-service"
|
||||||
version = "0.3.3"
|
version = "0.3.4"
|
||||||
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"]
|
||||||
@ -27,4 +27,4 @@ futures = "0.1.24"
|
|||||||
void = "1.0.2"
|
void = "1.0.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.1"
|
actix-rt = "0.2"
|
@ -1,121 +1,70 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use futures::{Async, Future, Poll};
|
|
||||||
|
|
||||||
use crate::and_then::AndThen;
|
|
||||||
use crate::{IntoNewService, NewService};
|
use crate::{IntoNewService, NewService};
|
||||||
|
|
||||||
/// `ApplyNewService` new service combinator
|
/// Create new ApplyConfig` service factory combinator
|
||||||
pub struct ApplyConfig<F, A, B, C1, C2> {
|
pub fn apply_cfg<F, S, C1, C2, U>(f: F, service: U) -> ApplyConfig<F, S, C1, C2>
|
||||||
a: A,
|
where
|
||||||
b: B,
|
S: NewService<C2>,
|
||||||
|
F: Fn(&C1) -> C2,
|
||||||
|
U: IntoNewService<S, C2>,
|
||||||
|
{
|
||||||
|
ApplyConfig::new(service.into_new_service(), f)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `ApplyConfig` service factory combinator
|
||||||
|
pub struct ApplyConfig<F, S, C1, C2> {
|
||||||
|
s: S,
|
||||||
f: F,
|
f: F,
|
||||||
r: PhantomData<(C1, C2)>,
|
r: PhantomData<(C1, C2)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, A, B, C1, C2> ApplyConfig<F, A, B, C1, C2>
|
impl<F, S, C1, C2> ApplyConfig<F, S, C1, C2>
|
||||||
where
|
where
|
||||||
A: NewService<C1>,
|
S: NewService<C2>,
|
||||||
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
|
||||||
F: Fn(&C1) -> C2,
|
F: Fn(&C1) -> C2,
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new ApplyConfig` service factory combinator
|
||||||
pub fn new<A1: IntoNewService<A, C1>, B1: IntoNewService<B, C2>>(
|
pub fn new<U: IntoNewService<S, C2>>(a: U, f: F) -> Self {
|
||||||
a: A1,
|
|
||||||
b: B1,
|
|
||||||
f: F,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
f,
|
f,
|
||||||
a: a.into_new_service(),
|
s: a.into_new_service(),
|
||||||
b: b.into_new_service(),
|
|
||||||
r: PhantomData,
|
r: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, A, B, C1, C2> Clone for ApplyConfig<F, A, B, C1, C2>
|
impl<F, S, C1, C2> Clone for ApplyConfig<F, S, C1, C2>
|
||||||
where
|
where
|
||||||
A: Clone,
|
S: Clone,
|
||||||
B: Clone,
|
|
||||||
F: Clone,
|
F: Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
a: self.a.clone(),
|
s: self.s.clone(),
|
||||||
b: self.b.clone(),
|
|
||||||
f: self.f.clone(),
|
f: self.f.clone(),
|
||||||
r: PhantomData,
|
r: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, A, B, C1, C2> NewService<C1> for ApplyConfig<F, A, B, C1, C2>
|
impl<F, S, C1, C2> NewService<C1> for ApplyConfig<F, S, C1, C2>
|
||||||
where
|
where
|
||||||
A: NewService<C1>,
|
S: NewService<C2>,
|
||||||
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
|
||||||
F: Fn(&C1) -> C2,
|
F: Fn(&C1) -> C2,
|
||||||
{
|
{
|
||||||
type Request = A::Request;
|
type Request = S::Request;
|
||||||
type Response = B::Response;
|
type Response = S::Response;
|
||||||
type Error = A::Error;
|
type Error = S::Error;
|
||||||
type Service = AndThen<A::Service, B::Service>;
|
type Service = S::Service;
|
||||||
|
|
||||||
type InitError = A::InitError;
|
type InitError = S::InitError;
|
||||||
type Future = ApplyConfigResponse<A, B, C1, C2>;
|
type Future = S::Future;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C1) -> Self::Future {
|
fn new_service(&self, cfg: &C1) -> Self::Future {
|
||||||
let cfg2 = (self.f)(cfg);
|
let cfg2 = (self.f)(cfg);
|
||||||
|
|
||||||
ApplyConfigResponse {
|
self.s.new_service(&cfg2)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,8 @@ 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_fn, apply_fn_factory};
|
pub use self::apply::{apply_fn, apply_fn_factory};
|
||||||
pub use self::apply_cfg::ApplyConfig;
|
pub use self::apply_cfg::apply_cfg;
|
||||||
|
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::fn_transform;
|
pub use self::fn_transform::fn_transform;
|
||||||
pub use self::from_err::{FromErr, FromErrNewService};
|
pub use self::from_err::{FromErr, FromErrNewService};
|
||||||
@ -257,19 +258,23 @@ pub trait NewService<Config = ()> {
|
|||||||
|
|
||||||
/// Map this service's config type to a different config,
|
/// Map this service's config type to a different config,
|
||||||
/// and use for nested service
|
/// and use for nested service
|
||||||
fn apply_cfg<F, C, B, B1>(self, service: B1, f: F) -> ApplyConfig<F, Self, B, Config, C>
|
fn apply_cfg<F, C, S, U>(
|
||||||
|
self,
|
||||||
|
service: U,
|
||||||
|
f: F,
|
||||||
|
) -> AndThenNewService<Self, ApplyConfig<F, S, Config, C>, Config>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
F: Fn(&Config) -> C,
|
F: Fn(&Config) -> C,
|
||||||
B1: IntoNewService<B, C>,
|
U: IntoNewService<S, C>,
|
||||||
B: NewService<
|
S: NewService<
|
||||||
C,
|
C,
|
||||||
Request = Self::Response,
|
Request = Self::Response,
|
||||||
Error = Self::Error,
|
Error = Self::Error,
|
||||||
InitError = Self::InitError,
|
InitError = Self::InitError,
|
||||||
>,
|
>,
|
||||||
{
|
{
|
||||||
ApplyConfig::new(self, service, f)
|
self.and_then(ApplyConfig::new(service, f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Call another service after call to this one has resolved successfully.
|
/// Call another service after call to this one has resolved successfully.
|
||||||
|
Loading…
Reference in New Issue
Block a user