mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-03 04:47:39 +03:00
Simplify fn_transform
This commit is contained in:
parent
a2a727fe16
commit
f49a0b8de2
4 changed files with 17 additions and 88 deletions
|
@ -1,5 +1,9 @@
|
|||
# Changes
|
||||
|
||||
## [0.2.1] - 2021-09-17
|
||||
|
||||
* Simplify fn_transform
|
||||
|
||||
## [0.2.0] - 2021-09-15
|
||||
|
||||
* Refactor Transform trait
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ntex-service"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
authors = ["ntex contributors <team@ntex.rs>"]
|
||||
description = "ntex service"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
|
|
|
@ -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<S, F, R, Req, Res, Err>(f: F) -> FnTransform<S, F, R, Req, Res, Err>
|
||||
where
|
||||
S: Service<Error = Err>,
|
||||
F: Fn(Req, &S) -> R + Clone,
|
||||
R: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
FnTransform::new(f)
|
||||
pub fn fn_transform<F>(f: F) -> FnTransform<F> {
|
||||
FnTransform(f)
|
||||
}
|
||||
|
||||
pub struct FnTransform<S, F, R, Req, Res, Err>
|
||||
where
|
||||
S: Service<Error = Err>,
|
||||
F: Fn(Req, &S) -> R + Clone,
|
||||
R: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
f: F,
|
||||
_t: PhantomData<(S, R, Req)>,
|
||||
}
|
||||
pub struct FnTransform<F>(F);
|
||||
|
||||
impl<S, F, R, Req, Res, Err> FnTransform<S, F, R, Req, Res, Err>
|
||||
impl<S, F, Out> Transform<S> for FnTransform<F>
|
||||
where
|
||||
S: Service<Error = Err>,
|
||||
F: Fn(Req, &S) -> R + Clone,
|
||||
R: Future<Output = Result<Res, Err>>,
|
||||
F: Fn(S) -> Out,
|
||||
{
|
||||
fn new(f: F) -> Self {
|
||||
FnTransform { f, _t: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, F, R, Req, Res, Err> Transform<S> for FnTransform<S, F, R, Req, Res, Err>
|
||||
where
|
||||
S: Service<Error = Err>,
|
||||
F: Fn(Req, &S) -> R + Clone,
|
||||
R: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
type Service = Apply<S, F, R, Req, Res, Err>;
|
||||
type Service = Out;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Service {
|
||||
apply_fn(service, self.f.clone())
|
||||
(self.0)(service)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, F, R, Req, Res, Err> Clone for FnTransform<S, F, R, Req, Res, Err>
|
||||
where
|
||||
S: Service<Error = Err>,
|
||||
F: Fn(Req, &S) -> R + Clone,
|
||||
R: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
impl<F: Clone> Clone for FnTransform<F> {
|
||||
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<usize, ()>;
|
||||
|
||||
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -81,7 +81,7 @@ where
|
|||
/// ```
|
||||
pub trait Transform<S> {
|
||||
/// 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<T, S> ServiceFactory for ApplyTransform<T, S>
|
|||
where
|
||||
S: ServiceFactory,
|
||||
T: Transform<S::Service>,
|
||||
T::Service: Service,
|
||||
{
|
||||
type Request = <T::Service as Service>::Request;
|
||||
type Response = <T::Service as Service>::Response;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue