diff --git a/CHANGES.md b/CHANGES.md index c282cc031..b4502f3d5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,10 @@ # Changes +## [0.3.0] - xxx + +* Use new `Service` trait + + ## [0.2.4] - 2018-11-21 ### Added diff --git a/Cargo.toml b/Cargo.toml index 5e00dca5e..f36384081 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-net" -version = "0.2.4" +version = "0.3.0" authors = ["Nikolay Kim "] description = "Actix net - framework for the compisible network services for Rust (experimental)" readme = "README.md" @@ -57,7 +57,7 @@ tokio-tcp = "0.1" tokio-timer = "0.2" tokio-reactor = "0.1" tokio-current-thread = "0.1" -tower-service = "0.1" +tower-service = { path="../../actix/tower/tower-service/" } trust-dns-proto = "^0.5.0" trust-dns-resolver = "^0.10.0" diff --git a/src/cloneable.rs b/src/cloneable.rs index ec3fd0b44..1e388ec10 100644 --- a/src/cloneable.rs +++ b/src/cloneable.rs @@ -1,31 +1,35 @@ +use std::marker::PhantomData; + use futures::Poll; use super::cell::Cell; use super::service::Service; /// Service that allows to turn non-clone service to a service with `Clone` impl -pub struct CloneableService { +pub struct CloneableService + 'static, R> { service: Cell, + _t: PhantomData, } -impl CloneableService { +impl + 'static, R> CloneableService { pub fn new(service: S) -> Self { Self { service: Cell::new(service), + _t: PhantomData, } } } -impl Clone for CloneableService { +impl + 'static, R> Clone for CloneableService { fn clone(&self) -> Self { Self { service: self.service.clone(), + _t: PhantomData, } } } -impl Service for CloneableService { - type Request = S::Request; +impl + 'static, R> Service for CloneableService { type Response = S::Response; type Error = S::Error; type Future = S::Future; @@ -34,7 +38,7 @@ impl Service for CloneableService { self.service.borrow_mut().poll_ready() } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: R) -> Self::Future { self.service.borrow_mut().call(req) } } diff --git a/src/connector.rs b/src/connector.rs index e244f3e46..d82f5dbc1 100644 --- a/src/connector.rs +++ b/src/connector.rs @@ -168,23 +168,24 @@ impl Connector { /// Create new connector with custom resolver pub fn with_resolver( resolver: Resolver, - ) -> impl Service - + Clone { + ) -> impl Service + Clone + { Connector { resolver } } - /// Create new default connector service - pub fn new_service_with_config( - cfg: ResolverConfig, - opts: ResolverOpts, - ) -> impl NewService< - Request = Connect, - Response = (Connect, TcpStream), - Error = ConnectorError, - InitError = E, - > + Clone { - move || -> FutureResult { ok(Connector::new(cfg.clone(), opts)) } - } + // /// Create new default connector service + // pub fn new_service_with_config( + // cfg: ResolverConfig, + // opts: ResolverOpts, + // ) -> impl NewService< + // Connect, + // Response = (Connect, TcpStream), + // Error = ConnectorError, + // InitError = E, + // Service = impl Service + Clone, + // > + Clone { + // move || -> FutureResult { ok(Connector::new(cfg.clone(), opts)) } + // } } impl Clone for Connector { @@ -195,8 +196,7 @@ impl Clone for Connector { } } -impl Service for Connector { - type Request = Connect; +impl Service for Connector { type Response = (Connect, TcpStream); type Error = ConnectorError; type Future = Either; @@ -205,7 +205,7 @@ impl Service for Connector { Ok(Async::Ready(())) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Connect) -> Self::Future { match req.kind { ConnectKind::Host { host: _, port: _ } => Either::A(ConnectorFuture { fut: self.resolver.call(req), @@ -273,8 +273,7 @@ impl Default for TcpConnector { } } -impl Service for TcpConnector { - type Request = (T, VecDeque); +impl Service<(T, VecDeque)> for TcpConnector { type Response = (T, TcpStream); type Error = io::Error; type Future = TcpConnectorResponse; @@ -283,7 +282,7 @@ impl Service for TcpConnector { Ok(Async::Ready(())) } - fn call(&mut self, (req, addrs): Self::Request) -> Self::Future { + fn call(&mut self, (req, addrs): (T, VecDeque)) -> Self::Future { TcpConnectorResponse::new(req, addrs) } } @@ -354,8 +353,7 @@ impl DefaultConnector { } } -impl Service for DefaultConnector { - type Request = Connect; +impl Service for DefaultConnector { type Response = TcpStream; type Error = ConnectorError; type Future = DefaultConnectorFuture; @@ -364,7 +362,7 @@ impl Service for DefaultConnector { self.0.poll_ready() } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Connect) -> Self::Future { DefaultConnectorFuture { fut: self.0.call(req), } diff --git a/src/either.rs b/src/either.rs index efc6b3d52..677ccf450 100644 --- a/src/either.rs +++ b/src/either.rs @@ -13,12 +13,11 @@ pub enum EitherService { B(B), } -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; @@ -30,7 +29,7 @@ where } } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: 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)), @@ -44,22 +43,16 @@ pub enum Either { B(B), } -impl NewService for Either +impl NewService for Either where - A: NewService, - B: NewService< - Request = A::Request, - Response = A::Response, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, { - type Request = A::Request; type Response = A::Response; type Error = A::Error; type InitError = A::InitError; type Service = EitherService; - type Future = EitherNewService; + type Future = EitherNewService; fn new_service(&self) -> Self::Future { match self { @@ -70,20 +63,15 @@ where } #[doc(hidden)] -pub enum EitherNewService { +pub enum EitherNewService, B: NewService, R> { A(A::Future), B(B::Future), } -impl Future for EitherNewService +impl Future for EitherNewService where - A: NewService, - B: NewService< - Request = A::Request, - Response = A::Response, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, { type Item = EitherService; type Error = A::InitError; diff --git a/src/framed.rs b/src/framed.rs index 69cd6bd68..9ea8f71fd 100644 --- a/src/framed.rs +++ b/src/framed.rs @@ -24,13 +24,13 @@ impl FramedNewService where T: AsyncRead + AsyncWrite, U: Decoder + Encoder, - S: NewService, Response = Response> + Clone, - <::Service as Service>::Future: 'static, - <::Service as Service>::Error: 'static, + S: NewService, Response = Response>, + <>>::Service as Service>>::Future: 'static, + <>>::Service as Service>>::Error: 'static, ::Item: 'static, ::Error: 'static, { - pub fn new>(factory: F1) -> Self { + pub fn new>>(factory: F1) -> Self { Self { factory: factory.into_new_service(), _t: PhantomData, @@ -50,17 +50,16 @@ where } } -impl NewService for FramedNewService +impl NewService> for FramedNewService where T: AsyncRead + AsyncWrite, U: Decoder + Encoder, - S: NewService, Response = Response> + Clone, - <::Service as Service>::Future: 'static, - <::Service as Service>::Error: 'static, + S: NewService, Response = Response> + Clone, + <>>::Service as Service>>::Future: 'static, + <>>::Service as Service>>::Error: 'static, ::Item: 'static, ::Error: 'static, { - type Request = Framed; type Response = FramedTransport; type Error = S::InitError; type InitError = S::InitError; @@ -92,17 +91,16 @@ where } } -impl Service for FramedService +impl Service> for FramedService where T: AsyncRead + AsyncWrite, U: Decoder + Encoder, - S: NewService, Response = Response>, - <::Service as Service>::Future: 'static, - <::Service as Service>::Error: 'static, + S: NewService, Response = Response>, + <>>::Service as Service>>::Future: 'static, + <>>::Service as Service>>::Error: 'static, ::Item: 'static, ::Error: 'static, { - type Request = Framed; type Response = FramedTransport; type Error = S::InitError; type Future = FramedServiceResponseFuture; @@ -111,7 +109,7 @@ where Ok(Async::Ready(())) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Framed) -> Self::Future { FramedServiceResponseFuture { fut: self.factory.new_service(), @@ -125,9 +123,9 @@ pub struct FramedServiceResponseFuture where T: AsyncRead + AsyncWrite, U: Decoder + Encoder, - S: NewService, Response = Response>, - <::Service as Service>::Future: 'static, - <::Service as Service>::Error: 'static, + S: NewService, Response = Response>, + <>>::Service as Service>>::Future: 'static, + <>>::Service as Service>>::Error: 'static, ::Item: 'static, ::Error: 'static, { @@ -139,9 +137,9 @@ impl Future for FramedServiceResponseFuture where T: AsyncRead + AsyncWrite, U: Decoder + Encoder, - S: NewService, Response = Response>, - <::Service as Service>::Future: 'static, - <::Service as Service>::Error: 'static, + S: NewService, Response = Response>, + <>>::Service as Service>>::Future: 'static, + <>>::Service as Service>>::Error: 'static, ::Item: 'static, ::Error: 'static, { @@ -176,7 +174,7 @@ impl From for FramedTransportError { /// and pass then to the service. pub struct FramedTransport where - S: Service, + S: Service, Response = Response>, T: AsyncRead + AsyncWrite, U: Encoder + Decoder, { @@ -190,7 +188,7 @@ where flushed: bool, } -enum TransportState { +enum TransportState>, U: Encoder + Decoder> { Processing, Error(FramedTransportError), EncoderError(FramedTransportError), @@ -201,12 +199,12 @@ impl FramedTransport where T: AsyncRead + AsyncWrite, U: Decoder + Encoder, - S: Service, Response = Response>, + S: Service, Response = Response>, S::Future: 'static, S::Error: 'static, ::Error: 'static, { - pub fn new>(framed: Framed, service: F) -> Self { + pub fn new>>(framed: Framed, service: F) -> Self { let (write_tx, write_rx) = mpsc::channel(16); FramedTransport { framed, @@ -248,7 +246,7 @@ impl FramedTransport where T: AsyncRead + AsyncWrite, U: Decoder + Encoder, - S: Service, Response = Response>, + S: Service, Response = Response>, S::Future: 'static, S::Error: 'static, ::Item: 'static, @@ -377,7 +375,7 @@ impl Future for FramedTransport where T: AsyncRead + AsyncWrite, U: Decoder + Encoder, - S: Service, Response = Response>, + S: Service, Response = Response>, S::Future: 'static, S::Error: 'static, ::Item: 'static, @@ -437,13 +435,12 @@ where } } -impl NewService for IntoFramed +impl NewService for IntoFramed where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, U: Encoder + Decoder, { - type Request = T; type Response = Framed; type Error = (); type InitError = (); @@ -468,13 +465,12 @@ where _t: PhantomData<(T,)>, } -impl Service for IntoFramedService +impl Service for IntoFramedService where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, U: Encoder + Decoder, { - type Request = T; type Response = Framed; type Error = (); type Future = FutureResult; @@ -483,7 +479,7 @@ where Ok(Async::Ready(())) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: T) -> Self::Future { ok(Framed::new(req, (self.factory)())) } } diff --git a/src/inflight.rs b/src/inflight.rs index 6ee82a8c1..acda9dfb5 100644 --- a/src/inflight.rs +++ b/src/inflight.rs @@ -12,11 +12,12 @@ pub struct InFlight { max_inflight: usize, } -impl InFlight -where - T: NewService, -{ - pub fn new>(factory: F) -> Self { +impl InFlight { + pub fn new(factory: F) -> Self + where + T: NewService, + F: IntoNewService, + { Self { factory: factory.into_new_service(), max_inflight: 15, @@ -32,16 +33,15 @@ where } } -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 +51,12 @@ where } } -pub struct InFlightResponseFuture { +pub struct InFlightResponseFuture, Request> { fut: T::Future, max_inflight: usize, } -impl Future for InFlightResponseFuture { +impl, Request> Future for InFlightResponseFuture { type Item = InFlightService; type Error = T::InitError; @@ -73,15 +73,23 @@ pub struct InFlightService { count: Counter, } -impl InFlightService { - pub fn new>(service: F) -> Self { +impl InFlightService { + pub fn new(service: F) -> Self + where + T: Service, + F: IntoService, + { Self { service: service.into_service(), count: Counter::new(15), } } - 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, + { Self { service: service.into_service(), count: Counter::new(max), @@ -89,11 +97,13 @@ impl InFlightService { } } -impl Service for InFlightService { - type Request = T::Request; +impl Service for InFlightService +where + T: Service, +{ 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(); @@ -103,22 +113,21 @@ impl Service for InFlightService { res } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Request) -> Self::Future { InFlightServiceResponse { fut: self.service.call(req), - guard: self.count.get(), + _guard: self.count.get(), } } } #[doc(hidden)] -pub struct InFlightServiceResponse { +pub struct InFlightServiceResponse, Request> { fut: T::Future, - #[allow(dead_code)] - guard: CounterGuard, + _guard: CounterGuard, } -impl Future for InFlightServiceResponse { +impl, Request> Future for InFlightServiceResponse { type Item = T::Response; type Error = T::Error; diff --git a/src/keepalive.rs b/src/keepalive.rs index 5ee0892d4..d3e31f330 100644 --- a/src/keepalive.rs +++ b/src/keepalive.rs @@ -32,7 +32,7 @@ where impl Clone for KeepAlive where - F: Fn() -> E + Clone, + F: Clone, { fn clone(&self) -> Self { KeepAlive { @@ -44,11 +44,10 @@ where } } -impl NewService for KeepAlive +impl NewService for KeepAlive where F: Fn() -> E + Clone, { - type Request = R; type Response = R; type Error = E; type InitError = Never; @@ -90,11 +89,10 @@ where } } -impl Service for KeepAliveService +impl Service for KeepAliveService where F: Fn() -> E, { - type Request = R; type Response = R; type Error = E; type Future = FutureResult; @@ -116,7 +114,7 @@ where } } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: R) -> Self::Future { self.expire = self.time.now() + self.ka; ok(req) } diff --git a/src/resolver.rs b/src/resolver.rs index 28196fee0..0c87c78fe 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -70,8 +70,7 @@ impl Clone for Resolver { } } -impl Service for Resolver { - type Request = T; +impl Service for Resolver { type Response = (T, VecDeque); type Error = ResolveError; type Future = ResolverFuture; @@ -80,7 +79,7 @@ impl Service for Resolver { Ok(Async::Ready(())) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: T) -> Self::Future { ResolverFuture::new(req, &self.resolver) } } diff --git a/src/server/config.rs b/src/server/config.rs index 6fba8f92a..d959c3028 100644 --- a/src/server/config.rs +++ b/src/server/config.rs @@ -154,8 +154,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, @@ -176,7 +176,7 @@ impl ServiceRuntime { type BoxedNewService = Box< NewService< - Request = (Option, ServerMessage), + (Option, ServerMessage), Response = (), Error = (), InitError = (), @@ -189,15 +189,14 @@ struct ServiceFactory { inner: T, } -impl NewService for ServiceFactory +impl NewService<(Option, ServerMessage)> for ServiceFactory where - T: NewService, + T: NewService, T::Future: 'static, T::Service: 'static, T::Error: 'static, T::InitError: fmt::Debug + 'static, { - type Request = (Option, ServerMessage); type Response = (); type Error = (); type InitError = (); diff --git a/src/server/services.rs b/src/server/services.rs index 58be7f737..151b62006 100644 --- a/src/server/services.rs +++ b/src/server/services.rs @@ -22,13 +22,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; } @@ -43,7 +43,7 @@ pub(crate) trait InternalServiceFactory: Send { pub(crate) type BoxedServerService = Box< Service< - Request = (Option, ServerMessage), + (Option, ServerMessage), Response = (), Error = (), Future = FutureResult<(), ()>, @@ -60,13 +60,12 @@ impl StreamService { } } -impl Service for StreamService +impl Service<(Option, ServerMessage)> for StreamService where - T: Service, + T: Service, T::Future: 'static, T::Error: 'static, { - type Request = (Option, ServerMessage); type Response = (); type Error = (); type Future = FutureResult<(), ()>; @@ -107,13 +106,12 @@ impl ServerService { } } -impl Service for ServerService +impl Service<(Option, ServerMessage)> for ServerService where - T: Service, + T: Service, T::Future: 'static, T::Error: 'static, { - type Request = (Option, ServerMessage); type Response = (); type Error = (); type Future = FutureResult<(), ()>; @@ -240,7 +238,7 @@ impl InternalServiceFactory for Box { impl ServiceFactory for F where F: Fn() -> T + Send + Clone + 'static, - T: NewService, + T: NewService, { type NewService = T; @@ -252,7 +250,7 @@ where impl StreamServiceFactory for F where F: Fn() -> T + Send + Clone + 'static, - T: NewService, + T: NewService, { type NewService = T; diff --git a/src/service/and_then.rs b/src/service/and_then.rs index 9dd672770..953ba8dbb 100644 --- a/src/service/and_then.rs +++ b/src/service/and_then.rs @@ -12,21 +12,21 @@ pub struct AndThen { b: Cell, } -impl AndThen -where - A: Service, - B: Service, -{ +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, + { Self { a, b: Cell::new(b) } } } impl Clone for AndThen where - A: Service + Clone, - B: Service, + A: Clone, + B: Clone, { fn clone(&self) -> Self { AndThen { @@ -36,40 +36,39 @@ where } } -impl Service for AndThen +impl Service for AndThen where - A: Service, - B: Service, + A: Service, + B: Service, { - type Request = A::Request; type Response = B::Response; type Error = A::Error; - type Future = AndThenFuture; + type Future = AndThenFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { let _ = try_ready!(self.a.poll_ready()); self.b.borrow_mut().poll_ready() } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: 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: A::Future, } -impl AndThenFuture +impl AndThenFuture where - A: Service, - B: Service, + A: Service, + B: Service, { fn new(fut_a: A::Future, b: Cell) -> Self { AndThenFuture { @@ -80,10 +79,10 @@ where } } -impl Future for AndThenFuture +impl Future for AndThenFuture where - A: Service, - B: Service, + A: Service, + B: Service, B::Error: Into, { type Item = B::Response; @@ -111,13 +110,13 @@ pub struct AndThenNewService { b: B, } -impl AndThenNewService -where - A: NewService, - B: NewService, -{ +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, + { Self { a, b: f.into_new_service(), @@ -125,18 +124,17 @@ where } } -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()) @@ -145,8 +143,8 @@ where impl Clone for AndThenNewService where - A: NewService + Clone, - B: NewService + Clone, + A: Clone, + B: Clone, { fn clone(&self) -> Self { Self { @@ -156,10 +154,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, @@ -167,10 +165,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 { @@ -182,10 +180,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; diff --git a/src/service/apply.rs b/src/service/apply.rs index 9161775c6..f4d559ae5 100644 --- a/src/service/apply.rs +++ b/src/service/apply.rs @@ -5,21 +5,23 @@ use futures::{Async, Future, IntoFuture, Poll}; use super::{IntoNewService, IntoService, NewService, Service}; /// `Apply` service combinator -pub struct Apply { +pub struct Apply +where + T: Service, +{ service: T, f: F, - r: PhantomData<(Req, R)>, + r: PhantomData<(In, Out, Request)>, } -impl Apply +impl Apply where - T: Service, - T::Error: Into<::Error>, - F: Fn(Req, &mut T) -> R, - R: IntoFuture, + T: Service, + F: Fn(In, &mut T) -> Out, + Out: IntoFuture, { /// 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, @@ -28,12 +30,10 @@ where } } -impl Clone for Apply +impl Clone for Apply where - T: Service + Clone, - T::Error: Into<::Error>, - F: Fn(Req, &mut T) -> R + Clone, - R: IntoFuture, + T: Service + Clone, + F: Clone, { fn clone(&self) -> Self { Apply { @@ -44,42 +44,43 @@ where } } -impl Service for Apply +impl Service for Apply where - T: Service, - T::Error: Into<::Error>, - F: Fn(Req, &mut T) -> R, - R: IntoFuture, + T: Service, + F: Fn(In, &mut T) -> Out, + Out: IntoFuture, { - type Request = Req; - type Response = ::Item; - type Error = ::Error; - type Future = R::Future; + type Response = ::Item; + type Error = ::Error; + type Future = Out::Future; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready().map_err(|e| e.into()) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: In) -> Self::Future { (self.f)(req, &mut self.service).into_future() } } /// `ApplyNewService` new service combinator -pub struct ApplyNewService { +pub struct ApplyNewService +where + T: NewService, +{ service: T, f: F, - r: PhantomData R>, + r: PhantomData<(In, Out, Request)>, } -impl ApplyNewService +impl ApplyNewService where - T: NewService, - F: Fn(Req, &mut T::Service) -> R, - R: IntoFuture, + T: NewService, + F: Fn(In, &mut T::Service) -> Out, + Out: IntoFuture, { /// 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(), @@ -88,11 +89,11 @@ where } } -impl Clone for ApplyNewService +impl Clone for ApplyNewService where - T: NewService + Clone, - F: Fn(Req, &mut T::Service) -> R + Clone, - R: IntoFuture, + T: NewService + Clone, + F: Fn(Out, &mut T::Service) -> Out + Clone, + Out: IntoFuture, { fn clone(&self) -> Self { Self { @@ -103,42 +104,40 @@ where } } -impl NewService for ApplyNewService +impl NewService for ApplyNewService where - T: NewService, - T::Error: Into<::Error>, - F: Fn(Req, &mut T::Service) -> R + Clone, - R: IntoFuture, + T: NewService, + F: Fn(In, &mut T::Service) -> Out + Clone, + Out: IntoFuture, { - type Request = Req; - type Response = ::Item; - type Error = ::Error; - type Service = Apply; + type Response = ::Item; + type Error = ::Error; + 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, - F: Fn(Req, &mut T::Service) -> R, - R: IntoFuture, + T: NewService, + F: Fn(In, &mut T::Service) -> Out, + Out: IntoFuture, { fut: T::Future, f: Option, - r: PhantomData R>, + r: PhantomData<(In, Out)>, } -impl ApplyNewServiceFuture +impl ApplyNewServiceFuture where - T: NewService, - F: Fn(Req, &mut T::Service) -> R, - R: IntoFuture, + T: NewService, + F: Fn(In, &mut T::Service) -> Out, + Out: IntoFuture, { fn new(fut: T::Future, f: F) -> Self { ApplyNewServiceFuture { @@ -149,14 +148,13 @@ where } } -impl Future for ApplyNewServiceFuture +impl Future for ApplyNewServiceFuture where - T: NewService, - T::Error: Into<::Error>, - F: Fn(Req, &mut T::Service) -> R, - R: IntoFuture, + T: NewService, + F: Fn(In, &mut T::Service) -> Out, + Out: IntoFuture, { - type Item = Apply; + type Item = Apply; type Error = T::InitError; fn poll(&mut self) -> Poll { diff --git a/src/service/fn_service.rs b/src/service/fn_service.rs index e81ca6429..d6e19e8a4 100644 --- a/src/service/fn_service.rs +++ b/src/service/fn_service.rs @@ -42,12 +42,11 @@ where } } -impl Service for FnService +impl Service for FnService where F: Fn(Req) -> Fut, Fut: IntoFuture, { - type Request = Req; type Response = Resp; type Error = E; type Future = Fut::Future; @@ -61,7 +60,7 @@ where } } -impl IntoService> for F +impl IntoService, Req> for F where F: Fn(Req) -> Fut + 'static, Fut: IntoFuture, @@ -93,12 +92,11 @@ where } } -impl NewService for FnNewService +impl NewService for FnNewService where F: Fn(Req) -> Fut + Clone, Fut: IntoFuture, { - type Request = Req; type Response = Resp; type Error = Err; type Service = FnService; @@ -110,7 +108,7 @@ where } } -impl IntoNewService> for F +impl IntoNewService, Req> for F where F: Fn(Req) -> Fut + Clone + 'static, Fut: IntoFuture, diff --git a/src/service/from_err.rs b/src/service/from_err.rs index 3c6961a0f..a82d662e2 100644 --- a/src/service/from_err.rs +++ b/src/service/from_err.rs @@ -7,16 +7,17 @@ use super::{NewService, Service}; /// Service for the `from_err` combinator, changing the error type of a service. /// /// This is created by the `ServiceExt::from_err` method. -pub struct FromErr -where - A: Service, -{ +pub struct FromErr { service: A, f: PhantomData, } -impl> FromErr { - pub(crate) fn new(service: A) -> Self { +impl FromErr { + pub(crate) fn new(service: A) -> Self + where + A: Service, + E: From, + { FromErr { service, f: PhantomData, @@ -26,8 +27,7 @@ impl> FromErr { impl Clone for FromErr where - A: Service + Clone, - E: From, + A: Clone, { fn clone(&self) -> Self { FromErr { @@ -37,21 +37,20 @@ where } } -impl Service for FromErr +impl Service for FromErr where - A: Service, + A: Service, E: From, { - type Request = A::Request; type Response = A::Response; type Error = E; - type Future = FromErrFuture; + type Future = FromErrFuture; fn poll_ready(&mut self) -> Poll<(), E> { Ok(self.service.poll_ready().map_err(E::from)?) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Request) -> Self::Future { FromErrFuture { fut: self.service.call(req), f: PhantomData, @@ -59,14 +58,14 @@ where } } -pub struct FromErrFuture { +pub struct FromErrFuture, E, Request> { fut: A::Future, f: PhantomData, } -impl Future for FromErrFuture +impl Future for FromErrFuture where - A: Service, + A: Service, E: From, { type Item = A::Response; @@ -86,21 +85,20 @@ pub struct FromErrNewService { e: PhantomData, } -impl FromErrNewService -where - A: NewService, - E: From, -{ +impl FromErrNewService { /// Create new `FromErr` new service instance - pub fn new(a: A) -> Self { + pub fn new(a: A) -> Self + where + A: NewService, + E: From, + { Self { a, e: PhantomData } } } impl Clone for FromErrNewService where - A: NewService + Clone, - E: From, + A: Clone, { fn clone(&self) -> Self { Self { @@ -110,18 +108,17 @@ where } } -impl NewService for FromErrNewService +impl NewService for FromErrNewService where - A: NewService, + A: NewService, E: From, { - type Request = A::Request; type Response = A::Response; type Error = E; type Service = FromErr; type InitError = A::InitError; - type Future = FromErrNewServiceFuture; + type Future = FromErrNewServiceFuture; fn new_service(&self) -> Self::Future { FromErrNewServiceFuture { @@ -131,18 +128,18 @@ where } } -pub struct FromErrNewServiceFuture +pub struct FromErrNewServiceFuture where - A: NewService, + A: NewService, E: From, { fut: A::Future, e: PhantomData, } -impl Future for FromErrNewServiceFuture +impl Future for FromErrNewServiceFuture where - A: NewService, + A: NewService, E: From, { type Item = FromErr; diff --git a/src/service/map.rs b/src/service/map.rs index e44a1d2d9..7095e9c07 100644 --- a/src/service/map.rs +++ b/src/service/map.rs @@ -1,4 +1,4 @@ -use std::marker; +use std::marker::PhantomData; use futures::{Async, Future, Poll}; @@ -7,83 +7,84 @@ use super::{NewService, Service}; /// Service for the `map` combinator, changing the type of a service's response. /// /// This is created by the `ServiceExt::map` method. -pub struct Map -where - A: Service, - F: Fn(A::Response) -> R, -{ +pub struct Map { service: A, f: F, + _t: PhantomData, } -impl Map -where - A: Service, - F: Fn(A::Response) -> R, -{ +impl Map { /// Create new `Map` combinator - pub fn new(service: A, f: F) -> Self { - Self { service, f } + pub fn new(service: A, f: F) -> Self + where + A: Service, + F: Fn(A::Response) -> Response, + { + Self { + service, + f, + _t: PhantomData, + } } } -impl Clone for Map +impl Clone for Map where - A: Service + Clone, - F: Fn(A::Response) -> R + Clone, + A: Clone, + F: Clone, { fn clone(&self) -> Self { Map { service: self.service.clone(), f: self.f.clone(), + _t: PhantomData, } } } -impl Service for Map +impl Service for Map where - A: Service, - F: Fn(A::Response) -> R + Clone, + A: Service, + F: Fn(A::Response) -> Response + Clone, { - type Request = A::Request; - type Response = R; + 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: Self::Request) -> Self::Future { + fn call(&mut self, req: Request) -> Self::Future { MapFuture::new(self.service.call(req), self.f.clone()) } } -pub struct MapFuture +pub struct MapFuture where - A: Service, - F: Fn(A::Response) -> R, + A: Service, + F: Fn(A::Response) -> Response, { f: F, fut: A::Future, } -impl MapFuture +impl MapFuture where - A: Service, - F: Fn(A::Response) -> R, + A: Service, + F: Fn(A::Response) -> Response, { fn new(fut: A::Future, f: F) -> Self { MapFuture { f, fut } } } -impl Future for MapFuture +impl Future for MapFuture where - A: Service, - F: Fn(A::Response) -> R, + A: Service, + F: Fn(A::Response) -> Response, { - type Item = R; + type Item = Response; type Error = A::Error; fn poll(&mut self) -> Poll { @@ -95,84 +96,83 @@ where } /// `MapNewService` new service combinator -pub struct MapNewService { +pub struct MapNewService { a: A, f: F, - r: marker::PhantomData, + r: PhantomData, } -impl MapNewService -where - A: NewService, - F: Fn(A::Response) -> R, -{ +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, + F: Fn(A::Response) -> Response, + { Self { a, f, - r: marker::PhantomData, + r: PhantomData, } } } -impl Clone for MapNewService +impl Clone for MapNewService where - A: NewService + Clone, - F: Fn(A::Response) -> R + Clone, + A: Clone, + F: Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), f: self.f.clone(), - r: marker::PhantomData, + r: PhantomData, } } } -impl NewService for MapNewService +impl NewService for MapNewService where - A: NewService, - F: Fn(A::Response) -> R + Clone, + A: NewService, + F: Fn(A::Response) -> Response + Clone, { - type Request = A::Request; - type Response = R; + type Response = Response; 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()) } } -pub struct MapNewServiceFuture +pub struct MapNewServiceFuture where - A: NewService, - F: Fn(A::Response) -> R, + A: NewService, + F: Fn(A::Response) -> Response, { fut: A::Future, f: Option, } -impl MapNewServiceFuture +impl MapNewServiceFuture where - A: NewService, - F: Fn(A::Response) -> R, + A: NewService, + F: Fn(A::Response) -> Response, { 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: Fn(A::Response) -> R, + A: NewService, + F: Fn(A::Response) -> Response, { - type Item = Map; + type Item = Map; type Error = A::InitError; fn poll(&mut self) -> Poll { diff --git a/src/service/map_err.rs b/src/service/map_err.rs index 6bef7c05a..1e8f5de7d 100644 --- a/src/service/map_err.rs +++ b/src/service/map_err.rs @@ -1,4 +1,4 @@ -use std::marker; +use std::marker::PhantomData; use futures::{Async, Future, Poll}; @@ -8,71 +8,71 @@ use super::{NewService, Service}; /// error. /// /// This is created by the `ServiceExt::map_err` method. -pub struct MapErr -where - A: Service, - F: Fn(A::Error) -> E, -{ +pub struct MapErr { service: A, f: F, + _t: PhantomData, } -impl MapErr -where - A: Service, - F: Fn(A::Error) -> E, -{ +impl MapErr { /// Create new `MapErr` combinator - pub fn new(service: A, f: F) -> Self { - Self { service, f } + pub fn new(service: A, f: F) -> Self + where + A: Service, + F: Fn(A::Error) -> E, + { + Self { + service, + f, + _t: PhantomData, + } } } impl Clone for MapErr where - A: Service + Clone, - F: Fn(A::Error) -> E + Clone, + A: Clone, + F: Clone, { fn clone(&self) -> Self { MapErr { service: self.service.clone(), f: self.f.clone(), + _t: PhantomData, } } } -impl Service for MapErr +impl Service for MapErr where - A: Service, - F: Fn(A::Error) -> E, - F: Clone, + 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: Self::Request) -> Self::Future { + fn call(&mut self, req: 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 +80,9 @@ where } } -impl Future for MapErrFuture +impl Future for MapErrFuture where - A: Service, + A: Service, F: Fn(A::Error) -> E, { type Item = A::Response; @@ -100,68 +100,67 @@ where pub struct MapErrNewService { a: A, f: F, - e: marker::PhantomData, + e: PhantomData, } -impl MapErrNewService -where - A: NewService, - F: Fn(A::Error) -> E, -{ +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, + F: Fn(A::Error) -> E, + { Self { a, f, - e: marker::PhantomData, + e: PhantomData, } } } impl Clone for MapErrNewService where - A: NewService + Clone, - F: Fn(A::Error) -> E + Clone, + A: Clone, + F: Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), f: self.f.clone(), - e: marker::PhantomData, + e: PhantomData, } } } -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 { @@ -169,9 +168,9 @@ where } } -impl Future for MapErrNewServiceFuture +impl Future for MapErrNewServiceFuture where - A: NewService, + A: NewService, F: Fn(A::Error) -> E + Clone, { type Item = MapErr; diff --git a/src/service/map_init_err.rs b/src/service/map_init_err.rs index e6129d29b..40ff97b5f 100644 --- a/src/service/map_init_err.rs +++ b/src/service/map_init_err.rs @@ -1,4 +1,4 @@ -use std::marker; +use std::marker::PhantomData; use futures::{Future, Poll}; @@ -8,68 +8,67 @@ use super::NewService; pub struct MapInitErr { a: A, f: F, - e: marker::PhantomData, + e: PhantomData, } -impl MapInitErr -where - A: NewService, - F: Fn(A::InitError) -> E, -{ +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, + F: Fn(A::InitError) -> E, + { Self { a, f, - e: marker::PhantomData, + e: PhantomData, } } } impl Clone for MapInitErr where - A: NewService + Clone, - F: Fn(A::InitError) -> E + Clone, + A: Clone, + F: Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), f: self.f.clone(), - e: marker::PhantomData, + e: PhantomData, } } } -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 { @@ -77,9 +76,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/src/service/mod.rs b/src/service/mod.rs index f8de56277..e944d29c9 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -1,7 +1,7 @@ use futures::{Future, IntoFuture}; /// re-export for convinience -pub use tower_service::{NewService, Service}; +pub use tower_service::Service; mod and_then; mod apply; @@ -23,21 +23,20 @@ pub use self::then::{Then, ThenNewService}; /// An extension trait for `Service`s that provides a variety of convenient /// adapters -pub trait ServiceExt: Service { +pub trait ServiceExt: Service { /// Apply function to specified service and use it as a next service in /// chain. - fn apply( + fn apply( self, service: I, f: F, - ) -> AndThen> + ) -> AndThen> where Self: Sized, - S: Service, - S::Error: Into<::Error>, - I: IntoService, - F: Fn(Self::Response, &mut S) -> R, - R: IntoFuture, + T: Service, + I: IntoService, + F: Fn(Self::Response, &mut T) -> Out, + Out: IntoFuture, { self.and_then(Apply::new(service.into_service(), f)) } @@ -54,17 +53,17 @@ 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()) } - /// Map this service's error to any error implementing `From` for - /// this service`s `Error`. - /// - /// Note that this function consumes the receiving service and returns a - /// wrapped version of it. + // /// Map this service's error to any error implementing `From` for + // /// this service`s `Error`. + // /// + // /// Note that this function consumes the receiving service and returns a + // /// wrapped version of it. fn from_err(self) -> FromErr where Self: Sized, @@ -73,28 +72,28 @@ pub trait ServiceExt: Service { FromErr::new(self) } - /// Chain on a computation for when a call to the service finished, - /// passing the result of the call to the next service `B`. - /// - /// Note that this function consumes the receiving future and returns a - /// wrapped version of it. + // /// Chain on a computation for when a call to the service finished, + // /// passing the result of the call to the next service `B`. + // /// + // /// Note that this function consumes the receiving future and returns a + // /// wrapped version of it. fn then(self, service: B) -> Then where Self: Sized, - B: Service, Error = Self::Error>, + B: Service, Error = Self::Error>, { Then::new(self, service) } - /// Map this service's output to a different type, returning a new service - /// of the resulting type. - /// - /// This function is similar to the `Option::map` or `Iterator::map` where - /// it will change the type of the underlying service. - /// - /// Note that this function consumes the receiving service and returns a - /// wrapped version of it, similar to the existing `map` methods in the - /// standard library. + // /// Map this service's output to a different type, returning a new service + // /// of the resulting type. + // /// + // /// This function is similar to the `Option::map` or `Iterator::map` where + // /// it will change the type of the underlying service. + // /// + // /// Note that this function consumes the receiving service and returns a + // /// wrapped version of it, similar to the existing `map` methods in the + // /// standard library. fn map(self, f: F) -> Map where Self: Sized, @@ -103,14 +102,14 @@ pub trait ServiceExt: Service { Map::new(self, f) } - /// Map this service's error to a different error, returning a new service. - /// - /// This function is similar to the `Result::map_err` where it will change - /// the error type of the underlying service. This is useful for example to - /// ensure that services have the same error type. - /// - /// Note that this function consumes the receiving service and returns a - /// wrapped version of it. + // /// Map this service's error to a different error, returning a new service. + // /// + // /// This function is similar to the `Result::map_err` where it will change + // /// the error type of the underlying service. This is useful for example to + // /// ensure that services have the same error type. + // /// + // /// Note that this function consumes the receiving service and returns a + // /// wrapped version of it. fn map_err(self, f: F) -> MapErr where Self: Sized, @@ -120,19 +119,47 @@ pub trait ServiceExt: Service { } } -pub trait NewServiceExt: NewService { - fn apply( +/// Creates new `Service` values. +/// +/// Acts as a service factory. This is useful for cases where new `Service` +/// values must be produced. One case is a TCP servier listener. The listner +/// 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 { + /// Responses given by the service + type Response; + + /// Errors produced by the service + type Error; + + /// The `Service` value created by this factory + type Service: Service; + + /// Errors produced while building a service. + type InitError; + + /// The future of the `Service` instance. + type Future: Future; + + /// Create and return a new service value asynchronously. + fn new_service(&self) -> Self::Future; +} + +pub trait NewServiceExt: NewService { + fn apply( self, service: I, f: F, - ) -> AndThenNewService> + ) -> AndThenNewService> where Self: Sized, - S: NewService, - S::Error: Into<::Error>, - I: IntoNewService, - F: Fn(Self::Response, &mut S::Service) -> R + Clone, - R: IntoFuture, + T: NewService, + I: IntoNewService, + F: Fn(Self::Response, &mut T::Service) -> Out + Clone, + Out: IntoFuture, { self.and_then(ApplyNewService::new(service, f)) } @@ -140,22 +167,18 @@ pub trait NewServiceExt: NewService { fn and_then(self, new_service: F) -> AndThenNewService where Self: Sized, - F: IntoNewService, - B: NewService< - Request = Self::Response, - Error = Self::Error, - InitError = Self::InitError, - >, + F: IntoNewService, + B: NewService, { AndThenNewService::new(self, new_service) } - /// `NewService` that create service to map this service's error - /// and new service's init error to any error - /// implementing `From` for this service`s `Error`. - /// - /// Note that this function consumes the receiving new service and returns a - /// wrapped version of it. + // /// `NewService` that create service to map this service's error + // /// and new service's init error to any error + // /// implementing `From` for this service`s `Error`. + // /// + // /// Note that this function consumes the receiving new service and returns a + // /// wrapped version of it. fn from_err(self) -> FromErrNewService where Self: Sized, @@ -164,18 +187,18 @@ pub trait NewServiceExt: NewService { FromErrNewService::new(self) } - /// Create `NewService` to chain on a computation for when a call to the - /// service finished, passing the result of the call to the next - /// service `B`. - /// - /// Note that this function consumes the receiving future and returns a - /// wrapped version of it. + // /// Create `NewService` to chain on a computation for when a call to the + // /// service finished, passing the result of the call to the next + // /// service `B`. + // /// + // /// Note that this function consumes the receiving future and returns a + // /// wrapped version of it. fn then(self, new_service: F) -> ThenNewService where Self: Sized, - F: IntoNewService, + F: IntoNewService>, B: NewService< - Request = Result, + Result, Error = Self::Error, InitError = Self::InitError, >, @@ -208,39 +231,39 @@ pub trait NewServiceExt: NewService { } } -impl ServiceExt for T where T: Service {} -impl NewServiceExt for T where T: NewService {} +impl ServiceExt for T where T: Service {} +impl NewServiceExt for T where T: NewService {} /// 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/src/service/then.rs b/src/service/then.rs index b10537b55..d48ac4b6b 100644 --- a/src/service/then.rs +++ b/src/service/then.rs @@ -12,21 +12,21 @@ pub struct Then { b: Cell, } -impl Then -where - A: Service, - B: Service, Error = A::Error>, -{ +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>, + { Then { a, b: Cell::new(b) } } } impl Clone for Then where - A: Service + Clone, - B: Service, Error = A::Error>, + A: Clone, + B: Clone, { fn clone(&self) -> Self { Then { @@ -36,40 +36,39 @@ where } } -impl Service for Then +impl Service for Then where - A: Service, - B: Service, Error = A::Error>, + A: Service, + B: Service, Error = A::Error>, { - type Request = A::Request; type Response = B::Response; type Error = B::Error; - type Future = ThenFuture; + type Future = ThenFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { let _ = try_ready!(self.a.poll_ready()); self.b.borrow_mut().poll_ready() } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: 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: A::Future, } -impl ThenFuture +impl ThenFuture where - A: Service, - B: Service>, + A: Service, + B: Service>, { fn new(fut_a: A::Future, b: Cell) -> Self { ThenFuture { @@ -80,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; @@ -113,13 +112,18 @@ pub struct ThenNewService { b: B, } -impl ThenNewService -where - A: NewService, - B: NewService, -{ +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, + B: NewService< + Result, + Error = A::Error, + InitError = A::InitError, + >, + F: IntoNewService>, + { Self { a, b: f.into_new_service(), @@ -127,22 +131,17 @@ where } } -impl NewService for ThenNewService +impl NewService for ThenNewService where - A: NewService, - B: NewService< - Request = Result, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, 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()) @@ -151,12 +150,8 @@ where impl Clone for ThenNewService where - A: NewService + Clone, - B: NewService< - Request = Result, - Error = A::Error, - InitError = A::InitError, - > + Clone, + A: Clone, + B: Clone, { fn clone(&self) -> Self { Self { @@ -166,10 +161,10 @@ where } } -pub struct ThenNewServiceFuture +pub struct ThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, Error = A::Error, InitError = A::InitError>, { fut_b: B::Future, fut_a: A::Future, @@ -177,10 +172,10 @@ where b: Option, } -impl ThenNewServiceFuture +impl ThenNewServiceFuture where - A: NewService, - B: NewService, + A: NewService, + B: NewService, Error = A::Error, InitError = A::InitError>, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { ThenNewServiceFuture { @@ -192,14 +187,10 @@ where } } -impl Future for ThenNewServiceFuture +impl Future for ThenNewServiceFuture where - A: NewService, - B: NewService< - Request = Result, - Error = A::Error, - InitError = A::InitError, - >, + A: NewService, + B: NewService, Error = A::Error, InitError = A::InitError>, { type Item = Then; type Error = A::InitError; diff --git a/src/ssl/nativetls.rs b/src/ssl/nativetls.rs index 7b9e2cd9c..f5adbc7e9 100644 --- a/src/ssl/nativetls.rs +++ b/src/ssl/nativetls.rs @@ -36,8 +36,7 @@ impl Clone for NativeTlsAcceptor { } } -impl NewService for NativeTlsAcceptor { - type Request = T; +impl NewService for NativeTlsAcceptor { type Response = TlsStream; type Error = Error; type Service = NativeTlsAcceptorService; @@ -61,8 +60,7 @@ pub struct NativeTlsAcceptorService { conns: Counter, } -impl Service for NativeTlsAcceptorService { - type Request = T; +impl Service for NativeTlsAcceptorService { type Response = TlsStream; type Error = Error; type Future = Accept; @@ -75,7 +73,7 @@ impl Service for NativeTlsAcceptorService { } } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: T) -> Self::Future { Accept { _guard: self.conns.get(), inner: Some(self.acceptor.accept(req)), diff --git a/src/ssl/openssl.rs b/src/ssl/openssl.rs index b94589276..ada4eaa36 100644 --- a/src/ssl/openssl.rs +++ b/src/ssl/openssl.rs @@ -37,8 +37,7 @@ impl Clone for OpensslAcceptor { } } -impl NewService for OpensslAcceptor { - type Request = T; +impl NewService for OpensslAcceptor { type Response = SslStream; type Error = Error; type Service = OpensslAcceptorService; @@ -62,8 +61,7 @@ pub struct OpensslAcceptorService { conns: Counter, } -impl Service for OpensslAcceptorService { - type Request = T; +impl Service for OpensslAcceptorService { type Response = SslStream; type Error = Error; type Future = OpensslAcceptorServiceFut; @@ -76,7 +74,7 @@ impl Service for OpensslAcceptorService { } } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: T) -> Self::Future { OpensslAcceptorServiceFut { _guard: self.conns.get(), fut: SslAcceptorExt::accept_async(&self.acceptor, req), @@ -119,7 +117,7 @@ impl OpensslConnector { impl OpensslConnector { pub fn service( connector: SslConnector, - ) -> impl Service), Error = Error> { + ) -> impl Service<(R, T), Response = (R, SslStream), Error = Error> { OpensslConnectorService { connector: connector, _t: PhantomData, @@ -136,8 +134,9 @@ impl Clone for OpensslConnector { } } -impl NewService for OpensslConnector { - type Request = (R, T); +impl NewService<(R, T)> + for OpensslConnector +{ type Response = (R, SslStream); type Error = Error; type Service = OpensslConnectorService; @@ -157,8 +156,9 @@ pub struct OpensslConnectorService { _t: PhantomData<(R, T)>, } -impl Service for OpensslConnectorService { - type Request = (R, T); +impl Service<(R, T)> + for OpensslConnectorService +{ type Response = (R, SslStream); type Error = Error; type Future = ConnectAsyncExt; @@ -167,7 +167,7 @@ impl Service for OpensslConnectorServ Ok(Async::Ready(())) } - fn call(&mut self, (req, stream): Self::Request) -> Self::Future { + fn call(&mut self, (req, stream): (R, T)) -> Self::Future { ConnectAsyncExt { fut: SslConnectorExt::connect_async(&self.connector, req.host(), stream), req: Some(req), diff --git a/src/ssl/rustls.rs b/src/ssl/rustls.rs index 56d3fd95a..5a5b18f0b 100644 --- a/src/ssl/rustls.rs +++ b/src/ssl/rustls.rs @@ -38,8 +38,7 @@ impl Clone for RustlsAcceptor { } } -impl NewService for RustlsAcceptor { - type Request = T; +impl NewService for RustlsAcceptor { type Response = TlsStream; type Error = io::Error; type Service = RustlsAcceptorService; @@ -63,8 +62,7 @@ pub struct RustlsAcceptorService { conns: Counter, } -impl Service for RustlsAcceptorService { - type Request = T; +impl Service for RustlsAcceptorService { type Response = TlsStream; type Error = io::Error; type Future = RustlsAcceptorServiceFut; @@ -77,7 +75,7 @@ impl Service for RustlsAcceptorService { } } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: T) -> Self::Future { RustlsAcceptorServiceFut { _guard: self.conns.get(), fut: self.acceptor.accept(req), diff --git a/src/stream.rs b/src/stream.rs index 5b08f4415..3f8948ca2 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -17,10 +17,13 @@ pub struct StreamDispatcher { impl StreamDispatcher where S: Stream, - T: Service, Response = (), Error = ()>, + T: Service, Response = (), Error = ()>, T::Future: 'static, { - pub fn new>(stream: S, service: F) -> Self { + pub fn new(stream: S, service: F) -> Self + where + F: IntoService>, + { let (stop_tx, stop_rx) = mpsc::unbounded(); StreamDispatcher { stream, @@ -35,7 +38,7 @@ where impl Future for StreamDispatcher where S: Stream, - T: Service, Response = (), Error = ()>, + T: Service, Response = (), Error = ()>, T::Future: 'static, { type Item = (); @@ -109,8 +112,7 @@ impl Clone for TakeItem { } } -impl NewService for TakeItem { - type Request = T; +impl NewService for TakeItem { type Response = (Option, T); type Error = T::Error; type InitError = (); @@ -133,8 +135,7 @@ impl Clone for TakeItemService { } } -impl Service for TakeItemService { - type Request = T; +impl Service for TakeItemService { type Response = (Option, T); type Error = T::Error; type Future = TakeItemServiceResponse; @@ -143,7 +144,7 @@ impl Service for TakeItemService { Ok(Async::Ready(())) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: T) -> Self::Future { TakeItemServiceResponse { stream: Some(req) } } } diff --git a/src/time.rs b/src/time.rs index 330aacaec..b1ca52fa1 100644 --- a/src/time.rs +++ b/src/time.rs @@ -43,8 +43,7 @@ impl Default for LowResTime { } } -impl NewService for LowResTime { - type Request = (); +impl NewService<()> for LowResTime { type Response = Instant; type Error = Never; type InitError = Never; @@ -88,8 +87,7 @@ impl LowResTimeService { } } -impl Service for LowResTimeService { - type Request = (); +impl Service<()> for LowResTimeService { type Response = Instant; type Error = Never; type Future = FutureResult; diff --git a/src/timeout.rs b/src/timeout.rs index 98398c249..ee7053dc4 100644 --- a/src/timeout.rs +++ b/src/timeout.rs @@ -12,7 +12,7 @@ use service::{NewService, Service}; /// Applies a timeout to requests. #[derive(Debug)] -pub struct Timeout { +pub struct Timeout { inner: T, timeout: Duration, } @@ -34,25 +34,24 @@ impl fmt::Debug for TimeoutError { } } -impl Timeout -where - T: NewService + Clone, -{ - pub fn new(timeout: Duration, inner: T) -> Self { +impl Timeout { + pub fn new(timeout: Duration, inner: T) -> Self + where + T: NewService + Clone, + { Timeout { inner, timeout } } } -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 { @@ -64,14 +63,14 @@ where /// `Timeout` response future #[derive(Debug)] -pub struct TimeoutFut { +pub struct TimeoutFut, Request> { 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; @@ -90,15 +89,15 @@ pub struct TimeoutService { } impl TimeoutService { - pub fn new(timeout: Duration, inner: T) -> Self { + pub fn new(timeout: Duration, inner: T) -> Self + where + T: Service, + { TimeoutService { inner, timeout } } } -impl Clone for TimeoutService -where - T: Clone, -{ +impl Clone for TimeoutService { fn clone(&self) -> Self { TimeoutService { inner: self.inner.clone(), @@ -107,14 +106,13 @@ where } } -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 @@ -122,7 +120,7 @@ where .map_err(|e| TimeoutError::Service(e)) } - fn call(&mut self, request: Self::Request) -> Self::Future { + fn call(&mut self, request: Request) -> Self::Future { TimeoutServiceResponse { fut: self.inner.call(request), sleep: Delay::new(clock::now() + self.timeout), @@ -132,14 +130,14 @@ where /// `TimeoutService` response future #[derive(Debug)] -pub struct TimeoutServiceResponse { +pub struct TimeoutServiceResponse, Request> { 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;