1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 02:21:07 +01:00

migrate to new Service<Request> trait

This commit is contained in:
Nikolay Kim 2018-11-29 16:56:15 -10:00
parent c62567f85f
commit 1b1ae01b5a
26 changed files with 595 additions and 605 deletions

View File

@ -1,5 +1,10 @@
# Changes # Changes
## [0.3.0] - xxx
* Use new `Service<Request>` trait
## [0.2.4] - 2018-11-21 ## [0.2.4] - 2018-11-21
### Added ### Added

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-net" name = "actix-net"
version = "0.2.4" version = "0.3.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix net - framework for the compisible network services for Rust (experimental)" description = "Actix net - framework for the compisible network services for Rust (experimental)"
readme = "README.md" readme = "README.md"
@ -57,7 +57,7 @@ tokio-tcp = "0.1"
tokio-timer = "0.2" tokio-timer = "0.2"
tokio-reactor = "0.1" tokio-reactor = "0.1"
tokio-current-thread = "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-proto = "^0.5.0"
trust-dns-resolver = "^0.10.0" trust-dns-resolver = "^0.10.0"

View File

@ -1,31 +1,35 @@
use std::marker::PhantomData;
use futures::Poll; use futures::Poll;
use super::cell::Cell; use super::cell::Cell;
use super::service::Service; use super::service::Service;
/// Service that allows to turn non-clone service to a service with `Clone` impl /// Service that allows to turn non-clone service to a service with `Clone` impl
pub struct CloneableService<S: Service + 'static> { pub struct CloneableService<S: Service<R> + 'static, R> {
service: Cell<S>, service: Cell<S>,
_t: PhantomData<R>,
} }
impl<S: Service + 'static> CloneableService<S> { impl<S: Service<R> + 'static, R> CloneableService<S, R> {
pub fn new(service: S) -> Self { pub fn new(service: S) -> Self {
Self { Self {
service: Cell::new(service), service: Cell::new(service),
_t: PhantomData,
} }
} }
} }
impl<S: Service + 'static> Clone for CloneableService<S> { impl<S: Service<R> + 'static, R> Clone for CloneableService<S, R> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
service: self.service.clone(), service: self.service.clone(),
_t: PhantomData,
} }
} }
} }
impl<S: Service + 'static> Service for CloneableService<S> { impl<S: Service<R> + 'static, R> Service<R> for CloneableService<S, R> {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Future = S::Future; type Future = S::Future;
@ -34,7 +38,7 @@ impl<S: Service + 'static> Service for CloneableService<S> {
self.service.borrow_mut().poll_ready() 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) self.service.borrow_mut().call(req)
} }
} }

View File

