1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 00:01:11 +01:00

add configuration parameter to transform factory

This commit is contained in:
Nikolay Kim 2019-03-02 12:16:30 -08:00
parent f1bc9d0deb
commit d0b8b6940c
8 changed files with 90 additions and 51 deletions

View File

@ -169,7 +169,7 @@ impl<T, A, B, C> NewService<C> for AndThenTransformNewService<T, A, B, C>
where where
A: NewService<C>, A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>, B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>, T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
type Request = A::Request; type Request = A::Request;
@ -187,7 +187,7 @@ where
t: None, t: None,
fut_a: self.a.new_service(cfg), fut_a: self.a.new_service(cfg),
fut_b: self.b.new_service(cfg), fut_b: self.b.new_service(cfg),
fut_t: self.t.new_transform(), fut_t: self.t.new_transform(cfg),
} }
} }
} }
@ -196,7 +196,7 @@ pub struct AndThenTransformNewServiceFuture<T, A, B, C>
where where
A: NewService<C>, A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>, B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>, T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
fut_b: B::Future, fut_b: B::Future,
@ -211,7 +211,7 @@ impl<T, A, B, C> Future for AndThenTransformNewServiceFuture<T, A, B, C>
where where
A: NewService<C>, A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>, B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>, T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
type Item = AndThenTransform<T::Transform, A::Service, B::Service>; type Item = AndThenTransform<T::Transform, A::Service, B::Service>;

View File

