diff --git a/actix-service/CHANGES.md b/actix-service/CHANGES.md index 82481f46..971741e8 100644 --- a/actix-service/CHANGES.md +++ b/actix-service/CHANGES.md @@ -1,8 +1,14 @@ # Changes ## Unreleased - 2020-xx-xx +* `Service`, other traits, and many type signatures now take the the request type as a type + parameter instead of an associated type. [#232] * Upgrade `pin-project` to `1.0`. + +[#232]: https://github.com/actix/actix-net/pull/232 + + ## 1.0.6 - 2020-08-09 ### Fixed diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index 16d077ef..27a09684 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -12,12 +12,12 @@ use super::{IntoService, IntoServiceFactory, Service, ServiceFactory}; /// Apply transform function to a service. /// /// The In and Out type params refer to the request and response types for the wrapped service. -pub fn apply_fn( - service: U, +pub fn apply_fn( + service: I, wrap_fn: F, ) -> Apply where - U: IntoService, + I: IntoService, S: Service, F: FnMut(Req, &mut S) -> Fut, Fut: Future>, @@ -28,12 +28,12 @@ where /// Service factory that produces `apply_fn` service. /// /// The In and Out type params refer to the request and response types for the wrapped service. -pub fn apply_fn_factory( - service: U, +pub fn apply_fn_factory( + service: I, f: F, ) -> ApplyFactory where - U: IntoServiceFactory, + I: IntoServiceFactory, SF: ServiceFactory, F: FnMut(Req, &mut SF::Service) -> Fut + Clone, Fut: Future>, diff --git a/actix-service/src/apply_cfg.rs b/actix-service/src/apply_cfg.rs index bb491485..da24e87d 100644 --- a/actix-service/src/apply_cfg.rs +++ b/actix-service/src/apply_cfg.rs @@ -7,150 +7,152 @@ use std::task::{Context, Poll}; use crate::{Service, ServiceFactory}; -/// Convert `Fn(Config, &mut Service1) -> Future` fn to a service factory -pub fn apply_cfg( - srv: T, +/// Convert `Fn(Config, &mut Service1) -> Future` fn to a service factory. +pub fn apply_cfg( + srv: S1, f: F, ) -> impl ServiceFactory< Req, - Config = C, - Response = S::Response, - Error = S::Error, - Service = S, - InitError = E, - Future = R, + Config = Cfg, + Response = S2::Response, + Error = S2::Error, + Service = S2, + InitError = Err, + Future = Fut, > + Clone where - F: FnMut(C, &mut T) -> R, - T: Service, - R: Future>, - S: Service, + S1: Service, + F: FnMut(Cfg, &mut S1) -> Fut, + Fut: Future>, + S2: Service, { ApplyConfigService { srv: Rc::new(RefCell::new((srv, f))), - _t: PhantomData, + _phantom: PhantomData, } } -/// Convert `Fn(Config, &mut Service1) -> Future` fn to a service factory +/// Convert `Fn(Config, &mut ServiceFactory1) -> Future` fn to a service factory. /// /// Service1 get constructed from `T` factory. -pub fn apply_cfg_factory( - factory: T, +pub fn apply_cfg_factory( + factory: SF, f: F, ) -> impl ServiceFactory< Req, - Config = C, + Config = Cfg, Response = S::Response, Error = S::Error, Service = S, - InitError = T::InitError, + InitError = SF::InitError, > + Clone where - F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, - T::InitError: From, - R: Future>, + SF: ServiceFactory, + F: FnMut(Cfg, &mut SF::Service) -> Fut, + SF::InitError: From, + Fut: Future>, S: Service, { ApplyConfigServiceFactory { srv: Rc::new(RefCell::new((factory, f))), - _t: PhantomData, + _phantom: PhantomData, } } /// Convert `Fn(Config, &mut Server) -> Future` fn to NewService\ -struct ApplyConfigService +struct ApplyConfigService where - F: FnMut(C, &mut T) -> R, - T: Service, - R: Future>, - S: Service, + S1: Service, + F: FnMut(Cfg, &mut S1) -> Fut, + Fut: Future>, + S2: Service, { - srv: Rc>, - _t: PhantomData<(C, Req, R, S)>, + srv: Rc>, + _phantom: PhantomData<(Cfg, Req, Fut, S2)>, } -impl Clone for ApplyConfigService +impl Clone for ApplyConfigService where - F: FnMut(C, &mut T) -> R, - T: Service, - R: Future>, - S: Service, + S1: Service, + F: FnMut(Cfg, &mut S1) -> Fut, + Fut: Future>, + S2: Service, { fn clone(&self) -> Self { ApplyConfigService { srv: self.srv.clone(), - _t: PhantomData, + _phantom: PhantomData, } } } -impl ServiceFactory for ApplyConfigService +impl ServiceFactory + for ApplyConfigService where - F: FnMut(C, &mut T) -> R, - T: Service, - R: Future>, - S: Service, + S1: Service, + F: FnMut(Cfg, &mut S1) -> Fut, + Fut: Future>, + S2: Service, { - type Config = C; - type Response = S::Response; - type Error = S::Error; - type Service = S; + type Config = Cfg; + type Response = S2::Response; + type Error = S2::Error; + type Service = S2; - type InitError = E; - type Future = R; + type InitError = Err; + type Future = Fut; - fn new_service(&self, cfg: C) -> Self::Future { + fn new_service(&self, cfg: Cfg) -> Self::Future { let (t, f) = &mut *self.srv.borrow_mut(); f(cfg, t) } } /// Convert `Fn(&Config) -> Future` fn to NewService -struct ApplyConfigServiceFactory +struct ApplyConfigServiceFactory where - F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, - R: Future>, + SF: ServiceFactory, + F: FnMut(Cfg, &mut SF::Service) -> Fut, + Fut: Future>, S: Service, { - srv: Rc>, - _t: PhantomData<(C, Req, R, S)>, + srv: Rc>, + _phantom: PhantomData<(Cfg, Req, Fut, S)>, } -impl Clone for ApplyConfigServiceFactory +impl Clone for ApplyConfigServiceFactory where - F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, - R: Future>, + SF: ServiceFactory, + F: FnMut(Cfg, &mut SF::Service) -> Fut, + Fut: Future>, S: Service, { fn clone(&self) -> Self { Self { srv: self.srv.clone(), - _t: PhantomData, + _phantom: PhantomData, } } } -impl ServiceFactory for ApplyConfigServiceFactory +impl ServiceFactory + for ApplyConfigServiceFactory where - F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, - T::InitError: From, - R: Future>, + SF: ServiceFactory, + SF::InitError: From, + F: FnMut(Cfg, &mut SF::Service) -> Fut, + Fut: Future>, S: Service, { - type Config = C; + type Config = Cfg; type Response = S::Response; type Error = S::Error; type Service = S; - type InitError = T::InitError; - type Future = ApplyConfigServiceFactoryResponse; + type InitError = SF::InitError; + type Future = ApplyConfigServiceFactoryResponse; - fn new_service(&self, cfg: C) -> Self::Future { + fn new_service(&self, cfg: Cfg) -> Self::Future { ApplyConfigServiceFactoryResponse { cfg: Some(cfg), store: self.srv.clone(), @@ -160,42 +162,43 @@ where } #[pin_project::pin_project] -struct ApplyConfigServiceFactoryResponse +struct ApplyConfigServiceFactoryResponse where - F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, - T::InitError: From, - R: Future>, + SF: ServiceFactory, + SF::InitError: From, + F: FnMut(Cfg, &mut SF::Service) -> Fut, + Fut: Future>, S: Service, { - cfg: Option, - store: Rc>, + cfg: Option, + store: Rc>, #[pin] - state: State, + state: State, } #[pin_project::pin_project(project = StateProj)] -enum State +enum State where - T: ServiceFactory, - T::InitError: From, - R: Future>, + SF: ServiceFactory, + SF::InitError: From, + Fut: Future>, S: Service, { - A(#[pin] T::Future), - B(T::Service), - C(#[pin] R), + A(#[pin] SF::Future), + B(SF::Service), + C(#[pin] Fut), } -impl Future for ApplyConfigServiceFactoryResponse +impl Future + for ApplyConfigServiceFactoryResponse where - F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, - T::InitError: From, - R: Future>, + SF: ServiceFactory, + SF::InitError: From, + F: FnMut(Cfg, &mut SF::Service) -> Fut, + Fut: Future>, S: Service, { - type Output = Result; + type Output = Result; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let mut this = self.as_mut().project(); diff --git a/actix-service/src/boxed.rs b/actix-service/src/boxed.rs index baf84e79..31b90ca9 100644 --- a/actix-service/src/boxed.rs +++ b/actix-service/src/boxed.rs @@ -14,17 +14,17 @@ pub type BoxService = pub struct BoxServiceFactory(Inner); /// Create boxed service factory -pub fn factory( - factory: T, -) -> BoxServiceFactory +pub fn factory( + factory: SF, +) -> BoxServiceFactory where - T: ServiceFactory + 'static, + SF: ServiceFactory + 'static, Req: 'static, - T::Response: 'static, - T::Service: 'static, - T::Future: 'static, - T::Error: 'static, - T::InitError: 'static, + SF::Response: 'static, + SF::Service: 'static, + SF::Future: 'static, + SF::Error: 'static, + SF::InitError: 'static, { BoxServiceFactory(Box::new(FactoryWrapper { factory, @@ -75,33 +75,33 @@ where } } -struct FactoryWrapper +struct FactoryWrapper where - T: ServiceFactory, + SF: ServiceFactory, { - factory: T, + factory: SF, _t: PhantomData<(C, Req)>, } -impl ServiceFactory for FactoryWrapper +impl ServiceFactory for FactoryWrapper where Req: 'static, Res: 'static, Err: 'static, InitErr: 'static, - T: ServiceFactory, - T::Future: 'static, - T::Service: 'static, - >::Future: 'static, + SF: ServiceFactory, + SF::Future: 'static, + SF::Service: 'static, + >::Future: 'static, { type Response = Res; type Error = Err; type InitError = InitErr; - type Config = C; + type Config = Cfg; type Service = BoxService; type Future = BoxFuture; - fn new_service(&self, cfg: C) -> Self::Future { + fn new_service(&self, cfg: Cfg) -> Self::Future { Box::pin( self.factory .new_service(cfg) @@ -123,10 +123,10 @@ where } } -impl Service for ServiceWrapper +impl Service for ServiceWrapper where - T: Service, - T::Future: 'static, + S: Service, + S::Future: 'static, { type Response = Res; type Error = Err; diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index 64d731ba..2dfa0dd7 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -338,10 +338,10 @@ where } } -/// Convert object of type `T` to a service `S` -pub fn into_service(tp: U) -> S +/// Convert object of type `U` to a service `S` +pub fn into_service(tp: I) -> S where - U: IntoService, + I: IntoService, S: Service, { tp.into_service() diff --git a/actix-service/src/map_config.rs b/actix-service/src/map_config.rs index d4382409..82b1789b 100644 --- a/actix-service/src/map_config.rs +++ b/actix-service/src/map_config.rs @@ -6,9 +6,9 @@ use super::{IntoServiceFactory, ServiceFactory}; /// /// Note that this function consumes the receiving service factory and returns /// a wrapped version of it. -pub fn map_config(factory: U, f: F) -> MapConfig +pub fn map_config(factory: I, f: F) -> MapConfig where - U: IntoServiceFactory, + I: IntoServiceFactory, SF: ServiceFactory, F: Fn(Cfg) -> SF::Config, { @@ -16,9 +16,9 @@ where } /// Replace config with unit. -pub fn unit_config(factory: U) -> UnitConfig +pub fn unit_config(factory: I) -> UnitConfig where - U: IntoServiceFactory, + I: IntoServiceFactory, SF: ServiceFactory, { UnitConfig::new(factory.into_factory()) diff --git a/actix-service/src/pipeline.rs b/actix-service/src/pipeline.rs index fd615d36..cba7ce78 100644 --- a/actix-service/src/pipeline.rs +++ b/actix-service/src/pipeline.rs @@ -10,9 +10,9 @@ use crate::then::{ThenService, ThenServiceFactory}; use crate::{IntoService, IntoServiceFactory, Service, ServiceFactory}; /// Construct new pipeline with one service in pipeline chain. -pub fn pipeline(service: U) -> Pipeline +pub fn pipeline(service: I) -> Pipeline where - U: IntoService, + I: IntoService, S: Service, { Pipeline { @@ -22,9 +22,9 @@ where } /// Construct new pipeline factory with one service factory. -pub fn pipeline_factory(factory: U) -> PipelineFactory +pub fn pipeline_factory(factory: I) -> PipelineFactory where - U: IntoServiceFactory, + I: IntoServiceFactory, SF: ServiceFactory, { PipelineFactory { @@ -52,14 +52,14 @@ where /// /// Note that this function consumes the receiving service and returns a /// wrapped version of it. - pub fn and_then( + pub fn and_then( self, - service: F, - ) -> Pipeline + Clone, Req> + service: I, + ) -> Pipeline + Clone, Req> where Self: Sized, - F: IntoService, - U: Service, + I: IntoService, + S1: Service, { Pipeline { service: AndThenService::new(self.service, service.into_service()), @@ -70,14 +70,14 @@ where /// Apply function to specified service and use it as a next service in chain. /// /// Short version of `pipeline_factory(...).and_then(apply_fn(...))` - pub fn and_then_apply_fn( + pub fn and_then_apply_fn( self, - service: U, + service: I, wrap_fn: F, ) -> Pipeline + Clone, Req> where Self: Sized, - U: IntoService, + I: IntoService, S1: Service, F: FnMut(S::Response, &mut S1) -> Fut, Fut: Future>, @@ -94,14 +94,14 @@ where /// /// Note that this function consumes the receiving pipeline and returns a /// wrapped version of it. - pub fn then( + pub fn then( self, service: F, - ) -> Pipeline + Clone, Req> + ) -> Pipeline + Clone, Req> where Self: Sized, - F: IntoService>, - U: Service, Error = S::Error>, + F: IntoService>, + S1: Service, Error = S::Error>, { Pipeline { service: ThenService::new(self.service, service.into_service()), @@ -161,13 +161,13 @@ where } } -impl, Req> Service for Pipeline { - type Response = T::Response; - type Error = T::Error; - type Future = T::Future; +impl, Req> Service for Pipeline { + type Response = S::Response; + type Error = S::Error; + type Future = S::Future; #[inline] - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { + fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { self.service.poll_ready(ctx) } @@ -188,25 +188,25 @@ where SF: ServiceFactory, { /// Call another service after call to this one has resolved successfully. - pub fn and_then( + pub fn and_then( self, - factory: F, + factory: I, ) -> PipelineFactory< impl ServiceFactory< Req, - Response = U::Response, + Response = SF1::Response, Error = SF::Error, Config = SF::Config, InitError = SF::InitError, - Service = impl Service + Clone, + Service = impl Service + Clone, > + Clone, Req, > where Self: Sized, SF::Config: Clone, - F: IntoServiceFactory, - U: ServiceFactory< + I: IntoServiceFactory, + SF1: ServiceFactory< SF::Response, Config = SF::Config, Error = SF::Error, @@ -222,9 +222,9 @@ where /// Apply function to specified service and use it as a next service in chain. /// /// Short version of `pipeline_factory(...).and_then(apply_fn_factory(...))` - pub fn and_then_apply_fn( + pub fn and_then_apply_fn( self, - factory: U, + factory: I, wrap_fn: F, ) -> PipelineFactory< impl ServiceFactory< @@ -240,7 +240,7 @@ where where Self: Sized, SF::Config: Clone, - U: IntoServiceFactory, + I: IntoServiceFactory, SF1: ServiceFactory, F: FnMut(SF::Response, &mut SF1::Service) -> Fut + Clone, Fut: Future>, @@ -258,25 +258,25 @@ where /// /// Note that this function consumes the receiving pipeline and returns a /// wrapped version of it. - pub fn then( + pub fn then( self, - factory: F, + factory: I, ) -> PipelineFactory< impl ServiceFactory< Req, - Response = U::Response, + Response = SF1::Response, Error = SF::Error, Config = SF::Config, InitError = SF::InitError, - Service = impl Service + Clone, + Service = impl Service + Clone, > + Clone, Req, > where Self: Sized, SF::Config: Clone, - F: IntoServiceFactory>, - U: ServiceFactory< + I: IntoServiceFactory>, + SF1: ServiceFactory< Result, Config = SF::Config, Error = SF::Error, @@ -342,16 +342,19 @@ where } } -impl, Req> ServiceFactory for PipelineFactory { - type Config = T::Config; - type Response = T::Response; - type Error = T::Error; - type Service = T::Service; - type InitError = T::InitError; - type Future = T::Future; +impl ServiceFactory for PipelineFactory +where + SF: ServiceFactory, +{ + type Config = SF::Config; + type Response = SF::Response; + type Error = SF::Error; + type Service = SF::Service; + type InitError = SF::InitError; + type Future = SF::Future; #[inline] - fn new_service(&self, cfg: T::Config) -> Self::Future { + fn new_service(&self, cfg: SF::Config) -> Self::Future { self.factory.new_service(cfg) } } diff --git a/actix-service/src/transform.rs b/actix-service/src/transform.rs index 7f0634e7..d4d49417 100644 --- a/actix-service/src/transform.rs +++ b/actix-service/src/transform.rs @@ -8,11 +8,11 @@ use crate::transform_err::TransformMapInitErr; use crate::{IntoServiceFactory, Service, ServiceFactory}; /// Apply transform to a service. -pub fn apply(t: T, factory: U) -> ApplyTransform +pub fn apply(t: T, factory: I) -> ApplyTransform where + I: IntoServiceFactory, S: ServiceFactory, T: Transform, - U: IntoServiceFactory, { ApplyTransform::new(t, factory.into_factory()) } diff --git a/actix-tracing/src/lib.rs b/actix-tracing/src/lib.rs index ada61daa..36aa21f2 100644 --- a/actix-tracing/src/lib.rs +++ b/actix-tracing/src/lib.rs @@ -103,14 +103,14 @@ where /// |req: &Request| Some(span!(Level::INFO, "request", req.id)) /// ); /// ``` -pub fn trace( - service_factory: U, +pub fn trace( + service_factory: I, make_span: F, ) -> ApplyTransform, S, Req> where + I: IntoServiceFactory, S: ServiceFactory, F: Fn(&Req) -> Option + Clone, - U: IntoServiceFactory, { apply( TracingTransform::new(make_span), diff --git a/actix-utils/src/timeout.rs b/actix-utils/src/timeout.rs index d253d25d..17647206 100644 --- a/actix-utils/src/timeout.rs +++ b/actix-utils/src/timeout.rs @@ -165,21 +165,21 @@ where pin_project! { /// `TimeoutService` response future #[derive(Debug)] - pub struct TimeoutServiceResponse + pub struct TimeoutServiceResponse where - T: Service + S: Service { #[pin] - fut: T::Future, + fut: S::Future, sleep: Delay, } } -impl Future for TimeoutServiceResponse +impl Future for TimeoutServiceResponse where - T: Service, + S: Service, { - type Output = Result>; + type Output = Result>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project();