From 862be49e30a6a807579b3289a001fcca6983955c Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 22 Feb 2019 12:44:37 -0800 Subject: [PATCH] add Config argument to NewService --- actix-connector/Cargo.toml | 3 +- actix-connector/src/connector.rs | 3 +- actix-connector/src/ssl/openssl.rs | 6 +- actix-server/Cargo.toml | 3 +- actix-server/src/builder.rs | 21 ----- actix-server/src/config.rs | 6 +- actix-server/src/lib.rs | 2 +- actix-server/src/services.rs | 102 +------------------------ actix-server/src/ssl/nativetls.rs | 2 +- actix-server/src/ssl/openssl.rs | 2 +- actix-server/src/ssl/rustls.rs | 2 +- actix-service/CHANGES.md | 7 ++ actix-service/Cargo.toml | 2 +- actix-service/src/and_then.rs | 49 ++++++------ actix-service/src/and_then_apply.rs | 47 +++++++----- actix-service/src/and_then_apply_fn.rs | 48 ++++++------ actix-service/src/apply.rs | 47 +++++++----- actix-service/src/blank.rs | 7 +- actix-service/src/fn_service.rs | 25 ++---- actix-service/src/from_err.rs | 30 ++++---- actix-service/src/lib.rs | 72 +++++++++-------- actix-service/src/map.rs | 50 ++++++------ actix-service/src/map_err.rs | 34 ++++----- actix-service/src/map_init_err.rs | 32 ++++---- actix-service/src/then.rs | 44 ++++++----- actix-utils/Cargo.toml | 3 +- actix-utils/src/boxed.rs | 43 +++++++---- actix-utils/src/either.rs | 30 ++++---- actix-utils/src/framed.rs | 52 +++++++------ actix-utils/src/keepalive.rs | 4 +- actix-utils/src/stream.rs | 37 +++++---- actix-utils/src/time.rs | 4 +- 32 files changed, 386 insertions(+), 433 deletions(-) diff --git a/actix-connector/Cargo.toml b/actix-connector/Cargo.toml index 9786585d..31918cab 100644 --- a/actix-connector/Cargo.toml +++ b/actix-connector/Cargo.toml @@ -27,7 +27,8 @@ default = [] ssl = ["openssl", "tokio-openssl"] [dependencies] -actix-service = "0.2.0" +#actix-service = "0.2.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 802cca3b..b274586d 100644 --- a/actix-connector/src/connector.rs +++ b/actix-connector/src/connector.rs @@ -177,12 +177,13 @@ impl Connector { cfg: ResolverConfig, opts: ResolverOpts, ) -> impl NewService< + (), Request = Connect, Response = (Connect, TcpStream), Error = ConnectorError, InitError = E, > + Clone { - move || -> FutureResult { ok(Connector::new(cfg.clone(), opts)) } + move |_: &()| -> FutureResult { ok(Connector::new(cfg.clone(), opts)) } } } diff --git a/actix-connector/src/ssl/openssl.rs b/actix-connector/src/ssl/openssl.rs index 43cd4fb4..3df9cab5 100644 --- a/actix-connector/src/ssl/openssl.rs +++ b/actix-connector/src/ssl/openssl.rs @@ -44,7 +44,9 @@ impl Clone for OpensslConnector { } } -impl NewService for OpensslConnector { +impl NewService<()> + for OpensslConnector +{ type Request = (R, T); type Response = (R, SslStream); type Error = HandshakeError; @@ -52,7 +54,7 @@ impl NewService for OpensslConnect type InitError = E; type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, _: &()) -> Self::Future { ok(OpensslConnectorService { connector: self.connector.clone(), _t: PhantomData, diff --git a/actix-server/Cargo.toml b/actix-server/Cargo.toml index 29d020f8..fa910da7 100644 --- a/actix-server/Cargo.toml +++ b/actix-server/Cargo.toml @@ -33,7 +33,8 @@ ssl = ["openssl", "tokio-openssl"] rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"] [dependencies] -actix-service = "0.2.1" +#actix-service = "0.2.1" +actix-service = { path="../actix-service" } actix-rt = "0.1.0" log = "0.4" diff --git a/actix-server/src/builder.rs b/actix-server/src/builder.rs index 0e533528..4b1ec19c 100644 --- a/actix-server/src/builder.rs +++ b/actix-server/src/builder.rs @@ -15,7 +15,6 @@ use crate::accept::{AcceptLoop, AcceptNotify, Command}; use crate::config::{ConfiguredService, ServiceConfig}; use crate::server::{Server, ServerCommand}; use crate::services::{InternalServiceFactory, StreamNewService, StreamServiceFactory}; -use crate::services::{ServiceFactory, ServiceNewService}; use crate::signals::{Signal, Signals}; use crate::worker::{self, Worker, WorkerAvailability, WorkerClient}; use crate::Token; @@ -176,26 +175,6 @@ impl ServerBuilder { self } - /// Add new service to the server. - pub fn listen2>( - mut self, - name: N, - lst: net::TcpListener, - factory: F, - ) -> Self - where - F: ServiceFactory, - { - let token = self.token.next(); - self.services.push(ServiceNewService::create( - name.as_ref().to_string(), - token, - factory, - )); - self.sockets.push((token, lst)); - self - } - /// Spawn new thread and start listening for incoming connections. /// /// This method spawns new thread and starts new actix system. Other than diff --git a/actix-server/src/config.rs b/actix-server/src/config.rs index fdf5646d..885f4254 100644 --- a/actix-server/src/config.rs +++ b/actix-server/src/config.rs @@ -114,7 +114,7 @@ impl InternalServiceFactory for ConfiguredService { // construct services let mut fut = Vec::new(); for (token, ns) in rt.services { - fut.push(ns.new_service().map(move |service| (token, service))); + fut.push(ns.new_service(&()).map(move |service| (token, service))); } Box::new(join_all(fut).map_err(|e| { @@ -219,8 +219,8 @@ where type Service = BoxedServerService; type Future = Box>; - fn new_service(&self) -> Self::Future { - Box::new(self.inner.new_service().map_err(|_| ()).map(|s| { + 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 })) diff --git a/actix-server/src/lib.rs b/actix-server/src/lib.rs index bfcdb0d4..5b90bd69 100644 --- a/actix-server/src/lib.rs +++ b/actix-server/src/lib.rs @@ -13,7 +13,7 @@ mod worker; pub use self::builder::ServerBuilder; pub use self::config::{ServiceConfig, ServiceRuntime}; pub use self::server::Server; -pub use self::services::{ServerMessage, ServiceFactory, StreamServiceFactory}; +pub use self::services::StreamServiceFactory; /// Socket id token #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] diff --git a/actix-server/src/services.rs b/actix-server/src/services.rs index 1340f890..a629b667 100644 --- a/actix-server/src/services.rs +++ b/actix-server/src/services.rs @@ -13,7 +13,7 @@ use super::Token; use crate::counter::CounterGuard; /// Server message -pub enum ServerMessage { +pub(crate) enum ServerMessage { /// New stream Connect(net::TcpStream), /// Gracefull shutdown @@ -28,12 +28,6 @@ pub trait StreamServiceFactory: Send + Clone + 'static { fn create(&self) -> Self::NewService; } -pub trait ServiceFactory: Send + Clone + 'static { - type NewService: NewService; - - fn create(&self) -> Self::NewService; -} - pub(crate) trait InternalServiceFactory: Send { fn name(&self, token: Token) -> &str; @@ -98,86 +92,6 @@ where } } -pub(crate) struct ServerService { - service: T, -} - -impl ServerService { - fn new(service: T) -> Self { - ServerService { service } - } -} - -impl Service for ServerService -where - T: Service, - T::Future: 'static, - T::Error: 'static, -{ - type Request = (Option, ServerMessage); - type Response = (); - type Error = (); - type Future = FutureResult<(), ()>; - - fn poll_ready(&mut self) -> Poll<(), Self::Error> { - self.service.poll_ready().map_err(|_| ()) - } - - fn call(&mut self, (guard, req): (Option, ServerMessage)) -> Self::Future { - spawn(self.service.call(req).then(move |res| { - drop(guard); - res.map_err(|_| ()).map(|_| ()) - })); - ok(()) - } -} - -pub(crate) struct ServiceNewService { - name: String, - inner: F, - token: Token, -} - -impl ServiceNewService -where - F: ServiceFactory, -{ - pub(crate) fn create(name: String, token: Token, inner: F) -> Box { - Box::new(Self { name, inner, token }) - } -} - -impl InternalServiceFactory for ServiceNewService -where - F: ServiceFactory, -{ - fn name(&self, _: Token) -> &str { - &self.name - } - - fn clone_factory(&self) -> Box { - Box::new(Self { - name: self.name.clone(), - inner: self.inner.clone(), - token: self.token, - }) - } - - fn create(&self) -> Box, Error = ()>> { - let token = self.token; - Box::new( - self.inner - .create() - .new_service() - .map_err(|_| ()) - .map(move |inner| { - let service: BoxedServerService = Box::new(ServerService::new(inner)); - vec![(token, service)] - }), - ) - } -} - pub(crate) struct StreamNewService { name: String, inner: F, @@ -214,7 +128,7 @@ where Box::new( self.inner .create() - .new_service() + .new_service(&()) .map_err(|_| ()) .map(move |inner| { let service: BoxedServerService = Box::new(StreamService::new(inner)); @@ -238,18 +152,6 @@ impl InternalServiceFactory for Box { } } -impl ServiceFactory for F -where - F: Fn() -> T + Send + Clone + 'static, - T: NewService, -{ - type NewService = T; - - fn create(&self) -> T { - (self)() - } -} - impl StreamServiceFactory for F where F: Fn() -> T + Send + Clone + 'static, diff --git a/actix-server/src/ssl/nativetls.rs b/actix-server/src/ssl/nativetls.rs index 88032ed0..aef07e7b 100644 --- a/actix-server/src/ssl/nativetls.rs +++ b/actix-server/src/ssl/nativetls.rs @@ -44,7 +44,7 @@ impl NewService for NativeTlsAcceptor { type InitError = (); type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, _: &()) -> Self::Future { MAX_CONN_COUNTER.with(|conns| { ok(NativeTlsAcceptorService { acceptor: self.acceptor.clone(), diff --git a/actix-server/src/ssl/openssl.rs b/actix-server/src/ssl/openssl.rs index 7f0bc782..5826ce1d 100644 --- a/actix-server/src/ssl/openssl.rs +++ b/actix-server/src/ssl/openssl.rs @@ -44,7 +44,7 @@ impl NewService for OpensslAcceptor { type InitError = (); type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, _: &()) -> Self::Future { MAX_CONN_COUNTER.with(|conns| { ok(OpensslAcceptorService { acceptor: self.acceptor.clone(), diff --git a/actix-server/src/ssl/rustls.rs b/actix-server/src/ssl/rustls.rs index 495ae4a3..dd05ede3 100644 --- a/actix-server/src/ssl/rustls.rs +++ b/actix-server/src/ssl/rustls.rs @@ -46,7 +46,7 @@ impl NewService for RustlsAcceptor { type InitError = (); type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, _: &()) -> Self::Future { MAX_CONN_COUNTER.with(|conns| { ok(RustlsAcceptorService { acceptor: self.config.clone().into(), diff --git a/actix-service/CHANGES.md b/actix-service/CHANGES.md index 7c2e4a69..d4efa741 100644 --- a/actix-service/CHANGES.md +++ b/actix-service/CHANGES.md @@ -1,5 +1,12 @@ # Changes +## [0.3.0] - 2019-02-xx + +## Changed + +* Added `Config` argument to `NewService` trait. + + ## [0.2.2] - 2019-02-19 ### Added diff --git a/actix-service/Cargo.toml b/actix-service/Cargo.toml index 5a06e33a..cb58980f 100644 --- a/actix-service/Cargo.toml +++ b/actix-service/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-service" -version = "0.2.2" +version = "0.3.0" authors = ["Nikolay Kim "] description = "Actix Service" keywords = ["network", "framework", "async", "futures"] diff --git a/actix-service/src/and_then.rs b/actix-service/src/and_then.rs index 2ef58bfc..ee0f5e18 100644 --- a/actix-service/src/and_then.rs +++ b/actix-service/src/and_then.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + use futures::{try_ready, Async, Future, Poll}; use super::{IntoNewService, NewService, Service}; @@ -105,29 +107,31 @@ where } /// `AndThenNewService` new service combinator -pub struct AndThenNewService { +pub struct AndThenNewService { a: A, b: B, + _t: PhantomData, } -impl AndThenNewService { +impl AndThenNewService { /// Create new `AndThen` combinator - pub fn new>(a: A, f: F) -> Self + pub fn new>(a: A, f: F) -> Self where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { Self { a, b: f.into_new_service(), + _t: PhantomData, } } } -impl NewService for AndThenNewService +impl NewService for AndThenNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { type Request = A::Request; type Response = B::Response; @@ -135,14 +139,14 @@ where 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()) + fn new_service(&self, cfg: &C) -> Self::Future { + AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) } } -impl Clone for AndThenNewService +impl Clone for AndThenNewService where A: Clone, B: Clone, @@ -151,14 +155,15 @@ where Self { a: self.a.clone(), b: self.b.clone(), + _t: PhantomData, } } } -pub struct AndThenNewServiceFuture +pub struct AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { fut_b: B::Future, fut_a: A::Future, @@ -166,10 +171,10 @@ where b: Option, } -impl AndThenNewServiceFuture +impl AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { AndThenNewServiceFuture { @@ -181,10 +186,10 @@ where } } -impl Future for AndThenNewServiceFuture +impl Future for AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, { type Item = AndThen; type Error = A::InitError; @@ -286,7 +291,7 @@ mod tests { let new_srv = blank .into_new_service() .and_then(move || Ok(Srv2(cnt.clone()))); - if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { + if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() { let res = srv.call("srv1").poll(); assert!(res.is_ok()); assert_eq!(res.unwrap(), Async::Ready(("srv1", "srv2"))); diff --git a/actix-service/src/and_then_apply.rs b/actix-service/src/and_then_apply.rs index 9c5fc493..74633071 100644 --- a/actix-service/src/and_then_apply.rs +++ b/actix-service/src/and_then_apply.rs @@ -124,26 +124,32 @@ where } /// `Apply` new service combinator -pub struct AndThenTransformNewService { +pub struct AndThenTransformNewService { a: A, b: B, t: T, + _t: std::marker::PhantomData, } -impl AndThenTransformNewService +impl AndThenTransformNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService, T: NewTransform, T::Error: From, { /// Create new `ApplyNewService` new service instance pub fn new(t: T, a: A, b: B) -> Self { - Self { a, b, t } + Self { + a, + b, + t, + _t: std::marker::PhantomData, + } } } -impl Clone for AndThenTransformNewService +impl Clone for AndThenTransformNewService where A: Clone, B: Clone, @@ -154,14 +160,15 @@ where a: self.a.clone(), b: self.b.clone(), t: self.t.clone(), + _t: std::marker::PhantomData, } } } -impl NewService for AndThenTransformNewService +impl NewService for AndThenTransformNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService, T: NewTransform, T::Error: From, { @@ -171,24 +178,24 @@ where type InitError = T::InitError; type Service = AndThenTransform; - type Future = AndThenTransformNewServiceFuture; + type Future = AndThenTransformNewServiceFuture; - fn new_service(&self) -> Self::Future { + fn new_service(&self, cfg: &C) -> Self::Future { AndThenTransformNewServiceFuture { a: None, b: None, t: None, - fut_a: self.a.new_service(), - fut_b: self.b.new_service(), + fut_a: self.a.new_service(cfg), + fut_b: self.b.new_service(cfg), fut_t: self.t.new_transform(), } } } -pub struct AndThenTransformNewServiceFuture +pub struct AndThenTransformNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, T: NewTransform, T::Error: From, { @@ -200,10 +207,10 @@ where t: Option, } -impl Future for AndThenTransformNewServiceFuture +impl Future for AndThenTransformNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, T: NewTransform, T::Error: From, { @@ -287,7 +294,7 @@ mod tests { |req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)), || Ok(Srv), ); - if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { + if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() { assert!(srv.poll_ready().is_ok()); let res = srv.call("srv").poll(); assert!(res.is_ok()); diff --git a/actix-service/src/and_then_apply_fn.rs b/actix-service/src/and_then_apply_fn.rs index 6565226f..3ac7756a 100644 --- a/actix-service/src/and_then_apply_fn.rs +++ b/actix-service/src/and_then_apply_fn.rs @@ -129,23 +129,27 @@ where } /// `ApplyNewService` new service combinator -pub struct AndThenApplyNewService { +pub struct AndThenApplyNewService { a: A, b: B, f: Cell, - r: PhantomData<(Out)>, + r: PhantomData<(Out, Cfg)>, } -impl AndThenApplyNewService +impl AndThenApplyNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService, F: FnMut(A::Response, &mut B::Service) -> Out, Out: IntoFuture, Out::Error: Into, { /// Create new `ApplyNewService` new service instance - pub fn new, B1: IntoNewService>(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(), @@ -155,7 +159,7 @@ where } } -impl Clone for AndThenApplyNewService +impl Clone for AndThenApplyNewService where A: Clone, B: Clone, @@ -170,10 +174,10 @@ 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, @@ -181,26 +185,26 @@ where type Request = A::Request; type Response = Out::Item; type Error = A::Error; + type Service = AndThenApply; type InitError = A::InitError; - type Service = AndThenApply; - type Future = AndThenApplyNewServiceFuture; + type Future = AndThenApplyNewServiceFuture; - fn new_service(&self) -> Self::Future { + fn new_service(&self, cfg: &Cfg) -> Self::Future { AndThenApplyNewServiceFuture { a: None, b: None, f: self.f.clone(), - fut_a: self.a.new_service(), - fut_b: self.b.new_service(), + fut_a: self.a.new_service(cfg), + fut_b: self.b.new_service(cfg), } } } -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, @@ -212,10 +216,10 @@ 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, @@ -290,7 +294,7 @@ mod tests { || Ok(Srv), |req: &'static str, srv| srv.call(()).map(move |res| (req, res)), ); - if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { + if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() { assert!(srv.poll_ready().is_ok()); let res = srv.call("srv").poll(); assert!(res.is_ok()); diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index f4bb77ae..27c83d9e 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -87,69 +87,74 @@ where } /// `ApplyNewService` new service combinator -pub struct ApplyNewService +pub struct ApplyNewService where T: NewTransform, T::Error: From, - S: NewService, + S: NewService, { transform: T, service: S, + _t: std::marker::PhantomData, } -impl ApplyNewService +impl ApplyNewService where T: NewTransform, T::Error: From, - S: NewService, + S: NewService, { /// Create new `ApplyNewService` new service instance - pub fn new, S1: IntoNewService>( + pub fn new, S1: IntoNewService>( transform: T1, service: S1, ) -> Self { Self { transform: transform.into_new_transform(), service: service.into_new_service(), + _t: std::marker::PhantomData, } } } -impl ApplyNewService, S> +impl + ApplyNewService, S, Cfg> where F: FnMut(In, &mut S::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From, - S: NewService, + S: NewService, { /// Create new `Apply` combinator factory - pub fn new_fn>(service: S1, transform: F) -> Self { + pub fn new_fn>(service: S1, transform: F) -> Self { Self { service: service.into_new_service(), transform: FnNewTransform::new(transform), + _t: std::marker::PhantomData, } } } -impl Clone for ApplyNewService +impl Clone for ApplyNewService where T: NewTransform + Clone, T::Error: From, - S: NewService + Clone, + S: NewService + Clone, { fn clone(&self) -> Self { Self { service: self.service.clone(), transform: self.transform.clone(), + _t: std::marker::PhantomData, } } } -impl NewService for ApplyNewService +impl NewService for ApplyNewService where T: NewTransform, T::Error: From, - S: NewService, + S: NewService, { type Request = T::Request; type Response = T::Response; @@ -157,23 +162,23 @@ where type Service = Apply; type InitError = T::InitError; - type Future = ApplyNewServiceFuture; + type Future = ApplyNewServiceFuture; - fn new_service(&self) -> Self::Future { + fn new_service(&self, cfg: &C) -> Self::Future { ApplyNewServiceFuture { fut_t: self.transform.new_transform(), - fut_s: self.service.new_service(), + fut_s: self.service.new_service(cfg), service: None, transform: None, } } } -pub struct ApplyNewServiceFuture +pub struct ApplyNewServiceFuture where T: NewTransform, T::Error: From, - S: NewService, + S: NewService, { fut_s: S::Future, fut_t: T::Future, @@ -181,11 +186,11 @@ where transform: Option, } -impl Future for ApplyNewServiceFuture +impl Future for ApplyNewServiceFuture where T: NewTransform, T::Error: From, - S: NewService, + S: NewService, { type Item = Apply; type Error = T::InitError; @@ -256,7 +261,7 @@ mod tests { |req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)), || Ok::<_, ()>(Srv), ); - if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { + if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() { assert!(srv.poll_ready().is_ok()); let res = srv.call("srv").poll(); assert!(res.is_ok()); @@ -272,7 +277,7 @@ mod tests { || Ok::<_, ()>(Srv), |req: &'static str, srv| srv.call(()).map(move |res| (req, res)), ); - if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { + if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() { assert!(srv.poll_ready().is_ok()); let res = srv.call("srv").poll(); assert!(res.is_ok()); diff --git a/actix-service/src/blank.rs b/actix-service/src/blank.rs index ac825aa7..ea10b424 100644 --- a/actix-service/src/blank.rs +++ b/actix-service/src/blank.rs @@ -68,15 +68,16 @@ impl Default for BlankNewService { } } -impl NewService for BlankNewService { +impl NewService<()> for BlankNewService { type Request = R; type Response = R; type Error = E1; - type InitError = E2; type Service = Blank; + + type InitError = E2; type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, _: &()) -> Self::Future { ok(Blank::default()) } } diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 9ede9826..83b602fc 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -3,7 +3,7 @@ use std::marker::PhantomData; use futures::future::{ok, FutureResult}; use futures::{Async, IntoFuture, Poll}; -use super::{IntoNewService, IntoService, NewService, Service}; +use super::{IntoService, NewService, Service}; pub struct FnService where @@ -66,16 +66,16 @@ where } } -pub struct FnNewService +pub struct FnNewService where F: FnMut(Req) -> Out, Out: IntoFuture, { f: F, - _t: PhantomData<(Req,)>, + _t: PhantomData<(Req, Cfg)>, } -impl FnNewService +impl FnNewService where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, @@ -85,7 +85,7 @@ where } } -impl NewService for FnNewService +impl NewService for FnNewService where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, @@ -94,25 +94,16 @@ where type Response = Out::Item; type Error = Out::Error; type Service = FnService; + type InitError = (); type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, _: &Cfg) -> Self::Future { ok(FnService::new(self.f.clone())) } } -impl IntoNewService> for F -where - F: FnMut(Req) -> Out + Clone + 'static, - Out: IntoFuture, -{ - fn into_new_service(self) -> FnNewService { - FnNewService::new(self) - } -} - -impl Clone for FnNewService +impl Clone for FnNewService where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, diff --git a/actix-service/src/from_err.rs b/actix-service/src/from_err.rs index 7731e275..31ccb2d9 100644 --- a/actix-service/src/from_err.rs +++ b/actix-service/src/from_err.rs @@ -81,23 +81,23 @@ where /// service's error. /// /// This is created by the `NewServiceExt::from_err` method. -pub struct FromErrNewService { +pub struct FromErrNewService { a: A, - e: PhantomData, + e: PhantomData<(E, C)>, } -impl FromErrNewService { +impl FromErrNewService { /// Create new `FromErr` new service instance pub fn new(a: A) -> Self where - A: NewService, + A: NewService, E: From, { Self { a, e: PhantomData } } } -impl Clone for FromErrNewService +impl Clone for FromErrNewService where A: Clone, { @@ -109,9 +109,9 @@ where } } -impl NewService for FromErrNewService +impl NewService for FromErrNewService where - A: NewService, + A: NewService, E: From, { type Request = A::Request; @@ -120,28 +120,28 @@ where type Service = FromErr; type InitError = A::InitError; - type Future = FromErrNewServiceFuture; + type Future = FromErrNewServiceFuture; - fn new_service(&self) -> Self::Future { + fn new_service(&self, cfg: &C) -> Self::Future { FromErrNewServiceFuture { - fut: self.a.new_service(), + fut: self.a.new_service(cfg), e: PhantomData, } } } -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; @@ -208,7 +208,7 @@ mod tests { fn test_new_service() { let blank = || Ok::<_, ()>(Srv); let new_srv = blank.into_new_service().from_err::(); - if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { + if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() { let res = srv.call(()).poll(); assert!(res.is_err()); assert_eq!(res.err().unwrap(), Error); diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index a960782e..73dbcd50 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -188,7 +188,9 @@ 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. -pub trait NewService { +/// +/// `Config` parameter defines service factory configuration type. +pub trait NewService { /// Requests handled by the service. type Request; @@ -212,7 +214,7 @@ pub trait NewService { type Future: Future; /// Create and return a new service value asynchronously. - fn new_service(&self) -> Self::Future; + fn new_service(&self, cfg: &Config) -> Self::Future; /// Apply function to specified service and use it as a next service in /// chain. @@ -220,14 +222,14 @@ pub trait NewService { self, transform: T1, service: B1, - ) -> AndThenTransformNewService + ) -> AndThenTransformNewService where Self: Sized, T: NewTransform, T::Error: From, T1: IntoNewTransform, - B: NewService, - B1: IntoNewService, + B: NewService, + B1: IntoNewService, { AndThenTransformNewService::new( transform.into_new_transform(), @@ -238,11 +240,15 @@ pub trait NewService { /// Apply function to specified service and use it as a next service in /// chain. - fn apply_fn(self, service: I, f: F) -> AndThenApplyNewService + fn apply_fn( + self, + service: I, + f: F, + ) -> 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, @@ -251,11 +257,12 @@ pub trait NewService { } /// Call another service after call to this one has resolved successfully. - fn and_then(self, new_service: F) -> AndThenNewService + fn and_then(self, new_service: F) -> AndThenNewService where Self: Sized, - F: IntoNewService, + F: IntoNewService, B: NewService< + Config, Request = Self::Response, Error = Self::Error, InitError = Self::InitError, @@ -270,7 +277,7 @@ pub trait NewService { /// /// Note that this function consumes the receiving new service and returns a /// wrapped version of it. - fn from_err(self) -> FromErrNewService + fn from_err(self) -> FromErrNewService where Self: Sized, E: From, @@ -284,11 +291,12 @@ pub trait NewService { /// /// Note that this function consumes the receiving future and returns a /// wrapped version of it. - fn then(self, new_service: F) -> ThenNewService + fn then(self, new_service: F) -> ThenNewService where Self: Sized, - F: IntoNewService, + F: IntoNewService, B: NewService< + Config, Request = Result, Error = Self::Error, InitError = Self::InitError, @@ -299,7 +307,7 @@ pub trait NewService { /// Map this service's output to a different type, returning a new service /// of the resulting type. - fn map(self, f: F) -> MapNewService + fn map(self, f: F) -> MapNewService where Self: Sized, F: FnMut(Self::Response) -> R, @@ -308,7 +316,7 @@ pub trait NewService { } /// Map this service's error to a different error, returning a new service. - fn map_err(self, f: F) -> MapErrNewService + fn map_err(self, f: F) -> MapErrNewService where Self: Sized, F: Fn(Self::Error) -> E, @@ -317,7 +325,7 @@ pub trait NewService { } /// Map this service's init error to a different error, returning a new service. - fn map_init_err(self, f: F) -> MapInitErr + fn map_init_err(self, f: F) -> MapInitErr where Self: Sized, F: Fn(Self::InitError) -> E, @@ -362,9 +370,9 @@ where } } -impl NewService for F +impl NewService for F where - F: Fn() -> R, + F: Fn(&C) -> R, R: IntoFuture, S: Service, { @@ -375,14 +383,14 @@ where type InitError = E; type Future = R::Future; - fn new_service(&self) -> Self::Future { - (*self)().into_future() + fn new_service(&self, cfg: &C) -> Self::Future { + (*self)(cfg).into_future() } } -impl NewService for Rc +impl NewService for Rc where - S: NewService, + S: NewService, { type Request = S::Request; type Response = S::Response; @@ -391,14 +399,14 @@ where type InitError = S::InitError; type Future = S::Future; - fn new_service(&self) -> S::Future { - self.as_ref().new_service() + fn new_service(&self, cfg: &C) -> S::Future { + self.as_ref().new_service(cfg) } } -impl NewService for Arc +impl NewService for Arc where - S: NewService, + S: NewService, { type Request = S::Request; type Response = S::Response; @@ -407,8 +415,8 @@ where type InitError = S::InitError; type Future = S::Future; - fn new_service(&self) -> S::Future { - self.as_ref().new_service() + fn new_service(&self, cfg: &C) -> S::Future { + self.as_ref().new_service(cfg) } } @@ -422,9 +430,9 @@ where } /// Trait for types that can be converted to a `NewService` -pub trait IntoNewService +pub trait IntoNewService where - T: NewService, + T: NewService, { /// Convert to an `NewService` fn into_new_service(self) -> T; @@ -439,9 +447,9 @@ where } } -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 7055448d..a98d14f5 100644 --- a/actix-service/src/map.rs +++ b/actix-service/src/map.rs @@ -97,18 +97,18 @@ where } /// `MapNewService` new service combinator -pub struct MapNewService { +pub struct MapNewService { a: A, f: F, - r: PhantomData, + r: PhantomData<(Res, Cfg)>, } -impl MapNewService { +impl MapNewService { /// Create new `Map` new service instance pub fn new(a: A, f: F) -> Self where - A: NewService, - F: FnMut(A::Response) -> Response, + A: NewService, + F: FnMut(A::Response) -> Res, { Self { a, @@ -118,7 +118,7 @@ impl MapNewService { } } -impl Clone for MapNewService +impl Clone for MapNewService where A: Clone, F: Clone, @@ -132,49 +132,49 @@ where } } -impl NewService for MapNewService +impl NewService for MapNewService where - A: NewService, - F: FnMut(A::Response) -> Response + Clone, + A: NewService, + F: FnMut(A::Response) -> Res + Clone, { type Request = A::Request; - type Response = Response; + type Response = Res; type Error = A::Error; - type Service = Map; + 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()) + fn new_service(&self, cfg: &Cfg) -> Self::Future { + MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) } } -pub struct MapNewServiceFuture +pub struct MapNewServiceFuture where - A: NewService, - F: FnMut(A::Response) -> Response, + A: NewService, + F: FnMut(A::Response) -> Res, { fut: A::Future, f: Option, } -impl MapNewServiceFuture +impl MapNewServiceFuture where - A: NewService, - F: FnMut(A::Response) -> Response, + A: NewService, + F: FnMut(A::Response) -> Res, { fn new(fut: A::Future, f: F) -> Self { MapNewServiceFuture { f: Some(f), fut } } } -impl Future for MapNewServiceFuture +impl Future for MapNewServiceFuture where - A: NewService, - F: FnMut(A::Response) -> Response, + A: NewService, + F: FnMut(A::Response) -> Res, { - type Item = Map; + type Item = Map; type Error = A::InitError; fn poll(&mut self) -> Poll { @@ -229,7 +229,7 @@ mod tests { fn test_new_service() { let blank = || Ok::<_, ()>(Srv); let new_srv = blank.into_new_service().map(|_| "ok"); - if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { + if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() { let res = srv.call(()).poll(); assert!(res.is_ok()); assert_eq!(res.unwrap(), Async::Ready("ok")); diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index 9a9dccb7..cd15d239 100644 --- a/actix-service/src/map_err.rs +++ b/actix-service/src/map_err.rs @@ -98,17 +98,17 @@ where /// service's error. /// /// This is created by the `NewServiceExt::map_err` method. -pub struct MapErrNewService { +pub struct MapErrNewService { a: A, f: F, - e: PhantomData, + e: PhantomData<(E, C)>, } -impl MapErrNewService { +impl MapErrNewService { /// Create new `MapErr` new service instance pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, F: Fn(A::Error) -> E, { Self { @@ -119,7 +119,7 @@ impl MapErrNewService { } } -impl Clone for MapErrNewService +impl Clone for MapErrNewService where A: Clone, F: Clone, @@ -133,9 +133,9 @@ where } } -impl NewService for MapErrNewService +impl NewService for MapErrNewService where - A: NewService, + A: NewService, F: Fn(A::Error) -> E + Clone, { type Request = A::Request; @@ -144,25 +144,25 @@ where 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()) + fn new_service(&self, cfg: &C) -> Self::Future { + MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) } } -pub struct MapErrNewServiceFuture +pub struct MapErrNewServiceFuture where - A: NewService, + A: NewService, F: Fn(A::Error) -> E, { fut: A::Future, f: F, } -impl MapErrNewServiceFuture +impl MapErrNewServiceFuture where - A: NewService, + A: NewService, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -170,9 +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; @@ -231,7 +231,7 @@ mod tests { fn test_new_service() { let blank = || Ok::<_, ()>(Srv); let new_srv = blank.into_new_service().map_err(|_| "error"); - if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { + if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() { let res = srv.call(()).poll(); assert!(res.is_err()); assert_eq!(res.err().unwrap(), "error"); diff --git a/actix-service/src/map_init_err.rs b/actix-service/src/map_init_err.rs index 2625f5d3..094a4dbe 100644 --- a/actix-service/src/map_init_err.rs +++ b/actix-service/src/map_init_err.rs @@ -5,17 +5,17 @@ use futures::{Future, Poll}; use super::NewService; /// `MapInitErr` service combinator -pub struct MapInitErr { +pub struct MapInitErr { a: A, f: F, - e: PhantomData, + e: PhantomData<(E, C)>, } -impl MapInitErr { +impl MapInitErr { /// Create new `MapInitErr` combinator pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E, { Self { @@ -26,7 +26,7 @@ impl MapInitErr { } } -impl Clone for MapInitErr +impl Clone for MapInitErr where A: Clone, F: Clone, @@ -40,9 +40,9 @@ where } } -impl NewService for MapInitErr +impl NewService for MapInitErr where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E + Clone, { type Request = A::Request; @@ -51,25 +51,25 @@ where 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()) + fn new_service(&self, cfg: &C) -> Self::Future { + MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone()) } } -pub struct MapInitErrFuture +pub struct MapInitErrFuture where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E, { f: F, fut: A::Future, } -impl MapInitErrFuture +impl MapInitErrFuture where - A: NewService, + A: NewService, F: Fn(A::InitError) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -77,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 be541616..c0a31bfd 100644 --- a/actix-service/src/then.rs +++ b/actix-service/src/then.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + use futures::{try_ready, Async, Future, Poll}; use super::{IntoNewService, NewService, Service}; @@ -109,34 +111,38 @@ where } /// `ThenNewService` new service combinator -pub struct ThenNewService { +pub struct ThenNewService { a: A, b: B, + _t: PhantomData, } -impl ThenNewService { +impl ThenNewService { /// Create new `AndThen` combinator pub fn new(a: A, f: F) -> Self where - A: NewService, + A: NewService, B: NewService< + C, Request = Result, Error = A::Error, InitError = A::InitError, >, - F: IntoNewService, + F: IntoNewService, { Self { a, b: f.into_new_service(), + _t: PhantomData, } } } -impl NewService for ThenNewService +impl NewService for ThenNewService where - A: NewService, + A: NewService, B: NewService< + C, Request = Result, Error = A::Error, InitError = A::InitError, @@ -148,14 +154,14 @@ where 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()) + fn new_service(&self, cfg: &C) -> Self::Future { + ThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) } } -impl Clone for ThenNewService +impl Clone for ThenNewService where A: Clone, B: Clone, @@ -164,14 +170,16 @@ where Self { a: self.a.clone(), b: self.b.clone(), + _t: PhantomData, } } } -pub struct ThenNewServiceFuture +pub struct ThenNewServiceFuture where - A: NewService, + A: NewService, B: NewService< + C, Request = Result, Error = A::Error, InitError = A::InitError, @@ -183,10 +191,11 @@ where b: Option, } -impl ThenNewServiceFuture +impl ThenNewServiceFuture where - A: NewService, + A: NewService, B: NewService< + C, Request = Result, Error = A::Error, InitError = A::InitError, @@ -202,10 +211,11 @@ where } } -impl Future for ThenNewServiceFuture +impl Future for ThenNewServiceFuture where - A: NewService, + A: NewService, B: NewService< + C, Request = Result, Error = A::Error, InitError = A::InitError, @@ -319,7 +329,7 @@ mod tests { let cnt2 = cnt.clone(); let blank = move || Ok::<_, ()>(Srv1(cnt2.clone())); let new_srv = blank.into_new_service().then(move || Ok(Srv2(cnt.clone()))); - if let Async::Ready(mut srv) = new_srv.clone().new_service().poll().unwrap() { + if let Async::Ready(mut srv) = new_srv.clone().new_service(&()).poll().unwrap() { let res = srv.call(Ok("srv1")).poll(); assert!(res.is_ok()); assert_eq!(res.unwrap(), Async::Ready(("srv1", "ok"))); diff --git a/actix-utils/Cargo.toml b/actix-utils/Cargo.toml index cceb7339..fb07b69b 100644 --- a/actix-utils/Cargo.toml +++ b/actix-utils/Cargo.toml @@ -18,7 +18,8 @@ name = "actix_utils" path = "src/lib.rs" [dependencies] -actix-service = "0.2.2" +# actix-service = "0.2.2" +actix-service = { path = "../actix-service" } actix-codec = "0.1.0" bytes = "0.4" futures = "0.1.24" diff --git a/actix-utils/src/boxed.rs b/actix-utils/src/boxed.rs index 0de432db..a34e78a1 100644 --- a/actix-utils/src/boxed.rs +++ b/actix-utils/src/boxed.rs @@ -11,14 +11,23 @@ pub type BoxedService = Box< >; /// Create boxed new service -pub fn new_service( +pub fn new_service( service: T, -) -> BoxedNewService +) -> BoxedNewService where - T: NewService + 'static, + C: 'static, + T: NewService + 'static, + T::Request: 'static, + T::Response: 'static, T::Service: 'static, + T::Future: 'static, + T::Error: 'static, + T::InitError: 'static, { - BoxedNewService(Box::new(NewServiceWrapper(service))) + BoxedNewService(Box::new(NewServiceWrapper { + service, + _t: std::marker::PhantomData, + })) } /// Create boxed service @@ -30,8 +39,9 @@ where Box::new(ServiceWrapper(service)) } -type Inner = Box< +type Inner = Box< NewService< + C, Request = Req, Response = Res, Error = Err, @@ -41,9 +51,9 @@ type Inner = Box< >, >; -pub struct BoxedNewService(Inner); +pub struct BoxedNewService(Inner); -impl NewService for BoxedNewService +impl NewService for BoxedNewService where Req: 'static, Res: 'static, @@ -57,20 +67,23 @@ where type Service = BoxedService; type Future = Box>; - fn new_service(&self) -> Self::Future { - self.0.new_service() + fn new_service(&self, cfg: &C) -> Self::Future { + self.0.new_service(cfg) } } -struct NewServiceWrapper(T); +struct NewServiceWrapper> { + service: T, + _t: std::marker::PhantomData, +} -impl NewService for NewServiceWrapper +impl NewService for NewServiceWrapper where Req: 'static, Res: 'static, Err: 'static, InitErr: 'static, - T: NewService, + T: NewService, T::Future: 'static, T::Service: 'static, ::Future: 'static, @@ -82,10 +95,10 @@ where type Service = BoxedService; type Future = Box>; - fn new_service(&self) -> Self::Future { + fn new_service(&self, cfg: &C) -> Self::Future { Box::new( - self.0 - .new_service() + self.service + .new_service(cfg) .map(|service| ServiceWrapper::boxed(service)), ) } diff --git a/actix-utils/src/either.rs b/actix-utils/src/either.rs index 10400281..8946ba9b 100644 --- a/actix-utils/src/either.rs +++ b/actix-utils/src/either.rs @@ -53,10 +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< + C, Request = A::Request, Response = A::Response, Error = A::Error, @@ -66,10 +67,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< + C, Request = A::Request, Response = A::Response, Error = A::Error, @@ -80,10 +82,11 @@ impl Either { } } -impl NewService for Either +impl NewService for Either where - A: NewService, + A: NewService, B: NewService< + C, Request = A::Request, Response = A::Response, Error = A::Error, @@ -95,12 +98,12 @@ where type Error = A::Error; type InitError = A::InitError; type Service = EitherService; - type Future = EitherNewService; + type Future = EitherNewService; - fn new_service(&self) -> Self::Future { + fn new_service(&self, cfg: &C) -> Self::Future { match self { - Either::A(ref inner) => EitherNewService::A(inner.new_service()), - Either::B(ref inner) => EitherNewService::B(inner.new_service()), + Either::A(ref inner) => EitherNewService::A(inner.new_service(cfg)), + Either::B(ref inner) => EitherNewService::B(inner.new_service(cfg)), } } } @@ -115,15 +118,16 @@ impl Clone for Either { } #[doc(hidden)] -pub enum EitherNewService { +pub enum EitherNewService, B: NewService, C> { A(A::Future), B(B::Future), } -impl Future for EitherNewService +impl Future for EitherNewService where - A: NewService, + A: NewService, B: NewService< + C, Request = A::Request, Response = A::Response, Error = A::Error, diff --git a/actix-utils/src/framed.rs b/actix-utils/src/framed.rs index 2e041bba..cee68ba9 100644 --- a/actix-utils/src/framed.rs +++ b/actix-utils/src/framed.rs @@ -15,14 +15,15 @@ use crate::cell::Cell; type Request = ::Item; type Response = ::Item; -pub struct FramedNewService { +pub struct FramedNewService { factory: S, - _t: PhantomData<(T, U)>, + _t: PhantomData<(T, U, C)>, } -impl FramedNewService +impl FramedNewService where - S: NewService, Response = Response>, + C: Clone, + S: NewService, Response = Response>, S::Error: 'static, ::Future: 'static, T: AsyncRead + AsyncWrite, @@ -30,7 +31,7 @@ where ::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, @@ -38,7 +39,7 @@ where } } -impl Clone for FramedNewService +impl Clone for FramedNewService where S: Clone, { @@ -50,9 +51,10 @@ where } } -impl NewService for FramedNewService +impl NewService for FramedNewService where - S: NewService, Response = Response> + Clone, + C: Clone, + S: NewService, Response = Response> + Clone, S::Error: 'static, ::Future: 'static, T: AsyncRead + AsyncWrite, @@ -64,48 +66,53 @@ where type Response = FramedTransport; type Error = S::InitError; type InitError = S::InitError; - type Service = FramedService; + type Service = FramedService; type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, cfg: &C) -> Self::Future { ok(FramedService { factory: self.factory.clone(), + config: cfg.clone(), _t: PhantomData, }) } } -pub struct FramedService { +pub struct FramedService { factory: S, + config: C, _t: PhantomData<(T, U)>, } -impl Clone for FramedService +impl Clone for FramedService where S: Clone, + C: Clone, { fn clone(&self) -> Self { Self { factory: self.factory.clone(), + config: self.config.clone(), _t: PhantomData, } } } -impl Service for FramedService +impl Service for FramedService where - S: NewService, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, ::Error: std::fmt::Debug, + C: Clone, { type Request = Framed; type Response = FramedTransport; type Error = S::InitError; - type Future = FramedServiceResponseFuture; + type Future = FramedServiceResponseFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { Ok(Async::Ready(())) @@ -113,17 +120,16 @@ where fn call(&mut self, req: Framed) -> Self::Future { FramedServiceResponseFuture { - fut: self.factory.new_service(), - + fut: self.factory.new_service(&self.config), framed: Some(req), } } } #[doc(hidden)] -pub struct FramedServiceResponseFuture +pub struct FramedServiceResponseFuture where - S: NewService, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, ::Future: 'static, T: AsyncRead + AsyncWrite, @@ -135,9 +141,9 @@ where framed: Option>, } -impl Future for FramedServiceResponseFuture +impl Future for FramedServiceResponseFuture where - S: NewService, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, ::Future: 'static, T: AsyncRead + AsyncWrite, @@ -402,7 +408,7 @@ where } } -impl NewService for IntoFramed +impl NewService<()> for IntoFramed where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, @@ -415,7 +421,7 @@ where type Service = IntoFramedService; type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, _: &()) -> Self::Future { ok(IntoFramedService { factory: self.factory.clone(), _t: PhantomData, diff --git a/actix-utils/src/keepalive.rs b/actix-utils/src/keepalive.rs index 6cd5c504..d119773d 100644 --- a/actix-utils/src/keepalive.rs +++ b/actix-utils/src/keepalive.rs @@ -44,7 +44,7 @@ where } } -impl NewService for KeepAlive +impl NewService<()> for KeepAlive where F: Fn() -> E + Clone, { @@ -55,7 +55,7 @@ where type Service = KeepAliveService; type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, _: &()) -> Self::Future { ok(KeepAliveService::new( self.ka, self.time.timer(), diff --git a/actix-utils/src/stream.rs b/actix-utils/src/stream.rs index 54a4c2fa..e98659c9 100644 --- a/actix-utils/src/stream.rs +++ b/actix-utils/src/stream.rs @@ -29,20 +29,21 @@ where } } -pub struct StreamNewService { +pub struct StreamNewService { factory: Rc, - _t: PhantomData<(S, E)>, + _t: PhantomData<(S, E, C)>, } -impl StreamNewService +impl StreamNewService where + C: Clone, S: IntoStream, - T: NewService, Response = (), Error = E, InitError = E>, + T: NewService, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: '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, @@ -50,7 +51,7 @@ where } } -impl Clone for StreamNewService { +impl Clone for StreamNewService { fn clone(&self) -> Self { Self { factory: self.factory.clone(), @@ -59,10 +60,11 @@ impl Clone for StreamNewService { } } -impl NewService for StreamNewService +impl NewService for StreamNewService where + C: Clone, S: IntoStream + 'static, - T: NewService, Response = (), Error = E, InitError = E>, + T: NewService, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, ::Future: 'static, @@ -71,29 +73,32 @@ where type Response = (); type Error = E; type InitError = E; - type Service = StreamService; + type Service = StreamService; type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, cfg: &C) -> Self::Future { ok(StreamService { factory: self.factory.clone(), + config: cfg.clone(), _t: PhantomData, }) } } -pub struct StreamService { +pub struct StreamService { factory: Rc, + config: C, _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, + C: Clone, { type Request = S; type Response = (); @@ -107,7 +112,7 @@ where fn call(&mut self, req: S) -> Self::Future { Box::new( self.factory - .new_service() + .new_service(&self.config) .and_then(move |srv| StreamDispatcher::new(req, srv)), ) } @@ -227,7 +232,7 @@ impl Clone for TakeItem { } } -impl NewService for TakeItem { +impl NewService<()> for TakeItem { type Request = T; type Response = (Option, T); type Error = T::Error; @@ -235,7 +240,7 @@ impl NewService for TakeItem { type Service = TakeItemService; type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, _: &()) -> Self::Future { ok(TakeItemService { _t: PhantomData }) } } diff --git a/actix-utils/src/time.rs b/actix-utils/src/time.rs index c7352406..06d853bc 100644 --- a/actix-utils/src/time.rs +++ b/actix-utils/src/time.rs @@ -42,7 +42,7 @@ impl Default for LowResTime { } } -impl NewService for LowResTime { +impl NewService<()> for LowResTime { type Request = (); type Response = Instant; type Error = Never; @@ -50,7 +50,7 @@ impl NewService for LowResTime { type Service = LowResTimeService; type Future = FutureResult; - fn new_service(&self) -> Self::Future { + fn new_service(&self, _: &()) -> Self::Future { ok(self.timer()) } }