From dfbb77f98d9099c6237d4965831163f28de0c2b7 Mon Sep 17 00:00:00 2001 From: Nikolay Kim <fafhrd91@gmail.com> 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<Connect>, - ) -> impl Service<Request = Connect, Response = (Connect, TcpStream), Error = ConnectorError> - + Clone { + ) -> impl Service<Connect, Response = (Connect, TcpStream), Error = ConnectorError> + 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<Connect> for Connector { type Response = (Connect, TcpStream); type Error = ConnectorError; type Future = Either<ConnectorFuture, ConnectorTcpFuture>; @@ -273,8 +272,7 @@ impl<T: RequestPort> Default for TcpConnector<T> { } } -impl<T: RequestPort> Service for TcpConnector<T> { - type Request = (T, VecDeque<IpAddr>); +impl<T: RequestPort> Service<(T, VecDeque<IpAddr>)> for TcpConnector<T> { type Response = (T, TcpStream); type Error = io::Error; type Future = TcpConnectorResponse<T>; @@ -354,8 +352,7 @@ impl DefaultConnector { } } -impl Service for DefaultConnector { - type Request = Connect; +impl Service<Connect> 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<T> Clone for Resolver<T> { } } -impl<T: RequestHost> Service for Resolver<T> { - type Request = T; +impl<T: RequestHost> Service<T> for Resolver<T> { type Response = (T, VecDeque<IpAddr>); type Error = ResolveError; type Future = ResolverFuture<T>; 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<T, F>(&mut self, name: &str, service: F) where - F: IntoNewService<T>, - T: NewService<Request = TcpStream, Response = ()> + 'static, + F: IntoNewService<T, TcpStream>, + T: NewService<TcpStream, Response = ()> + 'static, T::Future: 'static, T::Service: 'static, T::InitError: fmt::Debug, @@ -191,7 +191,7 @@ impl ServiceRuntime { type BoxedNewService = Box< NewService< - Request = (Option<CounterGuard>, ServerMessage), + (Option<CounterGuard>, ServerMessage), Response = (), Error = (), InitError = (), @@ -204,15 +204,14 @@ struct ServiceFactory<T> { inner: T, } -impl<T> NewService for ServiceFactory<T> +impl<T> NewService<(Option<CounterGuard>, ServerMessage)> for ServiceFactory<T> where - T: NewService<Request = TcpStream, Response = ()>, + T: NewService<TcpStream, Response = ()>, T::Future: 'static, T::Service: 'static, T::Error: 'static, T::InitError: fmt::Debug + 'static, { - type Request = (Option<CounterGuard>, ServerMessage); type Response = (); type Error = (); type InitError = (); @@ -220,14 +219,9 @@ where type Future = Box<Future<Item = BoxedServerService, Error = ()>>; 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<Request = TcpStream>; + type NewService: NewService<TcpStream>; fn create(&self) -> Self::NewService; } @@ -38,7 +38,7 @@ pub(crate) trait InternalServiceFactory: Send { pub(crate) type BoxedServerService = Box< Service< - Request = (Option<CounterGuard>, ServerMessage), + (Option<CounterGuard>, ServerMessage), Response = (), Error = (), Future = FutureResult<(), ()>, @@ -55,13 +55,12 @@ impl<T> StreamService<T> { } } -impl<T> Service for StreamService<T> +impl<T> Service<(Option<CounterGuard>, ServerMessage)> for StreamService<T> where - T: Service<Request = TcpStream>, + T: Service<TcpStream>, T::Future: 'static, T::Error: 'static, { - type Request = (Option<CounterGuard>, ServerMessage); type Response = (); type Error = (); type Future = FutureResult<(), ()>; @@ -155,7 +154,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> { impl<F, T> ServiceFactory for F where F: Fn() -> T + Send + Clone + 'static, - T: NewService<Request = TcpStream>, + T: NewService<TcpStream>, { 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<A, B> { impl<A, B> AndThen<A, B> { /// Create new `AndThen` combinator - pub fn new(a: A, b: B) -> Self + pub fn new<R>(a: A, b: B) -> Self where - A: Service, - B: Service<Request = A::Response, Error = A::Error>, + A: Service<R>, + B: Service<A::Response, Error = A::Error>, { Self { a, b: Cell::new(b) } } @@ -37,40 +35,39 @@ where } } -impl<A, B> Service for AndThen<A, B> +impl<A, B, R> Service<R> for AndThen<A, B> where - A: Service, - B: Service<Request = A::Response, Error = A::Error>, + A: Service<R>, + B: Service<A::Response, Error = A::Error>, { - type Request = A::Request; type Response = B::Response; type Error = A::Error; - type Future = AndThenFuture<A, B>; + type Future = AndThenFuture<A, B, R>; 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<A, B> +pub struct AndThenFuture<A, B, R> where - A: Service, - B: Service<Request = A::Response, Error = A::Error>, + A: Service<R>, + B: Service<A::Response, Error = A::Error>, { b: Cell<B>, fut_b: Option<B::Future>, fut_a: Option<A::Future>, } -impl<A, B> AndThenFuture<A, B> +impl<A, B, R> AndThenFuture<A, B, R> where - A: Service, - B: Service<Request = A::Response, Error = A::Error>, + A: Service<R>, + B: Service<A::Response, Error = A::Error>, { fn new(a: A::Future, b: Cell<B>) -> Self { AndThenFuture { @@ -81,10 +78,10 @@ where } } -impl<A, B> Future for AndThenFuture<A, B> +impl<A, B, R> Future for AndThenFuture<A, B, R> where - A: Service, - B: Service<Request = A::Response, Error = A::Error>, + A: Service<R>, + B: Service<A::Response, Error = A::Error>, { type Item = B::Response; type Error = A::Error; @@ -107,46 +104,43 @@ where } /// `AndThenNewService` new service combinator -pub struct AndThenNewService<A, B, C> { +pub struct AndThenNewService<A, B> { a: A, b: B, - _t: PhantomData<C>, } -impl<A, B, C> AndThenNewService<A, B, C> { +impl<A, B> AndThenNewService<A, B> { /// Create new `AndThen` combinator - pub fn new<F: IntoNewService<B, C>>(a: A, f: F) -> Self + pub fn new<R, C, F: IntoNewService<B, A::Response, C>>(a: A, f: F) -> Self where - A: NewService<C>, - B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>, + A: NewService<R, C>, + B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>, { Self { a, b: f.into_new_service(), - _t: PhantomData, } } } -impl<A, B, C> NewService<C> for AndThenNewService<A, B, C> +impl<A, B, R, C> NewService<R, C> for AndThenNewService<A, B> where - A: NewService<C>, - B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>, + A: NewService<R, C>, + B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>, { - type Request = A::Request; type Response = B::Response; type Error = A::Error; type Service = AndThen<A::Service, B::Service>; type InitError = A::InitError; - type Future = AndThenNewServiceFuture<A, B, C>; + type Future = AndThenNewServiceFuture<A, B, R, C>; fn new_service(&self, cfg: &C) -> Self::Future { AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) } } -impl<A, B, C> Clone for AndThenNewService<A, B, C> +impl<A, B> Clone for AndThenNewService<A, B> where A: Clone, B: Clone, @@ -155,15 +149,14 @@ where Self { a: self.a.clone(), b: self.b.clone(), - _t: PhantomData, } } } -pub struct AndThenNewServiceFuture<A, B, C> +pub struct AndThenNewServiceFuture<A, B, R, C> where - A: NewService<C>, - B: NewService<C, Request = A::Response>, + A: NewService<R, C>, + B: NewService<A::Response, C>, { fut_b: B::Future, fut_a: A::Future, @@ -171,10 +164,10 @@ where b: Option<B::Service>, } -impl<A, B, C> AndThenNewServiceFuture<A, B, C> +impl<A, B, R, C> AndThenNewServiceFuture<A, B, R, C> where - A: NewService<C>, - B: NewService<C, Request = A::Response>, + A: NewService<R, C>, + B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { AndThenNewServiceFuture { @@ -186,10 +179,10 @@ where } } -impl<A, B, C> Future for AndThenNewServiceFuture<A, B, C> +impl<A, B, R, C> Future for AndThenNewServiceFuture<A, B, R, C> where - A: NewService<C>, - B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>, + A: NewService<R, C>, + B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>, { type Item = AndThen<A::Service, B::Service>; type Error = A::InitError; @@ -229,8 +222,7 @@ mod tests { use crate::{NewService, Service, ServiceExt}; struct Srv1(Rc<Cell<usize>>); - impl Service for Srv1 { - type Request = &'static str; + impl Service<&'static str> for Srv1 { type Response = &'static str; type Error = (); type Future = FutureResult<Self::Response, ()>; @@ -248,8 +240,7 @@ mod tests { #[derive(Clone)] struct Srv2(Rc<Cell<usize>>); - 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<Self::Response, ()>; 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<T, A, B, C> { +pub struct AndThenTransform<T, A, B, BR> { a: A, b: B, t: Rc<T>, - _t: std::marker::PhantomData<C>, + _t: PhantomData<BR>, } -impl<T, A, B, C> AndThenTransform<T, A, B, C> -where - A: NewService<C>, - B: NewService<C, InitError = A::InitError>, - T: Transform<B::Service, Request = A::Response, InitError = A::InitError>, - T::Error: From<A::Error>, -{ +impl<T, A, B, BR> AndThenTransform<T, A, B, BR> { /// Create new `ApplyNewService` new service instance - pub fn new(t: T, a: A, b: B) -> Self { + pub fn new<AR, C>(t: T, a: A, b: B) -> Self + where + A: NewService<AR, C>, + B: NewService<BR, C, InitError = A::InitError>, + T: Transform<A::Response, B::Service, InitError = A::InitError>, + T::Error: From<A::Error>, + { Self { a, b, @@ -32,7 +33,7 @@ where } } -impl<T, A, B, C> Clone for AndThenTransform<T, A, B, C> +impl<T, A, B, BR> Clone for AndThenTransform<T, A, B, BR> where A: Clone, B: Clone, @@ -47,20 +48,19 @@ where } } -impl<T, A, B, C> NewService<C> for AndThenTransform<T, A, B, C> +impl<T, A, B, AR, BR, C> NewService<AR, C> for AndThenTransform<T, A, B, BR> where - A: NewService<C>, - B: NewService<C, InitError = A::InitError>, - T: Transform<B::Service, Request = A::Response, InitError = A::InitError>, + A: NewService<AR, C>, + B: NewService<BR, C, InitError = A::InitError>, + T: Transform<A::Response, B::Service, InitError = A::InitError>, T::Error: From<A::Error>, { - type Request = A::Request; type Response = T::Response; type Error = T::Error; type InitError = T::InitError; type Service = AndThen<FromErr<A::Service, T::Error>, T::Transform>; - type Future = AndThenTransformFuture<T, A, B, C>; + type Future = AndThenTransformFuture<T, A, B, AR, BR, C>; fn new_service(&self, cfg: &C) -> Self::Future { AndThenTransformFuture { @@ -74,11 +74,11 @@ where } } -pub struct AndThenTransformFuture<T, A, B, C> +pub struct AndThenTransformFuture<T, A, B, AR, BR, C> where - A: NewService<C>, - B: NewService<C, InitError = A::InitError>, - T: Transform<B::Service, Request = A::Response, InitError = A::InitError>, + A: NewService<AR, C>, + B: NewService<BR, C, InitError = A::InitError>, + T: Transform<A::Response, B::Service, InitError = A::InitError>, T::Error: From<A::Error>, { fut_a: A::Future, @@ -89,11 +89,11 @@ where t_cell: Rc<T>, } -impl<T, A, B, C> Future for AndThenTransformFuture<T, A, B, C> +impl<T, A, B, AR, BR, C> Future for AndThenTransformFuture<T, A, B, AR, BR, C> where - A: NewService<C>, - B: NewService<C, InitError = A::InitError>, - T: Transform<B::Service, Request = A::Response, InitError = A::InitError>, + A: NewService<AR, C>, + B: NewService<BR, C, InitError = A::InitError>, + T: Transform<A::Response, B::Service, InitError = A::InitError>, T::Error: From<A::Error>, { type Item = AndThen<FromErr<A::Service, T::Error>, 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<A, B, F, Out> -where - A: Service, - B: Service<Error = A::Error>, - F: FnMut(A::Response, &mut B) -> Out, - Out: IntoFuture, - Out::Error: Into<A::Error>, -{ +pub struct AndThenApply<A, B, F, Out, AReq, BReq> { a: A, b: Cell<B>, f: Cell<F>, - r: PhantomData<(Out,)>, + r: PhantomData<(Out, AReq, BReq)>, } -impl<A, B, F, Out> AndThenApply<A, B, F, Out> +impl<A, B, F, Out, AReq, BReq> AndThenApply<A, B, F, Out, AReq, BReq> where - A: Service, - B: Service<Error = A::Error>, + A: Service<AReq>, + B: Service<BReq, Error = A::Error>, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into<A::Error>, { /// Create new `Apply` combinator - pub fn new<A1: IntoService<A>, B1: IntoService<B>>(a: A1, b: B1, f: F) -> Self { + pub fn new<A1: IntoService<A, AReq>, B1: IntoService<B, BReq>>(a: A1, b: B1, f: F) -> Self { Self { f: Cell::new(f), a: a.into_service(), @@ -39,13 +32,9 @@ where } } -impl<A, B, F, Out> Clone for AndThenApply<A, B, F, Out> +impl<A, B, F, Out, AReq, BReq> Clone for AndThenApply<A, B, F, Out, AReq, BReq> where - A: Service + Clone, - B: Service<Error = A::Error>, - F: FnMut(A::Response, &mut B) -> Out, - Out: IntoFuture, - Out::Error: Into<A::Error>, + A: Clone, { fn clone(&self) -> Self { AndThenApply { @@ -57,38 +46,38 @@ where } } -impl<A, B, F, Out> Service for AndThenApply<A, B, F, Out> +impl<A, B, F, Out, AReq, BReq> Service<AReq> for AndThenApply<A, B, F, Out, AReq, BReq> where - A: Service, - B: Service<Error = A::Error>, + A: Service<AReq>, + B: Service<BReq, Error = A::Error>, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into<A::Error>, { - type Request = A::Request; type Response = Out::Item; type Error = A::Error; - type Future = AndThenApplyFuture<A, B, F, Out>; + type Future = AndThenApplyFuture<A, B, F, Out, AReq, BReq>; 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<A, B, F, Out> +pub struct AndThenApplyFuture<A, B, F, Out, AReq, BReq> where - A: Service, - B: Service<Error = A::Error>, + A: Service<AReq>, + B: Service<BReq, Error = A::Error>, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into<A::Error>, @@ -97,12 +86,13 @@ where f: Cell<F>, fut_a: Option<A::Future>, fut_b: Option<Out::Future>, + _t: PhantomData<(AReq, BReq)>, } -impl<A, B, F, Out> Future for AndThenApplyFuture<A, B, F, Out> +impl<A, B, F, Out, AReq, BReq> Future for AndThenApplyFuture<A, B, F, Out, AReq, BReq> where - A: Service, - B: Service<Error = A::Error>, + A: Service<AReq>, + B: Service<BReq, Error = A::Error>, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into<A::Error>, @@ -129,23 +119,23 @@ where } /// `ApplyNewService` new service combinator -pub struct AndThenApplyNewService<A, B, F, Out, Cfg> { +pub struct AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg> { a: A, b: B, f: Cell<F>, - r: PhantomData<(Out, Cfg)>, + r: PhantomData<(Out, AReq, BReq, Cfg)>, } -impl<A, B, F, Out, Cfg> AndThenApplyNewService<A, B, F, Out, Cfg> +impl<A, B, F, Out, AReq, BReq, Cfg> AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg> where - A: NewService<Cfg>, - B: NewService<Cfg, Error = A::Error, InitError = A::InitError>, + A: NewService<AReq, Cfg>, + B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into<A::Error>, { /// Create new `ApplyNewService` new service instance - pub fn new<A1: IntoNewService<A, Cfg>, B1: IntoNewService<B, Cfg>>( + pub fn new<A1: IntoNewService<A, AReq, Cfg>, B1: IntoNewService<B, BReq, Cfg>>( a: A1, b: B1, f: F, @@ -159,7 +149,8 @@ where } } -impl<A, B, F, Out, Cfg> Clone for AndThenApplyNewService<A, B, F, Out, Cfg> +impl<A, B, F, Out, AReq, BReq, Cfg> Clone + for AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg> where A: Clone, B: Clone, @@ -174,21 +165,21 @@ where } } -impl<A, B, F, Out, Cfg> NewService<Cfg> for AndThenApplyNewService<A, B, F, Out, Cfg> +impl<A, B, F, Out, AReq, BReq, Cfg> NewService<AReq, Cfg> + for AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg> where - A: NewService<Cfg>, - B: NewService<Cfg, Error = A::Error, InitError = A::InitError>, + A: NewService<AReq, Cfg>, + B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into<A::Error>, { - type Request = A::Request; type Response = Out::Item; type Error = A::Error; - type Service = AndThenApply<A::Service, B::Service, F, Out>; + type Service = AndThenApply<A::Service, B::Service, F, Out, AReq, BReq>; type InitError = A::InitError; - type Future = AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>; + type Future = AndThenApplyNewServiceFuture<A, B, F, Out, AReq, BReq, Cfg>; fn new_service(&self, cfg: &Cfg) -> Self::Future { AndThenApplyNewServiceFuture { @@ -201,10 +192,10 @@ where } } -pub struct AndThenApplyNewServiceFuture<A, B, F, Out, Cfg> +pub struct AndThenApplyNewServiceFuture<A, B, F, Out, AReq, BReq, Cfg> where - A: NewService<Cfg>, - B: NewService<Cfg, Error = A::Error, InitError = A::InitError>, + A: NewService<AReq, Cfg>, + B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into<A::Error>, @@ -216,15 +207,16 @@ where b: Option<B::Service>, } -impl<A, B, F, Out, Cfg> Future for AndThenApplyNewServiceFuture<A, B, F, Out, Cfg> +impl<A, B, F, Out, AReq, BReq, Cfg> Future + for AndThenApplyNewServiceFuture<A, B, F, Out, AReq, BReq, Cfg> where - A: NewService<Cfg>, - B: NewService<Cfg, Error = A::Error, InitError = A::InitError>, + A: NewService<AReq, Cfg>, + B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into<A::Error>, { - type Item = AndThenApply<A::Service, B::Service, F, Out>; + type Item = AndThenApply<A::Service, B::Service, F, Out, AReq, BReq>; type Error = A::InitError; fn poll(&mut self) -> Poll<Self::Item, Self::Error> { @@ -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<T, F, In, Out> -where - T: Service, -{ +pub struct Apply<T, R, F, In, Out> { service: T, f: F, - r: PhantomData<(In, Out)>, + r: PhantomData<(R, In, Out)>, } -impl<T, F, In, Out> Apply<T, F, In, Out> +impl<T, R, F, In, Out> Apply<T, R, F, In, Out> where - T: Service, F: FnMut(In, &mut T) -> Out, - Out: IntoFuture, - Out::Error: From<T::Error>, { /// Create new `Apply` combinator - pub fn new<I: IntoService<T>>(service: I, f: F) -> Self { + pub fn new<I: IntoService<T, R>>(service: I, f: F) -> Self + where + T: Service<R>, + Out: IntoFuture, + Out::Error: From<T::Error>, + { Self { service: service.into_service(), f, @@ -31,9 +30,9 @@ where } } -impl<T, F, In, Out> Clone for Apply<T, F, In, Out> +impl<T, R, F, In, Out> Clone for Apply<T, R, F, In, Out> where - T: Service + Clone, + T: Clone, F: Clone, { fn clone(&self) -> Self { @@ -45,14 +44,13 @@ where } } -impl<T, F, In, Out> Service for Apply<T, F, In, Out> +impl<T, R, F, In, Out> Service<In> for Apply<T, R, F, In, Out> where - T: Service, + T: Service<R>, F: FnMut(In, &mut T) -> Out, Out: IntoFuture, Out::Error: From<T::Error>, { - 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<T, F, In, Out, Cfg> -where - T: NewService<Cfg>, -{ +pub struct ApplyNewService<T, F, In, Out, Req> { service: T, f: F, - r: PhantomData<(In, Out, Cfg)>, + r: PhantomData<(In, Out, Req)>, } -impl<T, F, In, Out, Cfg> ApplyNewService<T, F, In, Out, Cfg> -where - T: NewService<Cfg>, - F: FnMut(In, &mut T::Service) -> Out + Clone, - Out: IntoFuture, - Out::Error: From<T::Error>, -{ +impl<T, F, In, Out, Req> ApplyNewService<T, F, In, Out, Req> { /// Create new `ApplyNewService` new service instance - pub fn new<F1: IntoNewService<T, Cfg>>(service: F1, f: F) -> Self { + pub fn new<Cfg, F1: IntoNewService<T, Req, Cfg>>(service: F1, f: F) -> Self + where + T: NewService<Req, Cfg>, + F: FnMut(In, &mut T::Service) -> Out + Clone, + Out: IntoFuture, + Out::Error: From<T::Error>, + { Self { f, service: service.into_new_service(), @@ -93,11 +88,10 @@ where } } -impl<T, F, In, Out, Cfg> Clone for ApplyNewService<T, F, In, Out, Cfg> +impl<T, F, In, Out, Req> Clone for ApplyNewService<T, F, In, Out, Req> where - T: NewService<Cfg> + 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<T, F, In, Out, Cfg> NewService<Cfg> for ApplyNewService<T, F, In, Out, Cfg> +impl<T, F, In, Out, Req, Cfg> NewService<In, Cfg> for ApplyNewService<T, F, In, Out, Req> where - T: NewService<Cfg>, + T: NewService<Req, Cfg>, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From<T::Error>, { - type Request = In; type Response = Out::Item; type Error = Out::Error; - type Service = Apply<T::Service, F, In, Out>; + type Service = Apply<T::Service, Req, F, In, Out>; type InitError = T::InitError; - type Future = ApplyNewServiceFuture<T, F, In, Out, Cfg>; + type Future = ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>; fn new_service(&self, cfg: &Cfg) -> Self::Future { ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone()) } } -pub struct ApplyNewServiceFuture<T, F, In, Out, Cfg> +pub struct ApplyNewServiceFuture<T, F, In, Out, Req, Cfg> where - T: NewService<Cfg>, + T: NewService<Req, Cfg>, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, { @@ -139,9 +132,9 @@ where r: PhantomData<(In, Out)>, } -impl<T, F, In, Out, Cfg> ApplyNewServiceFuture<T, F, In, Out, Cfg> +impl<T, F, In, Out, Req, Cfg> ApplyNewServiceFuture<T, F, In, Out, Req, Cfg> where - T: NewService<Cfg>, + T: NewService<Req, Cfg>, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, { @@ -154,14 +147,14 @@ where } } -impl<T, F, In, Out, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Cfg> +impl<T, F, In, Out, Req, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Req, Cfg> where - T: NewService<Cfg>, + T: NewService<Req, Cfg>, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From<T::Error>, { - type Item = Apply<T::Service, F, In, Out>; + type Item = Apply<T::Service, Req, F, In, Out>; type Error = T::InitError; fn poll(&mut self) -> Poll<Self::Item, Self::Error> { @@ -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<R, E> Default for Blank<R, E> { } } -impl<R, E> Service for Blank<R, E> { - type Request = R; +impl<R, E> Service<R> for Blank<R, E> { type Response = R; type Error = E; type Future = FutureResult<R, E>; @@ -68,8 +67,7 @@ impl<R, E1, E2> Default for BlankNewService<R, E1, E2> { } } -impl<R, E1, E2> NewService<()> for BlankNewService<R, E1, E2> { - type Request = R; +impl<R, E1, E2> NewService<R, ()> for BlankNewService<R, E1, E2> { type Response = R; type Error = E1; type Service = Blank<R, E1>; 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<Req, Res, Err> = Box< - Service< - Request = Req, - Response = Res, - Error = Err, - Future = Box<Future<Item = Res, Error = Err>>, - >, + Service<Req, Response = Res, Error = Err, Future = Box<Future<Item = Res, Error = Err>>>, >; /// Create boxed new service -pub fn new_service<T, C>( +pub fn new_service<T, R, C>( service: T, -) -> BoxedNewService<C, T::Request, T::Response, T::Error, T::InitError> +) -> BoxedNewService<C, R, T::Response, T::Error, T::InitError> where C: 'static, - T: NewService<C> + 'static, - T::Request: 'static, + T: NewService<R, C> + '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<T>(service: T) -> BoxedService<T::Request, T::Response, T::Error> +pub fn service<T, R>(service: T) -> BoxedService<R, T::Response, T::Error> where - T: Service + 'static, + T: Service<R> + 'static, T::Future: 'static, + R: 'static, { - Box::new(ServiceWrapper(service)) + Box::new(ServiceWrapper { + service, + _t: PhantomData, + }) } type Inner<C, Req, Res, Err, InitErr> = Box< NewService< + Req, C, - Request = Req, Response = Res, Error = Err, InitError = InitErr, @@ -53,14 +54,14 @@ type Inner<C, Req, Res, Err, InitErr> = Box< pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>); -impl<C, Req, Res, Err, InitErr> NewService<C> for BoxedNewService<C, Req, Res, Err, InitErr> +impl<C, Req, Res, Err, InitErr> NewService<Req, C> + for BoxedNewService<C, Req, Res, Err, InitErr> 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<C, T: NewService<C>> { +struct NewServiceWrapper<T: NewService<R, C>, R, C> { service: T, - _t: std::marker::PhantomData<C>, + _t: std::marker::PhantomData<(R, C)>, } -impl<C, T, Req, Res, Err, InitErr> NewService<C> for NewServiceWrapper<C, T> +impl<C, T, Req, Res, Err, InitErr> NewService<Req, C> for NewServiceWrapper<T, Req, C> where Req: 'static, Res: 'static, Err: 'static, InitErr: 'static, - T: NewService<C, Request = Req, Response = Res, Error = Err, InitError = InitErr>, + T: NewService<Req, C, Response = Res, Error = Err, InitError = InitErr>, T::Future: 'static, T::Service: 'static, - <T::Service as Service>::Future: 'static, + <T::Service as Service<Req>>::Future: 'static, { - type Request = Req; type Response = Res; type Error = Err; type InitError = InitErr; @@ -105,33 +105,40 @@ where } } -struct ServiceWrapper<T: Service>(T); +struct ServiceWrapper<T: Service<R>, R> { + service: T, + _t: PhantomData<R>, +} -impl<T> ServiceWrapper<T> +impl<T, R> ServiceWrapper<T, R> where - T: Service + 'static, + T: Service<R> + 'static, T::Future: 'static, + R: 'static, { - fn boxed(service: T) -> BoxedService<T::Request, T::Response, T::Error> { - Box::new(ServiceWrapper(service)) + fn boxed(service: T) -> BoxedService<R, T::Response, T::Error> { + Box::new(ServiceWrapper { + service, + _t: PhantomData, + }) } } -impl<T, Req, Res, Err> Service for ServiceWrapper<T> +impl<T, Req, Res, Err> Service<Req> for ServiceWrapper<T, Req> where - T: Service<Request = Req, Response = Res, Error = Err>, + T: Service<Req, Response = Res, Error = Err>, T::Future: 'static, + Req: 'static, { - type Request = Req; type Response = Res; type Error = Err; type Future = Box<Future<Item = Self::Response, Error = Self::Error>>; 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, R, S, E>(f: F) -> FnNewServiceNoConfig<F, R, S, E> +pub fn fn_factory<F, R, S, E, Req>(f: F) -> FnNewServiceNoConfig<F, R, S, E, Req> where F: Fn() -> R, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { FnNewServiceNoConfig::new(f) } /// Create `NewService` for function that can produce services with configuration -pub fn fn_cfg_factory<F, C, R, S, E>(f: F) -> FnNewServiceConfig<F, C, R, S, E> +pub fn fn_cfg_factory<F, C, R, S, E, Req>(f: F) -> FnNewServiceConfig<F, C, R, S, E, Req> where F: Fn(&C) -> R, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { FnNewServiceConfig::new(f) } @@ -66,12 +66,11 @@ where } } -impl<F, Req, Out> Service for FnService<F, Req, Out> +impl<F, Req, Out> Service<Req> for FnService<F, Req, Out> 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<F, Req, Out> IntoService<FnService<F, Req, Out>> for F +impl<F, Req, Out> IntoService<FnService<F, Req, Out>, Req> for F where F: FnMut(Req) -> Out + 'static, Out: IntoFuture, @@ -114,12 +113,11 @@ where } } -impl<F, Req, Out, Cfg> NewService<Cfg> for FnNewService<F, Req, Out, Cfg> +impl<F, Req, Out, Cfg> NewService<Req, Cfg> for FnNewService<F, Req, Out, Cfg> where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, { - type Request = Req; type Response = Out::Item; type Error = Out::Error; type Service = FnService<F, Req, Out>; @@ -142,7 +140,7 @@ where } } -impl<F, Req, Out, Cfg> IntoNewService<FnNewService<F, Req, Out, Cfg>, Cfg> for F +impl<F, Req, Out, Cfg> IntoNewService<FnNewService<F, Req, Out, Cfg>, Req, Cfg> for F where F: Fn(Req) -> Out + Clone, Out: IntoFuture, @@ -153,33 +151,33 @@ where } /// Converter for `Fn() -> Future<Service>` fn -pub struct FnNewServiceNoConfig<F, R, S, E> +pub struct FnNewServiceNoConfig<F, R, S, E, Req> where F: Fn() -> R, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { f: F, + _t: PhantomData<Req>, } -impl<F, R, S, E> FnNewServiceNoConfig<F, R, S, E> +impl<F, R, S, E, Req> FnNewServiceNoConfig<F, R, S, E, Req> where F: Fn() -> R, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { pub fn new(f: F) -> Self { - FnNewServiceNoConfig { f } + FnNewServiceNoConfig { f, _t: PhantomData } } } -impl<F, R, S, E> NewService<()> for FnNewServiceNoConfig<F, R, S, E> +impl<F, R, S, E, Req> NewService<Req, ()> for FnNewServiceNoConfig<F, R, S, E, Req> where F: Fn() -> R, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S; @@ -192,57 +190,56 @@ where } } -impl<F, R, S, E> Clone for FnNewServiceNoConfig<F, R, S, E> +impl<F, R, S, E, Req> Clone for FnNewServiceNoConfig<F, R, S, E, Req> where F: Fn() -> R + Clone, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { fn clone(&self) -> Self { Self::new(self.f.clone()) } } -impl<F, R, S, E> IntoNewService<FnNewServiceNoConfig<F, R, S, E>, ()> for F +impl<F, R, S, E, Req> IntoNewService<FnNewServiceNoConfig<F, R, S, E, Req>, Req, ()> for F where F: Fn() -> R, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { - fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E> { + fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E, Req> { FnNewServiceNoConfig::new(self) } } /// Convert `Fn(&Config) -> Future<Service>` fn to NewService -pub struct FnNewServiceConfig<F, C, R, S, E> +pub struct FnNewServiceConfig<F, C, R, S, E, Req> where F: Fn(&C) -> R, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { f: F, - _t: PhantomData<(C, R, S, E)>, + _t: PhantomData<(C, R, S, E, Req)>, } -impl<F, C, R, S, E> FnNewServiceConfig<F, C, R, S, E> +impl<F, C, R, S, E, Req> FnNewServiceConfig<F, C, R, S, E, Req> where F: Fn(&C) -> R, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { pub fn new(f: F) -> Self { FnNewServiceConfig { f, _t: PhantomData } } } -impl<F, C, R, S, E> NewService<C> for FnNewServiceConfig<F, C, R, S, E> +impl<F, C, R, S, E, Req> NewService<Req, C> for FnNewServiceConfig<F, C, R, S, E, Req> where F: Fn(&C) -> R, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S; @@ -255,24 +252,45 @@ where } } -impl<F, C, R, S, E> Clone for FnNewServiceConfig<F, C, R, S, E> +impl<F, C, R, S, E, Req> Clone for FnNewServiceConfig<F, C, R, S, E, Req> where F: Fn(&C) -> R + Clone, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { fn clone(&self) -> Self { Self::new(self.f.clone()) } } -impl<F, C, R, S, E> IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E>, C> for F +impl<F, C, R, S, E, Req> + IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E, Req>, Req, C> for F where F: Fn(&C) -> R, R: IntoFuture<Item = S, Error = E>, - S: Service, + S: Service<Req>, { - fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E> { + fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E, Req> { 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<usize, ()> { 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<F, S, In, Out, Err> +pub struct FnTransform<F, S, R, In, Out, Err> 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<F, S, In, Out, Err> FnTransform<F, S, In, Out, Err> +impl<F, S, R, In, Out, Err> FnTransform<F, S, R, In, Out, Err> where F: FnMut(In, &mut S) -> Out + Clone, Out: IntoFuture, @@ -24,17 +24,16 @@ where } } -impl<F, S, In, Out, Err> Transform<S> for FnTransform<F, S, In, Out, Err> +impl<F, S, R, In, Out, Err> Transform<In, S> for FnTransform<F, S, R, In, Out, Err> where - S: Service, + S: Service<R>, F: FnMut(In, &mut S) -> Out + Clone, Out: IntoFuture, Out::Error: From<S::Error>, { - type Request = In; type Response = Out::Item; type Error = Out::Error; - type Transform = Apply<S, F, In, Out>; + type Transform = Apply<S, R, F, In, Out>; type InitError = Err; type Future = FutureResult<Self::Transform, Self::InitError>; @@ -43,19 +42,19 @@ where } } -impl<F, S, In, Out, Err> IntoTransform<FnTransform<F, S, In, Out, Err>, S> for F +impl<F, S, R, In, Out, Err> IntoTransform<FnTransform<F, S, R, In, Out, Err>, In, S> for F where - S: Service, + S: Service<R>, F: FnMut(In, &mut S) -> Out + Clone, Out: IntoFuture, Out::Error: From<S::Error>, { - fn into_transform(self) -> FnTransform<F, S, In, Out, Err> { + fn into_transform(self) -> FnTransform<F, S, R, In, Out, Err> { FnTransform::new(self) } } -impl<F, S, In, Out, Err> Clone for FnTransform<F, S, In, Out, Err> +impl<F, S, R, In, Out, Err> Clone for FnTransform<F, S, R, In, Out, Err> 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<A, E> { } impl<A, E> FromErr<A, E> { - pub(crate) fn new(service: A) -> Self + pub(crate) fn new<R>(service: A) -> Self where - A: Service, + A: Service<R>, E: From<A::Error>, { FromErr { @@ -37,21 +37,20 @@ where } } -impl<A, E> Service for FromErr<A, E> +impl<A, E, R> Service<R> for FromErr<A, E> where - A: Service, + A: Service<R>, E: From<A::Error>, { - type Request = A::Request; type Response = A::Response; type Error = E; - type Future = FromErrFuture<A, E>; + type Future = FromErrFuture<A, R, E>; 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<A: Service, E> { +pub struct FromErrFuture<A: Service<R>, R, E> { fut: A::Future, f: PhantomData<E>, } -impl<A, E> Future for FromErrFuture<A, E> +impl<A, R, E> Future for FromErrFuture<A, R, E> where - A: Service, + A: Service<R>, E: From<A::Error>, { type Item = A::Response; @@ -88,9 +87,9 @@ pub struct FromErrNewService<A, E, C> { impl<A, E, C> FromErrNewService<A, E, C> { /// Create new `FromErr` new service instance - pub fn new(a: A) -> Self + pub fn new<R>(a: A) -> Self where - A: NewService<C>, + A: NewService<R, C>, E: From<A::Error>, { Self { a, e: PhantomData } @@ -109,18 +108,17 @@ where } } -impl<A, E, C> NewService<C> for FromErrNewService<A, E, C> +impl<A, E, C, R> NewService<R, C> for FromErrNewService<A, E, C> where - A: NewService<C>, + A: NewService<R, C>, E: From<A::Error>, { - type Request = A::Request; type Response = A::Response; type Error = E; type Service = FromErr<A::Service, E>; type InitError = A::InitError; - type Future = FromErrNewServiceFuture<A, E, C>; + type Future = FromErrNewServiceFuture<A, E, C, R>; fn new_service(&self, cfg: &C) -> Self::Future { FromErrNewServiceFuture { @@ -130,18 +128,18 @@ where } } -pub struct FromErrNewServiceFuture<A, E, C> +pub struct FromErrNewServiceFuture<A, E, C, R> where - A: NewService<C>, + A: NewService<R, C>, E: From<A::Error>, { fut: A::Future, e: PhantomData<E>, } -impl<A, E, C> Future for FromErrNewServiceFuture<A, E, C> +impl<A, E, C, R> Future for FromErrNewServiceFuture<A, E, C, R> where - A: NewService<C>, + A: NewService<R, C>, E: From<A::Error>, { type Item = FromErr<A::Service, E>; @@ -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<Request> { /// 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<Request>: Service<Request> { /// Apply function to specified service and use it as a next service in /// chain. - fn apply_fn<F, B, B1, Out>(self, service: B1, f: F) -> AndThenApply<Self, B, F, Out> + fn apply_fn<F, B, B1, Out, Req>( + self, + service: B1, + f: F, + ) -> AndThenApply<Self, B, F, Out, Request, Req> where Self: Sized, F: FnMut(Self::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into<Self::Error>, - B: Service<Error = Self::Error>, - B1: IntoService<B>, + B: Service<Req, Error = Self::Error>, + B1: IntoService<B, Req>, { AndThenApply::new(self, service, f) } @@ -101,8 +104,8 @@ pub trait ServiceExt: Service { fn and_then<F, B>(self, service: F) -> AndThen<Self, B> where Self: Sized, - F: IntoService<B>, - B: Service<Request = Self::Response, Error = Self::Error>, + F: IntoService<B, Self::Response>, + B: Service<Self::Response, Error = Self::Error>, { AndThen::new(self, service.into_service()) } @@ -128,7 +131,7 @@ pub trait ServiceExt: Service { fn then<B>(self, service: B) -> Then<Self, B> where Self: Sized, - B: Service<Request = Result<Self::Response, Self::Error>, Error = Self::Error>, + B: Service<Result<Self::Response, Self::Error>, Error = Self::Error>, { Then::new(self, service) } @@ -167,7 +170,7 @@ pub trait ServiceExt: Service { } } -impl<T: ?Sized> ServiceExt for T where T: Service {} +impl<T: ?Sized, R> ServiceExt<R> for T where T: Service<R> {} /// Creates new `Service` values. /// @@ -177,11 +180,9 @@ impl<T: ?Sized> 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<Config = ()> { - /// Requests handled by the service. - type Request; - +/// * `Request` - requests handled by the service. +/// * `Config` - is a service factory configuration type. +pub trait NewService<Request, Config = ()> { /// Responses given by the service type Response; @@ -189,11 +190,7 @@ pub trait NewService<Config = ()> { type Error; /// The `Service` value created by this factory - type Service: Service< - Request = Self::Request, - Response = Self::Response, - Error = Self::Error, - >; + type Service: Service<Request, Response = Self::Response, Error = Self::Error>; /// Errors produced while building a service. type InitError; @@ -206,33 +203,33 @@ pub trait NewService<Config = ()> { /// Apply function to specified service and use it as a next service in /// chain. - fn apply<T, T1, B, B1>( + fn apply<T, T1, B, B1, Req>( self, transform: T1, service: B1, - ) -> AndThenTransform<T, Self, B, Config> + ) -> AndThenTransform<T, Self, B, Req> where Self: Sized, - T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>, + T: Transform<Self::Response, B::Service, InitError = Self::InitError>, T::Error: From<Self::Error>, - T1: IntoTransform<T, B::Service>, - B: NewService<Config, InitError = Self::InitError>, - B1: IntoNewService<B, Config>, + T1: IntoTransform<T, Self::Response, B::Service>, + B: NewService<Req, Config, InitError = Self::InitError>, + B1: IntoNewService<B, Req, Config>, { 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<B, I, F, Out>( + fn apply_fn<B, I, F, Out, Req>( self, service: I, f: F, - ) -> AndThenApplyNewService<Self, B, F, Out, Config> + ) -> AndThenApplyNewService<Self, B, F, Out, Request, Req, Config> where Self: Sized, - B: NewService<Config, Error = Self::Error, InitError = Self::InitError>, - I: IntoNewService<B, Config>, + B: NewService<Req, Config, Error = Self::Error, InitError = Self::InitError>, + I: IntoNewService<B, Req, Config>, F: FnMut(Self::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into<Self::Error>, @@ -241,16 +238,11 @@ pub trait NewService<Config = ()> { } /// Call another service after call to this one has resolved successfully. - fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B, Config> + fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B> where Self: Sized, - F: IntoNewService<B, Config>, - B: NewService< - Config, - Request = Self::Response, - Error = Self::Error, - InitError = Self::InitError, - >, + F: IntoNewService<B, Self::Response, Config>, + B: NewService<Self::Response, Config, Error = Self::Error, InitError = Self::InitError>, { AndThenNewService::new(self, new_service) } @@ -278,15 +270,15 @@ pub trait NewService<Config = ()> { fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B, Config> where Self: Sized, - F: IntoNewService<B, Config>, + F: IntoNewService<B, Result<Self::Response, Self::Error>, Config>, B: NewService< + Result<Self::Response, Self::Error>, Config, - Request = Result<Self::Response, Self::Error>, 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<Config = ()> { } } -impl<'a, S> Service for &'a mut S +impl<'a, S, R> Service<R> for &'a mut S where - S: Service + 'a, + S: Service<R> + '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<S> Service for Box<S> +impl<S, R> Service<R> for Box<S> where - S: Service + ?Sized, + S: Service<R> + ?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<S, C> NewService<C> for Rc<S> +impl<S, R, C> NewService<R, C> for Rc<S> where - S: NewService<C>, + S: NewService<R, C>, { - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S::Service; @@ -370,11 +359,10 @@ where } } -impl<S, C> NewService<C> for Arc<S> +impl<S, R, C> NewService<R, C> for Arc<S> where - S: NewService<C>, + S: NewService<R, C>, { - 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<T> +pub trait IntoService<T, R> where - T: Service, + T: Service<R>, { /// Convert to a `Service` fn into_service(self) -> T; } /// Trait for types that can be converted to a `NewService` -pub trait IntoNewService<T, C = ()> +pub trait IntoNewService<T, R, C = ()> where - T: NewService<C>, + T: NewService<R, C>, { /// Convert to an `NewService` fn into_new_service(self) -> T; } -impl<T> IntoService<T> for T +impl<T, R> IntoService<T, R> for T where - T: Service, + T: Service<R>, { fn into_service(self) -> T { self } } -impl<T, C> IntoNewService<T, C> for T +impl<T, R, C> IntoNewService<T, R, C> for T where - T: NewService<C>, + T: NewService<R, C>, { 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<T, C> +pub trait IntoConfigurableNewService<T, R, C> where - T: NewService<C>, + T: NewService<R, C>, { /// Convert to an `NewService` fn into_new_service(self) -> T; } -impl<T, C> IntoConfigurableNewService<T, C> for T +impl<T, R, C> IntoConfigurableNewService<T, R, C> for T where - T: NewService<C>, + T: NewService<R, C>, { 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<A, F, Response> { impl<A, F, Response> Map<A, F, Response> { /// Create new `Map` combinator - pub fn new(service: A, f: F) -> Self + pub fn new<R>(service: A, f: F) -> Self where - A: Service, + A: Service<R>, F: FnMut(A::Response) -> Response, { Self { @@ -42,37 +42,36 @@ where } } -impl<A, F, Response> Service for Map<A, F, Response> +impl<A, F, R, Response> Service<R> for Map<A, F, Response> where - A: Service, + A: Service<R>, F: FnMut(A::Response) -> Response + Clone, { - type Request = A::Request; type Response = Response; type Error = A::Error; - type Future = MapFuture<A, F, Response>; + type Future = MapFuture<A, F, R, Response>; 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<A, F, Response> +pub struct MapFuture<A, F, R, Response> where - A: Service, + A: Service<R>, F: FnMut(A::Response) -> Response, { f: F, fut: A::Future, } -impl<A, F, Response> MapFuture<A, F, Response> +impl<A, F, R, Response> MapFuture<A, F, R, Response> where - A: Service, + A: Service<R>, F: FnMut(A::Response) -> Response, { fn new(fut: A::Future, f: F) -> Self { @@ -80,9 +79,9 @@ where } } -impl<A, F, Response> Future for MapFuture<A, F, Response> +impl<A, F, R, Response> Future for MapFuture<A, F, R, Response> where - A: Service, + A: Service<R>, F: FnMut(A::Response) -> Response, { type Item = Response; @@ -105,9 +104,9 @@ pub struct MapNewService<A, F, Res, Cfg> { impl<A, F, Res, Cfg> MapNewService<A, F, Res, Cfg> { /// Create new `Map` new service instance - pub fn new(a: A, f: F) -> Self + pub fn new<Req>(a: A, f: F) -> Self where - A: NewService<Cfg>, + A: NewService<Req, Cfg>, F: FnMut(A::Response) -> Res, { Self { @@ -132,36 +131,35 @@ where } } -impl<A, F, Res, Cfg> NewService<Cfg> for MapNewService<A, F, Res, Cfg> +impl<A, F, Req, Res, Cfg> NewService<Req, Cfg> for MapNewService<A, F, Res, Cfg> where - A: NewService<Cfg>, + A: NewService<Req, Cfg>, F: FnMut(A::Response) -> Res + Clone, { - type Request = A::Request; type Response = Res; type Error = A::Error; type Service = Map<A::Service, F, Res>; type InitError = A::InitError; - type Future = MapNewServiceFuture<A, F, Res, Cfg>; + type Future = MapNewServiceFuture<A, F, Req, Res, Cfg>; fn new_service(&self, cfg: &Cfg) -> Self::Future { MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) } } -pub struct MapNewServiceFuture<A, F, Res, Cfg> +pub struct MapNewServiceFuture<A, F, Req, Res, Cfg> where - A: NewService<Cfg>, + A: NewService<Req, Cfg>, F: FnMut(A::Response) -> Res, { fut: A::Future, f: Option<F>, } -impl<A, F, Res, Cfg> MapNewServiceFuture<A, F, Res, Cfg> +impl<A, F, Req, Res, Cfg> MapNewServiceFuture<A, F, Req, Res, Cfg> where - A: NewService<Cfg>, + A: NewService<Req, Cfg>, F: FnMut(A::Response) -> Res, { fn new(fut: A::Future, f: F) -> Self { @@ -169,9 +167,9 @@ where } } -impl<A, F, Res, Cfg> Future for MapNewServiceFuture<A, F, Res, Cfg> +impl<A, F, Req, Res, Cfg> Future for MapNewServiceFuture<A, F, Req, Res, Cfg> where - A: NewService<Cfg>, + A: NewService<Req, Cfg>, F: FnMut(A::Response) -> Res, { type Item = Map<A::Service, F, Res>; @@ -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<A, F, E> { impl<A, F, E> MapErr<A, F, E> { /// Create new `MapErr` combinator - pub fn new(service: A, f: F) -> Self + pub fn new<R>(service: A, f: F) -> Self where - A: Service, + A: Service<R>, F: Fn(A::Error) -> E, { Self { @@ -43,47 +43,39 @@ where } } -impl<A, F, E> Service for MapErr<A, F, E> +impl<A, F, E, R> Service<R> for MapErr<A, F, E> where - A: Service, + A: Service<R>, F: Fn(A::Error) -> E + Clone, { - type Request = A::Request; type Response = A::Response; type Error = E; - type Future = MapErrFuture<A, F, E>; + type Future = MapErrFuture<A, F, E, R>; 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<A, F, E> +pub struct MapErrFuture<A, F, E, R> where - A: Service, + A: Service<R>, F: Fn(A::Error) -> E, { f: F, fut: A::Future, } -impl<A, F, E> MapErrFuture<A, F, E> +impl<A, F, E, R> Future for MapErrFuture<A, F, E, R> where - A: Service, - F: Fn(A::Error) -> E, -{ - fn new(fut: A::Future, f: F) -> Self { - MapErrFuture { f, fut } - } -} - -impl<A, F, E> Future for MapErrFuture<A, F, E> -where - A: Service, + A: Service<R>, F: Fn(A::Error) -> E, { type Item = A::Response; @@ -106,9 +98,9 @@ pub struct MapErrNewService<A, F, E, C> { impl<A, F, E, C> MapErrNewService<A, F, E, C> { /// Create new `MapErr` new service instance - pub fn new(a: A, f: F) -> Self + pub fn new<R>(a: A, f: F) -> Self where - A: NewService<C>, + A: NewService<R, C>, F: Fn(A::Error) -> E, { Self { @@ -133,36 +125,35 @@ where } } -impl<A, F, E, C> NewService<C> for MapErrNewService<A, F, E, C> +impl<A, F, E, R, C> NewService<R, C> for MapErrNewService<A, F, E, C> where - A: NewService<C>, + A: NewService<R, C>, F: Fn(A::Error) -> E + Clone, { - type Request = A::Request; type Response = A::Response; type Error = E; type Service = MapErr<A::Service, F, E>; type InitError = A::InitError; - type Future = MapErrNewServiceFuture<A, F, E, C>; + type Future = MapErrNewServiceFuture<A, F, E, R, C>; fn new_service(&self, cfg: &C) -> Self::Future { MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) } } -pub struct MapErrNewServiceFuture<A, F, E, C> +pub struct MapErrNewServiceFuture<A, F, E, R, C> where - A: NewService<C>, + A: NewService<R, C>, F: Fn(A::Error) -> E, { fut: A::Future, f: F, } -impl<A, F, E, C> MapErrNewServiceFuture<A, F, E, C> +impl<A, F, E, R, C> MapErrNewServiceFuture<A, F, E, R, C> where - A: NewService<C>, + A: NewService<R, C>, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -170,9 +161,9 @@ where } } -impl<A, F, E, C> Future for MapErrNewServiceFuture<A, F, E, C> +impl<A, F, E, R, C> Future for MapErrNewServiceFuture<A, F, E, R, C> where - A: NewService<C>, + A: NewService<R, C>, F: Fn(A::Error) -> E + Clone, { type Item = MapErr<A::Service, F, E>; @@ -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<A, F, E, C> { impl<A, F, E, C> MapInitErr<A, F, E, C> { /// Create new `MapInitErr` combinator - pub fn new(a: A, f: F) -> Self + pub fn new<R>(a: A, f: F) -> Self where - A: NewService<C>, + A: NewService<R, C>, F: Fn(A::InitError) -> E, { Self { @@ -40,46 +40,38 @@ where } } -impl<A, F, E, C> NewService<C> for MapInitErr<A, F, E, C> +impl<A, F, E, R, C> NewService<R, C> for MapInitErr<A, F, E, C> where - A: NewService<C>, + A: NewService<R, C>, 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<A, F, E, C>; + type Future = MapInitErrFuture<A, F, E, R, C>; 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<A, F, E, C> +pub struct MapInitErrFuture<A, F, E, R, C> where - A: NewService<C>, + A: NewService<R, C>, F: Fn(A::InitError) -> E, { f: F, fut: A::Future, } -impl<A, F, E, C> MapInitErrFuture<A, F, E, C> +impl<A, F, E, R, C> Future for MapInitErrFuture<A, F, E, R, C> where - A: NewService<C>, - F: Fn(A::InitError) -> E, -{ - fn new(fut: A::Future, f: F) -> Self { - MapInitErrFuture { f, fut } - } -} - -impl<A, F, E, C> Future for MapInitErrFuture<A, F, E, C> -where - A: NewService<C>, + A: NewService<R, C>, 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<A, B> { impl<A, B> Then<A, B> { /// Create new `Then` combinator - pub fn new(a: A, b: B) -> Then<A, B> + pub fn new<R>(a: A, b: B) -> Then<A, B> where - A: Service, - B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>, + A: Service<R>, + B: Service<Result<A::Response, A::Error>, Error = A::Error>, { Then { a, b: Cell::new(b) } } @@ -37,40 +37,39 @@ where } } -impl<A, B> Service for Then<A, B> +impl<A, B, R> Service<R> for Then<A, B> where - A: Service, - B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>, + A: Service<R>, + B: Service<Result<A::Response, A::Error>, Error = A::Error>, { - type Request = A::Request; type Response = B::Response; type Error = B::Error; - type Future = ThenFuture<A, B>; + type Future = ThenFuture<A, B, R>; 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<A, B> +pub struct ThenFuture<A, B, R> where - A: Service, - B: Service<Request = Result<A::Response, A::Error>>, + A: Service<R>, + B: Service<Result<A::Response, A::Error>>, { b: Cell<B>, fut_b: Option<B::Future>, fut_a: Option<A::Future>, } -impl<A, B> ThenFuture<A, B> +impl<A, B, R> ThenFuture<A, B, R> where - A: Service, - B: Service<Request = Result<A::Response, A::Error>>, + A: Service<R>, + B: Service<Result<A::Response, A::Error>>, { fn new(a: A::Future, b: Cell<B>) -> Self { ThenFuture { @@ -81,10 +80,10 @@ where } } -impl<A, B> Future for ThenFuture<A, B> +impl<A, B, R> Future for ThenFuture<A, B, R> where - A: Service, - B: Service<Request = Result<A::Response, A::Error>>, + A: Service<R>, + B: Service<Result<A::Response, A::Error>>, { type Item = B::Response; type Error = B::Error; @@ -119,42 +118,35 @@ pub struct ThenNewService<A, B, C> { impl<A, B, C> ThenNewService<A, B, C> { /// Create new `AndThen` combinator - pub fn new<F>(a: A, f: F) -> Self + pub fn new<R>(a: A, f: B) -> Self where - A: NewService<C>, + A: NewService<R, C>, B: NewService< + Result<A::Response, A::Error>, C, - Request = Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError, >, - F: IntoNewService<B, C>, { Self { a, - b: f.into_new_service(), + b: f, _t: PhantomData, } } } -impl<A, B, C> NewService<C> for ThenNewService<A, B, C> +impl<A, B, R, C> NewService<R, C> for ThenNewService<A, B, C> where - A: NewService<C>, - B: NewService< - C, - Request = Result<A::Response, A::Error>, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService<R, C>, + B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>, { - type Request = A::Request; type Response = B::Response; type Error = A::Error; type Service = Then<A::Service, B::Service>; type InitError = A::InitError; - type Future = ThenNewServiceFuture<A, B, C>; + type Future = ThenNewServiceFuture<A, B, R, C>; 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<A, B, C> +pub struct ThenNewServiceFuture<A, B, R, C> where - A: NewService<C>, - B: NewService< - C, - Request = Result<A::Response, A::Error>, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService<R, C>, + B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>, { fut_b: B::Future, fut_a: A::Future, @@ -191,15 +178,10 @@ where b: Option<B::Service>, } -impl<A, B, C> ThenNewServiceFuture<A, B, C> +impl<A, B, R, C> ThenNewServiceFuture<A, B, R, C> where - A: NewService<C>, - B: NewService< - C, - Request = Result<A::Response, A::Error>, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService<R, C>, + B: NewService<Result<A::Response, A::Error>, 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<A, B, C> Future for ThenNewServiceFuture<A, B, C> +impl<A, B, R, C> Future for ThenNewServiceFuture<A, B, R, C> where - A: NewService<C>, - B: NewService< - C, - Request = Result<A::Response, A::Error>, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService<R, C>, + B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>, { type Item = Then<A::Service, B::Service>; type Error = A::InitError; @@ -259,8 +236,7 @@ mod tests { #[derive(Clone)] struct Srv1(Rc<Cell<usize>>); - impl Service for Srv1 { - type Request = Result<&'static str, &'static str>; + impl Service<Result<&'static str, &'static str>> for Srv1 { type Response = &'static str; type Error = (); type Future = FutureResult<Self::Response, Self::Error>; @@ -280,8 +256,7 @@ mod tests { struct Srv2(Rc<Cell<usize>>); - impl Service for Srv2 { - type Request = Result<&'static str, ()>; + impl Service<Result<&'static str, ()>> for Srv2 { type Response = (&'static str, &'static str); type Error = (); type Future = FutureResult<Self::Response, ()>; 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<S> { - /// Requests handled by the service. - type Request; - +/// +/// * `S` is a wrapped service. +/// * `R` requests handled by this transform service. +pub trait Transform<R, S> { /// Responses given by the service. type Response; @@ -21,11 +20,7 @@ pub trait Transform<S> { type Error; /// The `TransformService` value created by this factory - type Transform: Service< - Request = Self::Request, - Response = Self::Response, - Error = Self::Error, - >; + type Transform: Service<R, Response = Self::Response, Error = Self::Error>; /// Errors produced while building a service. type InitError; @@ -47,11 +42,10 @@ pub trait Transform<S> { } } -impl<T, S> Transform<S> for Rc<T> +impl<T, R, S> Transform<R, S> for Rc<T> where - T: Transform<S>, + T: Transform<R, S>, { - type Request = T::Request; type Response = T::Response; type Error = T::Error; type InitError = T::InitError; @@ -63,11 +57,10 @@ where } } -impl<T, S> Transform<S> for Arc<T> +impl<T, R, S> Transform<R, S> for Arc<T> where - T: Transform<S>, + T: Transform<R, S>, { - 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<T, S> +pub trait IntoTransform<T, R, S> where - T: Transform<S>, + T: Transform<R, S>, { /// Convert to a `TransformService` fn into_transform(self) -> T; } -impl<T, S> IntoTransform<T, S> for T +impl<T, S, R> IntoTransform<T, S, R> for T where - T: Transform<S>, + T: Transform<S, R>, { fn into_transform(self) -> T { self @@ -99,19 +92,19 @@ where /// `Apply` transform new service #[derive(Clone)] -pub struct ApplyTransform<T, A, C> { - a: A, +pub struct ApplyTransform<T, R, S, Req, Cfg> { + a: S, t: Rc<T>, - _t: std::marker::PhantomData<C>, + _t: std::marker::PhantomData<(R, Req, Cfg)>, } -impl<T, A, C> ApplyTransform<T, A, C> +impl<T, R, S, Req, Cfg> ApplyTransform<T, R, S, Req, Cfg> where - A: NewService<C>, - T: Transform<A::Service, Error = A::Error, InitError = A::InitError>, + S: NewService<Req, Cfg>, + T: Transform<R, S::Service, Error = S::Error, InitError = S::InitError>, { /// Create new `ApplyNewService` new service instance - pub fn new<F: IntoTransform<T, A::Service>>(t: F, a: A) -> Self { + pub fn new<F: IntoTransform<T, R, S::Service>>(t: F, a: S) -> Self { Self { a, t: Rc::new(t.into_transform()), @@ -120,20 +113,19 @@ where } } -impl<T, A, C> NewService<C> for ApplyTransform<T, A, C> +impl<T, R, S, Req, Cfg> NewService<R, Cfg> for ApplyTransform<T, R, S, Req, Cfg> where - A: NewService<C>, - T: Transform<A::Service, Error = A::Error, InitError = A::InitError>, + S: NewService<Req, Cfg>, + T: Transform<R, S::Service, Error = S::Error, InitError = S::InitError>, { - type Request = T::Request; type Response = T::Response; type Error = T::Error; type Service = T::Transform; type InitError = T::InitError; - type Future = ApplyTransformFuture<T, A, C>; + type Future = ApplyTransformFuture<T, R, S, Req, Cfg>; - 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<T, A, C> +pub struct ApplyTransformFuture<T, R, S, Req, Cfg> where - A: NewService<C>, - T: Transform<A::Service, Error = A::Error, InitError = A::InitError>, + S: NewService<Req, Cfg>, + T: Transform<R, S::Service, Error = S::Error, InitError = S::InitError>, { - fut_a: A::Future, - fut_t: Option<<T::Future as IntoFuture>::Future>, + fut_a: S::Future, + fut_t: Option<T::Future>, t_cell: Rc<T>, } -impl<T, A, C> Future for ApplyTransformFuture<T, A, C> +impl<T, R, S, Req, Cfg> Future for ApplyTransformFuture<T, R, S, Req, Cfg> where - A: NewService<C>, - T: Transform<A::Service, Error = A::Error, InitError = A::InitError>, + S: NewService<Req, Cfg>, + T: Transform<R, S::Service, Error = S::Error, InitError = S::InitError>, { 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<T, S, F, E> { impl<T, S, F, E> TransformMapInitErr<T, S, F, E> { /// Create new `MapInitErr` new transform instance - pub fn new(t: T, f: F) -> Self + pub fn new<R>(t: T, f: F) -> Self where - T: Transform<S>, + T: Transform<R, S>, F: Fn(T::InitError) -> E, { Self { @@ -43,36 +43,35 @@ where } } -impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E> +impl<T, R, S, F, E> Transform<R, S> for TransformMapInitErr<T, S, F, E> where - T: Transform<S>, + T: Transform<R, S>, 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<T, S, F, E>; + type Future = TransformMapInitErrFuture<T, R, S, F, E>; fn new_transform(&self, service: S) -> Self::Future { TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone()) } } -pub struct TransformMapInitErrFuture<T, S, F, E> +pub struct TransformMapInitErrFuture<T, R, S, F, E> where - T: Transform<S>, + T: Transform<R, S>, F: Fn(T::InitError) -> E, { fut: T::Future, f: F, } -impl<T, S, F, E> TransformMapInitErrFuture<T, S, F, E> +impl<T, R, S, F, E> TransformMapInitErrFuture<T, R, S, F, E> where - T: Transform<S>, + T: Transform<R, S>, F: Fn(T::InitError) -> E, { fn new(fut: T::Future, f: F) -> Self { @@ -80,9 +79,9 @@ where } } -impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E> +impl<T, R, S, F, E> Future for TransformMapInitErrFuture<T, R, S, F, E> where - T: Transform<S>, + T: Transform<R, S>, 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<T: 'static> { } impl<T: 'static> CloneableService<T> { - pub fn new(service: T) -> Self + pub fn new<R>(service: T) -> Self where - T: Service, + T: Service<R>, { Self { service: Cell::new(service), @@ -33,11 +33,10 @@ impl<T: 'static> Clone for CloneableService<T> { } } -impl<T> Service for CloneableService<T> +impl<T, R> Service<R> for CloneableService<T> where - T: Service + 'static, + T: Service<R> + '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<A: Clone, B: Clone> Clone for EitherService<A, B> { } } -impl<A, B> Service for EitherService<A, B> +impl<A, B, R> Service<R> for EitherService<A, B> where - A: Service, - B: Service<Request = A::Request, Response = A::Response, Error = A::Error>, + A: Service<R>, + B: Service<R, Response = A::Response, Error = A::Error>, { - type Request = A::Request; type Response = A::Response; type Error = A::Error; type Future = future::Either<A::Future, B::Future>; @@ -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<A, B> { } impl<A, B> Either<A, B> { - pub fn new_a<C>(srv: A) -> Self + pub fn new_a<R, C>(srv: A) -> Self where - A: NewService<C>, - B: NewService< - C, - Request = A::Request, - Response = A::Response, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService<R, C>, + B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>, { Either::A(srv) } - pub fn new_b<C>(srv: B) -> Self + pub fn new_b<R, C>(srv: B) -> Self where - A: NewService<C>, - B: NewService< - C, - Request = A::Request, - Response = A::Response, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService<R, C>, + B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>, { Either::B(srv) } } -impl<A, B, C> NewService<C> for Either<A, B> +impl<A, B, R, C> NewService<R, C> for Either<A, B> where - A: NewService<C>, - B: NewService< - C, - Request = A::Request, - Response = A::Response, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService<R, C>, + B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>, { - type Request = A::Request; type Response = A::Response; type Error = A::Error; type InitError = A::InitError; type Service = EitherService<A::Service, B::Service>; - type Future = EitherNewService<A, B, C>; + type Future = EitherNewService<A, B, R, C>; fn new_service(&self, cfg: &C) -> Self::Future { match self { @@ -118,21 +98,15 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> { } #[doc(hidden)] -pub enum EitherNewService<A: NewService<C>, B: NewService<C>, C> { +pub enum EitherNewService<A: NewService<R, C>, B: NewService<R, C>, R, C> { A(<A::Future as IntoFuture>::Future), B(<B::Future as IntoFuture>::Future), } -impl<A, B, C> Future for EitherNewService<A, B, C> +impl<A, B, R, C> Future for EitherNewService<A, B, R, C> where - A: NewService<C>, - B: NewService< - C, - Request = A::Request, - Response = A::Response, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService<R, C>, + B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>, { type Item = EitherService<A::Service, B::Service>; 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<S, T, U, C> { impl<S, T, U, C> FramedNewService<S, T, U, C> where C: Clone, - S: NewService<C, Request = Request<U>, Response = Response<U>>, + S: NewService<Request<U>, C, Response = Response<U>>, S::Error: 'static, - <S::Service as Service>::Future: 'static, + <S::Service as Service<Request<U>>>::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, <U as Encoder>::Item: 'static, <U as Encoder>::Error: std::fmt::Debug, { - pub fn new<F1: IntoNewService<S, C>>(factory: F1) -> Self { + pub fn new<F1: IntoNewService<S, Request<U>, C>>(factory: F1) -> Self { Self { factory: factory.into_new_service(), _t: PhantomData, @@ -51,18 +51,17 @@ where } } -impl<S, T, U, C> NewService<C> for FramedNewService<S, T, U, C> +impl<S, T, U, C> NewService<Framed<T, U>, C> for FramedNewService<S, T, U, C> where C: Clone, - S: NewService<C, Request = Request<U>, Response = Response<U>> + Clone, + S: NewService<Request<U>, C, Response = Response<U>> + Clone, S::Error: 'static, - <S::Service as Service>::Future: 'static, + <S::Service as Service<Request<U>>>::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, <U as Encoder>::Item: 'static, <U as Encoder>::Error: std::fmt::Debug, { - type Request = Framed<T, U>; type Response = FramedTransport<S::Service, T, U>; type Error = S::InitError; type InitError = S::InitError; @@ -98,18 +97,17 @@ where } } -impl<S, T, U, C> Service for FramedService<S, T, U, C> +impl<S, T, U, C> Service<Framed<T, U>> for FramedService<S, T, U, C> where - S: NewService<C, Request = Request<U>, Response = Response<U>>, + S: NewService<Request<U>, C, Response = Response<U>>, S::Error: 'static, - <S::Service as Service>::Future: 'static, + <S::Service as Service<Request<U>>>::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, <U as Encoder>::Item: 'static, <U as Encoder>::Error: std::fmt::Debug, C: Clone, { - type Request = Framed<T, U>; type Response = FramedTransport<S::Service, T, U>; type Error = S::InitError; type Future = FramedServiceResponseFuture<S, T, U, C>; @@ -129,9 +127,9 @@ where #[doc(hidden)] pub struct FramedServiceResponseFuture<S, T, U, C> where - S: NewService<C, Request = Request<U>, Response = Response<U>>, + S: NewService<Request<U>, C, Response = Response<U>>, S::Error: 'static, - <S::Service as Service>::Future: 'static, + <S::Service as Service<Request<U>>>::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, <U as Encoder>::Item: 'static, @@ -143,9 +141,9 @@ where impl<S, T, U, C> Future for FramedServiceResponseFuture<S, T, U, C> where - S: NewService<C, Request = Request<U>, Response = Response<U>>, + S: NewService<Request<U>, C, Response = Response<U>>, S::Error: 'static, - <S::Service as Service>::Future: 'static, + <S::Service as Service<Request<U>>>::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, <U as Encoder>::Item: 'static, @@ -182,7 +180,7 @@ impl<E, U: Encoder + Decoder> From<E> for FramedTransportError<E, U> { /// and pass then to the service. pub struct FramedTransport<S, T, U> where - S: Service<Request = Request<U>, Response = Response<U>>, + S: Service<Request<U>, Response = Response<U>>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -196,7 +194,7 @@ where inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>, } -enum TransportState<S: Service, U: Encoder + Decoder> { +enum TransportState<S: Service<Request<U>>, U: Encoder + Decoder> { Processing, Error(FramedTransportError<S::Error, U>), FramedError(FramedTransportError<S::Error, U>), @@ -210,7 +208,7 @@ struct FramedTransportInner<I, E> { impl<S, T, U> FramedTransport<S, T, U> where - S: Service<Request = Request<U>, Response = Response<U>>, + S: Service<Request<U>, Response = Response<U>>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -302,7 +300,7 @@ where impl<S, T, U> FramedTransport<S, T, U> where - S: Service<Request = Request<U>, Response = Response<U>>, + S: Service<Request<U>, Response = Response<U>>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -310,7 +308,7 @@ where <U as Encoder>::Item: 'static, <U as Encoder>::Error: std::fmt::Debug, { - pub fn new<F: IntoService<S>>(framed: Framed<T, U>, service: F) -> Self { + pub fn new<F: IntoService<S, Request<U>>>(framed: Framed<T, U>, service: F) -> Self { FramedTransport { framed, service: service.into_service(), @@ -348,7 +346,7 @@ where impl<S, T, U> Future for FramedTransport<S, T, U> where - S: Service<Request = Request<U>, Response = Response<U>>, + S: Service<Request<U>, Response = Response<U>>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -408,13 +406,12 @@ where } } -impl<T, U, F> NewService<()> for IntoFramed<T, U, F> +impl<T, U, F> NewService<T, ()> for IntoFramed<T, U, F> where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, U: Encoder + Decoder, { - type Request = T; type Response = Framed<T, U>; type Error = (); type InitError = (); @@ -439,13 +436,12 @@ where _t: PhantomData<(T,)>, } -impl<T, U, F> Service for IntoFramedService<T, U, F> +impl<T, U, F> Service<T> for IntoFramedService<T, U, F> where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, U: Encoder + Decoder, { - type Request = T; type Response = Framed<T, U>; type Error = (); type Future = FutureResult<Self::Response, Self::Error>; 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<S: Service> Transform<S> for InFlight { - type Request = S::Request; +impl<S: Service<R>, R> Transform<R, S> for InFlight { type Response = S::Response; type Error = S::Error; type InitError = Void; @@ -51,14 +50,13 @@ impl<S> InFlightService<S> { } } -impl<T> Service for InFlightService<T> +impl<T, R> Service<R> for InFlightService<T> where - T: Service, + T: Service<R>, { - type Request = T::Request; type Response = T::Response; type Error = T::Error; - type Future = InFlightServiceResponse<T>; + type Future = InFlightServiceResponse<T, R>; 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<T: Service> { +pub struct InFlightServiceResponse<T: Service<R>, R> { fut: T::Future, _guard: CounterGuard, } -impl<T: Service> Future for InFlightServiceResponse<T> { +impl<T: Service<R>, R> Future for InFlightServiceResponse<T, R> { 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<Future<Item = (), Error = ()>>; 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<R, E, F> NewService<()> for KeepAlive<R, E, F> +impl<R, E, F> NewService<R, ()> for KeepAlive<R, E, F> where F: Fn() -> E + Clone, { - type Request = R; type Response = R; type Error = E; type InitError = Void; @@ -89,11 +88,10 @@ where } } -impl<R, E, F> Service for KeepAliveService<R, E, F> +impl<R, E, F> Service<R> for KeepAliveService<R, E, F> where F: Fn() -> E, { - type Request = R; type Response = R; type Error = E; type Future = FutureResult<R, E>; 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<S> { _t: PhantomData<S>, } -impl<S> InOrder<S> -where - S: Service, - S::Response: 'static, - S::Future: 'static, - S::Error: 'static, -{ - pub fn new() -> Self { +impl<S> InOrder<S> { + pub fn new<R>() -> Self + where + S: Service<R>, + S::Response: 'static, + S::Future: 'static, + S::Error: 'static, + { Self { _t: PhantomData } } - pub fn service(service: S) -> InOrderService<S> { + pub fn service<R>(service: S) -> InOrderService<S, R> + where + S: Service<R>, + S::Response: 'static, + S::Future: 'static, + S::Error: 'static, + { InOrderService::new(service) } } -impl<S> Default for InOrder<S> +impl<S, R> Transform<R, S> for InOrder<S> where - S: Service, + S: Service<R>, S::Response: 'static, S::Future: 'static, S::Error: 'static, { - fn default() -> Self { - Self::new() - } -} - -impl<S> Transform<S> for InOrder<S> -where - S: Service, - S::Response: 'static, - S::Future: 'static, - S::Error: 'static, -{ - type Request = S::Request; type Response = S::Response; type Error = InOrderError<S::Error>; type InitError = Void; - type Transform = InOrderService<S>; + type Transform = InOrderService<S, R>; type Future = FutureResult<Self::Transform, Self::InitError>; fn new_transform(&self, service: S) -> Self::Future { @@ -99,15 +92,15 @@ where } } -pub struct InOrderService<S: Service> { +pub struct InOrderService<S: Service<R>, R> { service: S, task: Rc<AtomicTask>, acks: VecDeque<Record<S::Response, S::Error>>, } -impl<S> InOrderService<S> +impl<S, R> InOrderService<S, R> where - S: Service, + S: Service<R>, S::Response: 'static, S::Future: 'static, S::Error: 'static, @@ -121,17 +114,16 @@ where } } -impl<S> Service for InOrderService<S> +impl<S, R> Service<R> for InOrderService<S, R> where - S: Service, + S: Service<R>, S::Response: 'static, S::Future: 'static, S::Error: 'static, { - type Request = S::Request; type Response = S::Response; type Error = InOrderError<S::Error>; - type Future = InOrderServiceResponse<S>; + type Future = InOrderServiceResponse<S, R>; 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<S: Service> { +pub struct InOrderServiceResponse<S: Service<R>, R> { rx: oneshot::Receiver<Result<S::Response, S::Error>>, } -impl<S: Service> Future for InOrderServiceResponse<S> { +impl<S: Service<R>, R> Future for InOrderServiceResponse<S, R> { type Item = S::Response; type Error = InOrderError<S::Error>; @@ -204,8 +196,7 @@ mod tests { struct Srv; - impl Service for Srv { - type Request = oneshot::Receiver<usize>; + impl Service<oneshot::Receiver<usize>> for Srv { type Response = usize; type Error = (); type Future = Box<Future<Item = usize, Error = ()>>; @@ -219,11 +210,11 @@ mod tests { } } - struct SrvPoll<S: Service> { + struct SrvPoll<S: Service<oneshot::Receiver<usize>>> { s: S, } - impl<S: Service> Future for SrvPoll<S> { + impl<S: Service<oneshot::Receiver<usize>>> Future for SrvPoll<S> { 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<S, T, E, C> StreamNewService<S, T, E, C> where C: Clone, S: IntoStream, - T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>, + T: NewService<Request<S>, C, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - <T::Service as Service>::Future: 'static, + <T::Service as Service<Request<S>>>::Future: 'static, { - pub fn new<F: IntoNewService<T, C>>(factory: F) -> Self { + pub fn new<F: IntoNewService<T, Request<S>, C>>(factory: F) -> Self { Self { factory: Rc::new(factory.into_new_service()), _t: PhantomData, @@ -60,16 +60,15 @@ impl<S, T, E, C> Clone for StreamNewService<S, T, E, C> { } } -impl<S, T, E, C> NewService<C> for StreamNewService<S, T, E, C> +impl<S, T, E, C> NewService<S, C> for StreamNewService<S, T, E, C> where C: Clone, S: IntoStream + 'static, - T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>, + T: NewService<Request<S>, C, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - <T::Service as Service>::Future: 'static, + <T::Service as Service<Request<S>>>::Future: 'static, { - type Request = S; type Response = (); type Error = E; type InitError = E; @@ -91,16 +90,15 @@ pub struct StreamService<S, T, E, C = ()> { _t: PhantomData<(S, E)>, } -impl<S, T, E, C> Service for StreamService<S, T, E, C> +impl<S, T, E, C> Service<S> for StreamService<S, T, E, C> where S: IntoStream + 'static, - T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>, + T: NewService<Request<S>, C, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - <T::Service as Service>::Future: 'static, + <T::Service as Service<Request<S>>>::Future: 'static, C: Clone, { - type Request = S; type Response = (); type Error = E; type Future = Box<Future<Item = (), Error = E>>; @@ -121,7 +119,7 @@ where pub struct StreamDispatcher<S, T> where S: IntoStream + 'static, - T: Service<Request = Request<S>, Response = ()> + 'static, + T: Service<Request<S>, Response = ()> + 'static, T::Future: 'static, { stream: S, @@ -133,13 +131,13 @@ where impl<S, T> StreamDispatcher<S, T> where S: Stream, - T: Service<Request = Request<S>, Response = ()>, + T: Service<Request<S>, Response = ()>, T::Future: 'static, { pub fn new<F1, F2>(stream: F1, service: F2) -> Self where F1: IntoStream<Stream = S, Item = S::Item, Error = S::Error>, - F2: IntoService<T>, + F2: IntoService<T, Request<S>>, { let (err_tx, err_rx) = mpsc::unbounded(); StreamDispatcher { @@ -154,7 +152,7 @@ where impl<S, T> Future for StreamDispatcher<S, T> where S: Stream, - T: Service<Request = Request<S>, Response = ()>, + T: Service<Request<S>, Response = ()>, T::Future: 'static, { type Item = (); @@ -232,8 +230,7 @@ impl<T> Clone for TakeItem<T> { } } -impl<T: Stream> NewService<()> for TakeItem<T> { - type Request = T; +impl<T: Stream> NewService<T, ()> for TakeItem<T> { type Response = (Option<T::Item>, T); type Error = T::Error; type InitError = (); @@ -256,8 +253,7 @@ impl<T> Clone for TakeItemService<T> { } } -impl<T: Stream> Service for TakeItemService<T> { - type Request = T; +impl<T: Stream> Service<T> for TakeItemService<T> { type Response = (Option<T::Item>, T); type Error = T::Error; type Future = TakeItemServiceResponse<T>; 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<Self::Response, Self::Error>; 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<E> Clone for Timeout<E> { } } -impl<S, E> Transform<S> for Timeout<E> +impl<S, R, E> Transform<R, S> for Timeout<E> where - S: Service, + S: Service<R>, { - type Request = S::Request; type Response = S::Response; type Error = TimeoutError<S::Error>; type InitError = E; @@ -112,20 +111,19 @@ impl<S> TimeoutService<S> { } } -impl<S> Service for TimeoutService<S> +impl<S, R> Service<R> for TimeoutService<S> where - S: Service, + S: Service<R>, { - type Request = S::Request; type Response = S::Response; type Error = TimeoutError<S::Error>; - type Future = TimeoutServiceResponse<S>; + type Future = TimeoutServiceResponse<S, R>; 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<T: Service> { +pub struct TimeoutServiceResponse<T: Service<R>, R> { fut: T::Future, sleep: Delay, } -impl<T> Future for TimeoutServiceResponse<T> +impl<T, R> Future for TimeoutServiceResponse<T, R> where - T: Service, + T: Service<R>, { type Item = T::Response; type Error = TimeoutError<T::Error>; @@ -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<Future<Item = (), Error = ()>>;