Skip to content

Commit

Permalink
refactor ApplyConfig combinator
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Mar 12, 2019
1 parent 21c289d commit ae9bc5a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 89 deletions.
6 changes: 5 additions & 1 deletion actix-service/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

* 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_cfg` helper


## [0.3.3] - 2019-03-09

Expand Down
4 changes: 2 additions & 2 deletions actix-service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-service"
version = "0.3.3"
version = "0.3.4"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix Service"
keywords = ["network", "framework", "async", "futures"]
Expand All @@ -27,4 +27,4 @@ futures = "0.1.24"
void = "1.0.2"

[dev-dependencies]
actix-rt = "0.1"
actix-rt = "0.2"
111 changes: 30 additions & 81 deletions actix-service/src/apply_cfg.rs
Original file line number Diff line number Diff line change
@@ -1,121 +1,70 @@
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,
/// Create new ApplyConfig` service factory combinator
pub fn apply_cfg<F, S, C1, C2, U>(f: F, service: U) -> ApplyConfig<F, S, C1, C2>
where
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,
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
A: NewService<C1>,
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
S: NewService<C2>,
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 {
/// Create new ApplyConfig` service factory combinator
pub fn new<U: IntoNewService<S, C2>>(a: U, f: F) -> Self {
Self {
f,
a: a.into_new_service(),
b: b.into_new_service(),
s: a.into_new_service(),
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
A: Clone,
B: Clone,
S: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
b: self.b.clone(),
s: self.s.clone(),
f: self.f.clone(),
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
A: NewService<C1>,
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
S: NewService<C2>,
F: Fn(&C1) -> C2,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Service = AndThen<A::Service, B::Service>;
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;

type InitError = A::InitError;
type Future = ApplyConfigResponse<A, B, C1, C2>;
type InitError = S::InitError;
type Future = S::Future;

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)
}
self.s.new_service(&cfg2)
}
}

Expand Down
15 changes: 10 additions & 5 deletions actix-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ pub use self::and_then::{AndThen, AndThenNewService};
use self::and_then_apply::AndThenTransform;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
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_transform::fn_transform;
pub use self::from_err::{FromErr, FromErrNewService};
Expand Down Expand Up @@ -257,19 +258,23 @@ pub trait NewService<Config = ()> {

/// 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>
fn apply_cfg<F, C, S, U>(
self,
service: U,
f: F,
) -> AndThenNewService<Self, ApplyConfig<F, S, Config, C>, Config>
where
Self: Sized,
F: Fn(&Config) -> C,
B1: IntoNewService<B, C>,
B: NewService<
U: IntoNewService<S, C>,
S: NewService<
C,
Request = Self::Response,
Error = Self::Error,
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.
Expand Down

0 comments on commit ae9bc5a

Please sign in to comment.