1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-30 17:44:34 +01:00

add Config argument to NewService

This commit is contained in:
Nikolay Kim 2019-02-22 12:44:37 -08:00
parent 6ea128fac5
commit 862be49e30
32 changed files with 386 additions and 433 deletions

View File

@ -27,7 +27,8 @@ default = []
ssl = ["openssl", "tokio-openssl"] ssl = ["openssl", "tokio-openssl"]
[dependencies] [dependencies]
actix-service = "0.2.0" #actix-service = "0.2.0"
actix-service = { path="../actix-service" }
actix-codec = "0.1.0" actix-codec = "0.1.0"
futures = "0.1" futures = "0.1"
tokio-tcp = "0.1" tokio-tcp = "0.1"

View File

@ -177,12 +177,13 @@ impl Connector {
cfg: ResolverConfig, cfg: ResolverConfig,
opts: ResolverOpts, opts: ResolverOpts,
) -> impl NewService< ) -> impl NewService<
(),
Request = Connect, Request = Connect,
Response = (Connect, TcpStream), Response = (Connect, TcpStream),
Error = ConnectorError, Error = ConnectorError,
InitError = E, InitError = E,
> + Clone { > + Clone {
move || -> FutureResult<Connector, E> { ok(Connector::new(cfg.clone(), opts)) } move |_: &()| -> FutureResult<Connector, E> { ok(Connector::new(cfg.clone(), opts)) }
} }
} }

View File

@ -44,7 +44,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<()>
for OpensslConnector<R, T, E>
{
type Request = (R, T); type Request = (R, T);
type Response = (R, SslStream<T>); type Response = (R, SslStream<T>);
type Error = HandshakeError<T>; type Error = HandshakeError<T>;
@ -52,7 +54,7 @@ impl<R: RequestHost, T: AsyncRead + AsyncWrite, E> NewService for OpensslConnect
type InitError = E; type InitError = E;
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
ok(OpensslConnectorService { ok(OpensslConnectorService {
connector: self.connector.clone(), connector: self.connector.clone(),
_t: PhantomData, _t: PhantomData,

View File

@ -33,7 +33,8 @@ ssl = ["openssl", "tokio-openssl"]
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"] rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
[dependencies] [dependencies]
actix-service = "0.2.1" #actix-service = "0.2.1"
actix-service = { path="../actix-service" }
actix-rt = "0.1.0" actix-rt = "0.1.0"
log = "0.4" log = "0.4"

View File

@ -15,7 +15,6 @@ use crate::accept::{AcceptLoop, AcceptNotify, Command};
use crate::config::{ConfiguredService, ServiceConfig}; use crate::config::{ConfiguredService, ServiceConfig};
use crate::server::{Server, ServerCommand}; use crate::server::{Server, ServerCommand};
use crate::services::{InternalServiceFactory, StreamNewService, StreamServiceFactory}; use crate::services::{InternalServiceFactory, StreamNewService, StreamServiceFactory};
use crate::services::{ServiceFactory, ServiceNewService};
use crate::signals::{Signal, Signals}; use crate::signals::{Signal, Signals};
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient}; use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
use crate::Token; use crate::Token;
@ -176,26 +175,6 @@ impl ServerBuilder {
self self
} }
/// Add new service to the server.
pub fn listen2<F, N: AsRef<str>>(
mut self,
name: N,
lst: net::TcpListener,
factory: F,
) -> Self
where
F: ServiceFactory,
{
let token = self.token.next();
self.services.push(ServiceNewService::create(
name.as_ref().to_string(),
token,
factory,
));
self.sockets.push((token, lst));
self
}
/// Spawn new thread and start listening for incoming connections. /// Spawn new thread and start listening for incoming connections.
/// ///
/// This method spawns new thread and starts new actix system. Other than /// This method spawns new thread and starts new actix system. Other than

View File

@ -114,7 +114,7 @@ impl InternalServiceFactory for ConfiguredService {
// construct services // construct services
let mut fut = Vec::new(); let mut fut = Vec::new();
for (token, ns) in rt.services { for (token, ns) in rt.services {
fut.push(ns.new_service().map(move |service| (token, service))); fut.push(ns.new_service(&()).map(move |service| (token, service)));
} }
Box::new(join_all(fut).map_err(|e| { Box::new(join_all(fut).map_err(|e| {
@ -219,8 +219,8 @@ where
type Service = BoxedServerService; type Service = BoxedServerService;
type Future = Box<Future<Item = BoxedServerService, Error = ()>>; type Future = Box<Future<Item = BoxedServerService, Error = ()>>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
Box::new(self.inner.new_service().map_err(|_| ()).map(|s| { Box::new(self.inner.new_service(&()).map_err(|_| ()).map(|s| {
let service: BoxedServerService = Box::new(StreamService::new(s)); let service: BoxedServerService = Box::new(StreamService::new(s));
service service
})) }))

View File

@ -13,7 +13,7 @@ mod worker;
pub use self::builder::ServerBuilder; pub use self::builder::ServerBuilder;
pub use self::config::{ServiceConfig, ServiceRuntime}; pub use self::config::{ServiceConfig, ServiceRuntime};
pub use self::server::Server; pub use self::server::Server;
pub use self::services::{ServerMessage, ServiceFactory, StreamServiceFactory}; pub use self::services::StreamServiceFactory;
/// Socket id token /// Socket id token
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]

View File

@ -13,7 +13,7 @@ use super::Token;
use crate::counter::CounterGuard; use crate::counter::CounterGuard;
/// Server message /// Server message
pub enum ServerMessage { pub(crate) enum ServerMessage {
/// New stream /// New stream
Connect(net::TcpStream), Connect(net::TcpStream),
/// Gracefull shutdown /// Gracefull shutdown
@ -28,12 +28,6 @@ pub trait StreamServiceFactory: Send + Clone + 'static {
fn create(&self) -> Self::NewService; fn create(&self) -> Self::NewService;
} }
pub trait ServiceFactory: Send + Clone + 'static {
type NewService: NewService<Request = ServerMessage>;
fn create(&self) -> Self::NewService;
}
pub(crate) trait InternalServiceFactory: Send { pub(crate) trait InternalServiceFactory: Send {
fn name(&self, token: Token) -> &str; fn name(&self, token: Token) -> &str;
@ -98,86 +92,6 @@ where
} }
} }
pub(crate) struct ServerService<T> {
service: T,
}
impl<T> ServerService<T> {
fn new(service: T) -> Self {
ServerService { service }
}
}
impl<T> Service for ServerService<T>
where
T: Service<Request = ServerMessage>,
T::Future: 'static,
T::Error: 'static,
{
type Request = (Option<CounterGuard>, ServerMessage);
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready().map_err(|_| ())
}
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
spawn(self.service.call(req).then(move |res| {
drop(guard);
res.map_err(|_| ()).map(|_| ())
}));
ok(())
}
}
pub(crate) struct ServiceNewService<F: ServiceFactory> {
name: String,
inner: F,
token: Token,
}
impl<F> ServiceNewService<F>
where
F: ServiceFactory,
{
pub(crate) fn create(name: String, token: Token, inner: F) -> Box<InternalServiceFactory> {
Box::new(Self { name, inner, token })
}
}
impl<F> InternalServiceFactory for ServiceNewService<F>
where
F: ServiceFactory,
{
fn name(&self, _: Token) -> &str {
&self.name
}
fn clone_factory(&self) -> Box<InternalServiceFactory> {
Box::new(Self {
name: self.name.clone(),
inner: self.inner.clone(),
token: self.token,
})
}
fn create(&self) -> Box<Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> {
let token = self.token;
Box::new(
self.inner
.create()
.new_service()
.map_err(|_| ())
.map(move |inner| {
let service: BoxedServerService = Box::new(ServerService::new(inner));
vec![(token, service)]
}),
)
}
}
pub(crate) struct StreamNewService<F: StreamServiceFactory> { pub(crate) struct StreamNewService<F: StreamServiceFactory> {
name: String, name: String,
inner: F, inner: F,
@ -214,7 +128,7 @@ where
Box::new( Box::new(
self.inner self.inner
.create() .create()
.new_service() .new_service(&())
.map_err(|_| ()) .map_err(|_| ())
.map(move |inner| { .map(move |inner| {
let service: BoxedServerService = Box::new(StreamService::new(inner)); let service: BoxedServerService = Box::new(StreamService::new(inner));
@ -238,18 +152,6 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
} }
} }
impl<F, T> ServiceFactory for F
where
F: Fn() -> T + Send + Clone + 'static,
T: NewService<Request = ServerMessage>,
{
type NewService = T;
fn create(&self) -> T {
(self)()
}
}
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,

View File

@ -44,7 +44,7 @@ impl<T: AsyncRead + AsyncWrite> NewService for NativeTlsAcceptor<T> {
type InitError = (); type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
MAX_CONN_COUNTER.with(|conns| { MAX_CONN_COUNTER.with(|conns| {
ok(NativeTlsAcceptorService { ok(NativeTlsAcceptorService {
acceptor: self.acceptor.clone(), acceptor: self.acceptor.clone(),

View File

@ -44,7 +44,7 @@ impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> {
type InitError = (); type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
MAX_CONN_COUNTER.with(|conns| { MAX_CONN_COUNTER.with(|conns| {
ok(OpensslAcceptorService { ok(OpensslAcceptorService {
acceptor: self.acceptor.clone(), acceptor: self.acceptor.clone(),

View File

@ -46,7 +46,7 @@ impl<T: AsyncRead + AsyncWrite> NewService for RustlsAcceptor<T> {
type InitError = (); type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
MAX_CONN_COUNTER.with(|conns| { MAX_CONN_COUNTER.with(|conns| {
ok(RustlsAcceptorService { ok(RustlsAcceptorService {
acceptor: self.config.clone().into(), acceptor: self.config.clone().into(),

View File

@ -1,5 +1,12 @@
# Changes # Changes
## [0.3.0] - 2019-02-xx
## Changed
* Added `Config` argument to `NewService` trait.
## [0.2.2] - 2019-02-19 ## [0.2.2] - 2019-02-19
### Added ### Added

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-service" name = "actix-service"
version = "0.2.2" version = "0.3.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix Service" description = "Actix Service"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]

View File

@ -1,3 +1,5 @@
use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll}; use futures::{try_ready, Async, Future, Poll};
use super::{IntoNewService, NewService, Service}; use super::{IntoNewService, NewService, Service};
@ -105,29 +107,31 @@ where
} }
/// `AndThenNewService` new service combinator /// `AndThenNewService` new service combinator
pub struct AndThenNewService<A, B> { pub struct AndThenNewService<A, B, C> {
a: A, a: A,
b: B, b: B,
_t: PhantomData<C>,
} }
impl<A, B> AndThenNewService<A, B> { impl<A, B, C> AndThenNewService<A, B, C> {
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<F: IntoNewService<B>>(a: A, f: F) -> Self pub fn new<F: IntoNewService<B, C>>(a: A, f: F) -> Self
where where
A: NewService, A: NewService<C>,
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>, B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{ {
Self { Self {
a, a,
b: f.into_new_service(), b: f.into_new_service(),
_t: PhantomData,
} }
} }
} }
impl<A, B> NewService for AndThenNewService<A, B> impl<A, B, C> NewService<C> for AndThenNewService<A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>, B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{ {
type Request = A::Request; type Request = A::Request;
type Response = B::Response; type Response = B::Response;
@ -135,14 +139,14 @@ where
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, C>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
AndThenNewServiceFuture::new(self.a.new_service(), self.b.new_service()) AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg))
} }
} }
impl<A, B> Clone for AndThenNewService<A, B> impl<A, B, C> Clone for AndThenNewService<A, B, C>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
@ -151,14 +155,15 @@ where
Self { Self {
a: self.a.clone(), a: self.a.clone(),
b: self.b.clone(), b: self.b.clone(),
_t: PhantomData,
} }
} }
} }
pub struct AndThenNewServiceFuture<A, B> pub struct AndThenNewServiceFuture<A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService<Request = A::Response>, B: NewService<C, Request = A::Response>,
{ {
fut_b: B::Future, fut_b: B::Future,
fut_a: A::Future, fut_a: A::Future,
@ -166,10 +171,10 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B> AndThenNewServiceFuture<A, B> impl<A, B, C> AndThenNewServiceFuture<A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService<Request = A::Response>, B: NewService<C, Request = 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 {
@ -181,10 +186,10 @@ where
} }
} }
impl<A, B> Future for AndThenNewServiceFuture<A, B> impl<A, B, C> Future for AndThenNewServiceFuture<A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>, B: NewService<C, Request = 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;
@ -286,7 +291,7 @@ mod tests {
let new_srv = blank let new_srv = blank
.into_new_service() .into_new_service()
.and_then(move || Ok(Srv2(cnt.clone()))); .and_then(move || Ok(Srv2(cnt.clone())));
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
let res = srv.call("srv1").poll(); let res = srv.call("srv1").poll();
assert!(res.is_ok()); assert!(res.is_ok());
assert_eq!(res.unwrap(), Async::Ready(("srv1", "srv2"))); assert_eq!(res.unwrap(), Async::Ready(("srv1", "srv2")));

View File

@ -124,26 +124,32 @@ where
} }
/// `Apply` new service combinator /// `Apply` new service combinator
pub struct AndThenTransformNewService<T, A, B> { pub struct AndThenTransformNewService<T, A, B, C> {
a: A, a: A,
b: B, b: B,
t: T, t: T,
_t: std::marker::PhantomData<C>,
} }
impl<T, A, B> AndThenTransformNewService<T, A, B> impl<T, A, B, C> AndThenTransformNewService<T, A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService<Error = A::Error, InitError = A::InitError>, B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>, T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new(t: T, a: A, b: B) -> Self { pub fn new(t: T, a: A, b: B) -> Self {
Self { a, b, t } Self {
a,
b,
t,
_t: std::marker::PhantomData,
}
} }
} }
impl<T, A, B> Clone for AndThenTransformNewService<T, A, B> impl<T, A, B, C> Clone for AndThenTransformNewService<T, A, B, C>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
@ -154,14 +160,15 @@ where
a: self.a.clone(), a: self.a.clone(),
b: self.b.clone(), b: self.b.clone(),
t: self.t.clone(), t: self.t.clone(),
_t: std::marker::PhantomData,
} }
} }
} }
impl<T, A, B> NewService for AndThenTransformNewService<T, A, B> impl<T, A, B, C> NewService<C> for AndThenTransformNewService<T, A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService<Error = A::Error, InitError = A::InitError>, B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>, T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
@ -171,24 +178,24 @@ where
type InitError = T::InitError; type InitError = T::InitError;
type Service = AndThenTransform<T::Transform, A::Service, B::Service>; type Service = AndThenTransform<T::Transform, A::Service, B::Service>;
type Future = AndThenTransformNewServiceFuture<T, A, B>; type Future = AndThenTransformNewServiceFuture<T, A, B, C>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
AndThenTransformNewServiceFuture { AndThenTransformNewServiceFuture {
a: None, a: None,
b: None, b: None,
t: None, t: None,
fut_a: self.a.new_service(), fut_a: self.a.new_service(cfg),
fut_b: self.b.new_service(), fut_b: self.b.new_service(cfg),
fut_t: self.t.new_transform(), fut_t: self.t.new_transform(),
} }
} }
} }
pub struct AndThenTransformNewServiceFuture<T, A, B> pub struct AndThenTransformNewServiceFuture<T, A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService<Error = A::Error, InitError = A::InitError>, B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>, T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
@ -200,10 +207,10 @@ where
t: Option<T::Transform>, t: Option<T::Transform>,
} }
impl<T, A, B> Future for AndThenTransformNewServiceFuture<T, A, B> impl<T, A, B, C> Future for AndThenTransformNewServiceFuture<T, A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService<Error = A::Error, InitError = A::InitError>, B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>, T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
@ -287,7 +294,7 @@ mod tests {
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)), |req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|| Ok(Srv), || Ok(Srv),
); );
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
assert!(srv.poll_ready().is_ok()); assert!(srv.poll_ready().is_ok());
let res = srv.call("srv").poll(); let res = srv.call("srv").poll();
assert!(res.is_ok()); assert!(res.is_ok());

View File

@ -129,23 +129,27 @@ where
} }
/// `ApplyNewService` new service combinator /// `ApplyNewService` new service combinator
pub struct AndThenApplyNewService<A, B, F, Out> { pub struct AndThenApplyNewService<A, B, F, Out, Cfg> {
a: A, a: A,
b: B, b: B,
f: Cell<F>, f: Cell<F>,
r: PhantomData<(Out)>, r: PhantomData<(Out, Cfg)>,
} }
impl<A, B, F, Out> AndThenApplyNewService<A, B, F, Out> impl<A, B, F, Out, Cfg> AndThenApplyNewService<A, B, F, Out, Cfg>
where where
A: NewService, A: NewService<Cfg>,
B: NewService<Error = A::Error, InitError = A::InitError>, B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<A1: IntoNewService<A>, B1: IntoNewService<B>>(a: A1, b: B1, f: F) -> Self { pub fn new<A1: IntoNewService<A, Cfg>, B1: IntoNewService<B, Cfg>>(
a: A1,
b: B1,
f: F,
) -> Self {
Self { Self {
f: Cell::new(f), f: Cell::new(f),
a: a.into_new_service(), a: a.into_new_service(),
@ -155,7 +159,7 @@ where
} }
} }
impl<A, B, F, Out> Clone for AndThenApplyNewService<A, B, F, Out> impl<A, B, F, Out, Cfg> Clone for AndThenApplyNewService<A, B, F, Out, Cfg>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
@ -170,10 +174,10 @@ where
} }
} }
impl<A, B, F, Out> NewService for AndThenApplyNewService<A, B, F, Out> impl<A, B, F, Out, Cfg> NewService<Cfg> for AndThenApplyNewService<A, B, F, Out, Cfg>
where where
A: NewService, A: NewService<Cfg>,
B: NewService<Error = A::Error, InitError = A::InitError>, B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
@ -181,26 +185,26 @@ where
type Request = A::Request; type Request = A::Request;
type Response = Out::Item; type Response = Out::Item;
type Error = A::Error; type Error = A::Error;
type Service = AndThenApply<A::Service, B::Service, F, Out>;
type InitError = A::InitError; type InitError = A::InitError;
type Service = AndThenApply<A::Service, B::Service, F, Out>; type Future = AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>;
type Future = AndThenApplyNewServiceFuture<A, B, F, Out>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &Cfg) -> Self::Future {
AndThenApplyNewServiceFuture { AndThenApplyNewServiceFuture {
a: None, a: None,
b: None, b: None,
f: self.f.clone(), f: self.f.clone(),
fut_a: self.a.new_service(), fut_a: self.a.new_service(cfg),
fut_b: self.b.new_service(), fut_b: self.b.new_service(cfg),
} }
} }
} }
pub struct AndThenApplyNewServiceFuture<A, B, F, Out> pub struct AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>
where where
A: NewService, A: NewService<Cfg>,
B: NewService<Error = A::Error, InitError = A::InitError>, B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
@ -212,10 +216,10 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B, F, Out> Future for AndThenApplyNewServiceFuture<A, B, F, Out> impl<A, B, F, Out, Cfg> Future for AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>
where where
A: NewService, A: NewService<Cfg>,
B: NewService<Error = A::Error, InitError = A::InitError>, B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
@ -290,7 +294,7 @@ mod tests {
|| Ok(Srv), || Ok(Srv),
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)), |req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
); );
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
assert!(srv.poll_ready().is_ok()); assert!(srv.poll_ready().is_ok());
let res = srv.call("srv").poll(); let res = srv.call("srv").poll();
assert!(res.is_ok()); assert!(res.is_ok());

View File

@ -87,69 +87,74 @@ where
} }
/// `ApplyNewService` new service combinator /// `ApplyNewService` new service combinator
pub struct ApplyNewService<T, S> pub struct ApplyNewService<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError>, T: NewTransform<S::Service, InitError = S::InitError>,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService, S: NewService<C>,
{ {
transform: T, transform: T,
service: S, service: S,
_t: std::marker::PhantomData<C>,
} }
impl<T, S> ApplyNewService<T, S> impl<T, S, C> ApplyNewService<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError>, T: NewTransform<S::Service, InitError = S::InitError>,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService, S: NewService<C>,
{ {
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<T1: IntoNewTransform<T, S::Service>, S1: IntoNewService<S>>( pub fn new<T1: IntoNewTransform<T, S::Service>, S1: IntoNewService<S, C>>(
transform: T1, transform: T1,
service: S1, service: S1,
) -> Self { ) -> Self {
Self { Self {
transform: transform.into_new_transform(), transform: transform.into_new_transform(),
service: service.into_new_service(), service: service.into_new_service(),
_t: std::marker::PhantomData,
} }
} }
} }
impl<F, S, In, Out> ApplyNewService<FnNewTransform<F, S::Service, In, Out, S::InitError>, S> impl<F, S, In, Out, Cfg>
ApplyNewService<FnNewTransform<F, S::Service, In, Out, S::InitError>, S, Cfg>
where where
F: FnMut(In, &mut S::Service) -> Out + Clone, F: FnMut(In, &mut S::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
Out::Error: From<S::Error>, Out::Error: From<S::Error>,
S: NewService, S: NewService<Cfg>,
{ {
/// Create new `Apply` combinator factory /// Create new `Apply` combinator factory
pub fn new_fn<S1: IntoNewService<S>>(service: S1, transform: F) -> Self { pub fn new_fn<S1: IntoNewService<S, Cfg>>(service: S1, transform: F) -> Self {
Self { Self {
service: service.into_new_service(), service: service.into_new_service(),
transform: FnNewTransform::new(transform), transform: FnNewTransform::new(transform),
_t: std::marker::PhantomData,
} }
} }
} }
impl<T, S> Clone for ApplyNewService<T, S> impl<T, S, C> Clone for ApplyNewService<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError> + Clone, T: NewTransform<S::Service, InitError = S::InitError> + Clone,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService + Clone, S: NewService<C> + Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
service: self.service.clone(), service: self.service.clone(),
transform: self.transform.clone(), transform: self.transform.clone(),
_t: std::marker::PhantomData,
} }
} }
} }
impl<T, S> NewService for ApplyNewService<T, S> impl<T, S, C> NewService<C> for ApplyNewService<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError>, T: NewTransform<S::Service, InitError = S::InitError>,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService, S: NewService<C>,
{ {
type Request = T::Request; type Request = T::Request;
type Response = T::Response; type Response = T::Response;
@ -157,23 +162,23 @@ where
type Service = Apply<T::Transform, S::Service>; type Service = Apply<T::Transform, S::Service>;
type InitError = T::InitError; type InitError = T::InitError;
type Future = ApplyNewServiceFuture<T, S>; type Future = ApplyNewServiceFuture<T, S, C>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
ApplyNewServiceFuture { ApplyNewServiceFuture {
fut_t: self.transform.new_transform(), fut_t: self.transform.new_transform(),
fut_s: self.service.new_service(), fut_s: self.service.new_service(cfg),
service: None, service: None,
transform: None, transform: None,
} }
} }
} }
pub struct ApplyNewServiceFuture<T, S> pub struct ApplyNewServiceFuture<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError>, T: NewTransform<S::Service, InitError = S::InitError>,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService, S: NewService<C>,
{ {
fut_s: S::Future, fut_s: S::Future,
fut_t: T::Future, fut_t: T::Future,
@ -181,11 +186,11 @@ where
transform: Option<T::Transform>, transform: Option<T::Transform>,
} }
impl<T, S> Future for ApplyNewServiceFuture<T, S> impl<T, S, C> Future for ApplyNewServiceFuture<T, S, C>
where where
T: NewTransform<S::Service, InitError = S::InitError>, T: NewTransform<S::Service, InitError = S::InitError>,
T::Error: From<S::Error>, T::Error: From<S::Error>,
S: NewService, S: NewService<C>,
{ {
type Item = Apply<T::Transform, S::Service>; type Item = Apply<T::Transform, S::Service>;
type Error = T::InitError; type Error = T::InitError;
@ -256,7 +261,7 @@ mod tests {
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)), |req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|| Ok::<_, ()>(Srv), || Ok::<_, ()>(Srv),
); );
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
assert!(srv.poll_ready().is_ok()); assert!(srv.poll_ready().is_ok());
let res = srv.call("srv").poll(); let res = srv.call("srv").poll();
assert!(res.is_ok()); assert!(res.is_ok());
@ -272,7 +277,7 @@ mod tests {
|| Ok::<_, ()>(Srv), || Ok::<_, ()>(Srv),
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)), |req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
); );
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
assert!(srv.poll_ready().is_ok()); assert!(srv.poll_ready().is_ok());
let res = srv.call("srv").poll(); let res = srv.call("srv").poll();
assert!(res.is_ok()); assert!(res.is_ok());

View File

@ -68,15 +68,16 @@ impl<R, E1, E2> Default for BlankNewService<R, E1, E2> {
} }
} }
impl<R, E1, E2> NewService for BlankNewService<R, E1, E2> { impl<R, E1, E2> NewService<()> for BlankNewService<R, E1, E2> {
type Request = R; type Request = R;
type Response = R; type Response = R;
type Error = E1; type Error = E1;
type InitError = E2;
type Service = Blank<R, E1>; type Service = Blank<R, E1>;
type InitError = E2;
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
ok(Blank::default()) ok(Blank::default())
} }
} }

View File

@ -3,7 +3,7 @@ use std::marker::PhantomData;
use futures::future::{ok, FutureResult}; use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll}; use futures::{Async, IntoFuture, Poll};
use super::{IntoNewService, IntoService, NewService, Service}; use super::{IntoService, NewService, Service};
pub struct FnService<F, Req, Out> pub struct FnService<F, Req, Out>
where where
@ -66,16 +66,16 @@ where
} }
} }
pub struct FnNewService<F, Req, Out> pub struct FnNewService<F, Req, Out, Cfg>
where where
F: FnMut(Req) -> Out, F: FnMut(Req) -> Out,
Out: IntoFuture, Out: IntoFuture,
{ {
f: F, f: F,
_t: PhantomData<(Req,)>, _t: PhantomData<(Req, Cfg)>,
} }
impl<F, Req, Out> FnNewService<F, Req, Out> impl<F, Req, Out, Cfg> FnNewService<F, Req, Out, Cfg>
where where
F: FnMut(Req) -> Out + Clone, F: FnMut(Req) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
@ -85,7 +85,7 @@ where
} }
} }
impl<F, Req, Out> NewService for FnNewService<F, Req, Out> impl<F, Req, Out, Cfg> NewService<Cfg> for FnNewService<F, Req, Out, Cfg>
where where
F: FnMut(Req) -> Out + Clone, F: FnMut(Req) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
@ -94,25 +94,16 @@ where
type Response = Out::Item; type Response = Out::Item;
type Error = Out::Error; type Error = Out::Error;
type Service = FnService<F, Req, Out>; type Service = FnService<F, Req, Out>;
type InitError = (); type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &Cfg) -> Self::Future {
ok(FnService::new(self.f.clone())) ok(FnService::new(self.f.clone()))
} }
} }
impl<F, Req, Out> IntoNewService<FnNewService<F, Req, Out>> for F impl<F, Req, Out, Cfg> Clone for FnNewService<F, Req, Out, Cfg>
where
F: FnMut(Req) -> Out + Clone + 'static,
Out: IntoFuture,
{
fn into_new_service(self) -> FnNewService<F, Req, Out> {
FnNewService::new(self)
}
}
impl<F, Req, Out> Clone for FnNewService<F, Req, Out>
where where
F: FnMut(Req) -> Out + Clone, F: FnMut(Req) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,

View File

@ -81,23 +81,23 @@ where
/// service's error. /// service's error.
/// ///
/// This is created by the `NewServiceExt::from_err` method. /// This is created by the `NewServiceExt::from_err` method.
pub struct FromErrNewService<A, E> { pub struct FromErrNewService<A, E, C> {
a: A, a: A,
e: PhantomData<E>, e: PhantomData<(E, C)>,
} }
impl<A, E> FromErrNewService<A, E> { impl<A, E, C> FromErrNewService<A, E, C> {
/// Create new `FromErr` new service instance /// Create new `FromErr` new service instance
pub fn new(a: A) -> Self pub fn new(a: A) -> Self
where where
A: NewService, A: NewService<C>,
E: From<A::Error>, E: From<A::Error>,
{ {
Self { a, e: PhantomData } Self { a, e: PhantomData }
} }
} }
impl<A, E> Clone for FromErrNewService<A, E> impl<A, E, C> Clone for FromErrNewService<A, E, C>
where where
A: Clone, A: Clone,
{ {
@ -109,9 +109,9 @@ where
} }
} }
impl<A, E> NewService for FromErrNewService<A, E> impl<A, E, C> NewService<C> for FromErrNewService<A, E, C>
where where
A: NewService, A: NewService<C>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Request = A::Request; type Request = A::Request;
@ -120,28 +120,28 @@ where
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, C>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
FromErrNewServiceFuture { FromErrNewServiceFuture {
fut: self.a.new_service(), fut: self.a.new_service(cfg),
e: PhantomData, e: PhantomData,
} }
} }
} }
pub struct FromErrNewServiceFuture<A, E> pub struct FromErrNewServiceFuture<A, E, C>
where where
A: NewService, A: NewService<C>,
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, C> Future for FromErrNewServiceFuture<A, E, C>
where where
A: NewService, A: NewService<C>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Item = FromErr<A::Service, E>; type Item = FromErr<A::Service, E>;
@ -208,7 +208,7 @@ mod tests {
fn test_new_service() { fn test_new_service() {
let blank = || Ok::<_, ()>(Srv); let blank = || Ok::<_, ()>(Srv);
let new_srv = blank.into_new_service().from_err::<Error>(); let new_srv = blank.into_new_service().from_err::<Error>();
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
let res = srv.call(()).poll(); let res = srv.call(()).poll();
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.err().unwrap(), Error); assert_eq!(res.err().unwrap(), Error);

View File

@ -188,7 +188,9 @@ impl<T: ?Sized> ServiceExt for T where T: Service {}
/// accepts new TCP streams, obtains a new `Service` value using the /// accepts new TCP streams, obtains a new `Service` value using the
/// `NewService` trait, and uses that new `Service` value to process inbound /// `NewService` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream. /// requests on that new TCP stream.
pub trait NewService { ///
/// `Config` parameter defines service factory configuration type.
pub trait NewService<Config = ()> {
/// Requests handled by the service. /// Requests handled by the service.
type Request; type Request;
@ -212,7 +214,7 @@ pub trait NewService {
type Future: Future<Item = Self::Service, Error = Self::InitError>; type Future: Future<Item = Self::Service, Error = Self::InitError>;
/// Create and return a new service value asynchronously. /// Create and return a new service value asynchronously.
fn new_service(&self) -> Self::Future; fn new_service(&self, cfg: &Config) -> Self::Future;
/// Apply function to specified service and use it as a next service in /// Apply function to specified service and use it as a next service in
/// chain. /// chain.
@ -220,14 +222,14 @@ pub trait NewService {
self, self,
transform: T1, transform: T1,
service: B1, service: B1,
) -> AndThenTransformNewService<T, Self, B> ) -> AndThenTransformNewService<T, Self, B, Config>
where where
Self: Sized, Self: Sized,
T: NewTransform<B::Service, Request = Self::Response, InitError = Self::InitError>, T: NewTransform<B::Service, Request = Self::Response, InitError = Self::InitError>,
T::Error: From<Self::Error>, T::Error: From<Self::Error>,
T1: IntoNewTransform<T, B::Service>, T1: IntoNewTransform<T, B::Service>,
B: NewService<Error = Self::Error, InitError = Self::InitError>, B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
B1: IntoNewService<B>, B1: IntoNewService<B, Config>,
{ {
AndThenTransformNewService::new( AndThenTransformNewService::new(
transform.into_new_transform(), transform.into_new_transform(),
@ -238,11 +240,15 @@ pub trait NewService {
/// 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_fn<B, I, F, Out>(self, service: I, f: F) -> AndThenApplyNewService<Self, B, F, Out> fn apply_fn<B, I, F, Out>(
self,
service: I,
f: F,
) -> AndThenApplyNewService<Self, B, F, Out, Config>
where where
Self: Sized, Self: Sized,
B: NewService<Error = Self::Error, InitError = Self::InitError>, B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B>, I: IntoNewService<B, Config>,
F: FnMut(Self::Response, &mut B::Service) -> Out, F: FnMut(Self::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<Self::Error>, Out::Error: Into<Self::Error>,
@ -251,11 +257,12 @@ pub trait NewService {
} }
/// Call another service after call to this one has resolved successfully. /// Call another service after call to this one has resolved successfully.
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B> fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B, Config>
where where
Self: Sized, Self: Sized,
F: IntoNewService<B>, F: IntoNewService<B, Config>,
B: NewService< B: NewService<
Config,
Request = Self::Response, Request = Self::Response,
Error = Self::Error, Error = Self::Error,
InitError = Self::InitError, InitError = Self::InitError,
@ -270,7 +277,7 @@ pub trait NewService {
/// ///
/// 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, Config>
where where
Self: Sized, Self: Sized,
E: From<Self::Error>, E: From<Self::Error>,
@ -284,11 +291,12 @@ pub trait NewService {
/// ///
/// 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, Config>
where where
Self: Sized, Self: Sized,
F: IntoNewService<B>, F: IntoNewService<B, Config>,
B: NewService< B: NewService<
Config,
Request = Result<Self::Response, Self::Error>, Request = Result<Self::Response, Self::Error>,
Error = Self::Error, Error = Self::Error,
InitError = Self::InitError, InitError = Self::InitError,
@ -299,7 +307,7 @@ pub trait NewService {
/// 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.
fn map<F, R>(self, f: F) -> MapNewService<Self, F, R> fn map<F, R>(self, f: F) -> MapNewService<Self, F, R, Config>
where where
Self: Sized, Self: Sized,
F: FnMut(Self::Response) -> R, F: FnMut(Self::Response) -> R,
@ -308,7 +316,7 @@ pub trait NewService {
} }
/// Map this service's error to a different error, returning a new service. /// Map this service's error to a different error, returning a new service.
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E> fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E, Config>
where where
Self: Sized, Self: Sized,
F: Fn(Self::Error) -> E, F: Fn(Self::Error) -> E,
@ -317,7 +325,7 @@ pub trait NewService {
} }
/// Map this service's init error to a different error, returning a new service. /// Map this service's init error to a different error, returning a new service.
fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, F, E> fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, F, E, Config>
where where
Self: Sized, Self: Sized,
F: Fn(Self::InitError) -> E, F: Fn(Self::InitError) -> E,
@ -362,9 +370,9 @@ where
} }
} }
impl<F, R, E, S> NewService for F impl<F, R, E, S, C> NewService<C> for F
where where
F: Fn() -> R, F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service,
{ {
@ -375,14 +383,14 @@ where
type InitError = E; type InitError = E;
type Future = R::Future; type Future = R::Future;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
(*self)().into_future() (*self)(cfg).into_future()
} }
} }
impl<S> NewService for Rc<S> impl<S, C> NewService<C> for Rc<S>
where where
S: NewService, S: NewService<C>,
{ {
type Request = S::Request; type Request = S::Request;
type Response = S::Response; type Response = S::Response;
@ -391,14 +399,14 @@ where
type InitError = S::InitError; type InitError = S::InitError;
type Future = S::Future; type Future = S::Future;
fn new_service(&self) -> S::Future { fn new_service(&self, cfg: &C) -> S::Future {
self.as_ref().new_service() self.as_ref().new_service(cfg)
} }
} }
impl<S> NewService for Arc<S> impl<S, C> NewService<C> for Arc<S>
where where
S: NewService, S: NewService<C>,
{ {
type Request = S::Request; type Request = S::Request;
type Response = S::Response; type Response = S::Response;
@ -407,8 +415,8 @@ where
type InitError = S::InitError; type InitError = S::InitError;
type Future = S::Future; type Future = S::Future;
fn new_service(&self) -> S::Future { fn new_service(&self, cfg: &C) -> S::Future {
self.as_ref().new_service() self.as_ref().new_service(cfg)
} }
} }
@ -422,9 +430,9 @@ where
} }
/// Trait for types that can be converted to a `NewService` /// Trait for types that can be converted to a `NewService`
pub trait IntoNewService<T> pub trait IntoNewService<T, C = ()>
where where
T: NewService, T: NewService<C>,
{ {
/// Convert to an `NewService` /// Convert to an `NewService`
fn into_new_service(self) -> T; fn into_new_service(self) -> T;
@ -439,9 +447,9 @@ where
} }
} }
impl<T> IntoNewService<T> for T impl<T, C> IntoNewService<T, C> for T
where where
T: NewService, T: NewService<C>,
{ {
fn into_new_service(self) -> T { fn into_new_service(self) -> T {
self self

View File

@ -97,18 +97,18 @@ where
} }
/// `MapNewService` new service combinator /// `MapNewService` new service combinator
pub struct MapNewService<A, F, Response> { pub struct MapNewService<A, F, Res, Cfg> {
a: A, a: A,
f: F, f: F,
r: PhantomData<Response>, r: PhantomData<(Res, Cfg)>,
} }
impl<A, F, Response> MapNewService<A, F, Response> { impl<A, F, Res, Cfg> MapNewService<A, F, Res, Cfg> {
/// Create new `Map` new service instance /// Create new `Map` new service instance
pub fn new(a: A, f: F) -> Self pub fn new(a: A, f: F) -> Self
where where
A: NewService, A: NewService<Cfg>,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Res,
{ {
Self { Self {
a, a,
@ -118,7 +118,7 @@ impl<A, F, Response> MapNewService<A, F, Response> {
} }
} }
impl<A, F, Response> Clone for MapNewService<A, F, Response> impl<A, F, Res, Cfg> Clone for MapNewService<A, F, Res, Cfg>
where where
A: Clone, A: Clone,
F: Clone, F: Clone,
@ -132,49 +132,49 @@ where
} }
} }
impl<A, F, Response> NewService for MapNewService<A, F, Response> impl<A, F, Res, Cfg> NewService<Cfg> for MapNewService<A, F, Res, Cfg>
where where
A: NewService, A: NewService<Cfg>,
F: FnMut(A::Response) -> Response + Clone, F: FnMut(A::Response) -> Res + Clone,
{ {
type Request = A::Request; type Request = A::Request;
type Response = Response; type Response = Res;
type Error = A::Error; type Error = A::Error;
type Service = Map<A::Service, F, Response>; type Service = Map<A::Service, F, Res>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = MapNewServiceFuture<A, F, Response>; type Future = MapNewServiceFuture<A, F, Res, Cfg>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &Cfg) -> Self::Future {
MapNewServiceFuture::new(self.a.new_service(), self.f.clone()) MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
} }
} }
pub struct MapNewServiceFuture<A, F, Response> pub struct MapNewServiceFuture<A, F, Res, Cfg>
where where
A: NewService, A: NewService<Cfg>,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Res,
{ {
fut: A::Future, fut: A::Future,
f: Option<F>, f: Option<F>,
} }
impl<A, F, Response> MapNewServiceFuture<A, F, Response> impl<A, F, Res, Cfg> MapNewServiceFuture<A, F, Res, Cfg>
where where
A: NewService, A: NewService<Cfg>,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Res,
{ {
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, Response> Future for MapNewServiceFuture<A, F, Response> impl<A, F, Res, Cfg> Future for MapNewServiceFuture<A, F, Res, Cfg>
where where
A: NewService, A: NewService<Cfg>,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Res,
{ {
type Item = Map<A::Service, F, Response>; type Item = Map<A::Service, F, Res>;
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> {
@ -229,7 +229,7 @@ mod tests {
fn test_new_service() { fn test_new_service() {
let blank = || Ok::<_, ()>(Srv); let blank = || Ok::<_, ()>(Srv);
let new_srv = blank.into_new_service().map(|_| "ok"); let new_srv = blank.into_new_service().map(|_| "ok");
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
let res = srv.call(()).poll(); let res = srv.call(()).poll();
assert!(res.is_ok()); assert!(res.is_ok());
assert_eq!(res.unwrap(), Async::Ready("ok")); assert_eq!(res.unwrap(), Async::Ready("ok"));

View File

@ -98,17 +98,17 @@ where
/// service's error. /// service's error.
/// ///
/// This is created by the `NewServiceExt::map_err` method. /// This is created by the `NewServiceExt::map_err` method.
pub struct MapErrNewService<A, F, E> { pub struct MapErrNewService<A, F, E, C> {
a: A, a: A,
f: F, f: F,
e: PhantomData<E>, e: PhantomData<(E, C)>,
} }
impl<A, F, E> MapErrNewService<A, F, E> { impl<A, F, E, C> MapErrNewService<A, F, E, C> {
/// Create new `MapErr` new service instance /// Create new `MapErr` new service instance
pub fn new(a: A, f: F) -> Self pub fn new(a: A, f: F) -> Self
where where
A: NewService, A: NewService<C>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
Self { Self {
@ -119,7 +119,7 @@ impl<A, F, E> MapErrNewService<A, F, E> {
} }
} }
impl<A, F, E> Clone for MapErrNewService<A, F, E> impl<A, F, E, C> Clone for MapErrNewService<A, F, E, C>
where where
A: Clone, A: Clone,
F: Clone, F: Clone,
@ -133,9 +133,9 @@ where
} }
} }
impl<A, F, E> NewService for MapErrNewService<A, F, E> impl<A, F, E, C> NewService<C> for MapErrNewService<A, F, E, C>
where where
A: NewService, A: NewService<C>,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Request = A::Request; type Request = A::Request;
@ -144,25 +144,25 @@ where
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, C>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
MapErrNewServiceFuture::new(self.a.new_service(), self.f.clone()) MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
} }
} }
pub struct MapErrNewServiceFuture<A, F, E> pub struct MapErrNewServiceFuture<A, F, E, C>
where where
A: NewService, A: NewService<C>,
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, C> MapErrNewServiceFuture<A, F, E, C>
where where
A: NewService, A: NewService<C>,
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 {
@ -170,9 +170,9 @@ where
} }
} }
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E> impl<A, F, E, C> Future for MapErrNewServiceFuture<A, F, E, C>
where where
A: NewService, A: NewService<C>,
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>;
@ -231,7 +231,7 @@ mod tests {
fn test_new_service() { fn test_new_service() {
let blank = || Ok::<_, ()>(Srv); let blank = || Ok::<_, ()>(Srv);
let new_srv = blank.into_new_service().map_err(|_| "error"); let new_srv = blank.into_new_service().map_err(|_| "error");
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() { if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
let res = srv.call(()).poll(); let res = srv.call(()).poll();
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.err().unwrap(), "error"); assert_eq!(res.err().unwrap(), "error");

View File

@ -5,17 +5,17 @@ use futures::{Future, Poll};
use super::NewService; use super::NewService;
/// `MapInitErr` service combinator /// `MapInitErr` service combinator
pub struct MapInitErr<A, F, E> { pub struct MapInitErr<A, F, E, C> {
a: A, a: A,
f: F, f: F,
e: PhantomData<E>, e: PhantomData<(E, C)>,
} }
impl<A, F, E> MapInitErr<A, F, E> { impl<A, F, E, C> MapInitErr<A, F, E, C> {
/// Create new `MapInitErr` combinator /// Create new `MapInitErr` combinator
pub fn new(a: A, f: F) -> Self pub fn new(a: A, f: F) -> Self
where where
A: NewService, A: NewService<C>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
Self { Self {
@ -26,7 +26,7 @@ impl<A, F, E> MapInitErr<A, F, E> {
} }
} }
impl<A, F, E> Clone for MapInitErr<A, F, E> impl<A, F, E, C> Clone for MapInitErr<A, F, E, C>
where where
A: Clone, A: Clone,
F: Clone, F: Clone,
@ -40,9 +40,9 @@ where
} }
} }
impl<A, F, E> NewService for MapInitErr<A, F, E> impl<A, F, E, C> NewService<C> for MapInitErr<A, F, E, C>
where where
A: NewService, A: NewService<C>,
F: Fn(A::InitError) -> E + Clone, F: Fn(A::InitError) -> E + Clone,
{ {
type Request = A::Request; type Request = A::Request;
@ -51,25 +51,25 @@ where
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, C>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
MapInitErrFuture::new(self.a.new_service(), self.f.clone()) MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
} }
} }
pub struct MapInitErrFuture<A, F, E> pub struct MapInitErrFuture<A, F, E, C>
where where
A: NewService, A: NewService<C>,
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, C> MapInitErrFuture<A, F, E, C>
where where
A: NewService, A: NewService<C>,
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 +77,9 @@ where
} }
} }
impl<A, F, E> Future for MapInitErrFuture<A, F, E> impl<A, F, E, C> Future for MapInitErrFuture<A, F, E, C>
where where
A: NewService, A: NewService<C>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
type Item = A::Service; type Item = A::Service;

View File

@ -1,3 +1,5 @@
use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll}; use futures::{try_ready, Async, Future, Poll};
use super::{IntoNewService, NewService, Service}; use super::{IntoNewService, NewService, Service};
@ -109,34 +111,38 @@ where
} }
/// `ThenNewService` new service combinator /// `ThenNewService` new service combinator
pub struct ThenNewService<A, B> { pub struct ThenNewService<A, B, C> {
a: A, a: A,
b: B, b: B,
_t: PhantomData<C>,
} }
impl<A, B> ThenNewService<A, B> { impl<A, B, C> ThenNewService<A, B, C> {
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<F>(a: A, f: F) -> Self pub fn new<F>(a: A, f: F) -> Self
where where
A: NewService, A: NewService<C>,
B: NewService< B: NewService<
C,
Request = Result<A::Response, A::Error>, Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
InitError = A::InitError, InitError = A::InitError,
>, >,
F: IntoNewService<B>, F: IntoNewService<B, C>,
{ {
Self { Self {
a, a,
b: f.into_new_service(), b: f.into_new_service(),
_t: PhantomData,
} }
} }
} }
impl<A, B> NewService for ThenNewService<A, B> impl<A, B, C> NewService<C> for ThenNewService<A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService< B: NewService<
C,
Request = Result<A::Response, A::Error>, Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
InitError = A::InitError, InitError = A::InitError,
@ -148,14 +154,14 @@ where
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, C>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
ThenNewServiceFuture::new(self.a.new_service(), self.b.new_service()) ThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg))
} }
} }
impl<A, B> Clone for ThenNewService<A, B> impl<A, B, C> Clone for ThenNewService<A, B, C>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
@ -164,14 +170,16 @@ where
Self { Self {
a: self.a.clone(), a: self.a.clone(),
b: self.b.clone(), b: self.b.clone(),
_t: PhantomData,
} }
} }
} }
pub struct ThenNewServiceFuture<A, B> pub struct ThenNewServiceFuture<A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService< B: NewService<
C,
Request = Result<A::Response, A::Error>, Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
InitError = A::InitError, InitError = A::InitError,
@ -183,10 +191,11 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B> ThenNewServiceFuture<A, B> impl<A, B, C> ThenNewServiceFuture<A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService< B: NewService<
C,
Request = Result<A::Response, A::Error>, Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
InitError = A::InitError, InitError = A::InitError,
@ -202,10 +211,11 @@ where
} }
} }
impl<A, B> Future for ThenNewServiceFuture<A, B> impl<A, B, C> Future for ThenNewServiceFuture<A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService< B: NewService<
C,
Request = Result<A::Response, A::Error>, Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
InitError = A::InitError, InitError = A::InitError,
@ -319,7 +329,7 @@ mod tests {
let cnt2 = cnt.clone(); let cnt2 = cnt.clone();
let blank = move || Ok::<_, ()>(Srv1(cnt2.clone())); let blank = move || Ok::<_, ()>(Srv1(cnt2.clone()));
let new_srv = blank.into_new_service().then(move || Ok(Srv2(cnt.clone()))); let new_srv = blank.into_new_service().then(move || Ok(Srv2(cnt.clone())));
if let Async::Ready(mut srv) = new_srv.clone().new_service().poll().unwrap() { if let Async::Ready(mut srv) = new_srv.clone().new_service(&()).poll().unwrap() {
let res = srv.call(Ok("srv1")).poll(); let res = srv.call(Ok("srv1")).poll();
assert!(res.is_ok()); assert!(res.is_ok());
assert_eq!(res.unwrap(), Async::Ready(("srv1", "ok"))); assert_eq!(res.unwrap(), Async::Ready(("srv1", "ok")));

View File

@ -18,7 +18,8 @@ name = "actix_utils"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
actix-service = "0.2.2" # actix-service = "0.2.2"
actix-service = { path = "../actix-service" }
actix-codec = "0.1.0" actix-codec = "0.1.0"
bytes = "0.4" bytes = "0.4"
futures = "0.1.24" futures = "0.1.24"

View File

@ -11,14 +11,23 @@ pub type BoxedService<Req, Res, Err> = Box<
>; >;
/// Create boxed new service /// Create boxed new service
pub fn new_service<T>( pub fn new_service<T, C>(
service: T, service: T,
) -> BoxedNewService<T::Request, T::Response, T::Error, T::InitError> ) -> BoxedNewService<C, T::Request, T::Response, T::Error, T::InitError>
where where
T: NewService + 'static, C: 'static,
T: NewService<C> + 'static,
T::Request: 'static,
T::Response: 'static,
T::Service: 'static, T::Service: 'static,
T::Future: 'static,
T::Error: 'static,
T::InitError: 'static,
{ {
BoxedNewService(Box::new(NewServiceWrapper(service))) BoxedNewService(Box::new(NewServiceWrapper {
service,
_t: std::marker::PhantomData,
}))
} }
/// Create boxed service /// Create boxed service
@ -30,8 +39,9 @@ where
Box::new(ServiceWrapper(service)) Box::new(ServiceWrapper(service))
} }
type Inner<Req, Res, Err, InitErr> = Box< type Inner<C, Req, Res, Err, InitErr> = Box<
NewService< NewService<
C,
Request = Req, Request = Req,
Response = Res, Response = Res,
Error = Err, Error = Err,
@ -41,9 +51,9 @@ type Inner<Req, Res, Err, InitErr> = Box<
>, >,
>; >;
pub struct BoxedNewService<Req, Res, Err, InitErr>(Inner<Req, Res, Err, InitErr>); pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
impl<Req, Res, Err, InitErr> NewService for BoxedNewService<Req, Res, Err, InitErr> impl<C, Req, Res, Err, InitErr> NewService<C> for BoxedNewService<C, Req, Res, Err, InitErr>
where where
Req: 'static, Req: 'static,
Res: 'static, Res: 'static,
@ -57,20 +67,23 @@ where
type Service = BoxedService<Req, Res, Err>; type Service = BoxedService<Req, Res, Err>;
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>; type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
self.0.new_service() self.0.new_service(cfg)
} }
} }
struct NewServiceWrapper<T: NewService>(T); struct NewServiceWrapper<C, T: NewService<C>> {
service: T,
_t: std::marker::PhantomData<C>,
}
impl<T, Req, Res, Err, InitErr> NewService for NewServiceWrapper<T> impl<C, T, Req, Res, Err, InitErr> NewService<C> for NewServiceWrapper<C, T>
where where
Req: 'static, Req: 'static,
Res: 'static, Res: 'static,
Err: 'static, Err: 'static,
InitErr: 'static, InitErr: 'static,
T: NewService<Request = Req, Response = Res, Error = Err, InitError = InitErr>, T: NewService<C, Request = Req, Response = Res, Error = Err, InitError = InitErr>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
<T::Service as Service>::Future: 'static, <T::Service as Service>::Future: 'static,
@ -82,10 +95,10 @@ where
type Service = BoxedService<Req, Res, Err>; type Service = BoxedService<Req, Res, Err>;
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>; type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
Box::new( Box::new(
self.0 self.service
.new_service() .new_service(cfg)
.map(|service| ServiceWrapper::boxed(service)), .map(|service| ServiceWrapper::boxed(service)),
) )
} }

View File

@ -53,10 +53,11 @@ pub enum Either<A, B> {
} }
impl<A, B> Either<A, B> { impl<A, B> Either<A, B> {
pub fn new_a(srv: A) -> Self pub fn new_a<C>(srv: A) -> Self
where where
A: NewService, A: NewService<C>,
B: NewService< B: NewService<
C,
Request = A::Request, Request = A::Request,
Response = A::Response, Response = A::Response,
Error = A::Error, Error = A::Error,
@ -66,10 +67,11 @@ impl<A, B> Either<A, B> {
Either::A(srv) Either::A(srv)
} }
pub fn new_b(srv: B) -> Self pub fn new_b<C>(srv: B) -> Self
where where
A: NewService, A: NewService<C>,
B: NewService< B: NewService<
C,
Request = A::Request, Request = A::Request,
Response = A::Response, Response = A::Response,
Error = A::Error, Error = A::Error,
@ -80,10 +82,11 @@ impl<A, B> Either<A, B> {
} }
} }
impl<A, B> NewService for Either<A, B> impl<A, B, C> NewService<C> for Either<A, B>
where where
A: NewService, A: NewService<C>,
B: NewService< B: NewService<
C,
Request = A::Request, Request = A::Request,
Response = A::Response, Response = A::Response,
Error = A::Error, Error = A::Error,
@ -95,12 +98,12 @@ where
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, C>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
match self { match self {
Either::A(ref inner) => EitherNewService::A(inner.new_service()), Either::A(ref inner) => EitherNewService::A(inner.new_service(cfg)),
Either::B(ref inner) => EitherNewService::B(inner.new_service()), Either::B(ref inner) => EitherNewService::B(inner.new_service(cfg)),
} }
} }
} }
@ -115,15 +118,16 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> {
} }
#[doc(hidden)] #[doc(hidden)]
pub enum EitherNewService<A: NewService, B: NewService> { pub enum EitherNewService<A: NewService<C>, B: NewService<C>, C> {
A(A::Future), A(A::Future),
B(B::Future), B(B::Future),
} }
impl<A, B> Future for EitherNewService<A, B> impl<A, B, C> Future for EitherNewService<A, B, C>
where where
A: NewService, A: NewService<C>,
B: NewService< B: NewService<
C,
Request = A::Request, Request = A::Request,
Response = A::Response, Response = A::Response,
Error = A::Error, Error = A::Error,

View File

