diff --git a/actix-service/src/and_then_apply.rs b/actix-service/src/and_then_apply.rs index 74633071..3c4691eb 100644 --- a/actix-service/src/and_then_apply.rs +++ b/actix-service/src/and_then_apply.rs @@ -169,7 +169,7 @@ impl NewService for AndThenTransformNewService where A: NewService, B: NewService, - T: NewTransform, + T: NewTransform, T::Error: From, { type Request = A::Request; @@ -187,7 +187,7 @@ where t: None, fut_a: self.a.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 where A: NewService, B: NewService, - T: NewTransform, + T: NewTransform, T::Error: From, { fut_b: B::Future, @@ -211,7 +211,7 @@ impl Future for AndThenTransformNewServiceFuture where A: NewService, B: NewService, - T: NewTransform, + T: NewTransform, T::Error: From, { type Item = AndThenTransform; diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index 27c83d9e..3efef1bf 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -89,7 +89,7 @@ where /// `ApplyNewService` new service combinator pub struct ApplyNewService where - T: NewTransform, + T: NewTransform, T::Error: From, S: NewService, { @@ -100,12 +100,12 @@ where impl ApplyNewService where - T: NewTransform, + T: NewTransform, T::Error: From, S: NewService, { /// Create new `ApplyNewService` new service instance - pub fn new, S1: IntoNewService>( + pub fn new, S1: IntoNewService>( transform: T1, service: S1, ) -> Self { @@ -118,7 +118,7 @@ where } impl - ApplyNewService, S, Cfg> + ApplyNewService, S, Cfg> where F: FnMut(In, &mut S::Service) -> Out + Clone, Out: IntoFuture, @@ -137,7 +137,7 @@ where impl Clone for ApplyNewService where - T: NewTransform + Clone, + T: NewTransform + Clone, T::Error: From, S: NewService + Clone, { @@ -152,7 +152,7 @@ where impl NewService for ApplyNewService where - T: NewTransform, + T: NewTransform, T::Error: From, S: NewService, { @@ -166,7 +166,7 @@ where fn new_service(&self, cfg: &C) -> Self::Future { ApplyNewServiceFuture { - fut_t: self.transform.new_transform(), + fut_t: self.transform.new_transform(cfg), fut_s: self.service.new_service(cfg), service: None, transform: None, @@ -176,7 +176,7 @@ where pub struct ApplyNewServiceFuture where - T: NewTransform, + T: NewTransform, T::Error: From, S: NewService, { @@ -188,7 +188,7 @@ where impl Future for ApplyNewServiceFuture where - T: NewTransform, + T: NewTransform, T::Error: From, S: NewService, { diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 8e713bc2..0faf933a 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -3,7 +3,8 @@ use std::marker::PhantomData; use futures::future::{ok, FutureResult}; 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 pub fn fn_service(f: F) -> FnNewService @@ -124,7 +125,7 @@ where type Error = Out::Error; type Service = FnService; - type InitError = (); + type InitError = Never; type Future = FutureResult; fn new_service(&self, _: &Cfg) -> Self::Future { diff --git a/actix-service/src/fn_transform.rs b/actix-service/src/fn_transform.rs index fc9de01d..383b70bc 100644 --- a/actix-service/src/fn_transform.rs +++ b/actix-service/src/fn_transform.rs @@ -3,7 +3,7 @@ use std::marker::PhantomData; use futures::future::{ok, FutureResult}; use futures::{Async, IntoFuture, Poll}; -use super::{IntoNewTransform, IntoTransform, NewTransform, Transform}; +use crate::{IntoNewTransform, IntoTransform, NewTransform, Transform}; pub struct FnTransform where @@ -66,16 +66,16 @@ where } } -pub struct FnNewTransform +pub struct FnNewTransform where F: FnMut(Req, &mut S) -> Out + Clone, Out: IntoFuture, { f: F, - _t: PhantomData<(S, Req, Out, Err)>, + _t: PhantomData<(S, Req, Out, Err, Cfg)>, } -impl FnNewTransform +impl FnNewTransform where F: FnMut(Req, &mut S) -> Res + Clone, Res: IntoFuture, @@ -85,7 +85,7 @@ where } } -impl NewTransform for FnNewTransform +impl NewTransform for FnNewTransform where F: FnMut(Req, &mut S) -> Res + Clone, Res: IntoFuture, @@ -97,22 +97,23 @@ where type InitError = Err; type Future = FutureResult; - fn new_transform(&self) -> Self::Future { + fn new_transform(&self, _: &Cfg) -> Self::Future { ok(FnTransform::new(self.f.clone())) } } -impl IntoNewTransform, S> for F +impl + IntoNewTransform, S, Cfg> for F where F: FnMut(Req, &mut S) -> Res + Clone, Res: IntoFuture, { - fn into_new_transform(self) -> FnNewTransform { + fn into_new_transform(self) -> FnNewTransform { FnNewTransform::new(self) } } -impl Clone for FnNewTransform +impl Clone for FnNewTransform where F: FnMut(Req, &mut S) -> Res + Clone, Res: IntoFuture, diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index c4869947..a1eeb617 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -33,6 +33,9 @@ pub use self::map_init_err::MapInitErr; pub use self::then::{Then, ThenNewService}; pub use self::transform::{IntoNewTransform, IntoTransform, NewTransform, Transform}; +#[derive(Copy, Clone, Debug)] +pub enum Never {} + /// An asynchronous function from `Request` to a `Response`. pub trait Service { /// Requests handled by the service. @@ -189,7 +192,7 @@ impl ServiceExt for T where T: Service {} /// `NewService` trait, and uses that new `Service` value to process inbound /// requests on that new TCP stream. /// -/// `Config` parameter defines service factory configuration type. +/// `Config` is a service factory configuration type. pub trait NewService { /// Requests handled by the service. type Request; diff --git a/actix-service/src/transform.rs b/actix-service/src/transform.rs index 836e0c83..127d2ef8 100644 --- a/actix-service/src/transform.rs +++ b/actix-service/src/transform.rs @@ -1,3 +1,6 @@ +use std::rc::Rc; +use std::sync::Arc; + use futures::{Future, Poll}; use crate::transform_map_err::{TransformMapErr, TransformMapErrNewTransform}; @@ -44,7 +47,9 @@ pub trait Transform { } /// `Transform` service factory -pub trait NewTransform { +/// +/// `Config` is a service factory configuration type. +pub trait NewTransform { /// Requests handled by the service. type Request; @@ -69,11 +74,11 @@ pub trait NewTransform { type Future: Future; /// 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 /// of the resulting type. - fn map_err(self, f: F) -> TransformMapErrNewTransform + fn map_err(self, f: F) -> TransformMapErrNewTransform where Self: Sized, F: Fn(Self::Error) -> E, @@ -120,6 +125,38 @@ where } } +impl NewTransform for Rc +where + T: NewTransform, +{ + 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 NewTransform for Arc +where + T: NewTransform, +{ + 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` pub trait IntoTransform where @@ -130,9 +167,9 @@ where } /// Trait for types that can be converted to a TransfromNewService -pub trait IntoNewTransform +pub trait IntoNewTransform where - T: NewTransform, + T: NewTransform, { /// Convert to an `TranformNewService` fn into_new_transform(self) -> T; @@ -147,9 +184,9 @@ where } } -impl IntoNewTransform for T +impl IntoNewTransform for T where - T: NewTransform, + T: NewTransform, { fn into_new_transform(self) -> T { self diff --git a/actix-service/src/transform_map_err.rs b/actix-service/src/transform_map_err.rs index 9ae9f65a..048e6403 100644 --- a/actix-service/src/transform_map_err.rs +++ b/actix-service/src/transform_map_err.rs @@ -98,17 +98,17 @@ where /// transform's error. /// /// This is created by the `NewTransform::map_err` method. -pub struct TransformMapErrNewTransform { +pub struct TransformMapErrNewTransform { t: T, f: F, - e: PhantomData<(S, E)>, + e: PhantomData<(S, C, E)>, } -impl TransformMapErrNewTransform { +impl TransformMapErrNewTransform { /// Create new `MapErr` new service instance pub fn new(t: T, f: F) -> Self where - T: NewTransform, + T: NewTransform, F: Fn(T::Error) -> E, { Self { @@ -119,7 +119,7 @@ impl TransformMapErrNewTransform { } } -impl Clone for TransformMapErrNewTransform +impl Clone for TransformMapErrNewTransform where T: Clone, F: Clone, @@ -133,9 +133,9 @@ where } } -impl NewTransform for TransformMapErrNewTransform +impl NewTransform for TransformMapErrNewTransform where - T: NewTransform, + T: NewTransform, F: Fn(T::Error) -> E + Clone, { type Request = T::Request; @@ -144,25 +144,25 @@ where type Transform = TransformMapErr; type InitError = T::InitError; - type Future = TransformMapErrNewTransformFuture; + type Future = TransformMapErrNewTransformFuture; - fn new_transform(&self) -> Self::Future { - TransformMapErrNewTransformFuture::new(self.t.new_transform(), self.f.clone()) + fn new_transform(&self, cfg: &C) -> Self::Future { + TransformMapErrNewTransformFuture::new(self.t.new_transform(cfg), self.f.clone()) } } -pub struct TransformMapErrNewTransformFuture +pub struct TransformMapErrNewTransformFuture where - T: NewTransform, + T: NewTransform, F: Fn(T::Error) -> E, { fut: T::Future, f: F, } -impl TransformMapErrNewTransformFuture +impl TransformMapErrNewTransformFuture where - T: NewTransform, + T: NewTransform, F: Fn(T::Error) -> E, { fn new(fut: T::Future, f: F) -> Self { @@ -170,9 +170,9 @@ where } } -impl Future for TransformMapErrNewTransformFuture +impl Future for TransformMapErrNewTransformFuture where - T: NewTransform, + T: NewTransform, F: Fn(T::Error) -> E + Clone, { type Item = TransformMapErr; diff --git a/actix-utils/src/lib.rs b/actix-utils/src/lib.rs index 15bff92b..c49ddab2 100644 --- a/actix-utils/src/lib.rs +++ b/actix-utils/src/lib.rs @@ -10,6 +10,3 @@ pub mod order; pub mod stream; pub mod time; pub mod timeout; - -#[derive(Copy, Clone, Debug)] -pub enum Never {}