@ -168,23 +168,24 @@ impl Connector {
/// Create new connector with custom resolver /// Create new connector with custom resolver
pub fn with_resolver( pub fn with_resolver(
resolver: Resolver<Connect>, resolver: Resolver<Connect>,
) -> impl Service<Request = Connect, Response = (Connect, TcpStream), Error = ConnectorError> ) -> impl Service<Connect, Response = (Connect, TcpStream), Error = ConnectorError> + Clone
+ Clone { {
Connector { resolver } Connector { resolver }
} }
/// Create new default connector service // /// Create new default connector service
pub fn new_service_with_config<E>( // pub fn new_service_with_config<E>(
cfg: ResolverConfig, // cfg: ResolverConfig,
opts: ResolverOpts, // opts: ResolverOpts,
) -> impl NewService< // ) -> impl NewService<
Request = Connect, // Connect,
Response = (Connect, TcpStream), // Response = (Connect, TcpStream),
Error = ConnectorError, // Error = ConnectorError,
InitError = E, // InitError = E,
> + Clone { // Service = impl Service<Connect, Response = (Connect, TcpStream), Error = ConnectorError> + Clone,
move || -> FutureResult<Connector, E> { ok(Connector::new(cfg.clone(), opts)) } // > + Clone {
} // move || -> FutureResult<Connector, E> { ok(Connector::new(cfg.clone(), opts)) }
// }
} }
impl Clone for Connector { impl Clone for Connector {
@ -195,8 +196,7 @@ impl Clone for Connector {
} }
} }
impl Service for Connector { impl Service<Connect> for Connector {
type Request = Connect;
type Response = (Connect, TcpStream); type Response = (Connect, TcpStream);
type Error = ConnectorError; type Error = ConnectorError;
type Future = Either<ConnectorFuture, ConnectorTcpFuture>; type Future = Either<ConnectorFuture, ConnectorTcpFuture>;
@ -205,7 +205,7 @@ impl Service for Connector {
Ok(Async::Ready(())) Ok(Async::Ready(()))
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: Connect) -> Self::Future {
match req.kind { match req.kind {
ConnectKind::Host { host: _, port: _ } => Either::A(ConnectorFuture { ConnectKind::Host { host: _, port: _ } => Either::A(ConnectorFuture {
fut: self.resolver.call(req), fut: self.resolver.call(req),
@ -273,8 +273,7 @@ impl<T: RequestPort> Default for TcpConnector<T> {
} }
} }
impl<T: RequestPort> Service for TcpConnector<T> { impl<T: RequestPort> Service<(T, VecDeque<IpAddr>)> for TcpConnector<T> {
type Request = (T, VecDeque<IpAddr>);
type Response = (T, TcpStream); type Response = (T, TcpStream);
type Error = io::Error; type Error = io::Error;
type Future = TcpConnectorResponse<T>; type Future = TcpConnectorResponse<T>;
@ -283,7 +282,7 @@ impl<T: RequestPort> Service for TcpConnector<T> {
Ok(Async::Ready(())) Ok(Async::Ready(()))
} }
fn call(&mut self, (req, addrs): Self::Request) -> Self::Future { fn call(&mut self, (req, addrs): (T, VecDeque<IpAddr>)) -> Self::Future {
TcpConnectorResponse::new(req, addrs) TcpConnectorResponse::new(req, addrs)
} }
} }
@ -354,8 +353,7 @@ impl DefaultConnector {
} }
} }
impl Service for DefaultConnector { impl Service<Connect> for DefaultConnector {
type Request = Connect;
type Response = TcpStream; type Response = TcpStream;
type Error = ConnectorError; type Error = ConnectorError;
type Future = DefaultConnectorFuture; type Future = DefaultConnectorFuture;
@ -364,7 +362,7 @@ impl Service for DefaultConnector {
self.0.poll_ready() self.0.poll_ready()
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: Connect) -> Self::Future {
DefaultConnectorFuture { DefaultConnectorFuture {
fut: self.0.call(req), fut: self.0.call(req),
} }

View File

@ -13,12 +13,11 @@ pub enum EitherService<A, B> {
B(B), B(B),
} }
impl<A, B> Service for EitherService<A, B> impl<A, B, Request> Service<Request> for EitherService<A, B>
where where
A: Service, A: Service<Request>,
B: Service<Request = A::Request, Response = A::Response, Error = A::Error>, B: Service<Request, Response = A::Response, Error = A::Error>,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = A::Error; type Error = A::Error;
type Future = future::Either<A::Future, B::Future>; type Future = future::Either<A::Future, B::Future>;
@ -30,7 +29,7 @@ where
} }
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: Request) -> Self::Future {
match self { match self {
EitherService::A(ref mut inner) => future::Either::A(inner.call(req)), EitherService::A(ref mut inner) => future::Either::A(inner.call(req)),
EitherService::B(ref mut inner) => future::Either::B(inner.call(req)), EitherService::B(ref mut inner) => future::Either::B(inner.call(req)),
@ -44,22 +43,16 @@ pub enum Either<A, B> {
B(B), B(B),
} }
impl<A, B> NewService for Either<A, B> impl<A, B, Request> NewService<Request> for Either<A, B>
where where
A: NewService, A: NewService<Request>,
B: NewService< B: NewService<Request, Response = A::Response, Error = A::Error, InitError = A::InitError>,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = A::Error; type Error = A::Error;
type InitError = A::InitError; type InitError = A::InitError;
type Service = EitherService<A::Service, B::Service>; type Service = EitherService<A::Service, B::Service>;
type Future = EitherNewService<A, B>; type Future = EitherNewService<A, B, Request>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
match self { match self {
@ -70,20 +63,15 @@ where
} }
#[doc(hidden)] #[doc(hidden)]
pub enum EitherNewService<A: NewService, B: NewService> { pub enum EitherNewService<A: NewService<R>, B: NewService<R>, R> {
A(A::Future), A(A::Future),
B(B::Future), B(B::Future),
} }
impl<A, B> Future for EitherNewService<A, B> impl<A, B, Request> Future for EitherNewService<A, B, Request>
where where
A: NewService, A: NewService<Request>,
B: NewService< B: NewService<Request, Response = A::Response, Error = A::Error, InitError = A::InitError>,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{ {
type Item = EitherService<A::Service, B::Service>; type Item = EitherService<A::Service, B::Service>;
type Error = A::InitError; type Error = A::InitError;

View File

@ -24,13 +24,13 @@ impl<S, T, U> FramedNewService<S, T, U>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
S: NewService<Request = Request<U>, Response = Response<U>> + Clone, S: NewService<Request<U>, Response = Response<U>>,
<<S as NewService>::Service as Service>::Future: 'static, <<S as NewService<Request<U>>>::Service as Service<Request<U>>>::Future: 'static,
<<S as NewService>::Service as Service>::Error: 'static, <<S as NewService<Request<U>>>::Service as Service<Request<U>>>::Error: 'static,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: 'static, <U as Encoder>::Error: 'static,
{ {
pub fn new<F1: IntoNewService<S>>(factory: F1) -> Self { pub fn new<F1: IntoNewService<S, Request<U>>>(factory: F1) -> Self {
Self { Self {
factory: factory.into_new_service(), factory: factory.into_new_service(),
_t: PhantomData, _t: PhantomData,
@ -50,17 +50,16 @@ where
} }
} }
impl<S, T, U> NewService for FramedNewService<S, T, U> impl<S, T, U> NewService<Framed<T, U>> for FramedNewService<S, T, U>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
S: NewService<Request = Request<U>, Response = Response<U>> + Clone, S: NewService<Request<U>, Response = Response<U>> + Clone,
<<S as NewService>::Service as Service>::Future: 'static, <<S as NewService<Request<U>>>::Service as Service<Request<U>>>::Future: 'static,
<<S as NewService>::Service as Service>::Error: 'static, <<S as NewService<Request<U>>>::Service as Service<Request<U>>>::Error: 'static,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: 'static, <U as Encoder>::Error: 'static,
{ {
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>; type Response = FramedTransport<S::Service, T, U>;
type Error = S::InitError; type Error = S::InitError;
type InitError = S::InitError; type InitError = S::InitError;
@ -92,17 +91,16 @@ where
} }
} }
impl<S, T, U> Service for FramedService<S, T, U> impl<S, T, U> Service<Framed<T, U>> for FramedService<S, T, U>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
S: NewService<Request = Request<U>, Response = Response<U>>, S: NewService<Request<U>, Response = Response<U>>,
<<S as NewService>::Service as Service>::Future: 'static, <<S as NewService<Request<U>>>::Service as Service<Request<U>>>::Future: 'static,
<<S as NewService>::Service as Service>::Error: 'static, <<S as NewService<Request<U>>>::Service as Service<Request<U>>>::Error: 'static,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: 'static, <U as Encoder>::Error: 'static,
{ {
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>; type Response = FramedTransport<S::Service, T, U>;
type Error = S::InitError; type Error = S::InitError;
type Future = FramedServiceResponseFuture<S, T, U>; type Future = FramedServiceResponseFuture<S, T, U>;
@ -111,7 +109,7 @@ where
Ok(Async::Ready(())) Ok(Async::Ready(()))
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: Framed<T, U>) -> Self::Future {
FramedServiceResponseFuture { FramedServiceResponseFuture {
fut: self.factory.new_service(), fut: self.factory.new_service(),
@ -125,9 +123,9 @@ pub struct FramedServiceResponseFuture<S, T, U>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
S: NewService<Request = Request<U>, Response = Response<U>>, S: NewService<Request<U>, Response = Response<U>>,
<<S as NewService>::Service as Service>::Future: 'static, <<S as NewService<Request<U>>>::Service as Service<Request<U>>>::Future: 'static,
<<S as NewService>::Service as Service>::Error: 'static, <<S as NewService<Request<U>>>::Service as Service<Request<U>>>::Error: 'static,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: 'static, <U as Encoder>::Error: 'static,
{ {
@ -139,9 +137,9 @@ impl<S, T, U> Future for FramedServiceResponseFuture<S, T, U>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
S: NewService<Request = Request<U>, Response = Response<U>>, S: NewService<Request<U>, Response = Response<U>>,
<<S as NewService>::Service as Service>::Future: 'static, <<S as NewService<Request<U>>>::Service as Service<Request<U>>>::Future: 'static,
<<S as NewService>::Service as Service>::Error: 'static, <<S as NewService<Request<U>>>::Service as Service<Request<U>>>::Error: 'static,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: 'static, <U as Encoder>::Error: 'static,
{ {
@ -176,7 +174,7 @@ impl<E, U: Encoder + Decoder> From<E> for FramedTransportError<E, U> {
/// and pass then to the service. /// and pass then to the service.
pub struct FramedTransport<S, T, U> pub struct FramedTransport<S, T, U>
where where
S: Service, S: Service<Request<U>, Response = Response<U>>,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Encoder + Decoder, U: Encoder + Decoder,
{ {
@ -190,7 +188,7 @@ where
flushed: bool, flushed: bool,
} }
enum TransportState<S: Service, U: Encoder + Decoder> { enum TransportState<S: Service<Request<U>>, U: Encoder + Decoder> {
Processing, Processing,
Error(FramedTransportError<S::Error, U>), Error(FramedTransportError<S::Error, U>),
EncoderError(FramedTransportError<S::Error, U>), EncoderError(FramedTransportError<S::Error, U>),
@ -201,12 +199,12 @@ impl<S, T, U> FramedTransport<S, T, U>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
S: Service<Request = Request<U>, Response = Response<U>>, S: Service<Request<U>, Response = Response<U>>,
S::Future: 'static, S::Future: 'static,
S::Error: 'static, S::Error: 'static,
<U as Encoder>::Error: 'static, <U as Encoder>::Error: 'static,
{ {
pub fn new<F: IntoService<S>>(framed: Framed<T, U>, service: F) -> Self { pub fn new<F: IntoService<S, Request<U>>>(framed: Framed<T, U>, service: F) -> Self {
let (write_tx, write_rx) = mpsc::channel(16); let (write_tx, write_rx) = mpsc::channel(16);
FramedTransport { FramedTransport {
framed, framed,
@ -248,7 +246,7 @@ impl<S, T, U> FramedTransport<S, T, U>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
S: Service<Request = Request<U>, Response = Response<U>>, S: Service<Request<U>, Response = Response<U>>,
S::Future: 'static, S::Future: 'static,
S::Error: 'static, S::Error: 'static,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
@ -377,7 +375,7 @@ impl<S, T, U> Future for FramedTransport<S, T, U>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
S: Service<Request = Request<U>, Response = Response<U>>, S: Service<Request<U>, Response = Response<U>>,
S::Future: 'static, S::Future: 'static,
S::Error: 'static, S::Error: 'static,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
@ -437,13 +435,12 @@ where
} }
} }
impl<T, U, F> NewService for IntoFramed<T, U, F> impl<T, U, F> NewService<T> for IntoFramed<T, U, F>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static, F: Fn() -> U + Send + Clone + 'static,
U: Encoder + Decoder, U: Encoder + Decoder,
{ {
type Request = T;
type Response = Framed<T, U>; type Response = Framed<T, U>;
type Error = (); type Error = ();
type InitError = (); type InitError = ();
@ -468,13 +465,12 @@ where
_t: PhantomData<(T,)>, _t: PhantomData<(T,)>,
} }
impl<T, U, F> Service for IntoFramedService<T, U, F> impl<T, U, F> Service<T> for IntoFramedService<T, U, F>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static, F: Fn() -> U + Send + Clone + 'static,
U: Encoder + Decoder, U: Encoder + Decoder,
{ {
type Request = T;
type Response = Framed<T, U>; type Response = Framed<T, U>;
type Error = (); type Error = ();
type Future = FutureResult<Self::Response, Self::Error>; type Future = FutureResult<Self::Response, Self::Error>;
@ -483,7 +479,7 @@ where
Ok(Async::Ready(())) 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)())) ok(Framed::new(req, (self.factory)()))
} }
} }

View File

@ -12,11 +12,12 @@ pub struct InFlight<T> {
max_inflight: usize, max_inflight: usize,
} }
impl<T> InFlight<T> impl<T> InFlight<T> {
pub fn new<F, Request>(factory: F) -> Self
where where
T: NewService, T: NewService<Request>,
F: IntoNewService<T, Request>,
{ {
pub fn new<F: IntoNewService<T>>(factory: F) -> Self {
Self { Self {
factory: factory.into_new_service(), factory: factory.into_new_service(),
max_inflight: 15, max_inflight: 15,
@ -32,16 +33,15 @@ where
} }
} }
impl<T> NewService for InFlight<T> impl<T, Request> NewService<Request> for InFlight<T>
where where
T: NewService, T: NewService<Request>,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type InitError = T::InitError; type InitError = T::InitError;
type Service = InFlightService<T::Service>; type Service = InFlightService<T::Service>;
type Future = InFlightResponseFuture<T>; type Future = InFlightResponseFuture<T, Request>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
InFlightResponseFuture { InFlightResponseFuture {
@ -51,12 +51,12 @@ where
} }
} }
pub struct InFlightResponseFuture<T: NewService> { pub struct InFlightResponseFuture<T: NewService<Request>, Request> {
fut: T::Future, fut: T::Future,
max_inflight: usize, max_inflight: usize,
} }
impl<T: NewService> Future for InFlightResponseFuture<T> { impl<T: NewService<Request>, Request> Future for InFlightResponseFuture<T, Request> {
type Item = InFlightService<T::Service>; type Item = InFlightService<T::Service>;
type Error = T::InitError; type Error = T::InitError;
@ -73,15 +73,23 @@ pub struct InFlightService<T> {
count: Counter, count: Counter,
} }
impl<T: Service> InFlightService<T> { impl<T> InFlightService<T> {
pub fn new<F: IntoService<T>>(service: F) -> Self { pub fn new<F, Request>(service: F) -> Self
where
T: Service<Request>,
F: IntoService<T, Request>,
{
Self { Self {
service: service.into_service(), service: service.into_service(),
count: Counter::new(15), count: Counter::new(15),
} }
} }
pub fn with_max_inflight<F: IntoService<T>>(max: usize, service: F) -> Self { pub fn with_max_inflight<F, Request>(max: usize, service: F) -> Self
where
T: Service<Request>,
F: IntoService<T, Request>,
{
Self { Self {
service: service.into_service(), service: service.into_service(),
count: Counter::new(max), count: Counter::new(max),
@ -89,11 +97,13 @@ impl<T: Service> InFlightService<T> {
} }
} }
impl<T: Service> Service for InFlightService<T> { impl<T, Request> Service<Request> for InFlightService<T>
type Request = T::Request; where
T: Service<Request>,
{
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type Future = InFlightServiceResponse<T>; type Future = InFlightServiceResponse<T, Request>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
let res = self.service.poll_ready(); let res = self.service.poll_ready();
@ -103,22 +113,21 @@ impl<T: Service> Service for InFlightService<T> {
res res
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: Request) -> Self::Future {
InFlightServiceResponse { InFlightServiceResponse {
fut: self.service.call(req), fut: self.service.call(req),
guard: self.count.get(), _guard: self.count.get(),
} }
} }
} }
#[doc(hidden)] #[doc(hidden)]
pub struct InFlightServiceResponse<T: Service> { pub struct InFlightServiceResponse<T: Service<Request>, Request> {
fut: T::Future, fut: T::Future,
#[allow(dead_code)] _guard: CounterGuard,
guard: CounterGuard,
} }
impl<T: Service> Future for InFlightServiceResponse<T> { impl<T: Service<Request>, Request> Future for InFlightServiceResponse<T, Request> {
type Item = T::Response; type Item = T::Response;
type Error = T::Error; type Error = T::Error;

View File

@ -32,7 +32,7 @@ where
impl<R, E, F> Clone for KeepAlive<R, E, F> impl<R, E, F> Clone for KeepAlive<R, E, F>
where where
F: Fn() -> E + Clone, F: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
KeepAlive { KeepAlive {
@ -44,11 +44,10 @@ where
} }
} }
impl<R, E, F> NewService for KeepAlive<R, E, F> impl<R, E, F> NewService<R> for KeepAlive<R, E, F>
where where
F: Fn() -> E + Clone, F: Fn() -> E + Clone,
{ {
type Request = R;
type Response = R; type Response = R;
type Error = E; type Error = E;
type InitError = Never; type InitError = Never;
@ -90,11 +89,10 @@ where
} }
} }
impl<R, E, F> Service for KeepAliveService<R, E, F> impl<R, E, F> Service<R> for KeepAliveService<R, E, F>
where where
F: Fn() -> E, F: Fn() -> E,
{ {
type Request = R;
type Response = R; type Response = R;
type Error = E; type Error = E;
type Future = FutureResult<R, E>; type Future = FutureResult<R, E>;
@ -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; self.expire = self.time.now() + self.ka;
ok(req) ok(req)
} }

View File

@ -70,8 +70,7 @@ impl<T> Clone for Resolver<T> {
} }
} }
impl<T: RequestHost> Service for Resolver<T> { impl<T: RequestHost> Service<T> for Resolver<T> {
type Request = T;
type Response = (T, VecDeque<IpAddr>); type Response = (T, VecDeque<IpAddr>);
type Error = ResolveError; type Error = ResolveError;
type Future = ResolverFuture<T>; type Future = ResolverFuture<T>;
@ -80,7 +79,7 @@ impl<T: RequestHost> Service for Resolver<T> {
Ok(Async::Ready(())) 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) ResolverFuture::new(req, &self.resolver)
} }
} }

View File

@ -154,8 +154,8 @@ impl ServiceRuntime {
pub fn service<T, F>(&mut self, name: &str, service: F) pub fn service<T, F>(&mut self, name: &str, service: F)
where where
F: IntoNewService<T>, F: IntoNewService<T, TcpStream>,
T: NewService<Request = TcpStream, Response = ()> + 'static, T: NewService<TcpStream, Response = ()> + 'static,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
T::InitError: fmt::Debug, T::InitError: fmt::Debug,
@ -176,7 +176,7 @@ impl ServiceRuntime {
type BoxedNewService = Box< type BoxedNewService = Box<
NewService< NewService<
Request = (Option<CounterGuard>, ServerMessage), (Option<CounterGuard>, ServerMessage),
Response = (), Response = (),
Error = (), Error = (),
InitError = (), InitError = (),
@ -189,15 +189,14 @@ struct ServiceFactory<T> {
inner: T, inner: T,
} }
impl<T> NewService for ServiceFactory<T> impl<T> NewService<(Option<CounterGuard>, ServerMessage)> for ServiceFactory<T>
where where
T: NewService<Request = TcpStream, Response = ()>, T: NewService<TcpStream, Response = ()>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
T::Error: 'static, T::Error: 'static,
T::InitError: fmt::Debug + 'static, T::InitError: fmt::Debug + 'static,
{ {
type Request = (Option<CounterGuard>, ServerMessage);
type Response = (); type Response = ();
type Error = (); type Error = ();
type InitError = (); type InitError = ();

View File

@ -22,13 +22,13 @@ pub enum ServerMessage {
} }
pub trait StreamServiceFactory: Send + Clone + 'static { pub trait StreamServiceFactory: Send + Clone + 'static {
type NewService: NewService<Request = TcpStream, Response = ()>; type NewService: NewService<TcpStream, Response = ()>;
fn create(&self) -> Self::NewService; fn create(&self) -> Self::NewService;
} }
pub trait ServiceFactory: Send + Clone + 'static { pub trait ServiceFactory: Send + Clone + 'static {
type NewService: NewService<Request = ServerMessage, Response = ()>; type NewService: NewService<ServerMessage, Response = ()>;
fn create(&self) -> Self::NewService; fn create(&self) -> Self::NewService;
} }
@ -43,7 +43,7 @@ pub(crate) trait InternalServiceFactory: Send {
pub(crate) type BoxedServerService = Box< pub(crate) type BoxedServerService = Box<
Service< Service<
Request = (Option<CounterGuard>, ServerMessage), (Option<CounterGuard>, ServerMessage),
Response = (), Response = (),
Error = (), Error = (),
Future = FutureResult<(), ()>, Future = FutureResult<(), ()>,
@ -60,13 +60,12 @@ impl<T> StreamService<T> {
} }
} }
impl<T> Service for StreamService<T> impl<T> Service<(Option<CounterGuard>, ServerMessage)> for StreamService<T>
where where
T: Service<Request = TcpStream, Response = ()>, T: Service<TcpStream, Response = ()>,
T::Future: 'static, T::Future: 'static,
T::Error: 'static, T::Error: 'static,
{ {
type Request = (Option<CounterGuard>, ServerMessage);
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;
@ -107,13 +106,12 @@ impl<T> ServerService<T> {
} }
} }
impl<T> Service for ServerService<T> impl<T> Service<(Option<CounterGuard>, ServerMessage)> for ServerService<T>
where where
T: Service<Request = ServerMessage, Response = ()>, T: Service<ServerMessage, Response = ()>,
T::Future: 'static, T::Future: 'static,
T::Error: 'static, T::Error: 'static,
{ {
type Request = (Option<CounterGuard>, ServerMessage);
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;
@ -240,7 +238,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
impl<F, T> ServiceFactory for F impl<F, T> ServiceFactory for F
where where
F: Fn() -> T + Send + Clone + 'static, F: Fn() -> T + Send + Clone + 'static,
T: NewService<Request = ServerMessage, Response = ()>, T: NewService<ServerMessage, Response = ()>,
{ {
type NewService = T; type NewService = T;
@ -252,7 +250,7 @@ where
impl<F, T> StreamServiceFactory for F impl<F, T> StreamServiceFactory for F
where where
F: Fn() -> T + Send + Clone + 'static, F: Fn() -> T + Send + Clone + 'static,
T: NewService<Request = TcpStream, Response = ()>, T: NewService<TcpStream, Response = ()>,
{ {
type NewService = T; type NewService = T;

View File

@ -12,21 +12,21 @@ pub struct AndThen<A, B> {
b: Cell<B>, b: Cell<B>,
} }
impl<A, B> AndThen<A, B> impl<A, B> AndThen<A, B> {
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new(a: A, b: B) -> Self { pub fn new<Request>(a: A, b: B) -> Self
where
A: Service<Request>,
B: Service<A::Response, Error = A::Error>,
{
Self { a, b: Cell::new(b) } Self { a, b: Cell::new(b) }
} }
} }
impl<A, B> Clone for AndThen<A, B> impl<A, B> Clone for AndThen<A, B>
where where
A: Service + Clone, A: Clone,
B: Service<Request = A::Response, Error = A::Error>, B: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
AndThen { AndThen {
@ -36,40 +36,39 @@ where
} }
} }
impl<A, B> Service for AndThen<A, B> impl<A, B, Request> Service<Request> for AndThen<A, B>
where where
A: Service, A: Service<Request>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Future = AndThenFuture<A, B>; type Future = AndThenFuture<A, B, Request>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
let _ = try_ready!(self.a.poll_ready()); let _ = try_ready!(self.a.poll_ready());
self.b.borrow_mut().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()) AndThenFuture::new(self.a.call(req), self.b.clone())
} }
} }
pub struct AndThenFuture<A, B> pub struct AndThenFuture<A, B, Request>
where where
A: Service, A: Service<Request>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
b: Cell<B>, b: Cell<B>,
fut_b: Option<B::Future>, fut_b: Option<B::Future>,
fut_a: A::Future, fut_a: A::Future,
} }
impl<A, B> AndThenFuture<A, B> impl<A, B, Request> AndThenFuture<A, B, Request>
where where
A: Service, A: Service<Request>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
fn new(fut_a: A::Future, b: Cell<B>) -> Self { fn new(fut_a: A::Future, b: Cell<B>) -> Self {
AndThenFuture { AndThenFuture {
@ -80,10 +79,10 @@ where
} }
} }
impl<A, B> Future for AndThenFuture<A, B> impl<A, B, Request> Future for AndThenFuture<A, B, Request>
where where
A: Service, A: Service<Request>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
B::Error: Into<A::Error>, B::Error: Into<A::Error>,
{ {
type Item = B::Response; type Item = B::Response;
@ -111,13 +110,13 @@ pub struct AndThenNewService<A, B> {
b: B, b: B,
} }
impl<A, B> AndThenNewService<A, B> impl<A, B> AndThenNewService<A, B> {
where
A: NewService,
B: NewService,
{
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<F: IntoNewService<B>>(a: A, f: F) -> Self { pub fn new<Request, F: IntoNewService<B, A::Response>>(a: A, f: F) -> Self
where
A: NewService<Request>,
B: NewService<A::Response, Error = A::Error, InitError = A::InitError>,
{
Self { Self {
a, a,
b: f.into_new_service(), b: f.into_new_service(),
@ -125,18 +124,17 @@ where
} }
} }
impl<A, B> NewService for AndThenNewService<A, B> impl<A, B, Request> NewService<Request> for AndThenNewService<A, B>
where where
A: NewService, A: NewService<Request>,
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>, B: NewService<A::Response, Error = A::Error, InitError = A::InitError>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Service = AndThen<A::Service, B::Service>; type Service = AndThen<A::Service, B::Service>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = AndThenNewServiceFuture<A, B>; type Future = AndThenNewServiceFuture<A, B, Request>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
AndThenNewServiceFuture::new(self.a.new_service(), self.b.new_service()) AndThenNewServiceFuture::new(self.a.new_service(), self.b.new_service())
@ -145,8 +143,8 @@ where
impl<A, B> Clone for AndThenNewService<A, B> impl<A, B> Clone for AndThenNewService<A, B>
where where
A: NewService + Clone, A: Clone,
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError> + Clone, B: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
@ -156,10 +154,10 @@ where
} }
} }
pub struct AndThenNewServiceFuture<A, B> pub struct AndThenNewServiceFuture<A, B, Request>
where where
A: NewService, A: NewService<Request>,
B: NewService, B: NewService<A::Response>,
{ {
fut_b: B::Future, fut_b: B::Future,
fut_a: A::Future, fut_a: A::Future,
@ -167,10 +165,10 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B> AndThenNewServiceFuture<A, B> impl<A, B, Request> AndThenNewServiceFuture<A, B, Request>
where where
A: NewService, A: NewService<Request>,
B: NewService, B: NewService<A::Response>,
{ {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self { fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
AndThenNewServiceFuture { AndThenNewServiceFuture {
@ -182,10 +180,10 @@ where
} }
} }
impl<A, B> Future for AndThenNewServiceFuture<A, B> impl<A, B, Request> Future for AndThenNewServiceFuture<A, B, Request>
where where
A: NewService, A: NewService<Request>,
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>, B: NewService<A::Response, Error = A::Error, InitError = A::InitError>,
{ {
type Item = AndThen<A::Service, B::Service>; type Item = AndThen<A::Service, B::Service>;
type Error = A::InitError; type Error = A::InitError;

View File

@ -5,21 +5,23 @@ use futures::{Async, Future, IntoFuture, Poll};
use super::{IntoNewService, IntoService, NewService, Service}; use super::{IntoNewService, IntoService, NewService, Service};
/// `Apply` service combinator /// `Apply` service combinator
pub struct Apply<T, F, R, Req> { pub struct Apply<T, F, In, Out, Request>
where
T: Service<Request>,
{
service: T, service: T,
f: F, f: F,
r: PhantomData<(Req, R)>, r: PhantomData<(In, Out, Request)>,
} }
impl<T, F, R, Req> Apply<T, F, R, Req> impl<T, F, In, Out, Request> Apply<T, F, In, Out, Request>
where where
T: Service, T: Service<Request>,
T::Error: Into<<R::Future as Future>::Error>, F: Fn(In, &mut T) -> Out,
F: Fn(Req, &mut T) -> R, Out: IntoFuture,
R: IntoFuture,
{ {
/// Create new `Apply` combinator /// Create new `Apply` combinator
pub fn new<I: IntoService<T>>(service: I, f: F) -> Self { pub fn new<I: IntoService<T, Request>>(service: I, f: F) -> Self {
Self { Self {
service: service.into_service(), service: service.into_service(),
f, f,
@ -28,12 +30,10 @@ where
} }
} }
impl<T, F, R, Req> Clone for Apply<T, F, R, Req> impl<T, F, In, Out, Request> Clone for Apply<T, F, In, Out, Request>
where where
T: Service + Clone, T: Service<Request> + Clone,
T::Error: Into<<R::Future as Future>::Error>, F: Clone,
F: Fn(Req, &mut T) -> R + Clone,
R: IntoFuture,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Apply { Apply {
@ -44,42 +44,43 @@ where
} }
} }
impl<T, F, R, Req> Service for Apply<T, F, R, Req> impl<T, F, In, Out, Request> Service<In> for Apply<T, F, In, Out, Request>
where where
T: Service, T: Service<Request, Error = Out::Error>,
T::Error: Into<<R::Future as Future>::Error>, F: Fn(In, &mut T) -> Out,
F: Fn(Req, &mut T) -> R, Out: IntoFuture,
R: IntoFuture,
{ {
type Request = Req; type Response = <Out::Future as Future>::Item;
type Response = <R::Future as Future>::Item; type Error = <Out::Future as Future>::Error;
type Error = <R::Future as Future>::Error; type Future = Out::Future;
type Future = R::Future;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready().map_err(|e| e.into()) 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() (self.f)(req, &mut self.service).into_future()
} }
} }
/// `ApplyNewService` new service combinator /// `ApplyNewService` new service combinator
pub struct ApplyNewService<T, F, R, Req> { pub struct ApplyNewService<T, F, In, Out, Request>
where
T: NewService<Request>,
{
service: T, service: T,
f: F, f: F,
r: PhantomData<Fn(Req) -> R>, r: PhantomData<(In, Out, Request)>,
} }
impl<T, F, R, Req> ApplyNewService<T, F, R, Req> impl<T, F, In, Out, Request> ApplyNewService<T, F, In, Out, Request>
where where
T: NewService, T: NewService<Request>,
F: Fn(Req, &mut T::Service) -> R, F: Fn(In, &mut T::Service) -> Out,
R: IntoFuture, Out: IntoFuture,
{ {
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<F1: IntoNewService<T>>(service: F1, f: F) -> Self { pub fn new<F1: IntoNewService<T, Request>>(service: F1, f: F) -> Self {
Self { Self {
f, f,
service: service.into_new_service(), service: service.into_new_service(),
@ -88,11 +89,11 @@ where
} }
} }
impl<T, F, R, Req> Clone for ApplyNewService<T, F, R, Req> impl<T, F, In, Out, Request> Clone for ApplyNewService<T, F, In, Out, Request>
where where
T: NewService + Clone, T: NewService<Request> + Clone,
F: Fn(Req, &mut T::Service) -> R + Clone, F: Fn(Out, &mut T::Service) -> Out + Clone,
R: IntoFuture, Out: IntoFuture,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
@ -103,42 +104,40 @@ where
} }
} }
impl<T, F, R, Req> NewService for ApplyNewService<T, F, R, Req> impl<T, F, In, Out, Request> NewService<In> for ApplyNewService<T, F, In, Out, Request>
where where
T: NewService, T: NewService<Request, Error = Out::Error>,
T::Error: Into<<R::Future as Future>::Error>, F: Fn(In, &mut T::Service) -> Out + Clone,
F: Fn(Req, &mut T::Service) -> R + Clone, Out: IntoFuture,
R: IntoFuture,
{ {
type Request = Req; type Response = <Out::Future as Future>::Item;
type Response = <R::Future as Future>::Item; type Error = <Out::Future as Future>::Error;
type Error = <R::Future as Future>::Error; type Service = Apply<T::Service, F, In, Out, Request>;
type Service = Apply<T::Service, F, R, Req>;
type InitError = T::InitError; type InitError = T::InitError;
type Future = ApplyNewServiceFuture<T, F, R, Req>; type Future = ApplyNewServiceFuture<T, F, In, Out, Request>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone()) ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone())
} }
} }
pub struct ApplyNewServiceFuture<T, F, R, Req> pub struct ApplyNewServiceFuture<T, F, In, Out, Request>
where where
T: NewService, T: NewService<Request>,
F: Fn(Req, &mut T::Service) -> R, F: Fn(In, &mut T::Service) -> Out,
R: IntoFuture, Out: IntoFuture,
{ {
fut: T::Future, fut: T::Future,
f: Option<F>, f: Option<F>,
r: PhantomData<Fn(Req) -> R>, r: PhantomData<(In, Out)>,
} }
impl<T, F, R, Req> ApplyNewServiceFuture<T, F, R, Req> impl<T, F, In, Out, Request> ApplyNewServiceFuture<T, F, In, Out, Request>
where where
T: NewService, T: NewService<Request>,
F: Fn(Req, &mut T::Service) -> R, F: Fn(In, &mut T::Service) -> Out,
R: IntoFuture, Out: IntoFuture,
{ {
fn new(fut: T::Future, f: F) -> Self { fn new(fut: T::Future, f: F) -> Self {
ApplyNewServiceFuture { ApplyNewServiceFuture {
@ -149,14 +148,13 @@ where
} }
} }
impl<T, F, R, Req> Future for ApplyNewServiceFuture<T, F, R, Req> impl<T, F, In, Out, Request> Future for ApplyNewServiceFuture<T, F, In, Out, Request>
where where
T: NewService, T: NewService<Request>,
T::Error: Into<<R::Future as Future>::Error>, F: Fn(In, &mut T::Service) -> Out,
F: Fn(Req, &mut T::Service) -> R, Out: IntoFuture,
R: IntoFuture,
{ {
type Item = Apply<T::Service, F, R, Req>; type Item = Apply<T::Service, F, In, Out, Request>;
type Error = T::InitError; type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {

View File

@ -42,12 +42,11 @@ where
} }
} }
impl<F, Req, Resp, E, Fut> Service for FnService<F, Req, Resp, E, Fut> impl<F, Req, Resp, E, Fut> Service<Req> for FnService<F, Req, Resp, E, Fut>
where where
F: Fn(Req) -> Fut, F: Fn(Req) -> Fut,
Fut: IntoFuture<Item = Resp, Error = E>, Fut: IntoFuture<Item = Resp, Error = E>,
{ {
type Request = Req;
type Response = Resp; type Response = Resp;
type Error = E; type Error = E;
type Future = Fut::Future; type Future = Fut::Future;
@ -61,7 +60,7 @@ where
} }
} }
impl<F, Req, Resp, Err, Fut> IntoService<FnService<F, Req, Resp, Err, Fut>> for F impl<F, Req, Resp, Err, Fut> IntoService<FnService<F, Req, Resp, Err, Fut>, Req> for F
where where
F: Fn(Req) -> Fut + 'static, F: Fn(Req) -> Fut + 'static,
Fut: IntoFuture<Item = Resp, Error = Err>, Fut: IntoFuture<Item = Resp, Error = Err>,
@ -93,12 +92,11 @@ where
} }
} }
impl<F, Req, Resp, Err, Fut> NewService for FnNewService<F, Req, Resp, Err, Fut> impl<F, Req, Resp, Err, Fut> NewService<Req> for FnNewService<F, Req, Resp, Err, Fut>
where where
F: Fn(Req) -> Fut + Clone, F: Fn(Req) -> Fut + Clone,
Fut: IntoFuture<Item = Resp, Error = Err>, Fut: IntoFuture<Item = Resp, Error = Err>,
{ {
type Request = Req;
type Response = Resp; type Response = Resp;
type Error = Err; type Error = Err;
type Service = FnService<F, Req, Resp, Err, Fut>; type Service = FnService<F, Req, Resp, Err, Fut>;
@ -110,7 +108,7 @@ where
} }
} }
impl<F, Req, Resp, Err, Fut> IntoNewService<FnNewService<F, Req, Resp, Err, Fut>> for F impl<F, Req, Resp, Err, Fut> IntoNewService<FnNewService<F, Req, Resp, Err, Fut>, Req> for F
where where
F: Fn(Req) -> Fut + Clone + 'static, F: Fn(Req) -> Fut + Clone + 'static,
Fut: IntoFuture<Item = Resp, Error = Err>, Fut: IntoFuture<Item = Resp, Error = Err>,

