From dfbb77f98d9099c6237d4965831163f28de0c2b7 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 5 Mar 2019 07:35:26 -0800 Subject: [PATCH] make service Request type generic --- actix-connector/Cargo.toml | 3 +- actix-connector/src/connector.rs | 15 +-- actix-connector/src/resolver.rs | 3 +- actix-server/CHANGES.md | 5 + actix-server/src/config.rs | 24 ++-- actix-server/src/services.rs | 11 +- actix-service/Cargo.toml | 3 + 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 | 90 ++++++++------ 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 ++-- actix-test-server/Cargo.toml | 3 +- actix-utils/CHANGES.md | 5 + actix-utils/src/cloneable.rs | 11 +- actix-utils/src/either.rs | 62 +++------- actix-utils/src/framed.rs | 46 ++++--- actix-utils/src/inflight.rs | 19 ++- actix-utils/src/keepalive.rs | 6 +- actix-utils/src/order.rs | 69 +++++------ actix-utils/src/stream.rs | 34 +++--- actix-utils/src/time.rs | 4 +- actix-utils/src/timeout.rs | 23 ++-- 34 files changed, 622 insertions(+), 747 deletions(-) diff --git a/actix-connector/Cargo.toml b/actix-connector/Cargo.toml index fe4081fa..d20459ef 100644 --- a/actix-connector/Cargo.toml +++ b/actix-connector/Cargo.toml @@ -27,7 +27,8 @@ default = [] ssl = ["openssl", "tokio-openssl"] [dependencies] -actix-service = "0.3.0" +#actix-service = "0.3.0" +actix-service = { path="../actix-service" } actix-codec = "0.1.0" futures = "0.1" tokio-tcp = "0.1" diff --git a/actix-connector/src/connector.rs b/actix-connector/src/connector.rs index e3c12cc2..ba18b474 100644 --- a/actix-connector/src/connector.rs +++ b/actix-connector/src/connector.rs @@ -167,8 +167,8 @@ impl Connector { /// Create new connector with custom resolver pub fn with_resolver( resolver: Resolver, - ) -> impl Service - + Clone { + ) -> impl Service + Clone + { Connector { resolver } } @@ -177,8 +177,8 @@ impl Connector { cfg: ResolverConfig, opts: ResolverOpts, ) -> impl NewService< + Connect, (), - Request = Connect, Response = (Connect, TcpStream), Error = ConnectorError, InitError = E, @@ -195,8 +195,7 @@ impl Clone for Connector { } } -impl Service for Connector { - type Request = Connect; +impl Service for Connector { type Response = (Connect, TcpStream); type Error = ConnectorError; type Future = Either; @@ -273,8 +272,7 @@ impl Default for TcpConnector { } } -impl Service for TcpConnector { - type Request = (T, VecDeque); +impl Service<(T, VecDeque)> for TcpConnector { type Response = (T, TcpStream); type Error = io::Error; type Future = TcpConnectorResponse; @@ -354,8 +352,7 @@ impl DefaultConnector { } } -impl Service for DefaultConnector { - type Request = Connect; +impl Service for DefaultConnector { type Response = TcpStream; type Error = ConnectorError; type Future = DefaultConnectorFuture; diff --git a/actix-connector/src/resolver.rs b/actix-connector/src/resolver.rs index 443fdd75..d6ee2612 100644 --- a/actix-connector/src/resolver.rs +++ b/actix-connector/src/resolver.rs @@ -67,8 +67,7 @@ impl Clone for Resolver { } } -impl Service for Resolver { - type Request = T; +impl Service for Resolver { type Response = (T, VecDeque); type Error = ResolveError; type Future = ResolverFuture; diff --git a/actix-server/CHANGES.md b/actix-server/CHANGES.md index 52a62efe..c7d86cc0 100644 --- a/actix-server/CHANGES.md +++ b/actix-server/CHANGES.md @@ -1,5 +1,10 @@ # Changes +## [0.4.0] - 2019-03-xx + +* Upgrade actix-service + + ## [0.3.1] - 2019-03-04 ### Added diff --git a/actix-server/src/config.rs b/actix-server/src/config.rs index e142838a..c2fd5ec7 100644 --- a/actix-server/src/config.rs +++ b/actix-server/src/config.rs @@ -169,8 +169,8 @@ impl ServiceRuntime { pub fn service(&mut self, name: &str, service: F) where - F: IntoNewService, - T: NewService + 'static, + F: IntoNewService, + T: NewService + 'static, T::Future: 'static, T::Service: 'static, T::InitError: fmt::Debug, @@ -191,7 +191,7 @@ impl ServiceRuntime { type BoxedNewService = Box< NewService< - Request = (Option, ServerMessage), + (Option, ServerMessage), Response = (), Error = (), InitError = (), @@ -204,15 +204,14 @@ struct ServiceFactory { inner: T, } -impl NewService for ServiceFactory +impl NewService<(Option, ServerMessage)> for ServiceFactory where - T: NewService, + T: NewService, T::Future: 'static, T::Service: 'static, T::Error: 'static, T::InitError: fmt::Debug + 'static, { - type Request = (Option, ServerMessage); type Response = (); type Error = (); type InitError = (); @@ -220,14 +219,9 @@ where type Future = Box>; fn new_service(&self, _: &()) -> Self::Future { - Box::new( - self.inner - .new_service(&()) - .map_err(|_| ()) - .map(|s| { - let service: BoxedServerService = Box::new(StreamService::new(s)); - service - }), - ) + Box::new(self.inner.new_service(&()).map_err(|_| ()).map(|s| { + let service: BoxedServerService = Box::new(StreamService::new(s)); + service + })) } } diff --git a/actix-server/src/services.rs b/actix-server/src/services.rs index 2aaf998d..a78bbb48 100644 --- a/actix-server/src/services.rs +++ b/actix-server/src/services.rs @@ -23,7 +23,7 @@ pub(crate) enum ServerMessage { } pub trait ServiceFactory: Send + Clone + 'static { - type NewService: NewService; + type NewService: NewService; fn create(&self) -> Self::NewService; } @@ -38,7 +38,7 @@ pub(crate) trait InternalServiceFactory: Send { pub(crate) type BoxedServerService = Box< Service< - Request = (Option, ServerMessage), + (Option, ServerMessage), Response = (), Error = (), Future = FutureResult<(), ()>, @@ -55,13 +55,12 @@ impl StreamService { } } -impl Service for StreamService +impl Service<(Option, ServerMessage)> for StreamService where - T: Service, + T: Service, T::Future: 'static, T::Error: 'static, { - type Request = (Option, ServerMessage); type Response = (); type Error = (); type Future = FutureResult<(), ()>; @@ -155,7 +154,7 @@ impl InternalServiceFactory for Box { impl ServiceFactory for F where F: Fn() -> T + Send + Clone + 'static, - T: NewService, + T: NewService, { type NewService = T; diff --git a/actix-service/Cargo.toml b/actix-service/Cargo.toml index 56095859..705a762a 100644 --- a/actix-service/Cargo.toml +++ b/actix-service/Cargo.toml @@ -25,3 +25,6 @@ path = "src/lib.rs" [dependencies] futures = "0.1.24" void = "1.0.2" + +[dev-dependencies] +actix-rt = "0.1" \ No newline at end of file diff --git a/actix-service/src/and_then.rs b/actix-service/src/and_then.rs index ee0f5e18..c52f4da1 100644 --- a/actix-service/src/and_then.rs +++ b/actix-service/src/and_then.rs @@ -1,5 +1,3 @@ -use std::marker::PhantomData; - use futures::{try_ready, Async, Future, Poll}; use super::{IntoNewService, NewService, Service}; @@ -16,10 +14,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) } } @@ -37,40 +35,39 @@ 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: A::Request) -> Self::Future { + fn call(&mut self, req: R) -> 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 { @@ -81,10 +78,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; @@ -107,46 +104,43 @@ 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, @@ -155,15 +149,14 @@ 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, @@ -171,10 +164,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 { @@ -186,10 +179,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; @@ -229,8 +222,7 @@ mod tests { use crate::{NewService, Service, ServiceExt}; struct Srv1(Rc>); - impl Service for Srv1 { - type Request = &'static str; + impl Service<&'static str> for Srv1 { type Response = &'static str; type Error = (); type Future = FutureResult; @@ -248,8 +240,7 @@ mod tests { #[derive(Clone)] struct Srv2(Rc>); - impl Service for Srv2 { - type Request = &'static str; + impl Service<&'static str> for Srv2 { 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 7ea0dab3..d24479bc 100644 --- a/actix-service/src/and_then_apply.rs +++ b/actix-service/src/and_then_apply.rs @@ -1,3 +1,4 @@ +use std::marker::PhantomData; use std::rc::Rc; use futures::{Async, Future, Poll}; @@ -7,22 +8,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: std::marker::PhantomData, + _t: PhantomData
, } -impl AndThenTransform -where - A: NewService, - B: NewService, - T: Transform, - T::Error: From, -{ +impl AndThenTransform { /// Create new `ApplyNewService` new service instance - pub fn new(t: T, a: A, b: B) -> Self { + pub fn new(t: T, a: A, b: B) -> Self + where + A: NewService, + B: NewService, + T: Transform, + T::Error: From, + { Self { a, b, @@ -32,7 +33,7 @@ where } } -impl Clone for AndThenTransform +impl Clone for AndThenTransform where A: Clone, B: Clone, @@ -47,20 +48,19 @@ 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,8 +138,7 @@ mod tests { #[derive(Clone)] struct Srv; - impl Service for Srv { - type Request = (); + impl Service<()> for Srv { 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 41b536dc..8eafb738 100644 --- a/actix-service/src/and_then_apply_fn.rs +++ b/actix-service/src/and_then_apply_fn.rs @@ -6,30 +6,23 @@ use super::{IntoNewService, IntoService, NewService, Service}; use crate::cell::Cell; /// `Apply` service combinator -pub struct AndThenApply -where - A: Service, - B: Service, - F: FnMut(A::Response, &mut B) -> Out, - Out: IntoFuture, - Out::Error: Into, -{ +pub struct AndThenApply { a: A, b: Cell, f: Cell, - r: PhantomData<(Out,)>, + r: PhantomData<(Out, AReq, BReq)>, } -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(), @@ -39,13 +32,9 @@ where } } -impl Clone for AndThenApply +impl Clone for AndThenApply where - A: Service + Clone, - B: Service, - F: FnMut(A::Response, &mut B) -> Out, - Out: IntoFuture, - Out::Error: Into, + A: Clone, { fn clone(&self) -> Self { AndThenApply { @@ -57,38 +46,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: A::Request) -> Self::Future { + fn call(&mut self, req: AReq) -> 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, @@ -97,12 +86,13 @@ 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, @@ -129,23 +119,23 @@ where } /// `ApplyNewService` new service combinator -pub struct AndThenApplyNewService { +pub struct AndThenApplyNewService { a: A, b: B, f: Cell, - r: PhantomData<(Out, Cfg)>, + r: PhantomData<(Out, AReq, BReq, 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, @@ -159,7 +149,8 @@ where } } -impl Clone for AndThenApplyNewService +impl Clone + for AndThenApplyNewService where A: Clone, B: Clone, @@ -174,21 +165,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 { @@ -201,10 +192,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, @@ -216,15 +207,16 @@ 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 { @@ -263,8 +255,7 @@ mod tests { #[derive(Clone)] struct Srv; - impl Service for Srv { - type Request = (); + impl Service<()> for Srv { type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index d5dda2d4..bf794039 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -5,24 +5,23 @@ use futures::{Async, Future, IntoFuture, Poll}; use super::{IntoNewService, IntoService, NewService, Service}; /// `Apply` service combinator -pub struct Apply -where - T: Service, -{ +pub struct Apply { service: T, f: F, - r: PhantomData<(In, Out)>, + r: PhantomData<(R, 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 { + pub fn new>(service: I, f: F) -> Self + where + T: Service, + Out: IntoFuture, + Out::Error: From, + { Self { service: service.into_service(), f, @@ -31,9 +30,9 @@ where } } -impl Clone for Apply +impl Clone for Apply where - T: Service + Clone, + T: Clone, F: Clone, { fn clone(&self) -> Self { @@ -45,14 +44,13 @@ 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; @@ -67,24 +65,21 @@ where } /// `ApplyNewService` new service combinator -pub struct ApplyNewService -where - T: NewService, -{ +pub struct ApplyNewService { service: T, f: F, - r: PhantomData<(In, Out, Cfg)>, + r: PhantomData<(In, Out, Req)>, } -impl ApplyNewService -where - T: NewService, - F: FnMut(In, &mut T::Service) -> Out + Clone, - Out: IntoFuture, - Out::Error: From, -{ +impl ApplyNewService { /// Create new `ApplyNewService` new service instance - pub fn new>(service: F1, f: F) -> Self { + pub fn new>(service: F1, f: F) -> Self + where + T: NewService, + F: FnMut(In, &mut T::Service) -> Out + Clone, + Out: IntoFuture, + Out::Error: From, + { Self { f, service: service.into_new_service(), @@ -93,11 +88,10 @@ where } } -impl Clone for ApplyNewService +impl Clone for ApplyNewService where - T: NewService + Clone, - F: FnMut(In, &mut T::Service) -> Out + Clone, - Out: IntoFuture, + T: Clone, + F: Clone, { fn clone(&self) -> Self { Self { @@ -108,29 +102,28 @@ 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, { @@ -139,9 +132,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, { @@ -154,14 +147,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 { @@ -183,8 +176,7 @@ mod tests { #[derive(Clone)] struct Srv; - impl Service for Srv { - type Request = (); + impl Service<()> for Srv { type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/blank.rs b/actix-service/src/blank.rs index ea10b424..76701202 100644 --- a/actix-service/src/blank.rs +++ b/actix-service/src/blank.rs @@ -30,8 +30,7 @@ impl Default for Blank { } } -impl Service for Blank { - type Request = R; +impl Service for Blank { type Response = R; type Error = E; type Future = FutureResult; @@ -68,8 +67,7 @@ impl Default for BlankNewService { } } -impl NewService<()> for BlankNewService { - type Request = R; +impl NewService for BlankNewService { type Response = R; type Error = E1; type Service = Blank; diff --git a/actix-service/src/boxed.rs b/actix-service/src/boxed.rs index 91c7bbb2..63bad979 100644 --- a/actix-service/src/boxed.rs +++ b/actix-service/src/boxed.rs @@ -1,48 +1,49 @@ +use std::marker::PhantomData; + use crate::{NewService, Service}; use futures::{Future, IntoFuture, Poll}; pub type BoxedService = Box< - Service< - Request = Req, - Response = Res, - Error = Err, - Future = Box>, - >, + Service>>, >; /// Create boxed new service -pub fn new_service( +pub fn new_service( service: T, -) -> BoxedNewService +) -> BoxedNewService where C: 'static, - T: NewService + 'static, - T::Request: 'static, + T: NewService + 'static, T::Response: 'static, T::Service: 'static, T::Future: 'static, T::Error: 'static, T::InitError: 'static, + R: 'static, { BoxedNewService(Box::new(NewServiceWrapper { service, - _t: std::marker::PhantomData, + _t: 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)) + Box::new(ServiceWrapper { + service, + _t: PhantomData, + }) } type Inner = Box< NewService< + Req, C, - Request = Req, Response = Res, Error = Err, InitError = InitErr, @@ -53,14 +54,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; @@ -72,23 +73,22 @@ where } } -struct NewServiceWrapper> { +struct NewServiceWrapper, R, C> { service: T, - _t: std::marker::PhantomData, + _t: std::marker::PhantomData<(R, C)>, } -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,33 +105,40 @@ where } } -struct ServiceWrapper(T); +struct ServiceWrapper, R> { + service: T, + _t: PhantomData, +} -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)) + fn boxed(service: T) -> BoxedService { + Box::new(ServiceWrapper { + service, + _t: PhantomData, + }) } } -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.0.poll_ready() + self.service.poll_ready() } - fn call(&mut self, req: Self::Request) -> Self::Future { - Box::new(self.0.call(req)) + fn call(&mut self, req: Req) -> Self::Future { + Box::new(self.service.call(req)) } } diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 5b1319f7..e32ad120 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -15,21 +15,21 @@ 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, - S: Service, + S: Service, { FnNewServiceConfig::new(f) } @@ -66,12 +66,11 @@ 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 +84,7 @@ where } } -impl IntoService> for F +impl IntoService, Req> for F where F: FnMut(Req) -> Out + 'static, Out: IntoFuture, @@ -114,12 +113,11 @@ 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; @@ -142,7 +140,7 @@ where } } -impl IntoNewService, Cfg> for F +impl IntoNewService, Req, Cfg> for F where F: Fn(Req) -> Out + Clone, Out: IntoFuture, @@ -153,33 +151,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 } + FnNewServiceNoConfig { f, _t: PhantomData } } } -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; @@ -192,57 +190,56 @@ 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, ()> for F +impl IntoNewService, Req, ()> 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, - S: Service, + S: Service, { f: F, - _t: PhantomData<(C, R, S, E)>, + _t: PhantomData<(C, R, S, E, Req)>, } -impl FnNewServiceConfig +impl FnNewServiceConfig where F: Fn(&C) -> R, R: IntoFuture, - S: Service, + 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, - S: Service, + S: Service, { - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S; @@ -255,24 +252,45 @@ where } } -impl Clone for FnNewServiceConfig +impl Clone for FnNewServiceConfig where F: Fn(&C) -> R + Clone, R: IntoFuture, - S: Service, + S: Service, { fn clone(&self) -> Self { Self::new(self.f.clone()) } } -impl IntoConfigurableNewService, C> for F +impl + IntoConfigurableNewService, Req, C> for F where F: Fn(&C) -> R, R: IntoFuture, - S: Service, + S: Service, { - fn into_new_service(self) -> FnNewServiceConfig { + fn into_new_service(self) -> FnNewServiceConfig { FnNewServiceConfig::new(self) } } + +#[cfg(test)] +mod tests { + use futures::Future; + + use super::*; + use crate::{IntoService, NewService, 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 7c3773ba..24c56d2a 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, In, Out, Err)>, + _t: PhantomData<(S, R, In, Out, Err)>, } -impl FnTransform +impl FnTransform where F: FnMut(In, &mut S) -> Out + Clone, Out: IntoFuture, @@ -24,17 +24,16 @@ 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; @@ -43,19 +42,19 @@ where } } -impl IntoTransform, S> for F +impl IntoTransform, In, 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 31ccb2d9..dade2a3b 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,21 +37,20 @@ 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: A::Request) -> Self::Future { + fn call(&mut self, req: R) -> Self::Future { FromErrFuture { fut: self.service.call(req), f: PhantomData, @@ -59,14 +58,14 @@ where } } -pub struct FromErrFuture { +pub struct FromErrFuture, R, E> { fut: A::Future, f: PhantomData, } -impl Future for FromErrFuture +impl Future for FromErrFuture where - A: Service, + A: Service, E: From, { type Item = A::Response; @@ -88,9 +87,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 } @@ -109,18 +108,17 @@ 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 { @@ -130,18 +128,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; @@ -164,8 +162,7 @@ mod tests { use crate::{IntoNewService, NewService, Service, ServiceExt}; struct Srv; - impl Service for Srv { - type Request = (); + impl Service<()> for Srv { type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index 8dfc6b22..ca6f5b95 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -36,10 +36,9 @@ pub use self::then::{Then, ThenNewService}; pub use self::transform::{ApplyTransform, IntoTransform, Transform}; /// An asynchronous function from `Request` to a `Response`. -pub trait Service { - /// Requests handled by the service. - type Request; - +/// +/// `Request` - requests handled by the service. +pub trait Service { /// Responses given by the service. type Response; @@ -69,22 +68,26 @@ pub trait Service { /// /// Calling `call` without calling `poll_ready` is permitted. The /// implementation must be resilient to this fact. - fn call(&mut self, req: Self::Request) -> Self::Future; + fn call(&mut self, req: 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) } @@ -101,8 +104,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()) } @@ -128,7 +131,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) } @@ -167,7 +170,7 @@ pub trait ServiceExt: Service { } } -impl ServiceExt for T where T: Service {} +impl ServiceExt for T where T: Service {} /// Creates new `Service` values. /// @@ -177,11 +180,9 @@ 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` is a service factory configuration type. -pub trait NewService { - /// Requests handled by the service. - type Request; - +/// * `Request` - requests handled by the service. +/// * `Config` - is a service factory configuration type. +pub trait NewService { /// Responses given by the service type Response; @@ -189,11 +190,7 @@ pub trait NewService { type Error; /// The `Service` value created by this factory - type Service: Service< - Request = Self::Request, - Response = Self::Response, - Error = Self::Error, - >; + type Service: Service; /// Errors produced while building a service. type InitError; @@ -206,33 +203,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, @@ -241,16 +238,11 @@ 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< - Config, - Request = Self::Response, - Error = Self::Error, - InitError = Self::InitError, - >, + F: IntoNewService, + B: NewService, { AndThenNewService::new(self, new_service) } @@ -278,15 +270,15 @@ pub trait NewService { fn then(self, new_service: F) -> ThenNewService where Self: Sized, - F: IntoNewService, + F: IntoNewService, Config>, B: NewService< + Result, Config, - Request = Result, Error = Self::Error, InitError = Self::InitError, >, { - ThenNewService::new(self, new_service) + ThenNewService::new(self, new_service.into_new_service()) } /// Map this service's output to a different type, returning a new service @@ -318,11 +310,10 @@ pub trait NewService { } } -impl<'a, S> Service for &'a mut S +impl<'a, S, R> 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; @@ -331,16 +322,15 @@ where (**self).poll_ready() } - fn call(&mut self, request: Self::Request) -> S::Future { + fn call(&mut self, request: R) -> 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; @@ -349,16 +339,15 @@ where (**self).poll_ready() } - fn call(&mut self, request: Self::Request) -> S::Future { + fn call(&mut self, request: R) -> 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; @@ -370,11 +359,10 @@ 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; @@ -387,35 +375,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 @@ -423,17 +411,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 a98d14f5..e7ad15c5 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,37 +42,36 @@ 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: A::Request) -> Self::Future { + fn call(&mut self, req: R) -> 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 { @@ -80,9 +79,9 @@ where } } -impl Future for MapFuture +impl Future for MapFuture where - A: Service, + A: Service, F: FnMut(A::Response) -> Response, { type Item = Response; @@ -105,9 +104,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 { @@ -132,36 +131,35 @@ 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 { @@ -169,9 +167,9 @@ where } } -impl Future for MapNewServiceFuture +impl Future for MapNewServiceFuture where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Res, { type Item = Map; @@ -194,8 +192,7 @@ mod tests { use crate::{IntoNewService, Service, ServiceExt}; struct Srv; - impl Service for Srv { - type Request = (); + impl Service<()> for Srv { type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index cd15d239..f3d8c63d 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,47 +43,39 @@ 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: A::Request) -> Self::Future { - MapErrFuture::new(self.service.call(req), self.f.clone()) + fn call(&mut self, req: R) -> Self::Future { + MapErrFuture { + fut: self.service.call(req), + f: 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 MapErrFuture +impl Future for MapErrFuture where - 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, + A: Service, F: Fn(A::Error) -> E, { type Item = A::Response; @@ -106,9 +98,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 { @@ -133,36 +125,35 @@ 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 { @@ -170,9 +161,9 @@ where } } -impl Future for MapErrNewServiceFuture +impl Future for MapErrNewServiceFuture where - A: NewService, + A: NewService, F: Fn(A::Error) -> E + Clone, { type Item = MapErr; @@ -196,8 +187,7 @@ mod tests { struct Srv; - impl Service for Srv { - type Request = (); + impl Service<()> for Srv { 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 094a4dbe..b3f4f072 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,46 +40,38 @@ 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::new(self.a.new_service(cfg), self.f.clone()) + MapInitErrFuture { + fut: self.a.new_service(cfg), + f: 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 MapInitErrFuture +impl Future for MapInitErrFuture where - 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, + 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 c0a31bfd..777201f0 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::{IntoNewService, NewService, Service}; +use super::{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,40 +37,39 @@ 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: A::Request) -> Self::Future { + fn call(&mut self, req: R) -> 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 { @@ -81,10 +80,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; @@ -119,42 +118,35 @@ pub struct ThenNewService { impl ThenNewService { /// Create new `AndThen` combinator - pub fn new(a: A, f: F) -> Self + pub fn new(a: A, f: B) -> Self where - A: NewService, + A: NewService, B: NewService< + Result, C, - Request = Result, Error = A::Error, InitError = A::InitError, >, - F: IntoNewService, { Self { a, - b: f.into_new_service(), + b: f, _t: PhantomData, } } } -impl NewService for ThenNewService +impl NewService for ThenNewService where - A: NewService, - B: NewService< - C, - Request = Result, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, C, 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)) @@ -175,15 +167,10 @@ where } } -pub struct ThenNewServiceFuture +pub struct ThenNewServiceFuture where - A: NewService, - B: NewService< - C, - Request = Result, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, C, Error = A::Error, InitError = A::InitError>, { fut_b: B::Future, fut_a: A::Future, @@ -191,15 +178,10 @@ where b: Option, } -impl ThenNewServiceFuture +impl ThenNewServiceFuture where - A: NewService, - B: NewService< - C, - Request = Result, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, C, Error = A::Error, InitError = A::InitError>, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { ThenNewServiceFuture { @@ -211,15 +193,10 @@ where } } -impl Future for ThenNewServiceFuture +impl Future for ThenNewServiceFuture where - A: NewService, - B: NewService< - C, - Request = Result, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, C, Error = A::Error, InitError = A::InitError>, { type Item = Then; type Error = A::InitError; @@ -259,8 +236,7 @@ mod tests { #[derive(Clone)] struct Srv1(Rc>); - impl Service for Srv1 { - type Request = Result<&'static str, &'static str>; + impl Service> for Srv1 { type Response = &'static str; type Error = (); type Future = FutureResult; @@ -280,8 +256,7 @@ mod tests { struct Srv2(Rc>); - impl Service for Srv2 { - type Request = Result<&'static str, ()>; + impl Service> for Srv2 { 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 3e8a693e..98944183 100644 --- a/actix-service/src/transform.rs +++ b/actix-service/src/transform.rs @@ -9,11 +9,10 @@ use crate::{NewService, Service}; /// `Transform` service factory. /// /// Transform factory creates service that wraps other services. -/// `Config` is a service factory configuration type. -pub trait Transform { - /// Requests handled by the service. - type Request; - +/// +/// * `S` is a wrapped service. +/// * `R` requests handled by this transform service. +pub trait Transform { /// Responses given by the service. type Response; @@ -21,11 +20,7 @@ pub trait Transform { type Error; /// The `TransformService` value created by this factory - type Transform: Service< - Request = Self::Request, - Response = Self::Response, - Error = Self::Error, - >; + type Transform: Service; /// Errors produced while building a service. type InitError; @@ -47,11 +42,10 @@ 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; @@ -63,11 +57,10 @@ 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; @@ -80,17 +73,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 @@ -99,19 +92,19 @@ where /// `Apply` transform new service #[derive(Clone)] -pub struct ApplyTransform { - a: A, +pub struct ApplyTransform { + a: S, t: Rc, - _t: std::marker::PhantomData, + _t: std::marker::PhantomData<(R, Req, Cfg)>, } -impl ApplyTransform +impl ApplyTransform where - A: NewService, - T: Transform, + S: NewService, + T: Transform, { /// Create new `ApplyNewService` new service instance - pub fn new>(t: F, a: A) -> Self { + pub fn new>(t: F, a: S) -> Self { Self { a, t: Rc::new(t.into_transform()), @@ -120,20 +113,19 @@ where } } -impl NewService for ApplyTransform +impl NewService for ApplyTransform where - A: NewService, - T: Transform, + S: 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: &C) -> Self::Future { + fn new_service(&self, cfg: &Cfg) -> Self::Future { ApplyTransformFuture { t_cell: self.t.clone(), fut_a: self.a.new_service(cfg).into_future(), @@ -142,20 +134,20 @@ where } } -pub struct ApplyTransformFuture +pub struct ApplyTransformFuture where - A: NewService, - T: Transform, + S: NewService, + T: Transform, { - fut_a: A::Future, - fut_t: Option<::Future>, + fut_a: S::Future, + fut_t: Option, t_cell: Rc, } -impl Future for ApplyTransformFuture +impl Future for ApplyTransformFuture where - A: NewService, - T: Transform, + S: 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 0b0dd9b9..3655b6b3 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,36 +43,35 @@ 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 { @@ -80,9 +79,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; diff --git a/actix-test-server/Cargo.toml b/actix-test-server/Cargo.toml index 05801cba..2f721b48 100644 --- a/actix-test-server/Cargo.toml +++ b/actix-test-server/Cargo.toml @@ -34,7 +34,8 @@ rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"] [dependencies] actix-rt = "0.1.0" -actix-server = "0.3.0" +#actix-server = "0.3.0" +actix-server = { path="../actix-server" } log = "0.4" net2 = "0.2" diff --git a/actix-utils/CHANGES.md b/actix-utils/CHANGES.md index 64c361a1..dbda2050 100644 --- a/actix-utils/CHANGES.md +++ b/actix-utils/CHANGES.md @@ -1,5 +1,10 @@ # Changes +## [0.4.0] - 2019-03-xx + +* Upgrade actix-service + + ## [0.3.2] - 2019-03-04 ### Changed diff --git a/actix-utils/src/cloneable.rs b/actix-utils/src/cloneable.rs index 933e6a1f..16a8a2e3 100644 --- a/actix-utils/src/cloneable.rs +++ b/actix-utils/src/cloneable.rs @@ -13,9 +13,9 @@ pub struct CloneableService { } impl CloneableService { - pub fn new(service: T) -> Self + pub fn new(service: T) -> Self where - T: Service, + T: Service, { Self { service: Cell::new(service), @@ -33,11 +33,10 @@ impl Clone for CloneableService { } } -impl Service for CloneableService +impl Service for CloneableService where - T: Service + 'static, + T: Service + 'static, { - type Request = T::Request; type Response = T::Response; type Error = T::Error; type Future = T::Future; @@ -46,7 +45,7 @@ where self.service.get_mut().poll_ready() } - fn call(&mut self, req: T::Request) -> Self::Future { + fn call(&mut self, req: R) -> Self::Future { self.service.get_mut().call(req) } } diff --git a/actix-utils/src/either.rs b/actix-utils/src/either.rs index 885cd095..29bb5c7f 100644 --- a/actix-utils/src/either.rs +++ b/actix-utils/src/either.rs @@ -21,12 +21,11 @@ impl Clone for EitherService { } } -impl Service for EitherService +impl Service for EitherService where - A: Service, - B: Service, + A: Service, + B: Service, { - type Request = A::Request; type Response = A::Response; type Error = A::Error; type Future = future::Either; @@ -38,7 +37,7 @@ where } } - fn call(&mut self, req: A::Request) -> Self::Future { + fn call(&mut self, req: R) -> Self::Future { match self { EitherService::A(ref mut inner) => future::Either::A(inner.call(req)), EitherService::B(ref mut inner) => future::Either::B(inner.call(req)), @@ -53,52 +52,33 @@ pub enum Either { } impl Either { - pub fn new_a(srv: A) -> Self + pub fn new_a(srv: A) -> Self where - A: NewService, - B: NewService< - C, - Request = A::Request, - Response = A::Response, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, { Either::A(srv) } - pub fn new_b(srv: B) -> Self + pub fn new_b(srv: B) -> Self where - A: NewService, - B: NewService< - C, - Request = A::Request, - Response = A::Response, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, { Either::B(srv) } } -impl NewService for Either +impl NewService for Either where - A: NewService, - B: NewService< - C, - Request = A::Request, - Response = A::Response, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, { - type Request = A::Request; type Response = A::Response; type Error = A::Error; type InitError = A::InitError; type Service = EitherService; - type Future = EitherNewService; + type Future = EitherNewService; fn new_service(&self, cfg: &C) -> Self::Future { match self { @@ -118,21 +98,15 @@ impl Clone for Either { } #[doc(hidden)] -pub enum EitherNewService, B: NewService, C> { +pub enum EitherNewService, B: NewService, R, C> { A(::Future), B(::Future), } -impl Future for EitherNewService +impl Future for EitherNewService where - A: NewService, - B: NewService< - C, - Request = A::Request, - Response = A::Response, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, { type Item = EitherService; type Error = A::InitError; diff --git a/actix-utils/src/framed.rs b/actix-utils/src/framed.rs index d539a1e6..7b086248 100644 --- a/actix-utils/src/framed.rs +++ b/actix-utils/src/framed.rs @@ -23,15 +23,15 @@ pub struct FramedNewService { impl FramedNewService where C: Clone, - S: NewService, Response = Response>, + S: NewService, C, Response = Response>, S::Error: 'static, - ::Future: 'static, + >>::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, ::Error: std::fmt::Debug, { - pub fn new>(factory: F1) -> Self { + pub fn new, C>>(factory: F1) -> Self { Self { factory: factory.into_new_service(), _t: PhantomData, @@ -51,18 +51,17 @@ where } } -impl NewService for FramedNewService +impl NewService, C> for FramedNewService where C: Clone, - S: NewService, Response = Response> + Clone, + S: NewService, C, Response = Response> + Clone, S::Error: 'static, - ::Future: 'static, + >>::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, ::Error: std::fmt::Debug, { - type Request = Framed; type Response = FramedTransport; type Error = S::InitError; type InitError = S::InitError; @@ -98,18 +97,17 @@ where } } -impl Service for FramedService +impl Service> for FramedService where - S: NewService, Response = Response>, + S: NewService, C, Response = Response>, S::Error: 'static, - ::Future: 'static, + >>::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, ::Error: std::fmt::Debug, C: Clone, { - type Request = Framed; type Response = FramedTransport; type Error = S::InitError; type Future = FramedServiceResponseFuture; @@ -129,9 +127,9 @@ where #[doc(hidden)] pub struct FramedServiceResponseFuture where - S: NewService, Response = Response>, + S: NewService, C, Response = Response>, S::Error: 'static, - ::Future: 'static, + >>::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, @@ -143,9 +141,9 @@ where impl Future for FramedServiceResponseFuture where - S: NewService, Response = Response>, + S: NewService, C, Response = Response>, S::Error: 'static, - ::Future: 'static, + >>::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, @@ -182,7 +180,7 @@ impl From for FramedTransportError { /// and pass then to the service. pub struct FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -196,7 +194,7 @@ where inner: Cell::Item, S::Error>>, } -enum TransportState { +enum TransportState>, U: Encoder + Decoder> { Processing, Error(FramedTransportError), FramedError(FramedTransportError), @@ -210,7 +208,7 @@ struct FramedTransportInner { impl FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -302,7 +300,7 @@ where impl FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -310,7 +308,7 @@ where ::Item: 'static, ::Error: std::fmt::Debug, { - pub fn new>(framed: Framed, service: F) -> Self { + pub fn new>>(framed: Framed, service: F) -> Self { FramedTransport { framed, service: service.into_service(), @@ -348,7 +346,7 @@ where impl Future for FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -408,13 +406,12 @@ where } } -impl NewService<()> for IntoFramed +impl NewService for IntoFramed where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, U: Encoder + Decoder, { - type Request = T; type Response = Framed; type Error = (); type InitError = (); @@ -439,13 +436,12 @@ where _t: PhantomData<(T,)>, } -impl Service for IntoFramedService +impl Service for IntoFramedService where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, U: Encoder + Decoder, { - type Request = T; type Response = Framed; type Error = (); type Future = FutureResult; diff --git a/actix-utils/src/inflight.rs b/actix-utils/src/inflight.rs index 41955f35..82f425b3 100644 --- a/actix-utils/src/inflight.rs +++ b/actix-utils/src/inflight.rs @@ -24,8 +24,7 @@ impl Default for InFlight { } } -impl Transform for InFlight { - type Request = S::Request; +impl, R> Transform for InFlight { type Response = S::Response; type Error = S::Error; type InitError = Void; @@ -51,14 +50,13 @@ impl InFlightService { } } -impl Service for InFlightService +impl Service for InFlightService where - T: Service, + T: Service, { - type Request = T::Request; type Response = T::Response; type Error = T::Error; - type Future = InFlightServiceResponse; + type Future = InFlightServiceResponse; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready()?; @@ -71,7 +69,7 @@ where } } - fn call(&mut self, req: T::Request) -> Self::Future { + fn call(&mut self, req: R) -> Self::Future { InFlightServiceResponse { fut: self.service.call(req), _guard: self.count.get(), @@ -80,12 +78,12 @@ where } #[doc(hidden)] -pub struct InFlightServiceResponse { +pub struct InFlightServiceResponse, R> { fut: T::Future, _guard: CounterGuard, } -impl Future for InFlightServiceResponse { +impl, R> Future for InFlightServiceResponse { type Item = T::Response; type Error = T::Error; @@ -107,8 +105,7 @@ mod tests { struct SleepService(Duration); - impl Service for SleepService { - type Request = (); + impl Service<()> for SleepService { type Response = (); type Error = (); type Future = Box>; diff --git a/actix-utils/src/keepalive.rs b/actix-utils/src/keepalive.rs index 81305423..89929db7 100644 --- a/actix-utils/src/keepalive.rs +++ b/actix-utils/src/keepalive.rs @@ -43,11 +43,10 @@ where } } -impl NewService<()> for KeepAlive +impl NewService for KeepAlive where F: Fn() -> E + Clone, { - type Request = R; type Response = R; type Error = E; type InitError = Void; @@ -89,11 +88,10 @@ where } } -impl Service for KeepAliveService +impl Service for KeepAliveService where F: Fn() -> E, { - type Request = R; type Response = R; type Error = E; type Future = FutureResult; diff --git a/actix-utils/src/order.rs b/actix-utils/src/order.rs index b267a0b4..288b4e4a 100644 --- a/actix-utils/src/order.rs +++ b/actix-utils/src/order.rs @@ -52,46 +52,39 @@ pub struct InOrder { _t: PhantomData, } -impl InOrder -where - S: Service, - S::Response: 'static, - S::Future: 'static, - S::Error: 'static, -{ - pub fn new() -> Self { +impl InOrder { + pub fn new() -> Self + where + S: Service, + S::Response: 'static, + S::Future: 'static, + S::Error: 'static, + { Self { _t: PhantomData } } - pub fn service(service: S) -> InOrderService { + pub fn service(service: S) -> InOrderService + where + S: Service, + S::Response: 'static, + S::Future: 'static, + S::Error: 'static, + { InOrderService::new(service) } } -impl Default for InOrder +impl Transform for InOrder where - S: Service, + S: Service, S::Response: 'static, S::Future: 'static, S::Error: 'static, { - fn default() -> Self { - Self::new() - } -} - -impl Transform for InOrder -where - S: Service, - S::Response: 'static, - S::Future: 'static, - S::Error: 'static, -{ - type Request = S::Request; type Response = S::Response; type Error = InOrderError; type InitError = Void; - type Transform = InOrderService; + type Transform = InOrderService; type Future = FutureResult; fn new_transform(&self, service: S) -> Self::Future { @@ -99,15 +92,15 @@ where } } -pub struct InOrderService { +pub struct InOrderService, R> { service: S, task: Rc, acks: VecDeque>, } -impl InOrderService +impl InOrderService where - S: Service, + S: Service, S::Response: 'static, S::Future: 'static, S::Error: 'static, @@ -121,17 +114,16 @@ where } } -impl Service for InOrderService +impl Service for InOrderService where - S: Service, + S: Service, S::Response: 'static, S::Future: 'static, S::Error: 'static, { - type Request = S::Request; type Response = S::Response; type Error = InOrderError; - type Future = InOrderServiceResponse; + type Future = InOrderServiceResponse; fn poll_ready(&mut self) -> Poll<(), Self::Error> { // poll_ready could be called from different task @@ -156,7 +148,7 @@ where Ok(Async::Ready(())) } - fn call(&mut self, request: S::Request) -> Self::Future { + fn call(&mut self, request: R) -> Self::Future { let (tx1, rx1) = oneshot::channel(); let (tx2, rx2) = oneshot::channel(); self.acks.push_back(Record { rx: rx1, tx: tx2 }); @@ -173,11 +165,11 @@ where } #[doc(hidden)] -pub struct InOrderServiceResponse { +pub struct InOrderServiceResponse, R> { rx: oneshot::Receiver>, } -impl Future for InOrderServiceResponse { +impl, R> Future for InOrderServiceResponse { type Item = S::Response; type Error = InOrderError; @@ -204,8 +196,7 @@ mod tests { struct Srv; - impl Service for Srv { - type Request = oneshot::Receiver; + impl Service> for Srv { type Response = usize; type Error = (); type Future = Box>; @@ -219,11 +210,11 @@ mod tests { } } - struct SrvPoll { + struct SrvPoll>> { s: S, } - impl Future for SrvPoll { + impl>> Future for SrvPoll { type Item = (); type Error = (); diff --git a/actix-utils/src/stream.rs b/actix-utils/src/stream.rs index e98659c9..b82a66e8 100644 --- a/actix-utils/src/stream.rs +++ b/actix-utils/src/stream.rs @@ -38,12 +38,12 @@ impl StreamNewService where C: Clone, S: IntoStream, - T: NewService, Response = (), Error = E, InitError = E>, + T: NewService, C, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - ::Future: 'static, + >>::Future: 'static, { - pub fn new>(factory: F) -> Self { + pub fn new, C>>(factory: F) -> Self { Self { factory: Rc::new(factory.into_new_service()), _t: PhantomData, @@ -60,16 +60,15 @@ impl Clone for StreamNewService { } } -impl NewService for StreamNewService +impl NewService for StreamNewService where C: Clone, S: IntoStream + 'static, - T: NewService, Response = (), Error = E, InitError = E>, + T: NewService, C, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - ::Future: 'static, + >>::Future: 'static, { - type Request = S; type Response = (); type Error = E; type InitError = E; @@ -91,16 +90,15 @@ pub struct StreamService { _t: PhantomData<(S, E)>, } -impl Service for StreamService +impl Service for StreamService where S: IntoStream + 'static, - T: NewService, Response = (), Error = E, InitError = E>, + T: NewService, C, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - ::Future: 'static, + >>::Future: 'static, C: Clone, { - type Request = S; type Response = (); type Error = E; type Future = Box>; @@ -121,7 +119,7 @@ where pub struct StreamDispatcher where S: IntoStream + 'static, - T: Service, Response = ()> + 'static, + T: Service, Response = ()> + 'static, T::Future: 'static, { stream: S, @@ -133,13 +131,13 @@ where impl StreamDispatcher where S: Stream, - T: Service, Response = ()>, + T: Service, Response = ()>, T::Future: 'static, { pub fn new(stream: F1, service: F2) -> Self where F1: IntoStream, - F2: IntoService, + F2: IntoService>, { let (err_tx, err_rx) = mpsc::unbounded(); StreamDispatcher { @@ -154,7 +152,7 @@ where impl Future for StreamDispatcher where S: Stream, - T: Service, Response = ()>, + T: Service, Response = ()>, T::Future: 'static, { type Item = (); @@ -232,8 +230,7 @@ impl Clone for TakeItem { } } -impl NewService<()> for TakeItem { - type Request = T; +impl NewService for TakeItem { type Response = (Option, T); type Error = T::Error; type InitError = (); @@ -256,8 +253,7 @@ impl Clone for TakeItemService { } } -impl Service for TakeItemService { - type Request = T; +impl Service for TakeItemService { type Response = (Option, T); type Error = T::Error; type Future = TakeItemServiceResponse; diff --git a/actix-utils/src/time.rs b/actix-utils/src/time.rs index c30052d0..57e09846 100644 --- a/actix-utils/src/time.rs +++ b/actix-utils/src/time.rs @@ -42,7 +42,6 @@ impl Default for LowResTime { } impl NewService<()> for LowResTime { - type Request = (); type Response = Instant; type Error = Void; type InitError = Void; @@ -88,8 +87,7 @@ impl LowResTimeService { } } -impl Service for LowResTimeService { - type Request = (); +impl Service<()> for LowResTimeService { type Response = Instant; type Error = Void; type Future = FutureResult; diff --git a/actix-utils/src/timeout.rs b/actix-utils/src/timeout.rs index 85d69731..4493a112 100644 --- a/actix-utils/src/timeout.rs +++ b/actix-utils/src/timeout.rs @@ -80,11 +80,10 @@ impl Clone for Timeout { } } -impl Transform for Timeout +impl Transform for Timeout where - S: Service, + S: Service, { - type Request = S::Request; type Response = S::Response; type Error = TimeoutError; type InitError = E; @@ -112,20 +111,19 @@ impl TimeoutService { } } -impl Service for TimeoutService +impl Service for TimeoutService where - S: Service, + S: Service, { - type Request = S::Request; type Response = S::Response; type Error = TimeoutError; - type Future = TimeoutServiceResponse; + type Future = TimeoutServiceResponse; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready().map_err(TimeoutError::Service) } - fn call(&mut self, request: S::Request) -> Self::Future { + fn call(&mut self, request: R) -> Self::Future { TimeoutServiceResponse { fut: self.service.call(request), sleep: Delay::new(clock::now() + self.timeout), @@ -135,14 +133,14 @@ where /// `TimeoutService` response future #[derive(Debug)] -pub struct TimeoutServiceResponse { +pub struct TimeoutServiceResponse, R> { fut: T::Future, sleep: Delay, } -impl Future for TimeoutServiceResponse +impl Future for TimeoutServiceResponse where - T: Service, + T: Service, { type Item = T::Response; type Error = TimeoutError; @@ -177,8 +175,7 @@ mod tests { struct SleepService(Duration); - impl Service for SleepService { - type Request = (); + impl Service<()> for SleepService { type Response = (); type Error = (); type Future = Box>;