Skip to content

Commit

Permalink
Use Pipeline<T> instead of ApplyService<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Aug 14, 2023
1 parent 17ffaf1 commit 996f2af
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 36 deletions.
4 changes: 4 additions & 0 deletions ntex-service/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [1.2.5] - 2023-08-14

* Use Pipeline<T> instead of ApplyService<T>

## [1.2.4] - 2023-08-12

* Forward readiness check for Apply service
Expand Down
2 changes: 1 addition & 1 deletion ntex-service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-service"
version = "1.2.4"
version = "1.2.5"
authors = ["ntex contributors <team@ntex.rs>"]
description = "ntex service"
keywords = ["network", "framework", "async", "futures"]
Expand Down
43 changes: 13 additions & 30 deletions ntex-service/src/apply.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::type_complexity)]
use std::{future::Future, marker, pin::Pin, task, task::Poll};

use super::ctx::{ServiceCall, ServiceCtx};
use super::ctx::ServiceCtx;
use super::{IntoService, IntoServiceFactory, Pipeline, Service, ServiceFactory};

/// Apply transform function to a service.
Expand All @@ -11,7 +11,7 @@ pub fn apply_fn<T, Req, F, R, In, Out, Err, U>(
) -> Apply<T, Req, F, R, In, Out, Err>
where
T: Service<Req, Error = Err>,
F: Fn(In, ApplyService<T>) -> R,
F: Fn(In, Pipeline<T>) -> R,
R: Future<Output = Result<Out, Err>>,
U: IntoService<T, Req>,
{
Expand All @@ -25,7 +25,7 @@ pub fn apply_fn_factory<T, Req, Cfg, F, R, In, Out, Err, U>(
) -> ApplyFactory<T, Req, Cfg, F, R, In, Out, Err>
where
T: ServiceFactory<Req, Cfg, Error = Err>,
F: Fn(In, ApplyService<T::Service>) -> R + Clone,
F: Fn(In, Pipeline<T::Service>) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
U: IntoServiceFactory<T, Req, Cfg>,
{
Expand All @@ -42,24 +42,10 @@ where
r: marker::PhantomData<fn(Req) -> (In, Out, R)>,
}

pub struct ApplyService<S> {
service: Pipeline<S>,
}

impl<S> ApplyService<S> {
/// Check readiness and call enclosed service.
pub fn call<R>(&self, req: R) -> ServiceCall<'_, S, R>
where
S: Service<R>,
{
self.service.call(req)
}
}

impl<T, Req, F, R, In, Out, Err> Apply<T, Req, F, R, In, Out, Err>
where
T: Service<Req, Error = Err>,
F: Fn(In, ApplyService<T>) -> R,
F: Fn(In, Pipeline<T>) -> R,
R: Future<Output = Result<Out, Err>>,
{
pub(crate) fn new(service: T, f: F) -> Self {
Expand All @@ -74,7 +60,7 @@ where
impl<T, Req, F, R, In, Out, Err> Clone for Apply<T, Req, F, R, In, Out, Err>
where
T: Service<Req, Error = Err> + Clone,
F: Fn(In, ApplyService<T>) -> R + Clone,
F: Fn(In, Pipeline<T>) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
fn clone(&self) -> Self {
Expand All @@ -89,7 +75,7 @@ where
impl<T, Req, F, R, In, Out, Err> Service<In> for Apply<T, Req, F, R, In, Out, Err>
where
T: Service<Req, Error = Err>,
F: Fn(In, ApplyService<T>) -> R,
F: Fn(In, Pipeline<T>) -> R,
R: Future<Output = Result<Out, Err>>,
{
type Response = Out;
Expand All @@ -101,18 +87,15 @@ where

#[inline]
fn call<'a>(&'a self, req: In, _: ServiceCtx<'a, Self>) -> Self::Future<'a> {
let svc = ApplyService {
service: self.service.clone(),
};
(self.f)(req, svc)
(self.f)(req, self.service.clone())
}
}

/// `apply()` service factory
pub struct ApplyFactory<T, Req, Cfg, F, R, In, Out, Err>
where
T: ServiceFactory<Req, Cfg, Error = Err>,
F: Fn(In, ApplyService<T::Service>) -> R + Clone,
F: Fn(In, Pipeline<T::Service>) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
service: T,
Expand All @@ -123,7 +106,7 @@ where
impl<T, Req, Cfg, F, R, In, Out, Err> ApplyFactory<T, Req, Cfg, F, R, In, Out, Err>
where
T: ServiceFactory<Req, Cfg, Error = Err>,
F: Fn(In, ApplyService<T::Service>) -> R + Clone,
F: Fn(In, Pipeline<T::Service>) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
/// Create new `ApplyNewService` new service instance
Expand All @@ -140,7 +123,7 @@ impl<T, Req, Cfg, F, R, In, Out, Err> Clone
for ApplyFactory<T, Req, Cfg, F, R, In, Out, Err>
where
T: ServiceFactory<Req, Cfg, Error = Err> + Clone,
F: Fn(In, ApplyService<T::Service>) -> R + Clone,
F: Fn(In, Pipeline<T::Service>) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
fn clone(&self) -> Self {
Expand All @@ -156,7 +139,7 @@ impl<T, Req, Cfg, F, R, In, Out, Err> ServiceFactory<In, Cfg>
for ApplyFactory<T, Req, Cfg, F, R, In, Out, Err>
where
T: ServiceFactory<Req, Cfg, Error = Err>,
F: Fn(In, ApplyService<T::Service>) -> R + Clone,
F: Fn(In, Pipeline<T::Service>) -> R + Clone,
for<'r> R: Future<Output = Result<Out, Err>> + 'r,
{
type Response = Out;
Expand All @@ -181,7 +164,7 @@ pin_project_lite::pin_project! {
where
T: ServiceFactory<Req, Cfg, Error = Err>,
T: 'f,
F: Fn(In, ApplyService<T::Service>) -> R,
F: Fn(In, Pipeline<T::Service>) -> R,
T::Service: 'f,
R: Future<Output = Result<Out, Err>>,
Cfg: 'f,
Expand All @@ -197,7 +180,7 @@ impl<'f, T, Req, Cfg, F, R, In, Out, Err> Future
for ApplyFactoryResponse<'f, T, Req, Cfg, F, R, In, Out, Err>
where
T: ServiceFactory<Req, Cfg, Error = Err>,
F: Fn(In, ApplyService<T::Service>) -> R,
F: Fn(In, Pipeline<T::Service>) -> R,
R: Future<Output = Result<Out, Err>>,
{
type Output = Result<Apply<T::Service, Req, F, R, In, Out, Err>, T::InitError>;
Expand Down
6 changes: 3 additions & 3 deletions ntex-service/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{future::Future, marker::PhantomData};

use crate::and_then::{AndThen, AndThenFactory};
use crate::apply::{Apply, ApplyFactory, ApplyService};
use crate::apply::{Apply, ApplyFactory};
use crate::ctx::{ServiceCall, ServiceCtx};
use crate::map::{Map, MapFactory};
use crate::map_err::{MapErr, MapErrFactory};
Expand Down Expand Up @@ -128,7 +128,7 @@ impl<Svc: Service<Req>, Req> ServiceChain<Svc, Req> {
f: F,
) -> ServiceChain<Apply<Svc, Req, F, R, In, Out, Err>, In>
where
F: Fn(In, ApplyService<Svc>) -> R,
F: Fn(In, Pipeline<Svc>) -> R,
R: Future<Output = Result<Out, Err>>,
Svc: Service<Req, Error = Err>,
{
Expand Down Expand Up @@ -214,7 +214,7 @@ impl<T: ServiceFactory<Req, C>, Req, C> ServiceChainFactory<T, Req, C> {
f: F,
) -> ServiceChainFactory<ApplyFactory<T, Req, C, F, R, In, Out, Err>, In, C>
where
F: Fn(In, ApplyService<T::Service>) -> R + Clone,
F: Fn(In, Pipeline<T::Service>) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
T: ServiceFactory<Req, C, Error = Err>,
{
Expand Down
5 changes: 4 additions & 1 deletion ntex-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ where

pub mod dev {
pub use crate::and_then::{AndThen, AndThenFactory};
pub use crate::apply::{Apply, ApplyFactory, ApplyService};
pub use crate::apply::{Apply, ApplyFactory};
pub use crate::chain::{ServiceChain, ServiceChainFactory};
pub use crate::fn_service::{
FnService, FnServiceConfig, FnServiceFactory, FnServiceNoConfig,
Expand All @@ -402,4 +402,7 @@ pub mod dev {
pub use crate::middleware::ApplyMiddleware;
pub use crate::pipeline::CreatePipeline;
pub use crate::then::{Then, ThenFactory};

#[doc(hidden)]
pub type ApplyService<T> = crate::Pipeline<T>;
}
2 changes: 1 addition & 1 deletion ntex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ntex-codec = "0.6.2"
ntex-connect = "0.3.0"
ntex-http = "0.1.9"
ntex-router = "0.5.1"
ntex-service = "1.2.3"
ntex-service = "1.2.5"
ntex-macros = "0.1.3"
ntex-util = "0.3.0"
ntex-bytes = "0.1.19"
Expand Down

0 comments on commit 996f2af

Please sign in to comment.