View File

@ -7,16 +7,17 @@ use super::{NewService, Service};
/// Service for the `from_err` combinator, changing the error type of a service. /// Service for the `from_err` combinator, changing the error type of a service.
/// ///
/// This is created by the `ServiceExt::from_err` method. /// This is created by the `ServiceExt::from_err` method.
pub struct FromErr<A, E> pub struct FromErr<A, E> {
where
A: Service,
{
service: A, service: A,
f: PhantomData<E>, f: PhantomData<E>,
} }
impl<A: Service, E: From<A::Error>> FromErr<A, E> { impl<A, E> FromErr<A, E> {
pub(crate) fn new(service: A) -> Self { pub(crate) fn new<Request>(service: A) -> Self
where
A: Service<Request>,
E: From<A::Error>,
{
FromErr { FromErr {
service, service,
f: PhantomData, f: PhantomData,
@ -26,8 +27,7 @@ impl<A: Service, E: From<A::Error>> FromErr<A, E> {
impl<A, E> Clone for FromErr<A, E> impl<A, E> Clone for FromErr<A, E>
where where
A: Service + Clone, A: Clone,
E: From<A::Error>,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
FromErr { FromErr {
@ -37,21 +37,20 @@ where
} }
} }
impl<A, E> Service for FromErr<A, E> impl<A, E, Request> Service<Request> for FromErr<A, E>
where where
A: Service, A: Service<Request>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Future = FromErrFuture<A, E>; type Future = FromErrFuture<A, E, Request>;
fn poll_ready(&mut self) -> Poll<(), E> { fn poll_ready(&mut self) -> Poll<(), E> {
Ok(self.service.poll_ready().map_err(E::from)?) 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 { FromErrFuture {
fut: self.service.call(req), fut: self.service.call(req),
f: PhantomData, f: PhantomData,
@ -59,14 +58,14 @@ where
} }
} }
pub struct FromErrFuture<A: Service, E> { pub struct FromErrFuture<A: Service<Request>, E, Request> {
fut: A::Future, fut: A::Future,
f: PhantomData<E>, f: PhantomData<E>,
} }
impl<A, E> Future for FromErrFuture<A, E> impl<A, E, Request> Future for FromErrFuture<A, E, Request>
where where
A: Service, A: Service<Request>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Item = A::Response; type Item = A::Response;
@ -86,21 +85,20 @@ pub struct FromErrNewService<A, E> {
e: PhantomData<E>, e: PhantomData<E>,
} }
impl<A, E> FromErrNewService<A, E> impl<A, E> FromErrNewService<A, E> {
/// Create new `FromErr` new service instance
pub fn new<Request>(a: A) -> Self
where where
A: NewService, A: NewService<Request>,
E: From<A::Error>, E: From<A::Error>,
{ {
/// Create new `FromErr` new service instance
pub fn new(a: A) -> Self {
Self { a, e: PhantomData } Self { a, e: PhantomData }
} }
} }
impl<A, E> Clone for FromErrNewService<A, E> impl<A, E> Clone for FromErrNewService<A, E>
where where
A: NewService + Clone, A: Clone,
E: From<A::Error>,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
@ -110,18 +108,17 @@ where
} }
} }
impl<A, E> NewService for FromErrNewService<A, E> impl<A, E, Request> NewService<Request> for FromErrNewService<A, E>
where where
A: NewService, A: NewService<Request>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Service = FromErr<A::Service, E>; type Service = FromErr<A::Service, E>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = FromErrNewServiceFuture<A, E>; type Future = FromErrNewServiceFuture<A, E, Request>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
FromErrNewServiceFuture { FromErrNewServiceFuture {
@ -131,18 +128,18 @@ where
} }
} }
pub struct FromErrNewServiceFuture<A, E> pub struct FromErrNewServiceFuture<A, E, Request>
where where
A: NewService, A: NewService<Request>,
E: From<A::Error>, E: From<A::Error>,
{ {
fut: A::Future, fut: A::Future,
e: PhantomData<E>, e: PhantomData<E>,
} }
impl<A, E> Future for FromErrNewServiceFuture<A, E> impl<A, E, Request> Future for FromErrNewServiceFuture<A, E, Request>
where where
A: NewService, A: NewService<Request>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Item = FromErr<A::Service, E>; type Item = FromErr<A::Service, E>;

View File

@ -1,4 +1,4 @@
use std::marker; use std::marker::PhantomData;
use futures::{Async, Future, Poll}; 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. /// Service for the `map` combinator, changing the type of a service's response.
/// ///
/// This is created by the `ServiceExt::map` method. /// This is created by the `ServiceExt::map` method.
pub struct Map<A, F, R> pub struct Map<A, F, Response> {
where
A: Service,
F: Fn(A::Response) -> R,
{
service: A, service: A,
f: F, f: F,
_t: PhantomData<Response>,
} }
impl<A, F, R> Map<A, F, R> impl<A, F, Response> Map<A, F, Response> {
where
A: Service,
F: Fn(A::Response) -> R,
{
/// Create new `Map` combinator /// Create new `Map` combinator
pub fn new(service: A, f: F) -> Self { pub fn new<Request>(service: A, f: F) -> Self
Self { service, f } where
A: Service<Request>,
F: Fn(A::Response) -> Response,
{
Self {
service,
f,
_t: PhantomData,
}
} }
} }
impl<A, F, R> Clone for Map<A, F, R> impl<A, F, Response> Clone for Map<A, F, Response>
where where
A: Service + Clone, A: Clone,
F: Fn(A::Response) -> R + Clone, F: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Map { Map {
service: self.service.clone(), service: self.service.clone(),
f: self.f.clone(), f: self.f.clone(),
_t: PhantomData,
} }
} }
} }
impl<A, F, R> Service for Map<A, F, R> impl<A, F, Request, Response> Service<Request> for Map<A, F, Response>
where where
A: Service, A: Service<Request>,
F: Fn(A::Response) -> R + Clone, F: Fn(A::Response) -> Response + Clone,
{ {
type Request = A::Request; type Response = Response;
type Response = R;
type Error = A::Error; type Error = A::Error;
type Future = MapFuture<A, F, R>; type Future = MapFuture<A, F, Request, Response>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready() 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()) MapFuture::new(self.service.call(req), self.f.clone())
} }
} }
pub struct MapFuture<A, F, R> pub struct MapFuture<A, F, Request, Response>
where where
A: Service, A: Service<Request>,
F: Fn(A::Response) -> R, F: Fn(A::Response) -> Response,
{ {
f: F, f: F,
fut: A::Future, fut: A::Future,
} }
impl<A, F, R> MapFuture<A, F, R> impl<A, F, Request, Response> MapFuture<A, F, Request, Response>
where where
A: Service, A: Service<Request>,
F: Fn(A::Response) -> R, F: Fn(A::Response) -> Response,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
MapFuture { f, fut } MapFuture { f, fut }
} }
} }
impl<A, F, R> Future for MapFuture<A, F, R> impl<A, F, Request, Response> Future for MapFuture<A, F, Request, Response>
where where
A: Service, A: Service<Request>,
F: Fn(A::Response) -> R, F: Fn(A::Response) -> Response,
{ {
type Item = R; type Item = Response;
type Error = A::Error; type Error = A::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -95,84 +96,83 @@ where
} }
/// `MapNewService` new service combinator /// `MapNewService` new service combinator
pub struct MapNewService<A, F, R> { pub struct MapNewService<A, F, Response> {
a: A, a: A,
f: F, f: F,
r: marker::PhantomData<R>, r: PhantomData<Response>,
} }
impl<A, F, R> MapNewService<A, F, R> impl<A, F, Response> MapNewService<A, F, Response> {
where
A: NewService,
F: Fn(A::Response) -> R,
{
/// Create new `Map` new service instance /// Create new `Map` new service instance
pub fn new(a: A, f: F) -> Self { pub fn new<Request>(a: A, f: F) -> Self
where
A: NewService<Request>,
F: Fn(A::Response) -> Response,
{
Self { Self {
a, a,
f, f,
r: marker::PhantomData, r: PhantomData,
} }
} }
} }
impl<A, F, R> Clone for MapNewService<A, F, R> impl<A, F, Response> Clone for MapNewService<A, F, Response>
where where
A: NewService + Clone, A: Clone,
F: Fn(A::Response) -> R + Clone, F: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
a: self.a.clone(), a: self.a.clone(),
f: self.f.clone(), f: self.f.clone(),
r: marker::PhantomData, r: PhantomData,
} }
} }
} }
impl<A, F, R> NewService for MapNewService<A, F, R> impl<A, F, Request, Response> NewService<Request> for MapNewService<A, F, Response>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::Response) -> R + Clone, F: Fn(A::Response) -> Response + Clone,
{ {
type Request = A::Request; type Response = Response;
type Response = R;
type Error = A::Error; type Error = A::Error;
type Service = Map<A::Service, F, R>; type Service = Map<A::Service, F, Response>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = MapNewServiceFuture<A, F, R>; type Future = MapNewServiceFuture<A, F, Request, Response>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
MapNewServiceFuture::new(self.a.new_service(), self.f.clone()) MapNewServiceFuture::new(self.a.new_service(), self.f.clone())
} }
} }
pub struct MapNewServiceFuture<A, F, R> pub struct MapNewServiceFuture<A, F, Request, Response>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::Response) -> R, F: Fn(A::Response) -> Response,
{ {
fut: A::Future, fut: A::Future,
f: Option<F>, f: Option<F>,
} }
impl<A, F, R> MapNewServiceFuture<A, F, R> impl<A, F, Request, Response> MapNewServiceFuture<A, F, Request, Response>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::Response) -> R, F: Fn(A::Response) -> Response,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
MapNewServiceFuture { f: Some(f), fut } MapNewServiceFuture { f: Some(f), fut }
} }
} }
impl<A, F, R> Future for MapNewServiceFuture<A, F, R> impl<A, F, Request, Response> Future for MapNewServiceFuture<A, F, Request, Response>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::Response) -> R, F: Fn(A::Response) -> Response,
{ {
type Item = Map<A::Service, F, R>; type Item = Map<A::Service, F, Response>;
type Error = A::InitError; type Error = A::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {

View File

@ -1,4 +1,4 @@
use std::marker; use std::marker::PhantomData;
use futures::{Async, Future, Poll}; use futures::{Async, Future, Poll};
@ -8,71 +8,71 @@ use super::{NewService, Service};
/// error. /// error.
/// ///
/// This is created by the `ServiceExt::map_err` method. /// This is created by the `ServiceExt::map_err` method.
pub struct MapErr<A, F, E> pub struct MapErr<A, F, E> {
where
A: Service,
F: Fn(A::Error) -> E,
{
service: A, service: A,
f: F, f: F,
_t: PhantomData<E>,
} }
impl<A, F, E> MapErr<A, F, E> impl<A, F, E> MapErr<A, F, E> {
/// Create new `MapErr` combinator
pub fn new<Request>(service: A, f: F) -> Self
where where
A: Service, A: Service<Request>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
/// Create new `MapErr` combinator Self {
pub fn new(service: A, f: F) -> Self { service,
Self { service, f } f,
_t: PhantomData,
}
} }
} }
impl<A, F, E> Clone for MapErr<A, F, E> impl<A, F, E> Clone for MapErr<A, F, E>
where where
A: Service + Clone, A: Clone,
F: Fn(A::Error) -> E + Clone, F: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
MapErr { MapErr {
service: self.service.clone(), service: self.service.clone(),
f: self.f.clone(), f: self.f.clone(),
_t: PhantomData,
} }
} }
} }
impl<A, F, E> Service for MapErr<A, F, E> impl<A, F, E, Request> Service<Request> for MapErr<A, F, E>
where where
A: Service, A: Service<Request>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E + Clone,
F: Clone,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Future = MapErrFuture<A, F, E>; type Future = MapErrFuture<A, F, E, Request>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready().map_err(&self.f) 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()) MapErrFuture::new(self.service.call(req), self.f.clone())
} }
} }
pub struct MapErrFuture<A, F, E> pub struct MapErrFuture<A, F, E, Request>
where where
A: Service, A: Service<Request>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
f: F, f: F,
fut: A::Future, fut: A::Future,
} }
impl<A, F, E> MapErrFuture<A, F, E> impl<A, F, E, Request> MapErrFuture<A, F, E, Request>
where where
A: Service, A: Service<Request>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@ -80,9 +80,9 @@ where
} }
} }
impl<A, F, E> Future for MapErrFuture<A, F, E> impl<A, F, E, Request> Future for MapErrFuture<A, F, E, Request>
where where
A: Service, A: Service<Request>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
type Item = A::Response; type Item = A::Response;
@ -100,68 +100,67 @@ where
pub struct MapErrNewService<A, F, E> { pub struct MapErrNewService<A, F, E> {
a: A, a: A,
f: F, f: F,
e: marker::PhantomData<E>, e: PhantomData<E>,
} }
impl<A, F, E> MapErrNewService<A, F, E> impl<A, F, E> MapErrNewService<A, F, E> {
/// Create new `MapErr` new service instance
pub fn new<Request>(a: A, f: F) -> Self
where where
A: NewService, A: NewService<Request>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
/// Create new `MapErr` new service instance
pub fn new(a: A, f: F) -> Self {
Self { Self {
a, a,
f, f,
e: marker::PhantomData, e: PhantomData,
} }
} }
} }
impl<A, F, E> Clone for MapErrNewService<A, F, E> impl<A, F, E> Clone for MapErrNewService<A, F, E>
where where
A: NewService + Clone, A: Clone,
F: Fn(A::Error) -> E + Clone, F: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
a: self.a.clone(), a: self.a.clone(),
f: self.f.clone(), f: self.f.clone(),
e: marker::PhantomData, e: PhantomData,
} }
} }
} }
impl<A, F, E> NewService for MapErrNewService<A, F, E> impl<A, F, E, Request> NewService<Request> for MapErrNewService<A, F, E>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Service = MapErr<A::Service, F, E>; type Service = MapErr<A::Service, F, E>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = MapErrNewServiceFuture<A, F, E>; type Future = MapErrNewServiceFuture<A, F, E, Request>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
MapErrNewServiceFuture::new(self.a.new_service(), self.f.clone()) MapErrNewServiceFuture::new(self.a.new_service(), self.f.clone())
} }
} }
pub struct MapErrNewServiceFuture<A, F, E> pub struct MapErrNewServiceFuture<A, F, E, Request>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
fut: A::Future, fut: A::Future,
f: F, f: F,
} }
impl<A, F, E> MapErrNewServiceFuture<A, F, E> impl<A, F, E, Request> MapErrNewServiceFuture<A, F, E, Request>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@ -169,9 +168,9 @@ where
} }
} }
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E> impl<A, F, E, Request> Future for MapErrNewServiceFuture<A, F, E, Request>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Item = MapErr<A::Service, F, E>; type Item = MapErr<A::Service, F, E>;

View File

@ -1,4 +1,4 @@
use std::marker; use std::marker::PhantomData;
use futures::{Future, Poll}; use futures::{Future, Poll};
@ -8,68 +8,67 @@ use super::NewService;
pub struct MapInitErr<A, F, E> { pub struct MapInitErr<A, F, E> {
a: A, a: A,
f: F, f: F,
e: marker::PhantomData<E>, e: PhantomData<E>,
} }
impl<A, F, E> MapInitErr<A, F, E> impl<A, F, E> MapInitErr<A, F, E> {
/// Create new `MapInitErr` combinator
pub fn new<Request>(a: A, f: F) -> Self
where where
A: NewService, A: NewService<Request>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
/// Create new `MapInitErr` combinator
pub fn new(a: A, f: F) -> Self {
Self { Self {
a, a,
f, f,
e: marker::PhantomData, e: PhantomData,
} }
} }
} }
impl<A, F, E> Clone for MapInitErr<A, F, E> impl<A, F, E> Clone for MapInitErr<A, F, E>
where where
A: NewService + Clone, A: Clone,
F: Fn(A::InitError) -> E + Clone, F: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
a: self.a.clone(), a: self.a.clone(),
f: self.f.clone(), f: self.f.clone(),
e: marker::PhantomData, e: PhantomData,
} }
} }
} }
impl<A, F, E> NewService for MapInitErr<A, F, E> impl<A, F, E, Request> NewService<Request> for MapInitErr<A, F, E>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::InitError) -> E + Clone, F: Fn(A::InitError) -> E + Clone,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = A::Error; type Error = A::Error;
type Service = A::Service; type Service = A::Service;
type InitError = E; type InitError = E;
type Future = MapInitErrFuture<A, F, E>; type Future = MapInitErrFuture<A, F, E, Request>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
MapInitErrFuture::new(self.a.new_service(), self.f.clone()) MapInitErrFuture::new(self.a.new_service(), self.f.clone())
} }
} }
pub struct MapInitErrFuture<A, F, E> pub struct MapInitErrFuture<A, F, E, Request>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
f: F, f: F,
fut: A::Future, fut: A::Future,
} }
impl<A, F, E> MapInitErrFuture<A, F, E> impl<A, F, E, Request> MapInitErrFuture<A, F, E, Request>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@ -77,9 +76,9 @@ where
} }
} }
impl<A, F, E> Future for MapInitErrFuture<A, F, E> impl<A, F, E, Request> Future for MapInitErrFuture<A, F, E, Request>
where where
A: NewService, A: NewService<Request>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
type Item = A::Service; type Item = A::Service;

