From d83bf9530467ff2c02bca23d973496796789771f Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 1 Feb 2019 19:53:13 -0800 Subject: [PATCH] Use associated type instead of generic for Service definition --- Cargo.toml | 3 +- actix-connector/CHANGES.md | 6 +- actix-connector/Cargo.toml | 7 ++- actix-connector/src/connector.rs | 17 +++--- actix-connector/src/resolver.rs | 3 +- actix-connector/src/ssl/openssl.rs | 13 ++-- actix-server/CHANGES.md | 4 +- actix-server/Cargo.toml | 5 +- actix-server/src/config.rs | 11 ++-- actix-server/src/services.rs | 20 +++--- actix-server/src/ssl/nativetls.rs | 6 +- actix-server/src/ssl/openssl.rs | 6 +- actix-server/src/ssl/rustls.rs | 6 +- actix-service/CHANGES.md | 25 ++++++++ actix-service/Cargo.toml | 4 +- actix-service/src/and_then.rs | 74 ++++++++++++----------- actix-service/src/and_then_apply.rs | 94 ++++++++++++++--------------- actix-service/src/apply.rs | 63 ++++++++++--------- actix-service/src/fn_service.rs | 10 +-- actix-service/src/from_err.rs | 41 +++++++------ actix-service/src/lib.rs | 93 +++++++++++++++------------- actix-service/src/map.rs | 51 ++++++++-------- actix-service/src/map_err.rs | 51 ++++++++-------- actix-service/src/map_init_err.rs | 23 +++---- actix-service/src/then.rs | 92 +++++++++++++++++----------- actix-test-server/Cargo.toml | 3 +- actix-utils/Cargo.toml | 5 +- actix-utils/src/cloneable.rs | 11 ++-- actix-utils/src/either.rs | 48 +++++++++------ actix-utils/src/framed.rs | 46 +++++++------- actix-utils/src/inflight.rs | 42 +++++++------ actix-utils/src/keepalive.rs | 6 +- actix-utils/src/stream.rs | 34 ++++++----- actix-utils/src/time.rs | 6 +- actix-utils/src/timeout.rs | 36 +++++------ 35 files changed, 544 insertions(+), 421 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index df052b1e..6d2ec983 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,8 @@ members = [ ] [dev-dependencies] -actix-service = "0.1.1" +#actix-service = "0.2.0" +actix-service = { path="actix-service" } actix-codec = "0.1.0" actix-rt = { path="actix-rt" } actix-server = { path="actix-server", features=["ssl"] } diff --git a/actix-connector/CHANGES.md b/actix-connector/CHANGES.md index 2dc2b008..0c6ec946 100644 --- a/actix-connector/CHANGES.md +++ b/actix-connector/CHANGES.md @@ -1,6 +1,10 @@ # Changes -## [0.1.2] - 2019-01-xx +## [0.2.0] - 2019-01-xx + +### Changes + +* Migrate to actix-service 0.2 * Upgrade trust-dns-resolver diff --git a/actix-connector/Cargo.toml b/actix-connector/Cargo.toml index 25d2d87e..148d640a 100644 --- a/actix-connector/Cargo.toml +++ b/actix-connector/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-connector" -version = "0.1.2" +version = "0.2.0" authors = ["Nikolay Kim "] description = "Actix Connector - tcp connector service" keywords = ["network", "framework", "async", "futures"] @@ -27,12 +27,13 @@ default = [] ssl = ["openssl", "tokio-openssl"] [dependencies] -actix-service = "0.1.6" +#actix-service = "0.1.6" +actix-service = { path="../actix-service" } actix-codec = "0.1.0" futures = "0.1" tokio-tcp = "0.1" tokio-current-thread = "0.1" -trust-dns-resolver = { version="0.11.0-alpha.1", default-features = false } +trust-dns-resolver = { version="0.11.0-alpha.2", default-features = false } # openssl openssl = { version="0.10", optional = true } diff --git a/actix-connector/src/connector.rs b/actix-connector/src/connector.rs index cebff826..802cca3b 100644 --- a/actix-connector/src/connector.rs +++ b/actix-connector/src/connector.rs @@ -167,8 +167,8 @@ impl Connector { /// Create new connector with custom resolver pub fn with_resolver( resolver: Resolver, - ) -> impl Service + Clone - { + ) -> impl Service + + Clone { Connector { resolver } } @@ -177,7 +177,7 @@ impl Connector { cfg: ResolverConfig, opts: ResolverOpts, ) -> impl NewService< - Connect, + Request = Connect, Response = (Connect, TcpStream), Error = ConnectorError, InitError = E, @@ -194,7 +194,8 @@ impl Clone for Connector { } } -impl Service for Connector { +impl Service for Connector { + type Request = Connect; type Response = (Connect, TcpStream); type Error = ConnectorError; type Future = Either; @@ -271,7 +272,8 @@ impl Default for TcpConnector { } } -impl Service<(T, VecDeque)> for TcpConnector { +impl Service for TcpConnector { + type Request = (T, VecDeque); type Response = (T, TcpStream); type Error = io::Error; type Future = TcpConnectorResponse; @@ -317,7 +319,7 @@ impl Future for TcpConnectorResponse { if let Some(new) = self.stream.as_mut() { match new.poll() { Ok(Async::Ready(sock)) => { - return Ok(Async::Ready((self.req.take().unwrap(), sock))) + return Ok(Async::Ready((self.req.take().unwrap(), sock))); } Ok(Async::NotReady) => return Ok(Async::NotReady), Err(err) => { @@ -351,7 +353,8 @@ impl DefaultConnector { } } -impl Service for DefaultConnector { +impl Service for DefaultConnector { + type Request = Connect; type Response = TcpStream; type Error = ConnectorError; type Future = DefaultConnectorFuture; diff --git a/actix-connector/src/resolver.rs b/actix-connector/src/resolver.rs index d6ee2612..443fdd75 100644 --- a/actix-connector/src/resolver.rs +++ b/actix-connector/src/resolver.rs @@ -67,7 +67,8 @@ impl Clone for Resolver { } } -impl Service for Resolver { +impl Service for Resolver { + type Request = T; type Response = (T, VecDeque); type Error = ResolveError; type Future = ResolverFuture; diff --git a/actix-connector/src/ssl/openssl.rs b/actix-connector/src/ssl/openssl.rs index 71292d0e..43cd4fb4 100644 --- a/actix-connector/src/ssl/openssl.rs +++ b/actix-connector/src/ssl/openssl.rs @@ -26,7 +26,8 @@ impl OpensslConnector { impl OpensslConnector { pub fn service( connector: SslConnector, - ) -> impl Service<(R, T), Response = (R, SslStream), Error = HandshakeError> { + ) -> impl Service), Error = HandshakeError> + { OpensslConnectorService { connector: connector, _t: PhantomData, @@ -43,9 +44,8 @@ impl Clone for OpensslConnector { } } -impl NewService<(R, T)> - for OpensslConnector -{ +impl NewService for OpensslConnector { + type Request = (R, T); type Response = (R, SslStream); type Error = HandshakeError; type Service = OpensslConnectorService; @@ -65,9 +65,8 @@ pub struct OpensslConnectorService { _t: PhantomData<(R, T)>, } -impl Service<(R, T)> - for OpensslConnectorService -{ +impl Service for OpensslConnectorService { + type Request = (R, T); type Response = (R, SslStream); type Error = HandshakeError; type Future = ConnectAsyncExt; diff --git a/actix-server/CHANGES.md b/actix-server/CHANGES.md index 49016875..ace21e23 100644 --- a/actix-server/CHANGES.md +++ b/actix-server/CHANGES.md @@ -1,9 +1,11 @@ # Changes -## [0.1.4] - 2018-12-xx +## [0.2.0] - 2019-02-xx ## Changes +* Migrate to actix-service 0.2 + * Updated rustls dependency diff --git a/actix-server/Cargo.toml b/actix-server/Cargo.toml index d14f31ec..14505848 100644 --- a/actix-server/Cargo.toml +++ b/actix-server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-server" -version = "0.1.4" +version = "0.2.0" authors = ["Nikolay Kim "] description = "Actix server - General purpose tcp server" keywords = ["network", "framework", "async", "futures"] @@ -33,7 +33,8 @@ ssl = ["openssl", "tokio-openssl"] rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"] [dependencies] -actix-service = "0.1.1" +#actix-service = "0.2.0" +actix-service = { path="../actix-service" } actix-rt = "0.1.0" log = "0.4" diff --git a/actix-server/src/config.rs b/actix-server/src/config.rs index 603dadff..fdf5646d 100644 --- a/actix-server/src/config.rs +++ b/actix-server/src/config.rs @@ -169,8 +169,8 @@ impl ServiceRuntime { pub fn service(&mut self, name: &str, service: F) where - F: IntoNewService, - T: NewService + 'static, + F: IntoNewService, + T: NewService + 'static, T::Future: 'static, T::Service: 'static, T::InitError: fmt::Debug, @@ -191,7 +191,7 @@ impl ServiceRuntime { type BoxedNewService = Box< NewService< - (Option, ServerMessage), + Request = (Option, ServerMessage), Response = (), Error = (), InitError = (), @@ -204,14 +204,15 @@ struct ServiceFactory { inner: T, } -impl NewService<(Option, ServerMessage)> for ServiceFactory +impl NewService for ServiceFactory where - T: NewService, + T: NewService, T::Future: 'static, T::Service: 'static, T::Error: 'static, T::InitError: fmt::Debug + 'static, { + type Request = (Option, ServerMessage); type Response = (); type Error = (); type InitError = (); diff --git a/actix-server/src/services.rs b/actix-server/src/services.rs index 27c6fd8e..7152185a 100644 --- a/actix-server/src/services.rs +++ b/actix-server/src/services.rs @@ -23,13 +23,13 @@ pub enum ServerMessage { } pub trait StreamServiceFactory: Send + Clone + 'static { - type NewService: NewService; + type NewService: NewService; fn create(&self) -> Self::NewService; } pub trait ServiceFactory: Send + Clone + 'static { - type NewService: NewService; + type NewService: NewService; fn create(&self) -> Self::NewService; } @@ -44,7 +44,7 @@ pub(crate) trait InternalServiceFactory: Send { pub(crate) type BoxedServerService = Box< Service< - (Option, ServerMessage), + Request = (Option, ServerMessage), Response = (), Error = (), Future = FutureResult<(), ()>, @@ -61,12 +61,13 @@ impl StreamService { } } -impl Service<(Option, ServerMessage)> for StreamService +impl Service for StreamService where - T: Service, + T: Service, T::Future: 'static, T::Error: 'static, { + type Request = (Option, ServerMessage); type Response = (); type Error = (); type Future = FutureResult<(), ()>; @@ -107,12 +108,13 @@ impl ServerService { } } -impl Service<(Option, ServerMessage)> for ServerService +impl Service for ServerService where - T: Service, + T: Service, T::Future: 'static, T::Error: 'static, { + type Request = (Option, ServerMessage); type Response = (); type Error = (); type Future = FutureResult<(), ()>; @@ -239,7 +241,7 @@ impl InternalServiceFactory for Box { impl ServiceFactory for F where F: Fn() -> T + Send + Clone + 'static, - T: NewService, + T: NewService, { type NewService = T; @@ -251,7 +253,7 @@ where impl StreamServiceFactory for F where F: Fn() -> T + Send + Clone + 'static, - T: NewService, + T: NewService, { type NewService = T; diff --git a/actix-server/src/ssl/nativetls.rs b/actix-server/src/ssl/nativetls.rs index 76527bb3..88032ed0 100644 --- a/actix-server/src/ssl/nativetls.rs +++ b/actix-server/src/ssl/nativetls.rs @@ -36,7 +36,8 @@ impl Clone for NativeTlsAcceptor { } } -impl NewService for NativeTlsAcceptor { +impl NewService for NativeTlsAcceptor { + type Request = T; type Response = TlsStream; type Error = Error; type Service = NativeTlsAcceptorService; @@ -60,7 +61,8 @@ pub struct NativeTlsAcceptorService { conns: Counter, } -impl Service for NativeTlsAcceptorService { +impl Service for NativeTlsAcceptorService { + type Request = T; type Response = TlsStream; type Error = Error; type Future = Accept; diff --git a/actix-server/src/ssl/openssl.rs b/actix-server/src/ssl/openssl.rs index e45152e7..7f0bc782 100644 --- a/actix-server/src/ssl/openssl.rs +++ b/actix-server/src/ssl/openssl.rs @@ -36,7 +36,8 @@ impl Clone for OpensslAcceptor { } } -impl NewService for OpensslAcceptor { +impl NewService for OpensslAcceptor { + type Request = T; type Response = SslStream; type Error = HandshakeError; type Service = OpensslAcceptorService; @@ -60,7 +61,8 @@ pub struct OpensslAcceptorService { conns: Counter, } -impl Service for OpensslAcceptorService { +impl Service for OpensslAcceptorService { + type Request = T; type Response = SslStream; type Error = HandshakeError; type Future = OpensslAcceptorServiceFut; diff --git a/actix-server/src/ssl/rustls.rs b/actix-server/src/ssl/rustls.rs index 325e5dde..495ae4a3 100644 --- a/actix-server/src/ssl/rustls.rs +++ b/actix-server/src/ssl/rustls.rs @@ -38,7 +38,8 @@ impl Clone for RustlsAcceptor { } } -impl NewService for RustlsAcceptor { +impl NewService for RustlsAcceptor { + type Request = T; type Response = TlsStream; type Error = io::Error; type Service = RustlsAcceptorService; @@ -62,7 +63,8 @@ pub struct RustlsAcceptorService { conns: Counter, } -impl Service for RustlsAcceptorService { +impl Service for RustlsAcceptorService { + type Request = T; type Response = TlsStream; type Error = io::Error; type Future = RustlsAcceptorServiceFut; diff --git a/actix-service/CHANGES.md b/actix-service/CHANGES.md index ec911012..720d1485 100644 --- a/actix-service/CHANGES.md +++ b/actix-service/CHANGES.md @@ -1,5 +1,30 @@ # Changes +## [0.2.0] - 2019-02-01 + +### Changed + +* Use associated type instead of generic for Service definition. + + * Before: + + ```rust + impl Service for Client { + type Response = Response; + // ... + } + ``` + * After: + + ```rust + impl Service for Client { + type Request = Request; + type Response = Response; + // ... + } + ``` + + ## [0.1.6] - 2019-01-24 ### Changed diff --git a/actix-service/Cargo.toml b/actix-service/Cargo.toml index 9579e2b5..b5a80edd 100644 --- a/actix-service/Cargo.toml +++ b/actix-service/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-service" -version = "0.1.6" +version = "0.2.0" authors = ["Nikolay Kim "] description = "Actix Service" keywords = ["network", "framework", "async", "futures"] @@ -15,7 +15,7 @@ workspace = "../" [badges] travis-ci = { repository = "actix/actix-service", branch = "master" } -# appveyor = { repository = "fafhrd91/actix-web-hdy9d" } +appveyor = { repository = "actix/actix-net" } codecov = { repository = "actix/actix-service", branch = "master", service = "github" } [lib] diff --git a/actix-service/src/and_then.rs b/actix-service/src/and_then.rs index b2df9f7b..2ef58bfc 100644 --- a/actix-service/src/and_then.rs +++ b/actix-service/src/and_then.rs @@ -14,10 +14,10 @@ pub struct AndThen { impl AndThen { /// Create new `AndThen` combinator - pub fn new(a: A, b: B) -> Self + pub fn new(a: A, b: B) -> Self where - A: Service, - B: Service, + A: Service, + B: Service, { Self { a, b: Cell::new(b) } } @@ -35,39 +35,40 @@ where } } -impl Service for AndThen +impl Service for AndThen where - A: Service, - B: Service, + A: Service, + B: Service, { + type Request = A::Request; type Response = B::Response; type Error = A::Error; - type Future = AndThenFuture; + type Future = AndThenFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { try_ready!(self.a.poll_ready()); self.b.get_mut().poll_ready() } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { AndThenFuture::new(self.a.call(req), self.b.clone()) } } -pub struct AndThenFuture +pub struct AndThenFuture where - A: Service, - B: Service, + A: Service, + B: Service, { b: Cell, fut_b: Option, fut_a: Option, } -impl AndThenFuture +impl AndThenFuture where - A: Service, - B: Service, + A: Service, + B: Service, { fn new(a: A::Future, b: Cell) -> Self { AndThenFuture { @@ -78,10 +79,10 @@ where } } -impl Future for AndThenFuture +impl Future for AndThenFuture where - A: Service, - B: Service, + A: Service, + B: Service, { type Item = B::Response; type Error = A::Error; @@ -111,10 +112,10 @@ pub struct AndThenNewService { impl AndThenNewService { /// Create new `AndThen` combinator - pub fn new>(a: A, f: F) -> Self + pub fn new>(a: A, f: F) -> Self where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { Self { a, @@ -123,17 +124,18 @@ impl AndThenNewService { } } -impl NewService for AndThenNewService +impl NewService for AndThenNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { + type Request = A::Request; type Response = B::Response; type Error = A::Error; type Service = AndThen; type InitError = A::InitError; - type Future = AndThenNewServiceFuture; + type Future = AndThenNewServiceFuture; fn new_service(&self) -> Self::Future { AndThenNewServiceFuture::new(self.a.new_service(), self.b.new_service()) @@ -153,10 +155,10 @@ where } } -pub struct AndThenNewServiceFuture +pub struct AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { fut_b: B::Future, fut_a: A::Future, @@ -164,10 +166,10 @@ where b: Option, } -impl AndThenNewServiceFuture +impl AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { AndThenNewServiceFuture { @@ -179,10 +181,10 @@ where } } -impl Future for AndThenNewServiceFuture +impl Future for AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { type Item = AndThen; type Error = A::InitError; @@ -222,7 +224,8 @@ mod tests { use crate::{NewService, Service, ServiceExt}; struct Srv1(Rc>); - impl Service<&'static str> for Srv1 { + impl Service for Srv1 { + type Request = &'static str; type Response = &'static str; type Error = (); type Future = FutureResult; @@ -240,7 +243,8 @@ mod tests { #[derive(Clone)] struct Srv2(Rc>); - impl Service<&'static str> for Srv2 { + impl Service for Srv2 { + type Request = &'static str; type Response = (&'static str, &'static str); type Error = (); type Future = FutureResult; diff --git a/actix-service/src/and_then_apply.rs b/actix-service/src/and_then_apply.rs index 26db80f5..48ba8d52 100644 --- a/actix-service/src/and_then_apply.rs +++ b/actix-service/src/and_then_apply.rs @@ -6,10 +6,10 @@ use super::{IntoNewService, IntoService, NewService, Service}; use crate::cell::Cell; /// `Apply` service combinator -pub struct AndThenApply +pub struct AndThenApply where - A: Service, - B: Service, + A: Service, + B: Service, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, @@ -17,19 +17,19 @@ where a: A, b: Cell, f: Cell, - r: PhantomData<(Out, Req1, Req2)>, + r: PhantomData<(Out,)>, } -impl AndThenApply +impl AndThenApply where - A: Service, - B: Service, + A: Service, + B: Service, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, { /// Create new `Apply` combinator - pub fn new, B1: IntoService>(a: A1, b: B1, f: F) -> Self { + pub fn new, B1: IntoService>(a: A1, b: B1, f: F) -> Self { Self { f: Cell::new(f), a: a.into_service(), @@ -39,10 +39,10 @@ where } } -impl Clone for AndThenApply +impl Clone for AndThenApply where - A: Service + Clone, - B: Service, + A: Service + Clone, + B: Service, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, @@ -57,38 +57,38 @@ where } } -impl Service for AndThenApply +impl Service for AndThenApply where - A: Service, - B: Service, + A: Service, + B: Service, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, { + type Request = A::Request; type Response = Out::Item; type Error = A::Error; - type Future = AndThenApplyFuture; + type Future = AndThenApplyFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { try_ready!(self.a.poll_ready()); self.b.get_mut().poll_ready().map_err(|e| e.into()) } - fn call(&mut self, req: Req1) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { AndThenApplyFuture { b: self.b.clone(), f: self.f.clone(), fut_b: None, fut_a: Some(self.a.call(req)), - _t: PhantomData, } } } -pub struct AndThenApplyFuture +pub struct AndThenApplyFuture where - A: Service, - B: Service, + A: Service, + B: Service, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, @@ -97,13 +97,12 @@ where f: Cell, fut_a: Option, fut_b: Option, - _t: PhantomData, } -impl Future for AndThenApplyFuture +impl Future for AndThenApplyFuture where - A: Service, - B: Service, + A: Service, + B: Service, F: FnMut(A::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, @@ -130,27 +129,23 @@ where } /// `ApplyNewService` new service combinator -pub struct AndThenApplyNewService { +pub struct AndThenApplyNewService { a: A, b: B, f: Cell, - r: PhantomData<(Out, Req1, Req2)>, + r: PhantomData<(Out)>, } -impl AndThenApplyNewService +impl AndThenApplyNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, { /// Create new `ApplyNewService` new service instance - pub fn new, B1: IntoNewService>( - a: A1, - b: B1, - f: F, - ) -> Self { + pub fn new, B1: IntoNewService>(a: A1, b: B1, f: F) -> Self { Self { f: Cell::new(f), a: a.into_new_service(), @@ -160,7 +155,7 @@ where } } -impl Clone for AndThenApplyNewService +impl Clone for AndThenApplyNewService where A: Clone, B: Clone, @@ -175,21 +170,21 @@ where } } -impl NewService - for AndThenApplyNewService +impl NewService for AndThenApplyNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, { + type Request = A::Request; type Response = Out::Item; type Error = A::Error; type InitError = A::InitError; - type Service = AndThenApply; - type Future = AndThenApplyNewServiceFuture; + type Service = AndThenApply; + type Future = AndThenApplyNewServiceFuture; fn new_service(&self) -> Self::Future { AndThenApplyNewServiceFuture { @@ -202,10 +197,10 @@ where } } -pub struct AndThenApplyNewServiceFuture +pub struct AndThenApplyNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, @@ -217,15 +212,15 @@ where b: Option, } -impl Future for AndThenApplyNewServiceFuture +impl Future for AndThenApplyNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, { - type Item = AndThenApply; + type Item = AndThenApply; type Error = A::InitError; fn poll(&mut self) -> Poll { @@ -263,7 +258,8 @@ mod tests { #[derive(Clone)] struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index 8a73c19e..eb31acec 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -5,24 +5,24 @@ use futures::{Async, Future, IntoFuture, Poll}; use super::{IntoNewService, IntoService, NewService, Service}; /// `Apply` service combinator -pub struct Apply +pub struct Apply where - T: Service, + T: Service, { service: T, f: F, - r: PhantomData<(In, Out, Request)>, + r: PhantomData<(In, Out)>, } -impl Apply +impl Apply where - T: Service, + T: Service, F: FnMut(In, &mut T) -> Out, Out: IntoFuture, Out::Error: From, { /// Create new `Apply` combinator - pub fn new>(service: I, f: F) -> Self { + pub fn new>(service: I, f: F) -> Self { Self { service: service.into_service(), f, @@ -31,9 +31,9 @@ where } } -impl Clone for Apply +impl Clone for Apply where - T: Service + Clone, + T: Service + Clone, F: Clone, { fn clone(&self) -> Self { @@ -45,13 +45,14 @@ where } } -impl Service for Apply +impl Service for Apply where - T: Service, + T: Service, F: FnMut(In, &mut T) -> Out, Out: IntoFuture, Out::Error: From, { + type Request = In; type Response = Out::Item; type Error = Out::Error; type Future = Out::Future; @@ -66,24 +67,24 @@ where } /// `ApplyNewService` new service combinator -pub struct ApplyNewService +pub struct ApplyNewService where - T: NewService, + T: NewService, { service: T, f: F, - r: PhantomData<(In, Out, Request)>, + r: PhantomData<(In, Out)>, } -impl ApplyNewService +impl ApplyNewService where - T: NewService, + T: NewService, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From, { /// Create new `ApplyNewService` new service instance - pub fn new>(service: F1, f: F) -> Self { + pub fn new>(service: F1, f: F) -> Self { Self { f, service: service.into_new_service(), @@ -92,9 +93,9 @@ where } } -impl Clone for ApplyNewService +impl Clone for ApplyNewService where - T: NewService + Clone, + T: NewService + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, { @@ -107,28 +108,29 @@ where } } -impl NewService for ApplyNewService +impl NewService for ApplyNewService where - T: NewService, + T: NewService, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From, { + type Request = In; type Response = Out::Item; type Error = Out::Error; - type Service = Apply; + type Service = Apply; type InitError = T::InitError; - type Future = ApplyNewServiceFuture; + type Future = ApplyNewServiceFuture; fn new_service(&self) -> Self::Future { ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone()) } } -pub struct ApplyNewServiceFuture +pub struct ApplyNewServiceFuture where - T: NewService, + T: NewService, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, { @@ -137,9 +139,9 @@ where r: PhantomData<(In, Out)>, } -impl ApplyNewServiceFuture +impl ApplyNewServiceFuture where - T: NewService, + T: NewService, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, { @@ -152,14 +154,14 @@ where } } -impl Future for ApplyNewServiceFuture +impl Future for ApplyNewServiceFuture where - T: NewService, + T: NewService, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From, { - type Item = Apply; + type Item = Apply; type Error = T::InitError; fn poll(&mut self) -> Poll { @@ -180,7 +182,8 @@ mod tests { #[derive(Clone)] struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 0726ca78..88ed8917 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -42,11 +42,12 @@ where } } -impl Service for FnService +impl Service for FnService where F: FnMut(Req) -> Fut, Fut: IntoFuture, { + type Request = Req; type Response = Resp; type Error = E; type Future = Fut::Future; @@ -60,7 +61,7 @@ where } } -impl IntoService, Req> for F +impl IntoService> for F where F: FnMut(Req) -> Fut + 'static, Fut: IntoFuture, @@ -92,11 +93,12 @@ where } } -impl NewService for FnNewService +impl NewService for FnNewService where F: FnMut(Req) -> Fut + Clone, Fut: IntoFuture, { + type Request = Req; type Response = Resp; type Error = Err; type Service = FnService; @@ -108,7 +110,7 @@ where } } -impl IntoNewService, Req> for F +impl IntoNewService> for F where F: FnMut(Req) -> Fut + Clone + 'static, Fut: IntoFuture, diff --git a/actix-service/src/from_err.rs b/actix-service/src/from_err.rs index 9d624452..7731e275 100644 --- a/actix-service/src/from_err.rs +++ b/actix-service/src/from_err.rs @@ -13,9 +13,9 @@ pub struct FromErr { } impl FromErr { - pub(crate) fn new(service: A) -> Self + pub(crate) fn new(service: A) -> Self where - A: Service, + A: Service, E: From, { FromErr { @@ -37,20 +37,21 @@ where } } -impl Service for FromErr +impl Service for FromErr where - A: Service, + A: Service, E: From, { + type Request = A::Request; type Response = A::Response; type Error = E; - type Future = FromErrFuture; + type Future = FromErrFuture; fn poll_ready(&mut self) -> Poll<(), E> { self.service.poll_ready().map_err(E::from) } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { FromErrFuture { fut: self.service.call(req), f: PhantomData, @@ -58,14 +59,14 @@ where } } -pub struct FromErrFuture, E, Request> { +pub struct FromErrFuture { fut: A::Future, f: PhantomData, } -impl Future for FromErrFuture +impl Future for FromErrFuture where - A: Service, + A: Service, E: From, { type Item = A::Response; @@ -87,9 +88,9 @@ pub struct FromErrNewService { impl FromErrNewService { /// Create new `FromErr` new service instance - pub fn new(a: A) -> Self + pub fn new(a: A) -> Self where - A: NewService, + A: NewService, E: From, { Self { a, e: PhantomData } @@ -108,17 +109,18 @@ where } } -impl NewService for FromErrNewService +impl NewService for FromErrNewService where - A: NewService, + A: NewService, E: From, { + type Request = A::Request; type Response = A::Response; type Error = E; type Service = FromErr; type InitError = A::InitError; - type Future = FromErrNewServiceFuture; + type Future = FromErrNewServiceFuture; fn new_service(&self) -> Self::Future { FromErrNewServiceFuture { @@ -128,18 +130,18 @@ where } } -pub struct FromErrNewServiceFuture +pub struct FromErrNewServiceFuture where - A: NewService, + A: NewService, E: From, { fut: A::Future, e: PhantomData, } -impl Future for FromErrNewServiceFuture +impl Future for FromErrNewServiceFuture where - A: NewService, + A: NewService, E: From, { type Item = FromErr; @@ -162,7 +164,8 @@ mod tests { use crate::{IntoNewService, NewService, Service, ServiceExt}; struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index 4de01abb..017d5fdd 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -22,7 +22,10 @@ pub use self::map_init_err::MapInitErr; pub use self::then::{Then, ThenNewService}; /// An asynchronous function from `Request` to a `Response`. -pub trait Service { +pub trait Service { + /// Requests handled by the service. + type Request; + /// Responses given by the service. type Response; @@ -52,23 +55,19 @@ pub trait Service { /// /// Calling `call` without calling `poll_ready` is permitted. The /// implementation must be resilient to this fact. - fn call(&mut self, req: Request) -> Self::Future; + fn call(&mut self, req: Self::Request) -> Self::Future; } /// An extension trait for `Service`s that provides a variety of convenient /// adapters -pub trait ServiceExt: Service { +pub trait ServiceExt: Service { /// Apply function to specified service and use it as a next service in /// chain. - fn apply( - self, - service: I, - f: F, - ) -> AndThenApply + fn apply(self, service: I, f: F) -> AndThenApply where Self: Sized, - B: Service, - I: IntoService, + B: Service, + I: IntoService, F: FnMut(Self::Response, &mut B) -> Out, Out: IntoFuture, Out::Error: Into, @@ -88,8 +87,8 @@ pub trait ServiceExt: Service { fn and_then(self, service: F) -> AndThen where Self: Sized, - F: IntoService, - B: Service, + F: IntoService, + B: Service, { AndThen::new(self, service.into_service()) } @@ -115,7 +114,7 @@ pub trait ServiceExt: Service { fn then(self, service: B) -> Then where Self: Sized, - B: Service, Error = Self::Error>, + B: Service, Error = Self::Error>, { Then::new(self, service) } @@ -154,7 +153,7 @@ pub trait ServiceExt: Service { } } -impl ServiceExt for T where T: Service {} +impl ServiceExt for T where T: Service {} /// Creates new `Service` values. /// @@ -163,9 +162,10 @@ impl ServiceExt for T where T: Service {} /// accepts new TCP streams, obtains a new `Service` value using the /// `NewService` trait, and uses that new `Service` value to process inbound /// requests on that new TCP stream. -/// -/// Request - request handled by the service -pub trait NewService { +pub trait NewService { + /// Requests handled by the service. + type Request; + /// Responses given by the service type Response; @@ -173,7 +173,11 @@ pub trait NewService { type Error; /// The `Service` value created by this factory - type Service: Service; + type Service: Service< + Request = Self::Request, + Response = Self::Response, + Error = Self::Error, + >; /// Errors produced while building a service. type InitError; @@ -190,11 +194,11 @@ pub trait NewService { self, service: I, f: F, - ) -> AndThenApplyNewService + ) -> AndThenApplyNewService where Self: Sized, - B: NewService, - I: IntoNewService, + B: NewService, + I: IntoNewService, F: FnMut(Self::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, @@ -206,8 +210,12 @@ pub trait NewService { fn and_then(self, new_service: F) -> AndThenNewService where Self: Sized, - F: IntoNewService, - B: NewService, + F: IntoNewService, + B: NewService< + Request = Self::Response, + Error = Self::Error, + InitError = Self::InitError, + >, { AndThenNewService::new(self, new_service) } @@ -235,9 +243,9 @@ pub trait NewService { fn then(self, new_service: F) -> ThenNewService where Self: Sized, - F: IntoNewService>, + F: IntoNewService, B: NewService< - Result, + Request = Result, Error = Self::Error, InitError = Self::InitError, >, @@ -274,10 +282,11 @@ pub trait NewService { } } -impl<'a, S, Request> Service for &'a mut S +impl<'a, S> Service for &'a mut S where - S: Service + 'a, + S: Service + 'a, { + type Request = S::Request; type Response = S::Response; type Error = S::Error; type Future = S::Future; @@ -286,15 +295,16 @@ where (**self).poll_ready() } - fn call(&mut self, request: Request) -> S::Future { + fn call(&mut self, request: Self::Request) -> S::Future { (**self).call(request) } } -impl Service for Box +impl Service for Box where - S: Service + ?Sized, + S: Service + ?Sized, { + type Request = S::Request; type Response = S::Response; type Error = S::Error; type Future = S::Future; @@ -303,17 +313,18 @@ where (**self).poll_ready() } - fn call(&mut self, request: Request) -> S::Future { + fn call(&mut self, request: Self::Request) -> S::Future { (**self).call(request) } } -impl NewService for F +impl NewService for F where F: Fn() -> R, R: IntoFuture, - S: Service, + S: Service, { + type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S; @@ -326,35 +337,35 @@ where } /// Trait for types that can be converted to a `Service` -pub trait IntoService +pub trait IntoService where - T: Service, + T: Service, { /// Convert to a `Service` fn into_service(self) -> T; } /// Trait for types that can be converted to a Service -pub trait IntoNewService +pub trait IntoNewService where - T: NewService, + T: NewService, { /// Convert to an `NewService` fn into_new_service(self) -> T; } -impl IntoService for T +impl IntoService for T where - T: Service, + T: Service, { fn into_service(self) -> T { self } } -impl IntoNewService for T +impl IntoNewService for T where - T: NewService, + T: NewService, { fn into_new_service(self) -> T { self diff --git a/actix-service/src/map.rs b/actix-service/src/map.rs index ac255ff1..7055448d 100644 --- a/actix-service/src/map.rs +++ b/actix-service/src/map.rs @@ -15,9 +15,9 @@ pub struct Map { impl Map { /// Create new `Map` combinator - pub fn new(service: A, f: F) -> Self + pub fn new(service: A, f: F) -> Self where - A: Service, + A: Service, F: FnMut(A::Response) -> Response, { Self { @@ -42,36 +42,37 @@ where } } -impl Service for Map +impl Service for Map where - A: Service, + A: Service, F: FnMut(A::Response) -> Response + Clone, { + type Request = A::Request; type Response = Response; type Error = A::Error; - type Future = MapFuture; + type Future = MapFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready() } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { MapFuture::new(self.service.call(req), self.f.clone()) } } -pub struct MapFuture +pub struct MapFuture where - A: Service, + A: Service, F: FnMut(A::Response) -> Response, { f: F, fut: A::Future, } -impl MapFuture +impl MapFuture where - A: Service, + A: Service, F: FnMut(A::Response) -> Response, { fn new(fut: A::Future, f: F) -> Self { @@ -79,9 +80,9 @@ where } } -impl Future for MapFuture +impl Future for MapFuture where - A: Service, + A: Service, F: FnMut(A::Response) -> Response, { type Item = Response; @@ -104,9 +105,9 @@ pub struct MapNewService { impl MapNewService { /// Create new `Map` new service instance - pub fn new(a: A, f: F) -> Self + pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Response, { Self { @@ -131,35 +132,36 @@ where } } -impl NewService for MapNewService +impl NewService for MapNewService where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Response + Clone, { + type Request = A::Request; type Response = Response; type Error = A::Error; type Service = Map; type InitError = A::InitError; - type Future = MapNewServiceFuture; + type Future = MapNewServiceFuture; fn new_service(&self) -> Self::Future { MapNewServiceFuture::new(self.a.new_service(), self.f.clone()) } } -pub struct MapNewServiceFuture +pub struct MapNewServiceFuture where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Response, { fut: A::Future, f: Option, } -impl MapNewServiceFuture +impl MapNewServiceFuture where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Response, { fn new(fut: A::Future, f: F) -> Self { @@ -167,9 +169,9 @@ where } } -impl Future for MapNewServiceFuture +impl Future for MapNewServiceFuture where - A: NewService, + A: NewService, F: FnMut(A::Response) -> Response, { type Item = Map; @@ -192,7 +194,8 @@ mod tests { use crate::{IntoNewService, Service, ServiceExt}; struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index 63089e95..9a9dccb7 100644 --- a/actix-service/src/map_err.rs +++ b/actix-service/src/map_err.rs @@ -16,9 +16,9 @@ pub struct MapErr { impl MapErr { /// Create new `MapErr` combinator - pub fn new(service: A, f: F) -> Self + pub fn new(service: A, f: F) -> Self where - A: Service, + A: Service, F: Fn(A::Error) -> E, { Self { @@ -43,36 +43,37 @@ where } } -impl Service for MapErr +impl Service for MapErr where - A: Service, + A: Service, F: Fn(A::Error) -> E + Clone, { + type Request = A::Request; type Response = A::Response; type Error = E; - type Future = MapErrFuture; + type Future = MapErrFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready().map_err(&self.f) } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { MapErrFuture::new(self.service.call(req), self.f.clone()) } } -pub struct MapErrFuture +pub struct MapErrFuture where - A: Service, + A: Service, F: Fn(A::Error) -> E, { f: F, fut: A::Future, } -impl MapErrFuture +impl MapErrFuture where - A: Service, + A: Service, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -80,9 +81,9 @@ where } } -impl Future for MapErrFuture +impl Future for MapErrFuture where - A: Service, + A: Service, F: Fn(A::Error) -> E, { type Item = A::Response; @@ -105,9 +106,9 @@ pub struct MapErrNewService { impl MapErrNewService { /// Create new `MapErr` new service instance - pub fn new(a: A, f: F) -> Self + pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, F: Fn(A::Error) -> E, { Self { @@ -132,35 +133,36 @@ where } } -impl NewService for MapErrNewService +impl NewService for MapErrNewService where - A: NewService, + A: NewService, F: Fn(A::Error) -> E + Clone, { + type Request = A::Request; type Response = A::Response; type Error = E; type Service = MapErr; type InitError = A::InitError; - type Future = MapErrNewServiceFuture; + type Future = MapErrNewServiceFuture; fn new_service(&self) -> Self::Future { MapErrNewServiceFuture::new(self.a.new_service(), self.f.clone()) } } -pub struct MapErrNewServiceFuture +pub struct MapErrNewServiceFuture where - A: NewService, + A: NewService, F: Fn(A::Error) -> E, { fut: A::Future, f: F, } -impl MapErrNewServiceFuture +impl MapErrNewServiceFuture where - A: NewService, + A: NewService, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -168,9 +170,9 @@ where } } -impl Future for MapErrNewServiceFuture +impl Future for MapErrNewServiceFuture where - A: NewService, + A: NewService, F: Fn(A::Error) -> E + Clone, { type Item = MapErr; @@ -194,7 +196,8 @@ mod tests { struct Srv; - impl Service<()> for Srv { + impl Service for Srv { + type Request = (); type Response = (); type Error = (); type Future = FutureResult<(), ()>; diff --git a/actix-service/src/map_init_err.rs b/actix-service/src/map_init_err.rs index 40ff97b5..2625f5d3 100644 --- a/actix-service/src/map_init_err.rs +++ b/actix-service/src/map_init_err.rs @@ -13,9 +13,9 @@ pub struct MapInitErr { impl MapInitErr { /// Create new `MapInitErr` combinator - pub fn new(a: A, f: F) -> Self + pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E, { Self { @@ -40,35 +40,36 @@ where } } -impl NewService for MapInitErr +impl NewService for MapInitErr where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E + Clone, { + type Request = A::Request; type Response = A::Response; type Error = A::Error; type Service = A::Service; type InitError = E; - type Future = MapInitErrFuture; + type Future = MapInitErrFuture; fn new_service(&self) -> Self::Future { MapInitErrFuture::new(self.a.new_service(), self.f.clone()) } } -pub struct MapInitErrFuture +pub struct MapInitErrFuture where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E, { f: F, fut: A::Future, } -impl MapInitErrFuture +impl MapInitErrFuture where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -76,9 +77,9 @@ where } } -impl Future for MapInitErrFuture +impl Future for MapInitErrFuture where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E, { type Item = A::Service; diff --git a/actix-service/src/then.rs b/actix-service/src/then.rs index d4166c33..be541616 100644 --- a/actix-service/src/then.rs +++ b/actix-service/src/then.rs @@ -14,10 +14,10 @@ pub struct Then { impl Then { /// Create new `Then` combinator - pub fn new(a: A, b: B) -> Then + pub fn new(a: A, b: B) -> Then where - A: Service, - B: Service, Error = A::Error>, + A: Service, + B: Service, Error = A::Error>, { Then { a, b: Cell::new(b) } } @@ -35,39 +35,40 @@ where } } -impl Service for Then +impl Service for Then where - A: Service, - B: Service, Error = A::Error>, + A: Service, + B: Service, Error = A::Error>, { + type Request = A::Request; type Response = B::Response; type Error = B::Error; - type Future = ThenFuture; + type Future = ThenFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { try_ready!(self.a.poll_ready()); self.b.get_mut().poll_ready() } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { ThenFuture::new(self.a.call(req), self.b.clone()) } } -pub struct ThenFuture +pub struct ThenFuture where - A: Service, - B: Service>, + A: Service, + B: Service>, { b: Cell, fut_b: Option, fut_a: Option, } -impl ThenFuture +impl ThenFuture where - A: Service, - B: Service>, + A: Service, + B: Service>, { fn new(a: A::Future, b: Cell) -> Self { ThenFuture { @@ -78,10 +79,10 @@ where } } -impl Future for ThenFuture +impl Future for ThenFuture where - A: Service, - B: Service>, + A: Service, + B: Service>, { type Item = B::Response; type Error = B::Error; @@ -115,15 +116,15 @@ pub struct ThenNewService { impl ThenNewService { /// Create new `AndThen` combinator - pub fn new(a: A, f: F) -> Self + pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, B: NewService< - Result, + Request = Result, Error = A::Error, InitError = A::InitError, >, - F: IntoNewService>, + F: IntoNewService, { Self { a, @@ -132,17 +133,22 @@ impl ThenNewService { } } -impl NewService for ThenNewService +impl NewService for ThenNewService where - A: NewService, - B: NewService, Error = A::Error, InitError = A::InitError>, + A: NewService, + B: NewService< + Request = Result, + Error = A::Error, + InitError = A::InitError, + >, { + type Request = A::Request; type Response = B::Response; type Error = A::Error; type Service = Then; type InitError = A::InitError; - type Future = ThenNewServiceFuture; + type Future = ThenNewServiceFuture; fn new_service(&self) -> Self::Future { ThenNewServiceFuture::new(self.a.new_service(), self.b.new_service()) @@ -162,10 +168,14 @@ where } } -pub struct ThenNewServiceFuture +pub struct ThenNewServiceFuture where - A: NewService, - B: NewService, Error = A::Error, InitError = A::InitError>, + A: NewService, + B: NewService< + Request = Result, + Error = A::Error, + InitError = A::InitError, + >, { fut_b: B::Future, fut_a: A::Future, @@ -173,10 +183,14 @@ where b: Option, } -impl ThenNewServiceFuture +impl ThenNewServiceFuture where - A: NewService, - B: NewService, Error = A::Error, InitError = A::InitError>, + A: NewService, + B: NewService< + Request = Result, + Error = A::Error, + InitError = A::InitError, + >, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { ThenNewServiceFuture { @@ -188,10 +202,14 @@ where } } -impl Future for ThenNewServiceFuture +impl Future for ThenNewServiceFuture where - A: NewService, - B: NewService, Error = A::Error, InitError = A::InitError>, + A: NewService, + B: NewService< + Request = Result, + Error = A::Error, + InitError = A::InitError, + >, { type Item = Then; type Error = A::InitError; @@ -231,7 +249,8 @@ mod tests { #[derive(Clone)] struct Srv1(Rc>); - impl Service> for Srv1 { + impl Service for Srv1 { + type Request = Result<&'static str, &'static str>; type Response = &'static str; type Error = (); type Future = FutureResult; @@ -251,7 +270,8 @@ mod tests { struct Srv2(Rc>); - impl Service> for Srv2 { + impl Service for Srv2 { + type Request = Result<&'static str, ()>; type Response = (&'static str, &'static str); type Error = (); type Future = FutureResult; diff --git a/actix-test-server/Cargo.toml b/actix-test-server/Cargo.toml index ccc26f3a..c5071664 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.1.0" +#actix-server = "0.1.0" +actix-server = { path="../actix-server" } log = "0.4" diff --git a/actix-utils/Cargo.toml b/actix-utils/Cargo.toml index ceef0afe..535f4ed3 100644 --- a/actix-utils/Cargo.toml +++ b/actix-utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-utils" -version = "0.1.1" +version = "0.2.0" authors = ["Nikolay Kim "] description = "Actix utils - various actix net related services" keywords = ["network", "framework", "async", "futures"] @@ -18,7 +18,8 @@ name = "actix_utils" path = "src/lib.rs" [dependencies] -actix-service = "0.1.6" +#actix-service = "0.1.6" +actix-service = { path="../actix-service" } actix-codec = "0.1.0" bytes = "0.4" futures = "0.1" diff --git a/actix-utils/src/cloneable.rs b/actix-utils/src/cloneable.rs index 2c7ddf5b..933e6a1f 100644 --- a/actix-utils/src/cloneable.rs +++ b/actix-utils/src/cloneable.rs @@ -13,9 +13,9 @@ pub struct CloneableService { } impl CloneableService { - pub fn new(service: T) -> Self + pub fn new(service: T) -> Self where - T: Service, + T: Service, { Self { service: Cell::new(service), @@ -33,10 +33,11 @@ impl Clone for CloneableService { } } -impl Service for CloneableService +impl Service for CloneableService where - T: Service, + T: Service + 'static, { + type Request = T::Request; type Response = T::Response; type Error = T::Error; type Future = T::Future; @@ -45,7 +46,7 @@ where self.service.get_mut().poll_ready() } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: T::Request) -> Self::Future { self.service.get_mut().call(req) } } diff --git a/actix-utils/src/either.rs b/actix-utils/src/either.rs index 272b1213..10400281 100644 --- a/actix-utils/src/either.rs +++ b/actix-utils/src/either.rs @@ -21,11 +21,12 @@ impl Clone for EitherService { } } -impl Service for EitherService +impl Service for EitherService where - A: Service, - B: Service, + A: Service, + B: Service, { + type Request = A::Request; type Response = A::Response; type Error = A::Error; type Future = future::Either; @@ -37,7 +38,7 @@ where } } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: A::Request) -> 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)), @@ -52,11 +53,11 @@ pub enum Either { } impl Either { - pub fn new_a(srv: A) -> Self + pub fn new_a(srv: A) -> Self where - A: NewService, + A: NewService, B: NewService< - Request, + Request = A::Request, Response = A::Response, Error = A::Error, InitError = A::InitError, @@ -65,11 +66,11 @@ impl Either { Either::A(srv) } - pub fn new_b(srv: B) -> Self + pub fn new_b(srv: B) -> Self where - A: NewService, + A: NewService, B: NewService< - Request, + Request = A::Request, Response = A::Response, Error = A::Error, InitError = A::InitError, @@ -79,16 +80,22 @@ impl Either { } } -impl NewService for Either +impl NewService for Either where - A: NewService, - B: NewService, + A: NewService, + B: NewService< + Request = A::Request, + 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; - type Future = EitherNewService; + type Future = EitherNewService; fn new_service(&self) -> Self::Future { match self { @@ -108,15 +115,20 @@ impl Clone for Either { } #[doc(hidden)] -pub enum EitherNewService, B: NewService, R> { +pub enum EitherNewService { A(A::Future), B(B::Future), } -impl Future for EitherNewService +impl Future for EitherNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService< + Request = A::Request, + Response = A::Response, + Error = A::Error, + InitError = A::InitError, + >, { type Item = EitherService; type Error = A::InitError; diff --git a/actix-utils/src/framed.rs b/actix-utils/src/framed.rs index 40444f92..2e041bba 100644 --- a/actix-utils/src/framed.rs +++ b/actix-utils/src/framed.rs @@ -22,15 +22,15 @@ pub struct FramedNewService { impl FramedNewService where - S: NewService, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, - >>::Future: 'static, + ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, ::Error: std::fmt::Debug, { - pub fn new>>(factory: F1) -> Self { + pub fn new>(factory: F1) -> Self { Self { factory: factory.into_new_service(), _t: PhantomData, @@ -50,16 +50,17 @@ where } } -impl NewService> for FramedNewService +impl NewService for FramedNewService where - S: NewService, Response = Response> + Clone, + S: NewService, Response = Response> + Clone, S::Error: 'static, - >>::Future: 'static, + ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, ::Error: std::fmt::Debug, { + type Request = Framed; type Response = FramedTransport; type Error = S::InitError; type InitError = S::InitError; @@ -91,16 +92,17 @@ where } } -impl Service> for FramedService +impl Service for FramedService where - S: NewService, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, - >>::Future: 'static, + ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, ::Error: std::fmt::Debug, { + type Request = Framed; type Response = FramedTransport; type Error = S::InitError; type Future = FramedServiceResponseFuture; @@ -121,9 +123,9 @@ where #[doc(hidden)] pub struct FramedServiceResponseFuture where - S: NewService, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, - >>::Future: 'static, + ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, @@ -135,9 +137,9 @@ where impl Future for FramedServiceResponseFuture where - S: NewService, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, - >>::Future: 'static, + ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, @@ -174,7 +176,7 @@ impl From for FramedTransportError { /// and pass then to the service. pub struct FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -188,7 +190,7 @@ where inner: Cell::Item, S::Error>>, } -enum TransportState>, U: Encoder + Decoder> { +enum TransportState { Processing, Error(FramedTransportError), FramedError(FramedTransportError), @@ -202,7 +204,7 @@ struct FramedTransportInner { impl FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -294,7 +296,7 @@ where impl FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -302,7 +304,7 @@ where ::Item: 'static, ::Error: std::fmt::Debug, { - pub fn new>>(framed: Framed, service: F) -> Self { + pub fn new>(framed: Framed, service: F) -> Self { FramedTransport { framed, service: service.into_service(), @@ -340,7 +342,7 @@ where impl Future for FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -400,12 +402,13 @@ where } } -impl NewService for IntoFramed +impl NewService for IntoFramed where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, U: Encoder + Decoder, { + type Request = T; type Response = Framed; type Error = (); type InitError = (); @@ -430,12 +433,13 @@ where _t: PhantomData<(T,)>, } -impl Service for IntoFramedService +impl Service for IntoFramedService where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, U: Encoder + Decoder, { + type Request = T; type Response = Framed; type Error = (); type Future = FutureResult; diff --git a/actix-utils/src/inflight.rs b/actix-utils/src/inflight.rs index 31877d1f..ca4cce00 100644 --- a/actix-utils/src/inflight.rs +++ b/actix-utils/src/inflight.rs @@ -13,10 +13,10 @@ pub struct InFlight { } impl InFlight { - pub fn new(factory: F) -> Self + pub fn new(factory: F) -> Self where - T: NewService, - F: IntoNewService, + T: NewService, + F: IntoNewService, { Self { factory: factory.into_new_service(), @@ -33,15 +33,16 @@ impl InFlight { } } -impl NewService for InFlight +impl NewService for InFlight where - T: NewService, + T: NewService, { + type Request = T::Request; type Response = T::Response; type Error = T::Error; type InitError = T::InitError; type Service = InFlightService; - type Future = InFlightResponseFuture; + type Future = InFlightResponseFuture; fn new_service(&self) -> Self::Future { InFlightResponseFuture { @@ -51,12 +52,12 @@ where } } -pub struct InFlightResponseFuture, Request> { +pub struct InFlightResponseFuture { fut: T::Future, max_inflight: usize, } -impl, Request> Future for InFlightResponseFuture { +impl Future for InFlightResponseFuture { type Item = InFlightService; type Error = T::InitError; @@ -74,10 +75,10 @@ pub struct InFlightService { } impl InFlightService { - pub fn new(service: F) -> Self + pub fn new(service: F) -> Self where - T: Service, - F: IntoService, + T: Service, + F: IntoService, { Self { service: service.into_service(), @@ -85,10 +86,10 @@ impl InFlightService { } } - pub fn with_max_inflight(max: usize, service: F) -> Self + pub fn with_max_inflight(max: usize, service: F) -> Self where - T: Service, - F: IntoService, + T: Service, + F: IntoService, { Self { service: service.into_service(), @@ -97,13 +98,14 @@ impl InFlightService { } } -impl Service for InFlightService +impl Service for InFlightService where - T: Service, + T: Service, { + type Request = T::Request; type Response = T::Response; type Error = T::Error; - type Future = InFlightServiceResponse; + type Future = InFlightServiceResponse; fn poll_ready(&mut self) -> Poll<(), Self::Error> { let res = self.service.poll_ready()?; @@ -114,7 +116,7 @@ where Ok(res) } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: T::Request) -> Self::Future { InFlightServiceResponse { fut: self.service.call(req), _guard: self.count.get(), @@ -123,12 +125,12 @@ where } #[doc(hidden)] -pub struct InFlightServiceResponse, Request> { +pub struct InFlightServiceResponse { fut: T::Future, _guard: CounterGuard, } -impl, Request> Future for InFlightServiceResponse { +impl Future for InFlightServiceResponse { type Item = T::Response; type Error = T::Error; diff --git a/actix-utils/src/keepalive.rs b/actix-utils/src/keepalive.rs index 4590c70e..db2c0472 100644 --- a/actix-utils/src/keepalive.rs +++ b/actix-utils/src/keepalive.rs @@ -44,10 +44,11 @@ where } } -impl NewService for KeepAlive +impl NewService for KeepAlive where F: Fn() -> E + Clone, { + type Request = R; type Response = R; type Error = E; type InitError = Never; @@ -89,10 +90,11 @@ where } } -impl Service for KeepAliveService +impl Service for KeepAliveService where F: Fn() -> E, { + type Request = R; type Response = R; type Error = E; type Future = FutureResult; diff --git a/actix-utils/src/stream.rs b/actix-utils/src/stream.rs index 7f35575a..54a4c2fa 100644 --- a/actix-utils/src/stream.rs +++ b/actix-utils/src/stream.rs @@ -37,12 +37,12 @@ pub struct StreamNewService { impl StreamNewService where S: IntoStream, - T: NewService, Response = (), Error = E, InitError = E>, + T: NewService, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - >>::Future: 'static, + ::Future: 'static, { - pub fn new>>(factory: F) -> Self { + pub fn new>(factory: F) -> Self { Self { factory: Rc::new(factory.into_new_service()), _t: PhantomData, @@ -59,14 +59,15 @@ impl Clone for StreamNewService { } } -impl NewService for StreamNewService +impl NewService for StreamNewService where S: IntoStream + 'static, - T: NewService, Response = (), Error = E, InitError = E>, + T: NewService, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - >>::Future: 'static, + ::Future: 'static, { + type Request = S; type Response = (); type Error = E; type InitError = E; @@ -86,14 +87,15 @@ pub struct StreamService { _t: PhantomData<(S, E)>, } -impl Service for StreamService +impl Service for StreamService where S: IntoStream + 'static, - T: NewService, Response = (), Error = E, InitError = E>, + T: NewService, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - >>::Future: 'static, + ::Future: 'static, { + type Request = S; type Response = (); type Error = E; type Future = Box>; @@ -114,7 +116,7 @@ where pub struct StreamDispatcher where S: IntoStream + 'static, - T: Service, Response = ()> + 'static, + T: Service, Response = ()> + 'static, T::Future: 'static, { stream: S, @@ -126,13 +128,13 @@ where impl StreamDispatcher where S: Stream, - T: Service, Response = ()>, + T: Service, Response = ()>, T::Future: 'static, { pub fn new(stream: F1, service: F2) -> Self where F1: IntoStream, - F2: IntoService>, + F2: IntoService, { let (err_tx, err_rx) = mpsc::unbounded(); StreamDispatcher { @@ -147,7 +149,7 @@ where impl Future for StreamDispatcher where S: Stream, - T: Service, Response = ()>, + T: Service, Response = ()>, T::Future: 'static, { type Item = (); @@ -225,7 +227,8 @@ impl Clone for TakeItem { } } -impl NewService for TakeItem { +impl NewService for TakeItem { + type Request = T; type Response = (Option, T); type Error = T::Error; type InitError = (); @@ -248,7 +251,8 @@ impl Clone for TakeItemService { } } -impl Service for TakeItemService { +impl Service for TakeItemService { + type Request = T; type Response = (Option, T); type Error = T::Error; type Future = TakeItemServiceResponse; diff --git a/actix-utils/src/time.rs b/actix-utils/src/time.rs index d2bfe86a..e8cfcd75 100644 --- a/actix-utils/src/time.rs +++ b/actix-utils/src/time.rs @@ -42,7 +42,8 @@ impl Default for LowResTime { } } -impl NewService<()> for LowResTime { +impl NewService for LowResTime { + type Request = (); type Response = Instant; type Error = Never; type InitError = Never; @@ -88,7 +89,8 @@ impl LowResTimeService { } } -impl Service<()> for LowResTimeService { +impl Service for LowResTimeService { + type Request = (); type Response = Instant; type Error = Never; type Future = FutureResult; diff --git a/actix-utils/src/timeout.rs b/actix-utils/src/timeout.rs index d2e638ad..6846e7a8 100644 --- a/actix-utils/src/timeout.rs +++ b/actix-utils/src/timeout.rs @@ -35,9 +35,9 @@ impl fmt::Debug for TimeoutError { } impl Timeout { - pub fn new(timeout: Duration, inner: T) -> Self + pub fn new(timeout: Duration, inner: T) -> Self where - T: NewService + Clone, + T: NewService + Clone, { Timeout { inner, timeout } } @@ -55,15 +55,16 @@ where } } -impl NewService for Timeout +impl NewService for Timeout where - T: NewService + Clone, + T: NewService + Clone, { + type Request = T::Request; type Response = T::Response; type Error = TimeoutError; type InitError = T::InitError; type Service = TimeoutService; - type Future = TimeoutFut; + type Future = TimeoutFut; fn new_service(&self) -> Self::Future { TimeoutFut { @@ -75,14 +76,14 @@ where /// `Timeout` response future #[derive(Debug)] -pub struct TimeoutFut, Request> { +pub struct TimeoutFut { fut: T::Future, timeout: Duration, } -impl Future for TimeoutFut +impl Future for TimeoutFut where - T: NewService, + T: NewService, { type Item = TimeoutService; type Error = T::InitError; @@ -101,9 +102,9 @@ pub struct TimeoutService { } impl TimeoutService { - pub fn new(timeout: Duration, inner: T) -> Self + pub fn new(timeout: Duration, inner: T) -> Self where - T: Service, + T: Service, { TimeoutService { inner, timeout } } @@ -118,19 +119,20 @@ impl Clone for TimeoutService { } } -impl Service for TimeoutService +impl Service for TimeoutService where - T: Service, + T: Service, { + type Request = T::Request; type Response = T::Response; type Error = TimeoutError; - type Future = TimeoutServiceResponse; + type Future = TimeoutServiceResponse; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.inner.poll_ready().map_err(TimeoutError::Service) } - fn call(&mut self, request: Request) -> Self::Future { + fn call(&mut self, request: T::Request) -> Self::Future { TimeoutServiceResponse { fut: self.inner.call(request), sleep: Delay::new(clock::now() + self.timeout), @@ -140,14 +142,14 @@ where /// `TimeoutService` response future #[derive(Debug)] -pub struct TimeoutServiceResponse, Request> { +pub struct TimeoutServiceResponse { fut: T::Future, sleep: Delay, } -impl Future for TimeoutServiceResponse +impl Future for TimeoutServiceResponse where - T: Service, + T: Service, { type Item = T::Response; type Error = TimeoutError;