@ -15,14 +15,15 @@ use crate::cell::Cell;
type Request<U> = <U as Decoder>::Item; type Request<U> = <U as Decoder>::Item;
type Response<U> = <U as Encoder>::Item; type Response<U> = <U as Encoder>::Item;
pub struct FramedNewService<S, T, U> { pub struct FramedNewService<S, T, U, C> {
factory: S, factory: S,
_t: PhantomData<(T, U)>, _t: PhantomData<(T, U, C)>,
} }
impl<S, T, U> FramedNewService<S, T, U> impl<S, T, U, C> FramedNewService<S, T, U, C>
where where
S: NewService<Request = Request<U>, Response = Response<U>>, C: Clone,
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
<S::Service as Service>::Future: 'static, <S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
@ -30,7 +31,7 @@ where
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug, <U as Encoder>::Error: std::fmt::Debug,
{ {
pub fn new<F1: IntoNewService<S>>(factory: F1) -> Self { pub fn new<F1: IntoNewService<S, C>>(factory: F1) -> Self {
Self { Self {
factory: factory.into_new_service(), factory: factory.into_new_service(),
_t: PhantomData, _t: PhantomData,
@ -38,7 +39,7 @@ where
} }
} }
impl<S, T, U> Clone for FramedNewService<S, T, U> impl<S, T, U, C> Clone for FramedNewService<S, T, U, C>
where where
S: Clone, S: Clone,
{ {
@ -50,9 +51,10 @@ where
} }
} }
impl<S, T, U> NewService for FramedNewService<S, T, U> impl<S, T, U, C> NewService<C> for FramedNewService<S, T, U, C>
where where
S: NewService<Request = Request<U>, Response = Response<U>> + Clone, C: Clone,
S: NewService<C, Request = Request<U>, Response = Response<U>> + Clone,
S::Error: 'static, S::Error: 'static,
<S::Service as Service>::Future: 'static, <S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
@ -64,48 +66,53 @@ where
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;
type Service = FramedService<S, T, U>; type Service = FramedService<S, T, U, C>;
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
ok(FramedService { ok(FramedService {
factory: self.factory.clone(), factory: self.factory.clone(),
config: cfg.clone(),
_t: PhantomData, _t: PhantomData,
}) })
} }
} }
pub struct FramedService<S, T, U> { pub struct FramedService<S, T, U, C> {
factory: S, factory: S,
config: C,
_t: PhantomData<(T, U)>, _t: PhantomData<(T, U)>,
} }
impl<S, T, U> Clone for FramedService<S, T, U> impl<S, T, U, C> Clone for FramedService<S, T, U, C>
where where
S: Clone, S: Clone,
C: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
factory: self.factory.clone(), factory: self.factory.clone(),
config: self.config.clone(),
_t: PhantomData, _t: PhantomData,
} }
} }
} }
impl<S, T, U> Service for FramedService<S, T, U> impl<S, T, U, C> Service for FramedService<S, T, U, C>
where where
S: NewService<Request = Request<U>, Response = Response<U>>, S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
<S::Service as Service>::Future: 'static, <S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug, <U as Encoder>::Error: std::fmt::Debug,
C: Clone,
{ {
type Request = Framed<T, U>; 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, C>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(())) Ok(Async::Ready(()))
@ -113,17 +120,16 @@ where
fn call(&mut self, req: Framed<T, U>) -> Self::Future { fn call(&mut self, req: Framed<T, U>) -> Self::Future {
FramedServiceResponseFuture { FramedServiceResponseFuture {
fut: self.factory.new_service(), fut: self.factory.new_service(&self.config),
framed: Some(req), framed: Some(req),
} }
} }
} }
#[doc(hidden)] #[doc(hidden)]
pub struct FramedServiceResponseFuture<S, T, U> pub struct FramedServiceResponseFuture<S, T, U, C>
where where
S: NewService<Request = Request<U>, Response = Response<U>>, S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
<S::Service as Service>::Future: 'static, <S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
@ -135,9 +141,9 @@ where
framed: Option<Framed<T, U>>, framed: Option<Framed<T, U>>,
} }
impl<S, T, U> Future for FramedServiceResponseFuture<S, T, U> impl<S, T, U, C> Future for FramedServiceResponseFuture<S, T, U, C>
where where
S: NewService<Request = Request<U>, Response = Response<U>>, S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
<S::Service as Service>::Future: 'static, <S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
@ -402,7 +408,7 @@ where
} }
} }
impl<T, U, F> NewService for IntoFramed<T, U, F> impl<T, U, F> NewService<()> 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,
@ -415,7 +421,7 @@ where
type Service = IntoFramedService<T, U, F>; type Service = IntoFramedService<T, U, F>;
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
ok(IntoFramedService { ok(IntoFramedService {
factory: self.factory.clone(), factory: self.factory.clone(),
_t: PhantomData, _t: PhantomData,

View File

@ -44,7 +44,7 @@ where
} }
} }
impl<R, E, F> NewService for KeepAlive<R, E, F> impl<R, E, F> NewService<()> for KeepAlive<R, E, F>
where where
F: Fn() -> E + Clone, F: Fn() -> E + Clone,
{ {
@ -55,7 +55,7 @@ where
type Service = KeepAliveService<R, E, F>; type Service = KeepAliveService<R, E, F>;
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
ok(KeepAliveService::new( ok(KeepAliveService::new(
self.ka, self.ka,
self.time.timer(), self.time.timer(),

View File

@ -29,20 +29,21 @@ where
} }
} }
pub struct StreamNewService<S, T, E> { pub struct StreamNewService<S, T, E, C> {
factory: Rc<T>, factory: Rc<T>,
_t: PhantomData<(S, E)>, _t: PhantomData<(S, E, C)>,
} }
impl<S, T, E> StreamNewService<S, T, E> impl<S, T, E, C> StreamNewService<S, T, E, C>
where where
C: Clone,
S: IntoStream, S: IntoStream,
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>, T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
<T::Service as Service>::Future: 'static, <T::Service as Service>::Future: 'static,
{ {
pub fn new<F: IntoNewService<T>>(factory: F) -> Self { pub fn new<F: IntoNewService<T, C>>(factory: F) -> Self {
Self { Self {
factory: Rc::new(factory.into_new_service()), factory: Rc::new(factory.into_new_service()),
_t: PhantomData, _t: PhantomData,
@ -50,7 +51,7 @@ where
} }
} }
impl<S, T, E> Clone for StreamNewService<S, T, E> { impl<S, T, E, C> Clone for StreamNewService<S, T, E, C> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
factory: self.factory.clone(), factory: self.factory.clone(),
@ -59,10 +60,11 @@ impl<S, T, E> Clone for StreamNewService<S, T, E> {
} }
} }
impl<S, T, E> NewService for StreamNewService<S, T, E> impl<S, T, E, C> NewService<C> for StreamNewService<S, T, E, C>
where where
C: Clone,
S: IntoStream + 'static, S: IntoStream + 'static,
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>, T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
<T::Service as Service>::Future: 'static, <T::Service as Service>::Future: 'static,
@ -71,29 +73,32 @@ where
type Response = (); type Response = ();
type Error = E; type Error = E;
type InitError = E; type InitError = E;
type Service = StreamService<S, T, E>; type Service = StreamService<S, T, E, C>;
type Future = FutureResult<Self::Service, E>; type Future = FutureResult<Self::Service, E>;
fn new_service(&self) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
ok(StreamService { ok(StreamService {
factory: self.factory.clone(), factory: self.factory.clone(),
config: cfg.clone(),
_t: PhantomData, _t: PhantomData,
}) })
} }
} }
pub struct StreamService<S, T, E> { pub struct StreamService<S, T, E, C = ()> {
factory: Rc<T>, factory: Rc<T>,
config: C,
_t: PhantomData<(S, E)>, _t: PhantomData<(S, E)>,
} }
impl<S, T, E> Service for StreamService<S, T, E> impl<S, T, E, C> Service for StreamService<S, T, E, C>
where where
S: IntoStream + 'static, S: IntoStream + 'static,
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>, T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
<T::Service as Service>::Future: 'static, <T::Service as Service>::Future: 'static,
C: Clone,
{ {
type Request = S; type Request = S;
type Response = (); type Response = ();
@ -107,7 +112,7 @@ where
fn call(&mut self, req: S) -> Self::Future { fn call(&mut self, req: S) -> Self::Future {
Box::new( Box::new(
self.factory self.factory
.new_service() .new_service(&self.config)
.and_then(move |srv| StreamDispatcher::new(req, srv)), .and_then(move |srv| StreamDispatcher::new(req, srv)),
) )
} }
@ -227,7 +232,7 @@ impl<T> Clone for TakeItem<T> {
} }
} }
impl<T: Stream> NewService for TakeItem<T> { impl<T: Stream> NewService<()> for TakeItem<T> {
type Request = T; type Request = T;
type Response = (Option<T::Item>, T); type Response = (Option<T::Item>, T);
type Error = T::Error; type Error = T::Error;
@ -235,7 +240,7 @@ impl<T: Stream> NewService for TakeItem<T> {
type Service = TakeItemService<T>; type Service = TakeItemService<T>;
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
ok(TakeItemService { _t: PhantomData }) ok(TakeItemService { _t: PhantomData })
} }
} }

View File

@ -42,7 +42,7 @@ impl Default for LowResTime {
} }
} }
impl NewService for LowResTime { impl NewService<()> for LowResTime {
type Request = (); type Request = ();
type Response = Instant; type Response = Instant;
type Error = Never; type Error = Never;
@ -50,7 +50,7 @@ impl NewService for LowResTime {
type Service = LowResTimeService; type Service = LowResTimeService;
type Future = FutureResult<Self::Service, Self::InitError>; type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
ok(self.timer()) ok(self.timer())
} }
} }