View File

@ -1,7 +1,7 @@
use futures::{Future, IntoFuture}; use futures::{Future, IntoFuture};
/// re-export for convinience /// re-export for convinience
pub use tower_service::{NewService, Service}; pub use tower_service::Service;
mod and_then; mod and_then;
mod apply; mod apply;
@ -23,21 +23,20 @@ pub use self::then::{Then, ThenNewService};
/// An extension trait for `Service`s that provides a variety of convenient /// An extension trait for `Service`s that provides a variety of convenient
/// adapters /// adapters
pub trait ServiceExt: Service { pub trait ServiceExt<Request>: Service<Request> {
/// Apply function to specified service and use it as a next service in /// Apply function to specified service and use it as a next service in
/// chain. /// chain.
fn apply<S, I, F, R>( fn apply<T, I, F, Out, Req>(
self, self,
service: I, service: I,
f: F, f: F,
) -> AndThen<Self, Apply<S, F, R, Self::Response>> ) -> AndThen<Self, Apply<T, F, Self::Response, Out, Req>>
where where
Self: Sized, Self: Sized,
S: Service, T: Service<Req, Error = Out::Error>,
S::Error: Into<<R::Future as Future>::Error>, I: IntoService<T, Req>,
I: IntoService<S>, F: Fn(Self::Response, &mut T) -> Out,
F: Fn(Self::Response, &mut S) -> R, Out: IntoFuture<Error = Self::Error>,
R: IntoFuture<Error = Self::Error>,
{ {
self.and_then(Apply::new(service.into_service(), f)) self.and_then(Apply::new(service.into_service(), f))
} }
@ -54,17 +53,17 @@ pub trait ServiceExt: Service {
fn and_then<F, B>(self, service: F) -> AndThen<Self, B> fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
where where
Self: Sized, Self: Sized,
F: IntoService<B>, F: IntoService<B, Self::Response>,
B: Service<Request = Self::Response, Error = Self::Error>, B: Service<Self::Response, Error = Self::Error>,
{ {
AndThen::new(self, service.into_service()) AndThen::new(self, service.into_service())
} }
/// Map this service's error to any error implementing `From` for // /// Map this service's error to any error implementing `From` for
/// this service`s `Error`. // /// this service`s `Error`.
/// // ///
/// Note that this function consumes the receiving service and returns a // /// Note that this function consumes the receiving service and returns a
/// wrapped version of it. // /// wrapped version of it.
fn from_err<E>(self) -> FromErr<Self, E> fn from_err<E>(self) -> FromErr<Self, E>
where where
Self: Sized, Self: Sized,
@ -73,28 +72,28 @@ pub trait ServiceExt: Service {
FromErr::new(self) FromErr::new(self)
} }
/// Chain on a computation for when a call to the service finished, // /// Chain on a computation for when a call to the service finished,
/// passing the result of the call to the next service `B`. // /// passing the result of the call to the next service `B`.
/// // ///
/// Note that this function consumes the receiving future and returns a // /// Note that this function consumes the receiving future and returns a
/// wrapped version of it. // /// wrapped version of it.
fn then<B>(self, service: B) -> Then<Self, B> fn then<B>(self, service: B) -> Then<Self, B>
where where
Self: Sized, Self: Sized,
B: Service<Request = Result<Self::Response, Self::Error>, Error = Self::Error>, B: Service<Result<Self::Response, Self::Error>, Error = Self::Error>,
{ {
Then::new(self, service) Then::new(self, service)
} }
/// Map this service's output to a different type, returning a new service // /// Map this service's output to a different type, returning a new service
/// of the resulting type. // /// of the resulting type.
/// // ///
/// This function is similar to the `Option::map` or `Iterator::map` where // /// This function is similar to the `Option::map` or `Iterator::map` where
/// it will change the type of the underlying service. // /// it will change the type of the underlying service.
/// // ///
/// Note that this function consumes the receiving service and returns a // /// Note that this function consumes the receiving service and returns a
/// wrapped version of it, similar to the existing `map` methods in the // /// wrapped version of it, similar to the existing `map` methods in the
/// standard library. // /// standard library.
fn map<F, R>(self, f: F) -> Map<Self, F, R> fn map<F, R>(self, f: F) -> Map<Self, F, R>
where where
Self: Sized, Self: Sized,
@ -103,14 +102,14 @@ pub trait ServiceExt: Service {
Map::new(self, f) Map::new(self, f)
} }
/// Map this service's error to a different error, returning a new service. // /// 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 // /// 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 // /// the error type of the underlying service. This is useful for example to
/// ensure that services have the same error type. // /// ensure that services have the same error type.
/// // ///
/// Note that this function consumes the receiving service and returns a // /// Note that this function consumes the receiving service and returns a
/// wrapped version of it. // /// wrapped version of it.
fn map_err<F, E>(self, f: F) -> MapErr<Self, F, E> fn map_err<F, E>(self, f: F) -> MapErr<Self, F, E>
where where
Self: Sized, Self: Sized,
@ -120,19 +119,47 @@ pub trait ServiceExt: Service {
} }
} }
pub trait NewServiceExt: NewService { /// Creates new `Service` values.
fn apply<S, I, F, R>( ///
/// 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<Request> {
/// Responses given by the service
type Response;
/// Errors produced by the service
type Error;
/// The `Service` value created by this factory
type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
/// Errors produced while building a service.
type InitError;
/// The future of the `Service` instance.
type Future: Future<Item = Self::Service, Error = Self::InitError>;
/// Create and return a new service value asynchronously.
fn new_service(&self) -> Self::Future;
}
pub trait NewServiceExt<Request>: NewService<Request> {
fn apply<T, I, F, Out, Req>(
self, self,
service: I, service: I,
f: F, f: F,
) -> AndThenNewService<Self, ApplyNewService<S, F, R, Self::Response>> ) -> AndThenNewService<Self, ApplyNewService<T, F, Self::Response, Out, Req>>
where where
Self: Sized, Self: Sized,
S: NewService<InitError = Self::InitError>, T: NewService<Req, InitError = Self::InitError, Error = Out::Error>,
S::Error: Into<<R::Future as Future>::Error>, I: IntoNewService<T, Req>,
I: IntoNewService<S>, F: Fn(Self::Response, &mut T::Service) -> Out + Clone,
F: Fn(Self::Response, &mut S::Service) -> R + Clone, Out: IntoFuture<Error = Self::Error>,
R: IntoFuture<Error = Self::Error>,
{ {
self.and_then(ApplyNewService::new(service, f)) self.and_then(ApplyNewService::new(service, f))
} }
@ -140,22 +167,18 @@ pub trait NewServiceExt: NewService {
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B> fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
where where
Self: Sized, Self: Sized,
F: IntoNewService<B>, F: IntoNewService<B, Self::Response>,
B: NewService< B: NewService<Self::Response, Error = Self::Error, InitError = Self::InitError>,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
{ {
AndThenNewService::new(self, new_service) AndThenNewService::new(self, new_service)
} }
/// `NewService` that create service to map this service's error // /// `NewService` that create service to map this service's error
/// and new service's init error to any error // /// and new service's init error to any error
/// implementing `From` for this service`s `Error`. // /// implementing `From` for this service`s `Error`.
/// // ///
/// Note that this function consumes the receiving new service and returns a // /// Note that this function consumes the receiving new service and returns a
/// wrapped version of it. // /// wrapped version of it.
fn from_err<E>(self) -> FromErrNewService<Self, E> fn from_err<E>(self) -> FromErrNewService<Self, E>
where where
Self: Sized, Self: Sized,
@ -164,18 +187,18 @@ pub trait NewServiceExt: NewService {
FromErrNewService::new(self) FromErrNewService::new(self)
} }
/// Create `NewService` to chain on a computation for when a call to the // /// 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 finished, passing the result of the call to the next
/// service `B`. // /// service `B`.
/// // ///
/// Note that this function consumes the receiving future and returns a // /// Note that this function consumes the receiving future and returns a
/// wrapped version of it. // /// wrapped version of it.
fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B> fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B>
where where
Self: Sized, Self: Sized,
F: IntoNewService<B>, F: IntoNewService<B, Result<Self::Response, Self::Error>>,
B: NewService< B: NewService<
Request = Result<Self::Response, Self::Error>, Result<Self::Response, Self::Error>,
Error = Self::Error, Error = Self::Error,
InitError = Self::InitError, InitError = Self::InitError,
>, >,
@ -208,39 +231,39 @@ pub trait NewServiceExt: NewService {
} }
} }
impl<T: ?Sized> ServiceExt for T where T: Service {} impl<T: ?Sized, R> ServiceExt<R> for T where T: Service<R> {}
impl<T: ?Sized> NewServiceExt for T where T: NewService {} impl<T: ?Sized, R> NewServiceExt<R> for T where T: NewService<R> {}
/// Trait for types that can be converted to a `Service` /// Trait for types that can be converted to a `Service`
pub trait IntoService<T> pub trait IntoService<T, Request>
where where
T: Service, T: Service<Request>,
{ {
/// Convert to a `Service` /// Convert to a `Service`
fn into_service(self) -> T; fn into_service(self) -> T;
} }
/// Trait for types that can be converted to a Service /// Trait for types that can be converted to a Service
pub trait IntoNewService<T> pub trait IntoNewService<T, Request>
where where
T: NewService, T: NewService<Request>,
{ {
/// Convert to an `NewService` /// Convert to an `NewService`
fn into_new_service(self) -> T; fn into_new_service(self) -> T;
} }
impl<T> IntoService<T> for T impl<T, Request> IntoService<T, Request> for T
where where
T: Service, T: Service<Request>,
{ {
fn into_service(self) -> T { fn into_service(self) -> T {
self self
} }
} }
impl<T> IntoNewService<T> for T impl<T, Request> IntoNewService<T, Request> for T
where where
T: NewService, T: NewService<Request>,
{ {
fn into_new_service(self) -> T { fn into_new_service(self) -> T {
self self

View File

@ -12,21 +12,21 @@ pub struct Then<A, B> {
b: Cell<B>, b: Cell<B>,
} }
impl<A, B> Then<A, B> impl<A, B> Then<A, B> {
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
{
/// Create new `Then` combinator /// Create new `Then` combinator
pub fn new(a: A, b: B) -> Then<A, B> { pub fn new<Request>(a: A, b: B) -> Then<A, B>
where
A: Service<Request>,
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
{
Then { a, b: Cell::new(b) } Then { a, b: Cell::new(b) }
} }
} }
impl<A, B> Clone for Then<A, B> impl<A, B> Clone for Then<A, B>
where where
A: Service + Clone, A: Clone,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>, B: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Then { Then {
@ -36,40 +36,39 @@ where
} }
} }
impl<A, B> Service for Then<A, B> impl<A, B, Request> Service<Request> for Then<A, B>
where where
A: Service, A: Service<Request>,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>, B: Service<Result<A::Response, A::Error>, Error = A::Error>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = B::Error; type Error = B::Error;
type Future = ThenFuture<A, B>; type Future = ThenFuture<A, B, Request>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
let _ = try_ready!(self.a.poll_ready()); let _ = try_ready!(self.a.poll_ready());
self.b.borrow_mut().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()) ThenFuture::new(self.a.call(req), self.b.clone())
} }
} }
pub struct ThenFuture<A, B> pub struct ThenFuture<A, B, Request>
where where
A: Service, A: Service<Request>,
B: Service<Request = Result<A::Response, A::Error>>, B: Service<Result<A::Response, A::Error>>,
{ {
b: Cell<B>, b: Cell<B>,
fut_b: Option<B::Future>, fut_b: Option<B::Future>,
fut_a: A::Future, fut_a: A::Future,
} }
impl<A, B> ThenFuture<A, B> impl<A, B, Request> ThenFuture<A, B, Request>
where where
A: Service, A: Service<Request>,
B: Service<Request = Result<A::Response, A::Error>>, B: Service<Result<A::Response, A::Error>>,
{ {
fn new(fut_a: A::Future, b: Cell<B>) -> Self { fn new(fut_a: A::Future, b: Cell<B>) -> Self {
ThenFuture { ThenFuture {
@ -80,10 +79,10 @@ where
} }
} }
impl<A, B> Future for ThenFuture<A, B> impl<A, B, Request> Future for ThenFuture<A, B, Request>
where where
A: Service, A: Service<Request>,
B: Service<Request = Result<A::Response, A::Error>>, B: Service<Result<A::Response, A::Error>>,
{ {
type Item = B::Response; type Item = B::Response;
type Error = B::Error; type Error = B::Error;
@ -113,13 +112,18 @@ pub struct ThenNewService<A, B> {
b: B, b: B,
} }
impl<A, B> ThenNewService<A, B> impl<A, B> ThenNewService<A, B> {
where
A: NewService,
B: NewService,
{
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<F: IntoNewService<B>>(a: A, f: F) -> Self { pub fn new<F, Request>(a: A, f: F) -> Self
where
A: NewService<Request>,
B: NewService<
Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
F: IntoNewService<B, Result<A::Response, A::Error>>,
{
Self { Self {
a, a,
b: f.into_new_service(), b: f.into_new_service(),
@ -127,22 +131,17 @@ where
} }
} }
impl<A, B> NewService for ThenNewService<A, B> impl<A, B, Request> NewService<Request> for ThenNewService<A, B>
where where
A: NewService, A: NewService<Request>,
B: NewService< B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Service = Then<A::Service, B::Service>; type Service = Then<A::Service, B::Service>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = ThenNewServiceFuture<A, B>; type Future = ThenNewServiceFuture<A, B, Request>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
ThenNewServiceFuture::new(self.a.new_service(), self.b.new_service()) ThenNewServiceFuture::new(self.a.new_service(), self.b.new_service())
@ -151,12 +150,8 @@ where
impl<A, B> Clone for ThenNewService<A, B> impl<A, B> Clone for ThenNewService<A, B>
where where
A: NewService + Clone, A: Clone,
B: NewService< B: Clone,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
> + Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
@ -166,10 +161,10 @@ where
} }
} }
pub struct ThenNewServiceFuture<A, B> pub struct ThenNewServiceFuture<A, B, Request>
where where
A: NewService, A: NewService<Request>,
B: NewService, B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
{ {
fut_b: B::Future, fut_b: B::Future,
fut_a: A::Future, fut_a: A::Future,
@ -177,10 +172,10 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B> ThenNewServiceFuture<A, B> impl<A, B, Request> ThenNewServiceFuture<A, B, Request>
where where
A: NewService, A: NewService<Request>,
B: NewService, B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
{ {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self { fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
ThenNewServiceFuture { ThenNewServiceFuture {
@ -192,14 +187,10 @@ where
} }
} }
impl<A, B> Future for ThenNewServiceFuture<A, B> impl<A, B, Request> Future for ThenNewServiceFuture<A, B, Request>
where where
A: NewService, A: NewService<Request>,
B: NewService< B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{ {
type Item = Then<A::Service, B::Service>; type Item = Then<A::Service, B::Service>;
type Error = A::InitError; type Error = A::InitError;

View File

@ -36,8 +36,7 @@ impl<T: AsyncRead + AsyncWrite> Clone for NativeTlsAcceptor<T> {
} }
} }
impl<T: AsyncRead + AsyncWrite> NewService for NativeTlsAcceptor<T> { impl<T: AsyncRead + AsyncWrite> NewService<T> for NativeTlsAcceptor<T> {
type Request = T;
type Response = TlsStream<T>; type Response = TlsStream<T>;
type Error = Error; type Error = Error;
type Service = NativeTlsAcceptorService<T>; type Service = NativeTlsAcceptorService<T>;
@ -61,8 +60,7 @@ pub struct NativeTlsAcceptorService<T> {
conns: Counter, conns: Counter,
} }
impl<T: AsyncRead + AsyncWrite> Service for NativeTlsAcceptorService<T> { impl<T: AsyncRead + AsyncWrite> Service<T> for NativeTlsAcceptorService<T> {
type Request = T;
type Response = TlsStream<T>; type Response = TlsStream<T>;
type Error = Error; type Error = Error;
type Future = Accept<T>; type Future = Accept<T>;
@ -75,7 +73,7 @@ impl<T: AsyncRead + AsyncWrite> Service for NativeTlsAcceptorService<T> {
} }
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: T) -> Self::Future {
Accept { Accept {
_guard: self.conns.get(), _guard: self.conns.get(),
inner: Some(self.acceptor.accept(req)), inner: Some(self.acceptor.accept(req)),

View File

@ -37,8 +37,7 @@ impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
} }
} }
impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> { impl<T: AsyncRead + AsyncWrite> NewService<T> for OpensslAcceptor<T> {
type Request = T;
type Response = SslStream<T>; type Response = SslStream<T>;
type Error = Error; type Error = Error;
type Service = OpensslAcceptorService<T>; type Service = OpensslAcceptorService<T>;
@ -62,8 +61,7 @@ pub struct OpensslAcceptorService<T> {
conns: Counter, conns: Counter,
} }
impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> { impl<T: AsyncRead + AsyncWrite> Service<T> for OpensslAcceptorService<T> {
type Request = T;
type Response = SslStream<T>; type Response = SslStream<T>;
type Error = Error; type Error = Error;
type Future = OpensslAcceptorServiceFut<T>; type Future = OpensslAcceptorServiceFut<T>;
@ -76,7 +74,7 @@ impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
} }
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: T) -> Self::Future {
OpensslAcceptorServiceFut { OpensslAcceptorServiceFut {
_guard: self.conns.get(), _guard: self.conns.get(),
fut: SslAcceptorExt::accept_async(&self.acceptor, req), fut: SslAcceptorExt::accept_async(&self.acceptor, req),
@ -119,7 +117,7 @@ impl<R, T, E> OpensslConnector<R, T, E> {
impl<R: RequestHost, T: AsyncRead + AsyncWrite> OpensslConnector<R, T, ()> { impl<R: RequestHost, T: AsyncRead + AsyncWrite> OpensslConnector<R, T, ()> {
pub fn service( pub fn service(
connector: SslConnector, connector: SslConnector,
) -> impl Service<Request = (R, T), Response = (R, SslStream<T>), Error = Error> { ) -> impl Service<(R, T), Response = (R, SslStream<T>), Error = Error> {
OpensslConnectorService { OpensslConnectorService {
connector: connector, connector: connector,
_t: PhantomData, _t: PhantomData,
@ -136,8 +134,9 @@ impl<R, T, E> Clone for OpensslConnector<R, T, E> {
} }
} }
impl<R: RequestHost, T: AsyncRead + AsyncWrite, E> NewService for OpensslConnector<R, T, E> { impl<R: RequestHost, T: AsyncRead + AsyncWrite, E> NewService<(R, T)>
type Request = (R, T); for OpensslConnector<R, T, E>
{
type Response = (R, SslStream<T>); type Response = (R, SslStream<T>);
type Error = Error; type Error = Error;
type Service = OpensslConnectorService<R, T>; type Service = OpensslConnectorService<R, T>;
@ -157,8 +156,9 @@ pub struct OpensslConnectorService<R, T> {
_t: PhantomData<(R, T)>, _t: PhantomData<(R, T)>,
} }
impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<R, T> { impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service<(R, T)>
type Request = (R, T); for OpensslConnectorService<R, T>
{
type Response = (R, SslStream<T>); type Response = (R, SslStream<T>);
type Error = Error; type Error = Error;
type Future = ConnectAsyncExt<R, T>; type Future = ConnectAsyncExt<R, T>;
@ -167,7 +167,7 @@ impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service for OpensslConnectorServ
Ok(Async::Ready(())) Ok(Async::Ready(()))
} }
fn call(&mut self, (req, stream): Self::Request) -> Self::Future { fn call(&mut self, (req, stream): (R, T)) -> Self::Future {
ConnectAsyncExt { ConnectAsyncExt {
fut: SslConnectorExt::connect_async(&self.connector, req.host(), stream), fut: SslConnectorExt::connect_async(&self.connector, req.host(), stream),
req: Some(req), req: Some(req),

View File

@ -38,8 +38,7 @@ impl<T> Clone for RustlsAcceptor<T> {
} }
} }
impl<T: AsyncRead + AsyncWrite> NewService for RustlsAcceptor<T> { impl<T: AsyncRead + AsyncWrite> NewService<T> for RustlsAcceptor<T> {
type Request = T;
type Response = TlsStream<T, ServerSession>; type Response = TlsStream<T, ServerSession>;
type Error = io::Error; type Error = io::Error;
type Service = RustlsAcceptorService<T>; type Service = RustlsAcceptorService<T>;
@ -63,8 +62,7 @@ pub struct RustlsAcceptorService<T> {
conns: Counter, conns: Counter,
} }
impl<T: AsyncRead + AsyncWrite> Service for RustlsAcceptorService<T> { impl<T: AsyncRead + AsyncWrite> Service<T> for RustlsAcceptorService<T> {
type Request = T;
type Response = TlsStream<T, ServerSession>; type Response = TlsStream<T, ServerSession>;
type Error = io::Error; type Error = io::Error;
type Future = RustlsAcceptorServiceFut<T>; type Future = RustlsAcceptorServiceFut<T>;
@ -77,7 +75,7 @@ impl<T: AsyncRead + AsyncWrite> Service for RustlsAcceptorService<T> {
} }
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: T) -> Self::Future {
RustlsAcceptorServiceFut { RustlsAcceptorServiceFut {
_guard: self.conns.get(), _guard: self.conns.get(),
fut: self.acceptor.accept(req), fut: self.acceptor.accept(req),

View File

@ -17,10 +17,13 @@ pub struct StreamDispatcher<S: Stream, T> {
impl<S, T> StreamDispatcher<S, T> impl<S, T> StreamDispatcher<S, T>
where where
S: Stream, S: Stream,
T: Service<Request = Result<S::Item, S::Error>, Response = (), Error = ()>, T: Service<Result<S::Item, S::Error>, Response = (), Error = ()>,
T::Future: 'static, T::Future: 'static,
{ {
pub fn new<F: IntoService<T>>(stream: S, service: F) -> Self { pub fn new<F>(stream: S, service: F) -> Self
where
F: IntoService<T, Result<S::Item, S::Error>>,
{
let (stop_tx, stop_rx) = mpsc::unbounded(); let (stop_tx, stop_rx) = mpsc::unbounded();
StreamDispatcher { StreamDispatcher {
stream, stream,
@ -35,7 +38,7 @@ where
impl<S, T> Future for StreamDispatcher<S, T> impl<S, T> Future for StreamDispatcher<S, T>
where where
S: Stream, S: Stream,
T: Service<Request = Result<S::Item, S::Error>, Response = (), Error = ()>, T: Service<Result<S::Item, S::Error>, Response = (), Error = ()>,
T::Future: 'static, T::Future: 'static,
{ {
type Item = (); type Item = ();
@ -109,8 +112,7 @@ impl<T> Clone for TakeItem<T> {
} }
} }
impl<T: Stream> NewService for TakeItem<T> { impl<T: Stream> NewService<T> for TakeItem<T> {
type Request = T;
type Response = (Option<T::Item>, T); type Response = (Option<T::Item>, T);
type Error = T::Error; type Error = T::Error;
type InitError = (); type InitError = ();
@ -133,8 +135,7 @@ impl<T> Clone for TakeItemService<T> {
} }
} }
impl<T: Stream> Service for TakeItemService<T> { impl<T: Stream> Service<T> for TakeItemService<T> {
type Request = T;
type Response = (Option<T::Item>, T); type Response = (Option<T::Item>, T);
type Error = T::Error; type Error = T::Error;
type Future = TakeItemServiceResponse<T>; type Future = TakeItemServiceResponse<T>;
@ -143,7 +144,7 @@ impl<T: Stream> Service for TakeItemService<T> {
Ok(Async::Ready(())) Ok(Async::Ready(()))
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: T) -> Self::Future {
TakeItemServiceResponse { stream: Some(req) } TakeItemServiceResponse { stream: Some(req) }
} }
} }

View File

@ -43,8 +43,7 @@ impl Default for LowResTime {
} }
} }
impl NewService for LowResTime { impl NewService<()> for LowResTime {
type Request = ();
type Response = Instant; type Response = Instant;
type Error = Never; type Error = Never;
type InitError = Never; type InitError = Never;
@ -88,8 +87,7 @@ impl LowResTimeService {
} }
} }
impl Service for LowResTimeService { impl Service<()> for LowResTimeService {
type Request = ();
type Response = Instant; type Response = Instant;
type Error = Never; type Error = Never;
type Future = FutureResult<Self::Response, Self::Error>; type Future = FutureResult<Self::Response, Self::Error>;

View File

@ -12,7 +12,7 @@ use service::{NewService, Service};
/// Applies a timeout to requests. /// Applies a timeout to requests.
#[derive(Debug)] #[derive(Debug)]
pub struct Timeout<T: NewService + Clone> { pub struct Timeout<T> {
inner: T, inner: T,
timeout: Duration, timeout: Duration,
} }
@ -34,25 +34,24 @@ impl<E: fmt::Debug> fmt::Debug for TimeoutError<E> {
} }
} }
impl<T> Timeout<T> impl<T> Timeout<T> {
pub fn new<Request>(timeout: Duration, inner: T) -> Self
where where
T: NewService + Clone, T: NewService<Request> + Clone,
{ {
pub fn new(timeout: Duration, inner: T) -> Self {
Timeout { inner, timeout } Timeout { inner, timeout }
} }
} }
impl<T> NewService for Timeout<T> impl<T, Request> NewService<Request> for Timeout<T>
where where
T: NewService + Clone, T: NewService<Request> + Clone,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = TimeoutError<T::Error>; type Error = TimeoutError<T::Error>;
type InitError = T::InitError; type InitError = T::InitError;
type Service = TimeoutService<T::Service>; type Service = TimeoutService<T::Service>;
type Future = TimeoutFut<T>; type Future = TimeoutFut<T, Request>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
TimeoutFut { TimeoutFut {
@ -64,14 +63,14 @@ where
/// `Timeout` response future /// `Timeout` response future
#[derive(Debug)] #[derive(Debug)]
pub struct TimeoutFut<T: NewService> { pub struct TimeoutFut<T: NewService<Request>, Request> {
fut: T::Future, fut: T::Future,
timeout: Duration, timeout: Duration,
} }
impl<T> Future for TimeoutFut<T> impl<T, Request> Future for TimeoutFut<T, Request>
where where
T: NewService, T: NewService<Request>,
{ {
type Item = TimeoutService<T::Service>; type Item = TimeoutService<T::Service>;
type Error = T::InitError; type Error = T::InitError;
@ -90,15 +89,15 @@ pub struct TimeoutService<T> {
} }
impl<T> TimeoutService<T> { impl<T> TimeoutService<T> {
pub fn new(timeout: Duration, inner: T) -> Self { pub fn new<Request>(timeout: Duration, inner: T) -> Self
where
T: Service<Request>,
{
TimeoutService { inner, timeout } TimeoutService { inner, timeout }
} }
} }
impl<T> Clone for TimeoutService<T> impl<T: Clone> Clone for TimeoutService<T> {
where
T: Clone,
{
fn clone(&self) -> Self { fn clone(&self) -> Self {
TimeoutService { TimeoutService {
inner: self.inner.clone(), inner: self.inner.clone(),
@ -107,14 +106,13 @@ where
} }
} }
impl<T> Service for TimeoutService<T> impl<T, Request> Service<Request> for TimeoutService<T>
where where
T: Service, T: Service<Request>,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = TimeoutError<T::Error>; type Error = TimeoutError<T::Error>;
type Future = TimeoutServiceResponse<T>; type Future = TimeoutServiceResponse<T, Request>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.inner self.inner
@ -122,7 +120,7 @@ where
.map_err(|e| TimeoutError::Service(e)) .map_err(|e| TimeoutError::Service(e))
} }
fn call(&mut self, request: Self::Request) -> Self::Future { fn call(&mut self, request: Request) -> Self::Future {
TimeoutServiceResponse { TimeoutServiceResponse {
fut: self.inner.call(request), fut: self.inner.call(request),
sleep: Delay::new(clock::now() + self.timeout), sleep: Delay::new(clock::now() + self.timeout),
@ -132,14 +130,14 @@ where
/// `TimeoutService` response future /// `TimeoutService` response future
#[derive(Debug)] #[derive(Debug)]
pub struct TimeoutServiceResponse<T: Service> { pub struct TimeoutServiceResponse<T: Service<Request>, Request> {
fut: T::Future, fut: T::Future,
sleep: Delay, sleep: Delay,
} }
impl<T> Future for TimeoutServiceResponse<T> impl<T, Request> Future for TimeoutServiceResponse<T, Request>
where where
T: Service, T: Service<Request>,
{ {
type Item = T::Response; type Item = T::Response;
type Error = TimeoutError<T::Error>; type Error = TimeoutError<T::Error>;