From 6bbbdba9215f832783ba5e1eaca58c20ee9f123b Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 9 Mar 2019 06:36:23 -0800 Subject: [PATCH] revert generic Request change --- actix-service/src/and_then.rs | 85 +++++++------ actix-service/src/and_then_apply.rs | 53 ++++---- actix-service/src/and_then_apply_fn.rs | 97 ++++++++------- actix-service/src/apply.rs | 84 +++++++------ actix-service/src/blank.rs | 6 +- actix-service/src/boxed.rs | 75 ++++++------ actix-service/src/fn_service.rs | 115 ++++++++---------- actix-service/src/fn_transform.rs | 21 ++-- actix-service/src/from_err.rs | 41 ++++--- actix-service/src/lib.rs | 128 +++++++++++--------- actix-service/src/map.rs | 51 ++++---- actix-service/src/map_err.rs | 62 ++++++---- actix-service/src/map_init_err.rs | 34 ++++-- actix-service/src/then.rs | 99 +++++++++------ actix-service/src/transform.rs | 74 ++++++----- actix-service/src/transform_map_init_err.rs | 23 ++-- 16 files changed, 563 insertions(+), 485 deletions(-) diff --git a/actix-service/src/and_then.rs b/actix-service/src/and_then.rs index c52f4da1..ee0f5e18 100644 --- a/actix-service/src/and_then.rs +++ b/actix-service/src/and_then.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + use futures::{try_ready, Async, Future, Poll}; use super::{IntoNewService, NewService, Service}; @@ -14,10 +16,10 @@ pub struct AndThen { impl AndThen { /// Create new `AndThen` combinator - pub fn new(a: A, b: B) -> Self + pub fn new(a: A, b: B) -> Self where - A: Service, - B: Service, + A: Service, + B: Service, { Self { a, b: Cell::new(b) } } @@ -35,39 +37,40 @@ where } } -impl Service for AndThen +impl Service for AndThen where - A: Service, - B: Service, + A: Service, + B: Service, { + type Request = A::Request; type Response = B::Response; type Error = A::Error; - type Future = AndThenFuture; + type Future = AndThenFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { try_ready!(self.a.poll_ready()); self.b.get_mut().poll_ready() } - fn call(&mut self, req: R) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { AndThenFuture::new(self.a.call(req), self.b.clone()) } } -pub struct AndThenFuture +pub struct AndThenFuture where - A: Service, - B: Service, + A: Service, + B: Service, { b: Cell, fut_b: Option, fut_a: Option, } -impl AndThenFuture +impl AndThenFuture where - A: Service, - B: Service, + A: Service, + B: Service, { fn new(a: A::Future, b: Cell) -> Self { AndThenFuture { @@ -78,10 +81,10 @@ where } } -impl Future for AndThenFuture +impl Future for AndThenFuture where - A: Service, - B: Service, + A: Service, + B: Service, { type Item = B::Response; type Error = A::Error; @@ -104,43 +107,46 @@ where } /// `AndThenNewService` new service combinator -pub struct AndThenNewService { +pub struct AndThenNewService { a: A, b: B, + _t: PhantomData, } -impl AndThenNewService { +impl AndThenNewService { /// Create new `AndThen` combinator - pub fn new>(a: A, f: F) -> Self + pub fn new>(a: A, f: F) -> Self where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { Self { a, b: f.into_new_service(), + _t: PhantomData, } } } -impl NewService for AndThenNewService +impl NewService for AndThenNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { + type Request = A::Request; type Response = B::Response; type Error = A::Error; type Service = AndThen; type InitError = A::InitError; - type Future = AndThenNewServiceFuture; + type Future = AndThenNewServiceFuture; fn new_service(&self, cfg: &C) -> Self::Future { AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) } } -impl Clone for AndThenNewService +impl Clone for AndThenNewService where A: Clone, B: Clone, @@ -149,14 +155,15 @@ where Self { a: self.a.clone(), b: self.b.clone(), + _t: PhantomData, } } } -pub struct AndThenNewServiceFuture +pub struct AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { fut_b: B::Future, fut_a: A::Future, @@ -164,10 +171,10 @@ where b: Option, } -impl AndThenNewServiceFuture +impl AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { AndThenNewServiceFuture { @@ -179,10 +186,10 @@ where } } -impl Future for AndThenNewServiceFuture +impl Future for AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { type Item = AndThen; type Error = A::InitError; @@ -222,7 +229,8 @@ mod tests { use crate::{NewService, Service, ServiceExt}; struct Srv1(Rc>); - impl Service<&'static str> for Srv1 { + impl Service for Srv1 { + type Request = &'static str; type Response = &'static str; type Error = (); type Future = FutureResult; @@ -240,7 +248,8 @@ mod tests { #[derive(Clone)] struct Srv2(Rc>); - impl Service<&'static str> for Srv2 { + impl Service for Srv2 { + type Request = &'static str; type Response = (&'static str, &'static str); type Error = (); type Future = FutureResult; diff --git a/actix-service/src/and_then_apply.rs b/actix-service/src/and_then_apply.rs index 452f86d1..7ea0dab3 100644 --- a/actix-service/src/and_then_apply.rs +++ b/actix-service/src/and_then_apply.rs @@ -1,4 +1,3 @@ -use std::marker::PhantomData; use std::rc::Rc; use futures::{Async, Future, Poll}; @@ -8,22 +7,22 @@ use crate::from_err::FromErr; use crate::{NewService, Transform}; /// `Apply` new service combinator -pub struct AndThenTransform { +pub struct AndThenTransform { a: A, b: B, t: Rc, - _t: PhantomData
, + _t: std::marker::PhantomData, } -impl AndThenTransform { +impl AndThenTransform +where + A: NewService, + B: NewService, + T: Transform, + T::Error: From, +{ /// Create new `ApplyNewService` new service instance - pub fn new(t: T, a: A, b: B) -> Self - where - A: NewService, - B: NewService, - T: Transform, - T::Error: From, - { + pub fn new(t: T, a: A, b: B) -> Self { Self { a, b, @@ -33,7 +32,7 @@ impl AndThenTransform { } } -impl Clone for AndThenTransform +impl Clone for AndThenTransform where A: Clone, B: Clone, @@ -48,19 +47,20 @@ where } } -impl NewService for AndThenTransform +impl NewService for AndThenTransform where - A: NewService, - B: NewService, - T: Transform, + A: NewService, + B: NewService, + T: Transform, T::Error: From, { + type Request = A::Request; type Response = T::Response; type Error = T::Error; type InitError = T::InitError; type Service = AndThen, T::Transform>; - type Future = AndThenTransformFuture; + type Future = AndThenTransformFuture; fn new_service(&self, cfg: &C) -> Self::Future { AndThenTransformFuture { @@ -74,11 +74,11 @@ where } } -pub struct AndThenTransformFuture +pub struct AndThenTransformFuture where - A: NewService, - B: NewService, - T: Transform, + A: NewService, + B: NewService, + T: Transform, T::Error: From, { fut_a: A::Future, @@ -89,11 +89,11 @@ where t_cell: Rc, } -impl Future for AndThenTransformFuture +impl Future for AndThenTransformFuture where - A: NewService, - B: NewService, - T: Transform, + A: NewService, + B: NewService, + T: Transform, T::Error: From, { type Item = AndThen, T::Transform>; @@ -138,7 +138,8 @@ mod tests { #[derive(Clone)] struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/and_then_apply_fn.rs b/actix-service/src/and_then_apply_fn.rs index 8eafb738..41b536dc 100644 --- a/actix-service/src/and_then_apply_fn.rs +++ b/actix-service/src/and_then_apply_fn.rs @@ -6,23 +6,30 @@ use super::{IntoNewService, IntoService, NewService, Service}; use crate::cell::Cell; /// `Apply` service combinator -pub struct AndThenApply { +pub struct AndThenApply +where + A: Service, + B: Service, + F: FnMut(A::Response, &mut B) -> Out, + Out: IntoFuture, + Out::Error: Into, +{ a: A, b: Cell, f: Cell, - r: PhantomData<(Out, AReq, BReq)>, + r: PhantomData<(Out,)>, } -impl AndThenApply +impl AndThenApply where - A: Service, - B: Service, + A: Service, + B: Service, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, { /// Create new `Apply` combinator - pub fn new, B1: IntoService>(a: A1, b: B1, f: F) -> Self { + pub fn new, B1: IntoService>(a: A1, b: B1, f: F) -> Self { Self { f: Cell::new(f), a: a.into_service(), @@ -32,9 +39,13 @@ where } } -impl Clone for AndThenApply +impl Clone for AndThenApply where - A: Clone, + A: Service + Clone, + B: Service, + F: FnMut(A::Response, &mut B) -> Out, + Out: IntoFuture, + Out::Error: Into, { fn clone(&self) -> Self { AndThenApply { @@ -46,38 +57,38 @@ where } } -impl Service for AndThenApply +impl Service for AndThenApply where - A: Service, - B: Service, + A: Service, + B: Service, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, { + type Request = A::Request; type Response = Out::Item; type Error = A::Error; - type Future = AndThenApplyFuture; + type Future = AndThenApplyFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { try_ready!(self.a.poll_ready()); self.b.get_mut().poll_ready() } - fn call(&mut self, req: AReq) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { AndThenApplyFuture { b: self.b.clone(), f: self.f.clone(), fut_b: None, fut_a: Some(self.a.call(req)), - _t: PhantomData, } } } -pub struct AndThenApplyFuture +pub struct AndThenApplyFuture where - A: Service, - B: Service, + A: Service, + B: Service, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, @@ -86,13 +97,12 @@ where f: Cell, fut_a: Option, fut_b: Option, - _t: PhantomData<(AReq, BReq)>, } -impl Future for AndThenApplyFuture +impl Future for AndThenApplyFuture where - A: Service, - B: Service, + A: Service, + B: Service, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, @@ -119,23 +129,23 @@ where } /// `ApplyNewService` new service combinator -pub struct AndThenApplyNewService { +pub struct AndThenApplyNewService { a: A, b: B, f: Cell, - r: PhantomData<(Out, AReq, BReq, Cfg)>, + r: PhantomData<(Out, Cfg)>, } -impl AndThenApplyNewService +impl AndThenApplyNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, { /// Create new `ApplyNewService` new service instance - pub fn new, B1: IntoNewService>( + pub fn new, B1: IntoNewService>( a: A1, b: B1, f: F, @@ -149,8 +159,7 @@ where } } -impl Clone - for AndThenApplyNewService +impl Clone for AndThenApplyNewService where A: Clone, B: Clone, @@ -165,21 +174,21 @@ where } } -impl NewService - for AndThenApplyNewService +impl NewService for AndThenApplyNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, { + type Request = A::Request; type Response = Out::Item; type Error = A::Error; - type Service = AndThenApply; + type Service = AndThenApply; type InitError = A::InitError; - type Future = AndThenApplyNewServiceFuture; + type Future = AndThenApplyNewServiceFuture; fn new_service(&self, cfg: &Cfg) -> Self::Future { AndThenApplyNewServiceFuture { @@ -192,10 +201,10 @@ where } } -pub struct AndThenApplyNewServiceFuture +pub struct AndThenApplyNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, @@ -207,16 +216,15 @@ where b: Option, } -impl Future - for AndThenApplyNewServiceFuture +impl Future for AndThenApplyNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, { - type Item = AndThenApply; + type Item = AndThenApply; type Error = A::InitError; fn poll(&mut self) -> Poll { @@ -255,7 +263,8 @@ mod tests { #[derive(Clone)] struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index bf794039..d5dda2d4 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -5,23 +5,24 @@ use futures::{Async, Future, IntoFuture, Poll}; use super::{IntoNewService, IntoService, NewService, Service}; /// `Apply` service combinator -pub struct Apply { +pub struct Apply +where + T: Service, +{ service: T, f: F, - r: PhantomData<(R, In, Out)>, + r: PhantomData<(In, Out)>, } -impl Apply +impl Apply where + T: Service, F: FnMut(In, &mut T) -> Out, + Out: IntoFuture, + Out::Error: From, { /// Create new `Apply` combinator - pub fn new>(service: I, f: F) -> Self - where - T: Service, - Out: IntoFuture, - Out::Error: From, - { + pub fn new>(service: I, f: F) -> Self { Self { service: service.into_service(), f, @@ -30,9 +31,9 @@ where } } -impl Clone for Apply +impl Clone for Apply where - T: Clone, + T: Service + Clone, F: Clone, { fn clone(&self) -> Self { @@ -44,13 +45,14 @@ where } } -impl Service for Apply +impl Service for Apply where - T: Service, + T: Service, F: FnMut(In, &mut T) -> Out, Out: IntoFuture, Out::Error: From, { + type Request = In; type Response = Out::Item; type Error = Out::Error; type Future = Out::Future; @@ -65,21 +67,24 @@ where } /// `ApplyNewService` new service combinator -pub struct ApplyNewService { +pub struct ApplyNewService +where + T: NewService, +{ service: T, f: F, - r: PhantomData<(In, Out, Req)>, + r: PhantomData<(In, Out, Cfg)>, } -impl ApplyNewService { +impl ApplyNewService +where + T: NewService, + F: FnMut(In, &mut T::Service) -> Out + Clone, + Out: IntoFuture, + Out::Error: From, +{ /// Create new `ApplyNewService` new service instance - pub fn new>(service: F1, f: F) -> Self - where - T: NewService, - F: FnMut(In, &mut T::Service) -> Out + Clone, - Out: IntoFuture, - Out::Error: From, - { + pub fn new>(service: F1, f: F) -> Self { Self { f, service: service.into_new_service(), @@ -88,10 +93,11 @@ impl ApplyNewService { } } -impl Clone for ApplyNewService +impl Clone for ApplyNewService where - T: Clone, - F: Clone, + T: NewService + Clone, + F: FnMut(In, &mut T::Service) -> Out + Clone, + Out: IntoFuture, { fn clone(&self) -> Self { Self { @@ -102,28 +108,29 @@ where } } -impl NewService for ApplyNewService +impl NewService for ApplyNewService where - T: NewService, + T: NewService, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From, { + type Request = In; type Response = Out::Item; type Error = Out::Error; - type Service = Apply; + type Service = Apply; type InitError = T::InitError; - type Future = ApplyNewServiceFuture; + type Future = ApplyNewServiceFuture; fn new_service(&self, cfg: &Cfg) -> Self::Future { ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone()) } } -pub struct ApplyNewServiceFuture +pub struct ApplyNewServiceFuture where - T: NewService, + T: NewService, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, { @@ -132,9 +139,9 @@ where r: PhantomData<(In, Out)>, } -impl ApplyNewServiceFuture +impl ApplyNewServiceFuture where - T: NewService, + T: NewService, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, { @@ -147,14 +154,14 @@ where } } -impl Future for ApplyNewServiceFuture +impl Future for ApplyNewServiceFuture where - T: NewService, + T: NewService, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From, { - type Item = Apply; + type Item = Apply; type Error = T::InitError; fn poll(&mut self) -> Poll { @@ -176,7 +183,8 @@ mod tests { #[derive(Clone)] struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/blank.rs b/actix-service/src/blank.rs index 76701202..ea10b424 100644 --- a/actix-service/src/blank.rs +++ b/actix-service/src/blank.rs @@ -30,7 +30,8 @@ impl Default for Blank { } } -impl Service for Blank { +impl Service for Blank { + type Request = R; type Response = R; type Error = E; type Future = FutureResult; @@ -67,7 +68,8 @@ impl Default for BlankNewService { } } -impl NewService for BlankNewService { +impl NewService<()> for BlankNewService { + type Request = R; type Response = R; type Error = E1; type Service = Blank; diff --git a/actix-service/src/boxed.rs b/actix-service/src/boxed.rs index 63bad979..91c7bbb2 100644 --- a/actix-service/src/boxed.rs +++ b/actix-service/src/boxed.rs @@ -1,49 +1,48 @@ -use std::marker::PhantomData; - use crate::{NewService, Service}; use futures::{Future, IntoFuture, Poll}; pub type BoxedService = Box< - Service>>, + Service< + Request = Req, + Response = Res, + Error = Err, + Future = Box>, + >, >; /// Create boxed new service -pub fn new_service( +pub fn new_service( service: T, -) -> BoxedNewService +) -> BoxedNewService where C: 'static, - T: NewService + 'static, + T: NewService + 'static, + T::Request: 'static, T::Response: 'static, T::Service: 'static, T::Future: 'static, T::Error: 'static, T::InitError: 'static, - R: 'static, { BoxedNewService(Box::new(NewServiceWrapper { service, - _t: PhantomData, + _t: std::marker::PhantomData, })) } /// Create boxed service -pub fn service(service: T) -> BoxedService +pub fn service(service: T) -> BoxedService where - T: Service + 'static, + T: Service + 'static, T::Future: 'static, - R: 'static, { - Box::new(ServiceWrapper { - service, - _t: PhantomData, - }) + Box::new(ServiceWrapper(service)) } type Inner = Box< NewService< - Req, C, + Request = Req, Response = Res, Error = Err, InitError = InitErr, @@ -54,14 +53,14 @@ type Inner = Box< pub struct BoxedNewService(Inner); -impl NewService - for BoxedNewService +impl NewService for BoxedNewService where Req: 'static, Res: 'static, Err: 'static, InitErr: 'static, { + type Request = Req; type Response = Res; type Error = Err; type InitError = InitErr; @@ -73,22 +72,23 @@ where } } -struct NewServiceWrapper, R, C> { +struct NewServiceWrapper> { service: T, - _t: std::marker::PhantomData<(R, C)>, + _t: std::marker::PhantomData, } -impl NewService for NewServiceWrapper +impl NewService for NewServiceWrapper where Req: 'static, Res: 'static, Err: 'static, InitErr: 'static, - T: NewService, + T: NewService, T::Future: 'static, T::Service: 'static, - >::Future: 'static, + ::Future: 'static, { + type Request = Req; type Response = Res; type Error = Err; type InitError = InitErr; @@ -105,40 +105,33 @@ where } } -struct ServiceWrapper, R> { - service: T, - _t: PhantomData, -} +struct ServiceWrapper(T); -impl ServiceWrapper +impl ServiceWrapper where - T: Service + 'static, + T: Service + 'static, T::Future: 'static, - R: 'static, { - fn boxed(service: T) -> BoxedService { - Box::new(ServiceWrapper { - service, - _t: PhantomData, - }) + fn boxed(service: T) -> BoxedService { + Box::new(ServiceWrapper(service)) } } -impl Service for ServiceWrapper +impl Service for ServiceWrapper where - T: Service, + T: Service, T::Future: 'static, - Req: 'static, { + type Request = Req; type Response = Res; type Error = Err; type Future = Box>; fn poll_ready(&mut self) -> Poll<(), Self::Error> { - self.service.poll_ready() + self.0.poll_ready() } - fn call(&mut self, req: Req) -> Self::Future { - Box::new(self.service.call(req)) + fn call(&mut self, req: Self::Request) -> Self::Future { + Box::new(self.0.call(req)) } } diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 9ec7849a..c665b71f 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -15,22 +15,22 @@ where } /// Create `NewService` for function that can produce services -pub fn fn_factory(f: F) -> FnNewServiceNoConfig +pub fn fn_factory(f: F) -> FnNewServiceNoConfig where F: Fn() -> R, R: IntoFuture, - S: Service, + S: Service, { FnNewServiceNoConfig::new(f) } /// Create `NewService` for function that can produce services with configuration -pub fn fn_cfg_factory(f: F) -> FnNewServiceConfig +pub fn fn_cfg_factory(f: F) -> FnNewServiceConfig where F: Fn(&C) -> R, R: IntoFuture, - R::Item: IntoService, - S: Service, + R::Item: IntoService, + S: Service, { FnNewServiceConfig::new(f) } @@ -67,11 +67,12 @@ where } } -impl Service for FnService +impl Service for FnService where F: FnMut(Req) -> Out, Out: IntoFuture, { + type Request = Req; type Response = Out::Item; type Error = Out::Error; type Future = Out::Future; @@ -85,7 +86,7 @@ where } } -impl IntoService, Req> for F +impl IntoService> for F where F: FnMut(Req) -> Out + 'static, Out: IntoFuture, @@ -114,11 +115,12 @@ where } } -impl NewService for FnNewService +impl NewService for FnNewService where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, { + type Request = Req; type Response = Out::Item; type Error = Out::Error; type Service = FnService; @@ -141,7 +143,7 @@ where } } -impl IntoNewService, Req, Cfg> for F +impl IntoNewService, Cfg> for F where F: Fn(Req) -> Out + Clone, Out: IntoFuture, @@ -152,33 +154,33 @@ where } /// Converter for `Fn() -> Future` fn -pub struct FnNewServiceNoConfig +pub struct FnNewServiceNoConfig where F: Fn() -> R, R: IntoFuture, - S: Service, + S: Service, { f: F, - _t: PhantomData, } -impl FnNewServiceNoConfig +impl FnNewServiceNoConfig where F: Fn() -> R, R: IntoFuture, - S: Service, + S: Service, { pub fn new(f: F) -> Self { - FnNewServiceNoConfig { f, _t: PhantomData } + FnNewServiceNoConfig { f } } } -impl NewService for FnNewServiceNoConfig +impl NewService<()> for FnNewServiceNoConfig where F: Fn() -> R, R: IntoFuture, - S: Service, + S: Service, { + type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S; @@ -191,65 +193,66 @@ where } } -impl Clone for FnNewServiceNoConfig +impl Clone for FnNewServiceNoConfig where F: Fn() -> R + Clone, R: IntoFuture, - S: Service, + S: Service, { fn clone(&self) -> Self { Self::new(self.f.clone()) } } -impl IntoNewService, Req, ()> for F +impl IntoNewService, ()> for F where F: Fn() -> R, R: IntoFuture, - S: Service, + S: Service, { - fn into_new_service(self) -> FnNewServiceNoConfig { + fn into_new_service(self) -> FnNewServiceNoConfig { FnNewServiceNoConfig::new(self) } } /// Convert `Fn(&Config) -> Future` fn to NewService -pub struct FnNewServiceConfig +pub struct FnNewServiceConfig where F: Fn(&C) -> R, R: IntoFuture, - R::Item: IntoService, - S: Service, + R::Item: IntoService, + S: Service, { f: F, - _t: PhantomData<(C, R, S, E, Req)>, + _t: PhantomData<(C, R, S, E)>, } -impl FnNewServiceConfig +impl FnNewServiceConfig where F: Fn(&C) -> R, R: IntoFuture, - R::Item: IntoService, - S: Service, + R::Item: IntoService, + S: Service, { pub fn new(f: F) -> Self { FnNewServiceConfig { f, _t: PhantomData } } } -impl NewService for FnNewServiceConfig +impl NewService for FnNewServiceConfig where F: Fn(&C) -> R, R: IntoFuture, - R::Item: IntoService, - S: Service, + R::Item: IntoService, + S: Service, { + type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S; type InitError = E; - type Future = FnNewServiceConfigFut; + type Future = FnNewServiceConfigFut; fn new_service(&self, cfg: &C) -> Self::Future { FnNewServiceConfigFut { @@ -259,21 +262,21 @@ where } } -pub struct FnNewServiceConfigFut +pub struct FnNewServiceConfigFut where R: IntoFuture, - R::Item: IntoService, - S: Service, + R::Item: IntoService, + S: Service, { fut: R::Future, - _t: PhantomData<(S, Req)>, + _t: PhantomData<(S,)>, } -impl Future for FnNewServiceConfigFut +impl Future for FnNewServiceConfigFut where R: IntoFuture, - R::Item: IntoService, - S: Service, + R::Item: IntoService, + S: Service, { type Item = S; type Error = R::Error; @@ -283,44 +286,26 @@ where } } -impl Clone for FnNewServiceConfig +impl Clone for FnNewServiceConfig where F: Fn(&C) -> R + Clone, R: IntoFuture, - R::Item: IntoService, - S: Service, + R::Item: IntoService, + S: Service, { fn clone(&self) -> Self { Self::new(self.f.clone()) } } -impl - IntoConfigurableNewService, Req, C> for F +impl IntoConfigurableNewService, C> for F where F: Fn(&C) -> R, R: IntoFuture, - R::Item: IntoService, - S: Service, + R::Item: IntoService, + S: Service, { - fn into_new_service(self) -> FnNewServiceConfig { + fn into_new_service(self) -> FnNewServiceConfig { FnNewServiceConfig::new(self) } } - -#[cfg(test)] -mod tests { - use crate::{IntoService, Service, ServiceExt}; - - #[test] - fn test_fn_service() { - let mut rt = actix_rt::Runtime::new().unwrap(); - - let srv = (|_t: &str| -> Result { Ok(1) }).into_service(); - let mut srv = srv.and_then(|test: usize| Ok(test)); - - let s = "HELLO".to_owned(); - let res = rt.block_on(srv.call(&s)).unwrap(); - assert_eq!(res, 1); - } -} diff --git a/actix-service/src/fn_transform.rs b/actix-service/src/fn_transform.rs index c3737f22..7c3773ba 100644 --- a/actix-service/src/fn_transform.rs +++ b/actix-service/src/fn_transform.rs @@ -5,16 +5,16 @@ use futures::IntoFuture; use crate::{Apply, IntoTransform, Service, Transform}; -pub struct FnTransform +pub struct FnTransform where F: FnMut(In, &mut S) -> Out + Clone, Out: IntoFuture, { f: F, - _t: PhantomData<(S, R, In, Out, Err)>, + _t: PhantomData<(S, In, Out, Err)>, } -impl FnTransform +impl FnTransform where F: FnMut(In, &mut S) -> Out + Clone, Out: IntoFuture, @@ -24,16 +24,17 @@ where } } -impl Transform for FnTransform +impl Transform for FnTransform where - S: Service, + S: Service, F: FnMut(In, &mut S) -> Out + Clone, Out: IntoFuture, Out::Error: From, { + type Request = In; type Response = Out::Item; type Error = Out::Error; - type Transform = Apply; + type Transform = Apply; type InitError = Err; type Future = FutureResult; @@ -42,19 +43,19 @@ where } } -impl IntoTransform, S, In> for F +impl IntoTransform, S> for F where - S: Service, + S: Service, F: FnMut(In, &mut S) -> Out + Clone, Out: IntoFuture, Out::Error: From, { - fn into_transform(self) -> FnTransform { + fn into_transform(self) -> FnTransform { FnTransform::new(self) } } -impl Clone for FnTransform +impl Clone for FnTransform where F: FnMut(In, &mut S) -> Out + Clone, Out: IntoFuture, diff --git a/actix-service/src/from_err.rs b/actix-service/src/from_err.rs index dade2a3b..31ccb2d9 100644 --- a/actix-service/src/from_err.rs +++ b/actix-service/src/from_err.rs @@ -13,9 +13,9 @@ pub struct FromErr { } impl FromErr { - pub(crate) fn new(service: A) -> Self + pub(crate) fn new(service: A) -> Self where - A: Service, + A: Service, E: From, { FromErr { @@ -37,20 +37,21 @@ where } } -impl Service for FromErr +impl Service for FromErr where - A: Service, + A: Service, E: From, { + type Request = A::Request; type Response = A::Response; type Error = E; - type Future = FromErrFuture; + type Future = FromErrFuture; fn poll_ready(&mut self) -> Poll<(), E> { self.service.poll_ready().map_err(E::from) } - fn call(&mut self, req: R) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { FromErrFuture { fut: self.service.call(req), f: PhantomData, @@ -58,14 +59,14 @@ where } } -pub struct FromErrFuture, R, E> { +pub struct FromErrFuture { fut: A::Future, f: PhantomData, } -impl Future for FromErrFuture +impl Future for FromErrFuture where - A: Service, + A: Service, E: From, { type Item = A::Response; @@ -87,9 +88,9 @@ pub struct FromErrNewService { impl FromErrNewService { /// Create new `FromErr` new service instance - pub fn new(a: A) -> Self + pub fn new(a: A) -> Self where - A: NewService, + A: NewService, E: From, { Self { a, e: PhantomData } @@ -108,17 +109,18 @@ where } } -impl NewService for FromErrNewService +impl NewService for FromErrNewService where - A: NewService, + A: NewService, E: From, { + type Request = A::Request; type Response = A::Response; type Error = E; type Service = FromErr; type InitError = A::InitError; - type Future = FromErrNewServiceFuture; + type Future = FromErrNewServiceFuture; fn new_service(&self, cfg: &C) -> Self::Future { FromErrNewServiceFuture { @@ -128,18 +130,18 @@ where } } -pub struct FromErrNewServiceFuture +pub struct FromErrNewServiceFuture where - A: NewService, + A: NewService, E: From, { fut: A::Future, e: PhantomData, } -impl Future for FromErrNewServiceFuture +impl Future for FromErrNewServiceFuture where - A: NewService, + A: NewService, E: From, { type Item = FromErr; @@ -162,7 +164,8 @@ mod tests { use crate::{IntoNewService, NewService, Service, ServiceExt}; struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index 0bff0813..8dfc6b22 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -36,9 +36,10 @@ pub use self::then::{Then, ThenNewService}; pub use self::transform::{ApplyTransform, IntoTransform, Transform}; /// An asynchronous function from `Request` to a `Response`. -/// -/// `Request` - requests handled by the service. -pub trait Service { +pub trait Service { + /// Requests handled by the service. + type Request; + /// Responses given by the service. type Response; @@ -68,26 +69,22 @@ pub trait Service { /// /// Calling `call` without calling `poll_ready` is permitted. The /// implementation must be resilient to this fact. - fn call(&mut self, req: Request) -> Self::Future; + fn call(&mut self, req: Self::Request) -> Self::Future; } /// An extension trait for `Service`s that provides a variety of convenient /// adapters -pub trait ServiceExt: Service { +pub trait ServiceExt: Service { /// Apply function to specified service and use it as a next service in /// chain. - fn apply_fn( - self, - service: B1, - f: F, - ) -> AndThenApply + fn apply_fn(self, service: B1, f: F) -> AndThenApply where Self: Sized, F: FnMut(Self::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, - B: Service, - B1: IntoService, + B: Service, + B1: IntoService, { AndThenApply::new(self, service, f) } @@ -104,8 +101,8 @@ pub trait ServiceExt: Service { fn and_then(self, service: F) -> AndThen where Self: Sized, - F: IntoService, - B: Service, + F: IntoService, + B: Service, { AndThen::new(self, service.into_service()) } @@ -131,7 +128,7 @@ pub trait ServiceExt: Service { fn then(self, service: B) -> Then where Self: Sized, - B: Service, Error = Self::Error>, + B: Service, Error = Self::Error>, { Then::new(self, service) } @@ -170,7 +167,7 @@ pub trait ServiceExt: Service { } } -impl ServiceExt for T where T: Service {} +impl ServiceExt for T where T: Service {} /// Creates new `Service` values. /// @@ -180,9 +177,11 @@ impl ServiceExt for T where T: Service {} /// `NewService` trait, and uses that new `Service` value to process inbound /// requests on that new TCP stream. /// -/// * `Request` - requests handled by the service. -/// * `Config` - is a service factory configuration type. -pub trait NewService { +/// `Config` is a service factory configuration type. +pub trait NewService { + /// Requests handled by the service. + type Request; + /// Responses given by the service type Response; @@ -190,7 +189,11 @@ pub trait NewService { type Error; /// The `Service` value created by this factory - type Service: Service; + type Service: Service< + Request = Self::Request, + Response = Self::Response, + Error = Self::Error, + >; /// Errors produced while building a service. type InitError; @@ -203,33 +206,33 @@ pub trait NewService { /// Apply function to specified service and use it as a next service in /// chain. - fn apply( + fn apply( self, transform: T1, service: B1, - ) -> AndThenTransform + ) -> AndThenTransform where Self: Sized, - T: Transform, + T: Transform, T::Error: From, - T1: IntoTransform, - B: NewService, - B1: IntoNewService, + T1: IntoTransform, + B: NewService, + B1: IntoNewService, { AndThenTransform::new(transform.into_transform(), self, service.into_new_service()) } /// Apply function to specified service and use it as a next service in /// chain. - fn apply_fn( + fn apply_fn( self, service: I, f: F, - ) -> AndThenApplyNewService + ) -> AndThenApplyNewService where Self: Sized, - B: NewService, - I: IntoNewService, + B: NewService, + I: IntoNewService, F: FnMut(Self::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, @@ -238,11 +241,16 @@ pub trait NewService { } /// Call another service after call to this one has resolved successfully. - fn and_then(self, new_service: F) -> AndThenNewService + fn and_then(self, new_service: F) -> AndThenNewService where Self: Sized, - F: IntoNewService, - B: NewService, + F: IntoNewService, + B: NewService< + Config, + Request = Self::Response, + Error = Self::Error, + InitError = Self::InitError, + >, { AndThenNewService::new(self, new_service) } @@ -270,15 +278,15 @@ pub trait NewService { fn then(self, new_service: F) -> ThenNewService where Self: Sized, - F: IntoNewService, Config>, + F: IntoNewService, B: NewService< - Result, Config, + Request = Result, Error = Self::Error, InitError = Self::InitError, >, { - ThenNewService::new(self, new_service.into_new_service()) + ThenNewService::new(self, new_service) } /// Map this service's output to a different type, returning a new service @@ -310,10 +318,11 @@ pub trait NewService { } } -impl<'a, S, R> Service for &'a mut S +impl<'a, S> Service for &'a mut S where - S: Service + 'a, + S: Service + 'a, { + type Request = S::Request; type Response = S::Response; type Error = S::Error; type Future = S::Future; @@ -322,15 +331,16 @@ where (**self).poll_ready() } - fn call(&mut self, request: R) -> S::Future { + fn call(&mut self, request: Self::Request) -> S::Future { (**self).call(request) } } -impl Service for Box +impl Service for Box where - S: Service + ?Sized, + S: Service + ?Sized, { + type Request = S::Request; type Response = S::Response; type Error = S::Error; type Future = S::Future; @@ -339,15 +349,16 @@ where (**self).poll_ready() } - fn call(&mut self, request: R) -> S::Future { + fn call(&mut self, request: Self::Request) -> S::Future { (**self).call(request) } } -impl NewService for Rc +impl NewService for Rc where - S: NewService, + S: NewService, { + type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S::Service; @@ -359,10 +370,11 @@ where } } -impl NewService for Arc +impl NewService for Arc where - S: NewService, + S: NewService, { + type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S::Service; @@ -375,35 +387,35 @@ where } /// Trait for types that can be converted to a `Service` -pub trait IntoService +pub trait IntoService where - T: Service, + T: Service, { /// Convert to a `Service` fn into_service(self) -> T; } /// Trait for types that can be converted to a `NewService` -pub trait IntoNewService +pub trait IntoNewService where - T: NewService, + T: NewService, { /// Convert to an `NewService` fn into_new_service(self) -> T; } -impl IntoService for T +impl IntoService for T where - T: Service, + T: Service, { fn into_service(self) -> T { self } } -impl IntoNewService for T +impl IntoNewService for T where - T: NewService, + T: NewService, { fn into_new_service(self) -> T { self @@ -411,17 +423,17 @@ where } /// Trait for types that can be converted to a configurable `NewService` -pub trait IntoConfigurableNewService +pub trait IntoConfigurableNewService where - T: NewService, + T: NewService, { /// Convert to an `NewService` fn into_new_service(self) -> T; } -impl IntoConfigurableNewService for T +impl IntoConfigurableNewService for T where - T: NewService, + T: NewService, { fn into_new_service(self) -> T { self diff --git a/actix-service/src/map.rs b/actix-service/src/map.rs index e7ad15c5..a98d14f5 100644 --- a/actix-service/src/map.rs +++ b/actix-service/src/map.rs @@ -15,9 +15,9 @@ pub struct Map { impl Map { /// Create new `Map` combinator - pub fn new(service: A, f: F) -> Self + pub fn new(service: A, f: F) -> Self where - A: Service, + A: Service, F: FnMut(A::Response) -> Response, { Self { @@ -42,36 +42,37 @@ where } } -impl Service for Map +impl Service for Map where - A: Service, + A: Service, F: FnMut(A::Response) -> Response + Clone, { + type Request = A::Request; type Response = Response; type Error = A::Error; - type Future = MapFuture; + type Future = MapFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready() } - fn call(&mut self, req: R) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { MapFuture::new(self.service.call(req), self.f.clone()) } } -pub struct MapFuture +pub struct MapFuture where - A: Service, + A: Service, F: FnMut(A::Response) -> Response, { f: F, fut: A::Future, } -impl MapFuture +impl MapFuture where - A: Service, + A: Service, F: FnMut(A::Response) -> Response, { fn new(fut: A::Future, f: F) -> Self { @@ -79,9 +80,9 @@ where } } -impl Future for MapFuture +impl Future for MapFuture where - A: Service, + A: Service, F: FnMut(A::Response) -> Response, { type Item = Response; @@ -104,9 +105,9 @@ pub struct MapNewService { impl MapNewService { /// Create new `Map` new service instance - pub fn new(a: A, f: F) -> Self + pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Res, { Self { @@ -131,35 +132,36 @@ where } } -impl NewService for MapNewService +impl NewService for MapNewService where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Res + Clone, { + type Request = A::Request; type Response = Res; type Error = A::Error; type Service = Map; type InitError = A::InitError; - type Future = MapNewServiceFuture; + type Future = MapNewServiceFuture; fn new_service(&self, cfg: &Cfg) -> Self::Future { MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) } } -pub struct MapNewServiceFuture +pub struct MapNewServiceFuture where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Res, { fut: A::Future, f: Option, } -impl MapNewServiceFuture +impl MapNewServiceFuture where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Res, { fn new(fut: A::Future, f: F) -> Self { @@ -167,9 +169,9 @@ where } } -impl Future for MapNewServiceFuture +impl Future for MapNewServiceFuture where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Res, { type Item = Map; @@ -192,7 +194,8 @@ mod tests { use crate::{IntoNewService, Service, ServiceExt}; struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index f3d8c63d..cd15d239 100644 --- a/actix-service/src/map_err.rs +++ b/actix-service/src/map_err.rs @@ -16,9 +16,9 @@ pub struct MapErr { impl MapErr { /// Create new `MapErr` combinator - pub fn new(service: A, f: F) -> Self + pub fn new(service: A, f: F) -> Self where - A: Service, + A: Service, F: Fn(A::Error) -> E, { Self { @@ -43,39 +43,47 @@ where } } -impl Service for MapErr +impl Service for MapErr where - A: Service, + A: Service, F: Fn(A::Error) -> E + Clone, { + type Request = A::Request; type Response = A::Response; type Error = E; - type Future = MapErrFuture; + type Future = MapErrFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready().map_err(&self.f) } - fn call(&mut self, req: R) -> Self::Future { - MapErrFuture { - fut: self.service.call(req), - f: self.f.clone(), - } + fn call(&mut self, req: A::Request) -> Self::Future { + MapErrFuture::new(self.service.call(req), self.f.clone()) } } -pub struct MapErrFuture +pub struct MapErrFuture where - A: Service, + A: Service, F: Fn(A::Error) -> E, { f: F, fut: A::Future, } -impl Future for MapErrFuture +impl MapErrFuture where - A: Service, + A: Service, + F: Fn(A::Error) -> E, +{ + fn new(fut: A::Future, f: F) -> Self { + MapErrFuture { f, fut } + } +} + +impl Future for MapErrFuture +where + A: Service, F: Fn(A::Error) -> E, { type Item = A::Response; @@ -98,9 +106,9 @@ pub struct MapErrNewService { impl MapErrNewService { /// Create new `MapErr` new service instance - pub fn new(a: A, f: F) -> Self + pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, F: Fn(A::Error) -> E, { Self { @@ -125,35 +133,36 @@ where } } -impl NewService for MapErrNewService +impl NewService for MapErrNewService where - A: NewService, + A: NewService, F: Fn(A::Error) -> E + Clone, { + type Request = A::Request; type Response = A::Response; type Error = E; type Service = MapErr; type InitError = A::InitError; - type Future = MapErrNewServiceFuture; + type Future = MapErrNewServiceFuture; fn new_service(&self, cfg: &C) -> Self::Future { MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) } } -pub struct MapErrNewServiceFuture +pub struct MapErrNewServiceFuture where - A: NewService, + A: NewService, F: Fn(A::Error) -> E, { fut: A::Future, f: F, } -impl MapErrNewServiceFuture +impl MapErrNewServiceFuture where - A: NewService, + A: NewService, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -161,9 +170,9 @@ where } } -impl Future for MapErrNewServiceFuture +impl Future for MapErrNewServiceFuture where - A: NewService, + A: NewService, F: Fn(A::Error) -> E + Clone, { type Item = MapErr; @@ -187,7 +196,8 @@ mod tests { struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/map_init_err.rs b/actix-service/src/map_init_err.rs index b3f4f072..094a4dbe 100644 --- a/actix-service/src/map_init_err.rs +++ b/actix-service/src/map_init_err.rs @@ -13,9 +13,9 @@ pub struct MapInitErr { impl MapInitErr { /// Create new `MapInitErr` combinator - pub fn new(a: A, f: F) -> Self + pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E, { Self { @@ -40,38 +40,46 @@ where } } -impl NewService for MapInitErr +impl NewService for MapInitErr where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E + Clone, { + type Request = A::Request; type Response = A::Response; type Error = A::Error; type Service = A::Service; type InitError = E; - type Future = MapInitErrFuture; + type Future = MapInitErrFuture; fn new_service(&self, cfg: &C) -> Self::Future { - MapInitErrFuture { - fut: self.a.new_service(cfg), - f: self.f.clone(), - } + MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone()) } } -pub struct MapInitErrFuture +pub struct MapInitErrFuture where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E, { f: F, fut: A::Future, } -impl Future for MapInitErrFuture +impl MapInitErrFuture where - A: NewService, + A: NewService, + F: Fn(A::InitError) -> E, +{ + fn new(fut: A::Future, f: F) -> Self { + MapInitErrFuture { f, fut } + } +} + +impl Future for MapInitErrFuture +where + A: NewService, F: Fn(A::InitError) -> E, { type Item = A::Service; diff --git a/actix-service/src/then.rs b/actix-service/src/then.rs index 777201f0..c0a31bfd 100644 --- a/actix-service/src/then.rs +++ b/actix-service/src/then.rs @@ -2,7 +2,7 @@ use std::marker::PhantomData; use futures::{try_ready, Async, Future, Poll}; -use super::{NewService, Service}; +use super::{IntoNewService, NewService, Service}; use crate::cell::Cell; /// Service for the `then` combinator, chaining a computation onto the end of @@ -16,10 +16,10 @@ pub struct Then { impl Then { /// Create new `Then` combinator - pub fn new(a: A, b: B) -> Then + pub fn new(a: A, b: B) -> Then where - A: Service, - B: Service, Error = A::Error>, + A: Service, + B: Service, Error = A::Error>, { Then { a, b: Cell::new(b) } } @@ -37,39 +37,40 @@ where } } -impl Service for Then +impl Service for Then where - A: Service, - B: Service, Error = A::Error>, + A: Service, + B: Service, Error = A::Error>, { + type Request = A::Request; type Response = B::Response; type Error = B::Error; - type Future = ThenFuture; + type Future = ThenFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { try_ready!(self.a.poll_ready()); self.b.get_mut().poll_ready() } - fn call(&mut self, req: R) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { ThenFuture::new(self.a.call(req), self.b.clone()) } } -pub struct ThenFuture +pub struct ThenFuture where - A: Service, - B: Service>, + A: Service, + B: Service>, { b: Cell, fut_b: Option, fut_a: Option, } -impl ThenFuture +impl ThenFuture where - A: Service, - B: Service>, + A: Service, + B: Service>, { fn new(a: A::Future, b: Cell) -> Self { ThenFuture { @@ -80,10 +81,10 @@ where } } -impl Future for ThenFuture +impl Future for ThenFuture where - A: Service, - B: Service>, + A: Service, + B: Service>, { type Item = B::Response; type Error = B::Error; @@ -118,35 +119,42 @@ pub struct ThenNewService { impl ThenNewService { /// Create new `AndThen` combinator - pub fn new(a: A, f: B) -> Self + pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, B: NewService< - Result, C, + Request = Result, Error = A::Error, InitError = A::InitError, >, + F: IntoNewService, { Self { a, - b: f, + b: f.into_new_service(), _t: PhantomData, } } } -impl NewService for ThenNewService +impl NewService for ThenNewService where - A: NewService, - B: NewService, C, Error = A::Error, InitError = A::InitError>, + A: NewService, + B: NewService< + C, + Request = Result, + Error = A::Error, + InitError = A::InitError, + >, { + type Request = A::Request; type Response = B::Response; type Error = A::Error; type Service = Then; type InitError = A::InitError; - type Future = ThenNewServiceFuture; + type Future = ThenNewServiceFuture; fn new_service(&self, cfg: &C) -> Self::Future { ThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) @@ -167,10 +175,15 @@ where } } -pub struct ThenNewServiceFuture +pub struct ThenNewServiceFuture where - A: NewService, - B: NewService, C, Error = A::Error, InitError = A::InitError>, + A: NewService, + B: NewService< + C, + Request = Result, + Error = A::Error, + InitError = A::InitError, + >, { fut_b: B::Future, fut_a: A::Future, @@ -178,10 +191,15 @@ where b: Option, } -impl ThenNewServiceFuture +impl ThenNewServiceFuture where - A: NewService, - B: NewService, C, Error = A::Error, InitError = A::InitError>, + A: NewService, + B: NewService< + C, + Request = Result, + Error = A::Error, + InitError = A::InitError, + >, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { ThenNewServiceFuture { @@ -193,10 +211,15 @@ where } } -impl Future for ThenNewServiceFuture +impl Future for ThenNewServiceFuture where - A: NewService, - B: NewService, C, Error = A::Error, InitError = A::InitError>, + A: NewService, + B: NewService< + C, + Request = Result, + Error = A::Error, + InitError = A::InitError, + >, { type Item = Then; type Error = A::InitError; @@ -236,7 +259,8 @@ mod tests { #[derive(Clone)] struct Srv1(Rc>); - impl Service> for Srv1 { + impl Service for Srv1 { + type Request = Result<&'static str, &'static str>; type Response = &'static str; type Error = (); type Future = FutureResult; @@ -256,7 +280,8 @@ mod tests { struct Srv2(Rc>); - impl Service> for Srv2 { + impl Service for Srv2 { + type Request = Result<&'static str, ()>; type Response = (&'static str, &'static str); type Error = (); type Future = FutureResult; diff --git a/actix-service/src/transform.rs b/actix-service/src/transform.rs index edba905e..3e8a693e 100644 --- a/actix-service/src/transform.rs +++ b/actix-service/src/transform.rs @@ -9,10 +9,11 @@ use crate::{NewService, Service}; /// `Transform` service factory. /// /// Transform factory creates service that wraps other services. -/// -/// * `S` is a wrapped service. -/// * `R` requests handled by this transform service. -pub trait Transform { +/// `Config` is a service factory configuration type. +pub trait Transform { + /// Requests handled by the service. + type Request; + /// Responses given by the service. type Response; @@ -20,7 +21,11 @@ pub trait Transform { type Error; /// The `TransformService` value created by this factory - type Transform: Service; + type Transform: Service< + Request = Self::Request, + Response = Self::Response, + Error = Self::Error, + >; /// Errors produced while building a service. type InitError; @@ -42,10 +47,11 @@ pub trait Transform { } } -impl Transform for Rc +impl Transform for Rc where - T: Transform, + T: Transform, { + type Request = T::Request; type Response = T::Response; type Error = T::Error; type InitError = T::InitError; @@ -57,10 +63,11 @@ where } } -impl Transform for Arc +impl Transform for Arc where - T: Transform, + T: Transform, { + type Request = T::Request; type Response = T::Response; type Error = T::Error; type InitError = T::InitError; @@ -73,17 +80,17 @@ where } /// Trait for types that can be converted to a *transform service* -pub trait IntoTransform +pub trait IntoTransform where - T: Transform, + T: Transform, { /// Convert to a `TransformService` fn into_transform(self) -> T; } -impl IntoTransform for T +impl IntoTransform for T where - T: Transform, + T: Transform, { fn into_transform(self) -> T { self @@ -92,19 +99,19 @@ where /// `Apply` transform new service #[derive(Clone)] -pub struct ApplyTransform { - a: S, +pub struct ApplyTransform { + a: A, t: Rc, - _t: std::marker::PhantomData<(R, Req, Cfg)>, + _t: std::marker::PhantomData, } -impl ApplyTransform +impl ApplyTransform where - S: NewService, - T: Transform, + A: NewService, + T: Transform, { /// Create new `ApplyNewService` new service instance - pub fn new>(t: F, a: S) -> Self { + pub fn new>(t: F, a: A) -> Self { Self { a, t: Rc::new(t.into_transform()), @@ -113,19 +120,20 @@ where } } -impl NewService for ApplyTransform +impl NewService for ApplyTransform where - S: NewService, - T: Transform, + A: NewService, + T: Transform, { + type Request = T::Request; type Response = T::Response; type Error = T::Error; type Service = T::Transform; type InitError = T::InitError; - type Future = ApplyTransformFuture; + type Future = ApplyTransformFuture; - fn new_service(&self, cfg: &Cfg) -> Self::Future { + fn new_service(&self, cfg: &C) -> Self::Future { ApplyTransformFuture { t_cell: self.t.clone(), fut_a: self.a.new_service(cfg).into_future(), @@ -134,20 +142,20 @@ where } } -pub struct ApplyTransformFuture +pub struct ApplyTransformFuture where - S: NewService, - T: Transform, + A: NewService, + T: Transform, { - fut_a: S::Future, - fut_t: Option, + fut_a: A::Future, + fut_t: Option<::Future>, t_cell: Rc, } -impl Future for ApplyTransformFuture +impl Future for ApplyTransformFuture where - S: NewService, - T: Transform, + A: NewService, + T: Transform, { type Item = T::Transform; type Error = T::InitError; diff --git a/actix-service/src/transform_map_init_err.rs b/actix-service/src/transform_map_init_err.rs index 48589ffc..0b0dd9b9 100644 --- a/actix-service/src/transform_map_init_err.rs +++ b/actix-service/src/transform_map_init_err.rs @@ -16,9 +16,9 @@ pub struct TransformMapInitErr { impl TransformMapInitErr { /// Create new `MapInitErr` new transform instance - pub fn new(t: T, f: F) -> Self + pub fn new(t: T, f: F) -> Self where - T: Transform, + T: Transform, F: Fn(T::InitError) -> E, { Self { @@ -43,35 +43,36 @@ where } } -impl Transform for TransformMapInitErr +impl Transform for TransformMapInitErr where - T: Transform, + T: Transform, F: Fn(T::InitError) -> E + Clone, { + type Request = T::Request; type Response = T::Response; type Error = T::Error; type Transform = T::Transform; type InitError = E; - type Future = TransformMapInitErrFuture; + type Future = TransformMapInitErrFuture; fn new_transform(&self, service: S) -> Self::Future { TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone()) } } -pub struct TransformMapInitErrFuture +pub struct TransformMapInitErrFuture where - T: Transform, + T: Transform, F: Fn(T::InitError) -> E, { fut: T::Future, f: F, } -impl TransformMapInitErrFuture +impl TransformMapInitErrFuture where - T: Transform, + T: Transform, F: Fn(T::InitError) -> E, { fn new(fut: T::Future, f: F) -> Self { @@ -79,9 +80,9 @@ where } } -impl Future for TransformMapInitErrFuture +impl Future for TransformMapInitErrFuture where - T: Transform, + T: Transform, F: Fn(T::InitError) -> E + Clone, { type Item = T::Transform;