From f49a0b8de28d2ac0f96f800cb1353aab853d1a89 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 17 Sep 2021 10:40:13 +0600 Subject: [PATCH] Simplify fn_transform --- ntex-service/CHANGES.md | 4 ++ ntex-service/Cargo.toml | 2 +- ntex-service/src/fn_transform.rs | 96 ++++---------------------------- ntex-service/src/transform.rs | 3 +- 4 files changed, 17 insertions(+), 88 deletions(-) diff --git a/ntex-service/CHANGES.md b/ntex-service/CHANGES.md index 573a9068..7d8f82a5 100644 --- a/ntex-service/CHANGES.md +++ b/ntex-service/CHANGES.md @@ -1,5 +1,9 @@ # Changes +## [0.2.1] - 2021-09-17 + +* Simplify fn_transform + ## [0.2.0] - 2021-09-15 * Refactor Transform trait diff --git a/ntex-service/Cargo.toml b/ntex-service/Cargo.toml index ccf93926..7dc4cb9e 100644 --- a/ntex-service/Cargo.toml +++ b/ntex-service/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ntex-service" -version = "0.2.0" +version = "0.2.1" authors = ["ntex contributors "] description = "ntex service" keywords = ["network", "framework", "async", "futures"] diff --git a/ntex-service/src/fn_transform.rs b/ntex-service/src/fn_transform.rs index 781d7330..99fc1196 100644 --- a/ntex-service/src/fn_transform.rs +++ b/ntex-service/src/fn_transform.rs @@ -1,101 +1,25 @@ -use std::{future::Future, marker::PhantomData}; - -use crate::{apply_fn, dev::Apply, Service, Transform}; +use crate::Transform; /// Use function as transform service -pub fn fn_transform(f: F) -> FnTransform -where - S: Service, - F: Fn(Req, &S) -> R + Clone, - R: Future>, -{ - FnTransform::new(f) +pub fn fn_transform(f: F) -> FnTransform { + FnTransform(f) } -pub struct FnTransform -where - S: Service, - F: Fn(Req, &S) -> R + Clone, - R: Future>, -{ - f: F, - _t: PhantomData<(S, R, Req)>, -} +pub struct FnTransform(F); -impl FnTransform +impl Transform for FnTransform where - S: Service, - F: Fn(Req, &S) -> R + Clone, - R: Future>, + F: Fn(S) -> Out, { - fn new(f: F) -> Self { - FnTransform { f, _t: PhantomData } - } -} - -impl Transform for FnTransform -where - S: Service, - F: Fn(Req, &S) -> R + Clone, - R: Future>, -{ - type Service = Apply; + type Service = Out; fn new_transform(&self, service: S) -> Self::Service { - apply_fn(service, self.f.clone()) + (self.0)(service) } } -impl Clone for FnTransform -where - S: Service, - F: Fn(Req, &S) -> R + Clone, - R: Future>, -{ +impl Clone for FnTransform { fn clone(&self) -> Self { - Self::new(self.f.clone()) - } -} - -#[cfg(test)] -#[allow(clippy::redundant_clone)] -mod tests { - use ntex_util::future::{lazy, Ready}; - use std::task::{Context, Poll}; - - use super::*; - use crate::Service; - - #[derive(Clone)] - struct Srv; - - impl Service for Srv { - type Request = usize; - type Response = usize; - type Error = (); - type Future = Ready; - - fn poll_ready(&self, _: &mut Context<'_>) -> Poll> { - Poll::Ready(Ok(())) - } - - fn call(&self, i: usize) -> Self::Future { - Ready::Ok(i * 2) - } - } - - #[ntex::test] - async fn transform() { - let srv = fn_transform(|i: usize, srv: &Srv| srv.call(i + 1)).new_transform(Srv); - - let res = srv.call(10usize).await; - assert!(res.is_ok()); - assert_eq!(res.unwrap(), 22); - - let res = lazy(|cx| srv.poll_ready(cx)).await; - assert_eq!(res, Poll::Ready(Ok(()))); - - let res = lazy(|cx| srv.poll_shutdown(cx, true)).await; - assert_eq!(res, Poll::Ready(())); + Self(self.0.clone()) } } diff --git a/ntex-service/src/transform.rs b/ntex-service/src/transform.rs index 25f9727b..54d865cd 100644 --- a/ntex-service/src/transform.rs +++ b/ntex-service/src/transform.rs @@ -81,7 +81,7 @@ where /// ``` pub trait Transform { /// The `TransformService` value created by this factory - type Service: Service; + type Service; /// Creates and returns a new Transform component, asynchronously fn new_transform(&self, service: S) -> Self::Service; @@ -122,6 +122,7 @@ impl ServiceFactory for ApplyTransform where S: ServiceFactory, T: Transform, + T::Service: Service, { type Request = ::Request; type Response = ::Response;