@ -89,7 +89,7 @@ where
/// `ApplyNewService` new service combinator /// `ApplyNewService` new service combinator
pub struct ApplyNewService<T, S, C> pub struct ApplyNewService<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError>, T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService<C>, S: NewService<C>,
{ {
@ -100,12 +100,12 @@ where
impl<T, S, C> ApplyNewService<T, S, C> impl<T, S, C> ApplyNewService<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError>, T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService<C>, S: NewService<C>,
{ {
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<T1: IntoNewTransform<T, S::Service>, S1: IntoNewService<S, C>>( pub fn new<T1: IntoNewTransform<T, S::Service, C>, S1: IntoNewService<S, C>>(
transform: T1, transform: T1,
service: S1, service: S1,
) -> Self { ) -> Self {
@ -118,7 +118,7 @@ where
} }
impl<F, S, In, Out, Cfg> impl<F, S, In, Out, Cfg>
ApplyNewService<FnNewTransform<F, S::Service, In, Out, S::InitError>, S, Cfg> ApplyNewService<FnNewTransform<F, S::Service, In, Out, S::InitError, Cfg>, S, Cfg>
where where
F: FnMut(In, &mut S::Service) -> Out + Clone, F: FnMut(In, &mut S::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
@ -137,7 +137,7 @@ where
impl<T, S, C> Clone for ApplyNewService<T, S, C> impl<T, S, C> Clone for ApplyNewService<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError> + Clone, T: NewTransform<S::Service, C, InitError = S::InitError> + Clone,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService<C> + Clone, S: NewService<C> + Clone,
{ {
@ -152,7 +152,7 @@ where
impl<T, S, C> NewService<C> for ApplyNewService<T, S, C> impl<T, S, C> NewService<C> for ApplyNewService<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError>, T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService<C>, S: NewService<C>,
{ {
@ -166,7 +166,7 @@ where
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
ApplyNewServiceFuture { ApplyNewServiceFuture {
fut_t: self.transform.new_transform(), fut_t: self.transform.new_transform(cfg),
fut_s: self.service.new_service(cfg), fut_s: self.service.new_service(cfg),
service: None, service: None,
transform: None, transform: None,
@ -176,7 +176,7 @@ where
pub struct ApplyNewServiceFuture<T, S, C> pub struct ApplyNewServiceFuture<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError>, T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService<C>, S: NewService<C>,
{ {
@ -188,7 +188,7 @@ where
impl<T, S, C> Future for ApplyNewServiceFuture<T, S, C> impl<T, S, C> Future for ApplyNewServiceFuture<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError>, T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService<C>, S: NewService<C>,
{ {

View File

@ -3,7 +3,8 @@ use std::marker::PhantomData;
use futures::future::{ok, FutureResult}; use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll}; use futures::{Async, IntoFuture, Poll};
use super::{IntoConfigurableNewService, IntoNewService, IntoService, NewService, Service}; use crate::Never;
use crate::{IntoConfigurableNewService, IntoNewService, IntoService, NewService, Service};
/// Create `NewService` for function that can act as Service /// Create `NewService` for function that can act as Service
pub fn fn_service<F, Req, Out, Cfg>(f: F) -> FnNewService<F, Req, Out, Cfg> pub fn fn_service<F, Req, Out, Cfg>(f: F) -> FnNewService<F, Req, Out, Cfg>
@ -124,7 +125,7 @@ where
type Error = Out::Error; type Error = Out::Error;
type Service = FnService<F, Req, Out>; type Service = FnService<F, Req, Out>;
type InitError = (); type InitError = Never;
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &Cfg) -> Self::Future { fn new_service(&self, _: &Cfg) -> Self::Future {

View File

@ -3,7 +3,7 @@ use std::marker::PhantomData;
use futures::future::{ok, FutureResult}; use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll}; use futures::{Async, IntoFuture, Poll};
use super::{IntoNewTransform, IntoTransform, NewTransform, Transform}; use crate::{IntoNewTransform, IntoTransform, NewTransform, Transform};
pub struct FnTransform<F, S, Req, Res> pub struct FnTransform<F, S, Req, Res>
where where
@ -66,16 +66,16 @@ where
} }
} }
pub struct FnNewTransform<F, S, Req, Out, Err> pub struct FnNewTransform<F, S, Req, Out, Err, Cfg>
where where
F: FnMut(Req, &mut S) -> Out + Clone, F: FnMut(Req, &mut S) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
f: F, f: F,
_t: PhantomData<(S, Req, Out, Err)>, _t: PhantomData<(S, Req, Out, Err, Cfg)>,
} }
impl<F, S, Req, Res, Err> FnNewTransform<F, S, Req, Res, Err> impl<F, S, Req, Res, Err, Cfg> FnNewTransform<F, S, Req, Res, Err, Cfg>
where where
F: FnMut(Req, &mut S) -> Res + Clone, F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture, Res: IntoFuture,
@ -85,7 +85,7 @@ where
} }
} }
impl<F, S, Req, Res, Err> NewTransform<S> for FnNewTransform<F, S, Req, Res, Err> impl<F, S, Req, Res, Err, Cfg> NewTransform<S, Cfg> for FnNewTransform<F, S, Req, Res, Err, Cfg>
where where
F: FnMut(Req, &mut S) -> Res + Clone, F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture, Res: IntoFuture,
@ -97,22 +97,23 @@ where
type InitError = Err; type InitError = Err;
type Future = FutureResult<Self::Transform, Self::InitError>; type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self) -> Self::Future { fn new_transform(&self, _: &Cfg) -> Self::Future {
ok(FnTransform::new(self.f.clone())) ok(FnTransform::new(self.f.clone()))
} }
} }
impl<F, S, Req, Res, Err> IntoNewTransform<FnNewTransform<F, S, Req, Res, Err>, S> for F impl<F, S, Req, Res, Err, Cfg>
IntoNewTransform<FnNewTransform<F, S, Req, Res, Err, Cfg>, S, Cfg> for F
where where
F: FnMut(Req, &mut S) -> Res + Clone, F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture, Res: IntoFuture,
{ {
fn into_new_transform(self) -> FnNewTransform<F, S, Req, Res, Err> { fn into_new_transform(self) -> FnNewTransform<F, S, Req, Res, Err, Cfg> {
FnNewTransform::new(self) FnNewTransform::new(self)
} }
} }
impl<F, S, Req, Res, Err> Clone for FnNewTransform<F, S, Req, Res, Err> impl<F, S, Req, Res, Err, Cfg> Clone for FnNewTransform<F, S, Req, Res, Err, Cfg>
where where
F: FnMut(Req, &mut S) -> Res + Clone, F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture, Res: IntoFuture,

View File

@ -33,6 +33,9 @@ pub use self::map_init_err::MapInitErr;
pub use self::then::{Then, ThenNewService}; pub use self::then::{Then, ThenNewService};
pub use self::transform::{IntoNewTransform, IntoTransform, NewTransform, Transform}; pub use self::transform::{IntoNewTransform, IntoTransform, NewTransform, Transform};
#[derive(Copy, Clone, Debug)]
pub enum Never {}
/// An asynchronous function from `Request` to a `Response`. /// An asynchronous function from `Request` to a `Response`.
pub trait Service { pub trait Service {
/// Requests handled by the service. /// Requests handled by the service.
@ -189,7 +192,7 @@ impl<T: ?Sized> ServiceExt for T where T: Service {}
/// `NewService` trait, and uses that new `Service` value to process inbound /// `NewService` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream. /// requests on that new TCP stream.
/// ///
/// `Config` parameter defines service factory configuration type. /// `Config` is a service factory configuration type.
pub trait NewService<Config = ()> { pub trait NewService<Config = ()> {
/// Requests handled by the service. /// Requests handled by the service.
type Request; type Request;

View File

@ -1,3 +1,6 @@
use std::rc::Rc;
use std::sync::Arc;
use futures::{Future, Poll}; use futures::{Future, Poll};
use crate::transform_map_err::{TransformMapErr, TransformMapErrNewTransform}; use crate::transform_map_err::{TransformMapErr, TransformMapErrNewTransform};
@ -44,7 +47,9 @@ pub trait Transform<Service> {
} }
/// `Transform` service factory /// `Transform` service factory
pub trait NewTransform<Service> { ///
/// `Config` is a service factory configuration type.
pub trait NewTransform<Service, Config = ()> {
/// Requests handled by the service. /// Requests handled by the service.
type Request; type Request;
@ -69,11 +74,11 @@ pub trait NewTransform<Service> {
type Future: Future<Item = Self::Transform, Error = Self::InitError>; type Future: Future<Item = Self::Transform, Error = Self::InitError>;
/// Create and return a new service value asynchronously. /// Create and return a new service value asynchronously.
fn new_transform(&self) -> Self::Future; fn new_transform(&self, cfg: &Config) -> Self::Future;
/// Map this transforms's output to a different type, returning a new transform /// Map this transforms's output to a different type, returning a new transform
/// of the resulting type. /// of the resulting type.
fn map_err<F, E>(self, f: F) -> TransformMapErrNewTransform<Self, Service, F, E> fn map_err<F, E>(self, f: F) -> TransformMapErrNewTransform<Self, Service, Config, F, E>
where where
Self: Sized, Self: Sized,
F: Fn(Self::Error) -> E, F: Fn(Self::Error) -> E,
@ -120,6 +125,38 @@ where
} }
} }
impl<S, C, T> NewTransform<S, C> for Rc<T>
where
T: NewTransform<S, C>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = T::InitError;
type Future = T::Future;
fn new_transform(&self, cfg: &C) -> T::Future {
self.as_ref().new_transform(cfg)
}
}
impl<S, C, T> NewTransform<S, C> for Arc<T>
where
T: NewTransform<S, C>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = T::InitError;
type Future = T::Future;
fn new_transform(&self, cfg: &C) -> T::Future {
self.as_ref().new_transform(cfg)
}
}
/// Trait for types that can be converted to a `TransformService` /// Trait for types that can be converted to a `TransformService`
pub trait IntoTransform<T, S> pub trait IntoTransform<T, S>
where where
@ -130,9 +167,9 @@ where
} }
/// Trait for types that can be converted to a TransfromNewService /// Trait for types that can be converted to a TransfromNewService
pub trait IntoNewTransform<T, S> pub trait IntoNewTransform<T, S, C = ()>
where where
T: NewTransform<S>, T: NewTransform<S, C>,
{ {
/// Convert to an `TranformNewService` /// Convert to an `TranformNewService`
fn into_new_transform(self) -> T; fn into_new_transform(self) -> T;
@ -147,9 +184,9 @@ where
} }
} }
impl<T, S> IntoNewTransform<T, S> for T impl<T, S, C> IntoNewTransform<T, S, C> for T
where where
T: NewTransform<S>, T: NewTransform<S, C>,
{ {
fn into_new_transform(self) -> T { fn into_new_transform(self) -> T {
self self

View File

@ -98,17 +98,17 @@ where
/// transform's error. /// transform's error.
/// ///
/// This is created by the `NewTransform::map_err` method. /// This is created by the `NewTransform::map_err` method.
pub struct TransformMapErrNewTransform<T, S, F, E> { pub struct TransformMapErrNewTransform<T, S, C, F, E> {
t: T, t: T,
f: F, f: F,
e: PhantomData<(S, E)>, e: PhantomData<(S, C, E)>,
} }
impl<T, S, F, E> TransformMapErrNewTransform<T, S, F, E> { impl<T, S, C, F, E> TransformMapErrNewTransform<T, S, C, F, E> {
/// Create new `MapErr` new service instance /// Create new `MapErr` new service instance
pub fn new(t: T, f: F) -> Self pub fn new(t: T, f: F) -> Self
where where
T: NewTransform<S>, T: NewTransform<S, C>,
F: Fn(T::Error) -> E, F: Fn(T::Error) -> E,
{ {
Self { Self {
@ -119,7 +119,7 @@ impl<T, S, F, E> TransformMapErrNewTransform<T, S, F, E> {
} }
} }
impl<T, S, F, E> Clone for TransformMapErrNewTransform<T, S, F, E> impl<T, S, C, F, E> Clone for TransformMapErrNewTransform<T, S, C, F, E>
where where
T: Clone, T: Clone,
F: Clone, F: Clone,
@ -133,9 +133,9 @@ where
} }
} }
impl<T, S, F, E> NewTransform<S> for TransformMapErrNewTransform<T, S, F, E> impl<T, S, C, F, E> NewTransform<S, C> for TransformMapErrNewTransform<T, S, C, F, E>
where where
T: NewTransform<S>, T: NewTransform<S, C>,
F: Fn(T::Error) -> E + Clone, F: Fn(T::Error) -> E + Clone,
{ {
type Request = T::Request; type Request = T::Request;
@ -144,25 +144,25 @@ where
type Transform = TransformMapErr<T::Transform, S, F, E>; type Transform = TransformMapErr<T::Transform, S, F, E>;
type InitError = T::InitError; type InitError = T::InitError;
type Future = TransformMapErrNewTransformFuture<T, S, F, E>; type Future = TransformMapErrNewTransformFuture<T, S, C, F, E>;
fn new_transform(&self) -> Self::Future { fn new_transform(&self, cfg: &C) -> Self::Future {
TransformMapErrNewTransformFuture::new(self.t.new_transform(), self.f.clone()) TransformMapErrNewTransformFuture::new(self.t.new_transform(cfg), self.f.clone())
} }
} }
pub struct TransformMapErrNewTransformFuture<T, S, F, E> pub struct TransformMapErrNewTransformFuture<T, S, C, F, E>
where where
T: NewTransform<S>, T: NewTransform<S, C>,
F: Fn(T::Error) -> E, F: Fn(T::Error) -> E,
{ {
fut: T::Future, fut: T::Future,
f: F, f: F,
} }
impl<T, S, F, E> TransformMapErrNewTransformFuture<T, S, F, E> impl<T, S, C, F, E> TransformMapErrNewTransformFuture<T, S, C, F, E>
where where
T: NewTransform<S>, T: NewTransform<S, C>,
F: Fn(T::Error) -> E, F: Fn(T::Error) -> E,
{ {
fn new(fut: T::Future, f: F) -> Self { fn new(fut: T::Future, f: F) -> Self {
@ -170,9 +170,9 @@ where
} }
} }
impl<T, S, F, E> Future for TransformMapErrNewTransformFuture<T, S, F, E> impl<T, S, C, F, E> Future for TransformMapErrNewTransformFuture<T, S, C, F, E>
where where
T: NewTransform<S>, T: NewTransform<S, C>,
F: Fn(T::Error) -> E + Clone, F: Fn(T::Error) -> E + Clone,
{ {
type Item = TransformMapErr<T::Transform, S, F, E>; type Item = TransformMapErr<T::Transform, S, F, E>;

View File

@ -10,6 +10,3 @@ pub mod order;
pub mod stream; pub mod stream;
pub mod time; pub mod time;
pub mod timeout; pub mod timeout;
#[derive(Copy, Clone, Debug)]
pub enum Never {}