1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 15:42:57 +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"]
[dependencies]
actix-service = "0.2.0"
#actix-service = "0.2.0"
actix-service = { path="../actix-service" }
actix-codec = "0.1.0"
futures = "0.1"
tokio-tcp = "0.1"

View File

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

View File

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

View File

@ -15,7 +15,6 @@ use crate::accept::{AcceptLoop, AcceptNotify, Command};
use crate::config::{ConfiguredService, ServiceConfig};
use crate::server::{Server, ServerCommand};
use crate::services::{InternalServiceFactory, StreamNewService, StreamServiceFactory};
use crate::services::{ServiceFactory, ServiceNewService};
use crate::signals::{Signal, Signals};
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
use crate::Token;
@ -176,26 +175,6 @@ impl ServerBuilder {
self
}
/// Add new service to the server.
pub fn listen2<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.
///
/// This method spawns new thread and starts new actix system. Other than

View File

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

View File

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

View File

@ -13,7 +13,7 @@ use super::Token;
use crate::counter::CounterGuard;
/// Server message
pub enum ServerMessage {
pub(crate) enum ServerMessage {
/// New stream
Connect(net::TcpStream),
/// Gracefull shutdown
@ -28,12 +28,6 @@ pub trait StreamServiceFactory: Send + Clone + 'static {
fn create(&self) -> Self::NewService;
}
pub trait ServiceFactory: Send + Clone + 'static {
type NewService: NewService<Request = ServerMessage>;
fn create(&self) -> Self::NewService;
}
pub(crate) trait InternalServiceFactory: Send {
fn name(&self, token: Token) -> &str;
@ -98,86 +92,6 @@ where
}
}
pub(crate) struct ServerService<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> {
name: String,
inner: F,
@ -214,7 +128,7 @@ where
Box::new(
self.inner
.create()
.new_service()
.new_service(&())
.map_err(|_| ())
.map(move |inner| {
let service: BoxedServerService = Box::new(StreamService::new(inner));
@ -238,18 +152,6 @@ impl InternalServiceFactory for Box<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
where
F: Fn() -> T + Send + Clone + 'static,

View File

@ -44,7 +44,7 @@ impl<T: AsyncRead + AsyncWrite> NewService for NativeTlsAcceptor<T> {
type 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| {
ok(NativeTlsAcceptorService {
acceptor: self.acceptor.clone(),

View File

@ -44,7 +44,7 @@ impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> {
type 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| {
ok(OpensslAcceptorService {
acceptor: self.acceptor.clone(),

View File

@ -46,7 +46,7 @@ impl<T: AsyncRead + AsyncWrite> NewService for RustlsAcceptor<T> {
type 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| {
ok(RustlsAcceptorService {
acceptor: self.config.clone().into(),

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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 Response = R;
type Error = E1;
type InitError = E2;
type Service = Blank<R, E1>;
type InitError = E2;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
fn new_service(&self, _: &()) -> Self::Future {
ok(Blank::default())
}
}

View File

@ -3,7 +3,7 @@ use std::marker::PhantomData;
use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll};
use super::{IntoNewService, IntoService, NewService, Service};
use super::{IntoService, NewService, Service};
pub struct FnService<F, Req, Out>
where
@ -66,16 +66,16 @@ where
}
}
pub struct FnNewService<F, Req, Out>
pub struct FnNewService<F, Req, Out, Cfg>
where
F: FnMut(Req) -> Out,
Out: IntoFuture,
{
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
F: FnMut(Req) -> Out + Clone,
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
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
@ -94,25 +94,16 @@ where
type Response = Out::Item;
type Error = Out::Error;
type Service = FnService<F, Req, Out>;
type 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()))
}
}
impl<F, Req, Out> IntoNewService<FnNewService<F, Req, Out>> for F
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>
impl<F, Req, Out, Cfg> Clone for FnNewService<F, Req, Out, Cfg>
where
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,

View File

@ -81,23 +81,23 @@ where
/// service's error.
///
/// This is created by the `NewServiceExt::from_err` method.
pub struct FromErrNewService<A, E> {
pub struct FromErrNewService<A, E, C> {
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
pub fn new(a: A) -> Self
where
A: NewService,
A: NewService<C>,
E: From<A::Error>,
{
Self { a, e: PhantomData }
}
}
impl<A, E> Clone for FromErrNewService<A, E>
impl<A, E, C> Clone for FromErrNewService<A, E, C>
where
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
A: NewService,
A: NewService<C>,
E: From<A::Error>,
{
type Request = A::Request;
@ -120,28 +120,28 @@ where
type Service = FromErr<A::Service, E>;
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 {
fut: self.a.new_service(),
fut: self.a.new_service(cfg),
e: PhantomData,
}
}
}
pub struct FromErrNewServiceFuture<A, E>
pub struct FromErrNewServiceFuture<A, E, C>
where
A: NewService,
A: NewService<C>,
E: From<A::Error>,
{
fut: A::Future,
e: PhantomData<E>,
}
impl<A, E> Future for FromErrNewServiceFuture<A, E>
impl<A, E, C> Future for FromErrNewServiceFuture<A, E, C>
where
A: NewService,
A: NewService<C>,
E: From<A::Error>,
{
type Item = FromErr<A::Service, E>;
@ -208,7 +208,7 @@ mod tests {
fn test_new_service() {
let blank = || Ok::<_, ()>(Srv);
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();
assert!(res.is_err());
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
/// `NewService` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream.
pub trait NewService {
///
/// `Config` parameter defines service factory configuration type.
pub trait NewService<Config = ()> {
/// Requests handled by the service.
type Request;
@ -212,7 +214,7 @@ pub trait NewService {
type Future: Future<Item = Self::Service, Error = Self::InitError>;
/// Create and return a new service value asynchronously.
fn new_service(&self) -> Self::Future;
fn new_service(&self, cfg: &Config) -> Self::Future;
/// Apply function to specified service and use it as a next service in
/// chain.
@ -220,14 +222,14 @@ pub trait NewService {
self,
transform: T1,
service: B1,
) -> AndThenTransformNewService<T, Self, B>
) -> AndThenTransformNewService<T, Self, B, Config>
where
Self: Sized,
T: NewTransform<B::Service, Request = Self::Response, InitError = Self::InitError>,
T::Error: From<Self::Error>,
T1: IntoNewTransform<T, B::Service>,
B: NewService<Error = Self::Error, InitError = Self::InitError>,
B1: IntoNewService<B>,
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
B1: IntoNewService<B, Config>,
{
AndThenTransformNewService::new(
transform.into_new_transform(),
@ -238,11 +240,15 @@ pub trait NewService {
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply_fn<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
Self: Sized,
B: NewService<Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B>,
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Config>,
F: FnMut(Self::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<Self::Error>,
@ -251,11 +257,12 @@ pub trait NewService {
}
/// 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
Self: Sized,
F: IntoNewService<B>,
F: IntoNewService<B, Config>,
B: NewService<
Config,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
@ -270,7 +277,7 @@ pub trait NewService {
///
/// Note that this function consumes the receiving new service and returns a
/// wrapped version of it.
fn from_err<E>(self) -> FromErrNewService<Self, E>
fn from_err<E>(self) -> FromErrNewService<Self, E, Config>
where
Self: Sized,
E: From<Self::Error>,
@ -284,11 +291,12 @@ pub trait NewService {
///
/// Note that this function consumes the receiving future and returns a
/// 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
Self: Sized,
F: IntoNewService<B>,
F: IntoNewService<B, Config>,
B: NewService<
Config,
Request = Result<Self::Response, Self::Error>,
Error = Self::Error,
InitError = Self::InitError,
@ -299,7 +307,7 @@ pub trait NewService {
/// Map this service's output to a different type, returning a new service
/// of the resulting type.
fn map<F, R>(self, f: F) -> MapNewService<Self, F, R>
fn map<F, R>(self, f: F) -> MapNewService<Self, F, R, Config>
where
Self: Sized,
F: FnMut(Self::Response) -> R,
@ -308,7 +316,7 @@ pub trait NewService {
}
/// Map this service's error to a different error, returning a new service.
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E>
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E, Config>
where
Self: Sized,
F: Fn(Self::Error) -> E,
@ -317,7 +325,7 @@ pub trait NewService {
}
/// Map this service's init error to a different error, returning a new service.
fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, F, E>
fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, F, E, Config>
where
Self: Sized,
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
F: Fn() -> R,
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
@ -375,14 +383,14 @@ where
type InitError = E;
type Future = R::Future;
fn new_service(&self) -> Self::Future {
(*self)().into_future()
fn new_service(&self, cfg: &C) -> Self::Future {
(*self)(cfg).into_future()
}
}
impl<S> NewService for Rc<S>
impl<S, C> NewService<C> for Rc<S>
where
S: NewService,
S: NewService<C>,
{
type Request = S::Request;
type Response = S::Response;
@ -391,14 +399,14 @@ where
type InitError = S::InitError;
type Future = S::Future;
fn new_service(&self) -> S::Future {
self.as_ref().new_service()
fn new_service(&self, cfg: &C) -> S::Future {
self.as_ref().new_service(cfg)
}
}
impl<S> NewService for Arc<S>
impl<S, C> NewService<C> for Arc<S>
where
S: NewService,
S: NewService<C>,
{
type Request = S::Request;
type Response = S::Response;
@ -407,8 +415,8 @@ where
type InitError = S::InitError;
type Future = S::Future;
fn new_service(&self) -> S::Future {
self.as_ref().new_service()
fn new_service(&self, cfg: &C) -> S::Future {
self.as_ref().new_service(cfg)
}
}
@ -422,9 +430,9 @@ where
}
/// Trait for types that can be converted to a `NewService`
pub trait IntoNewService<T>
pub trait IntoNewService<T, C = ()>
where
T: NewService,
T: NewService<C>,
{
/// Convert to an `NewService`
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
T: NewService,
T: NewService<C>,
{
fn into_new_service(self) -> T {
self

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -15,14 +15,15 @@ use crate::cell::Cell;
type Request<U> = <U as Decoder>::Item;
type Response<U> = <U as Encoder>::Item;
pub struct FramedNewService<S, T, U> {
pub struct FramedNewService<S, T, U, C> {
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
S: NewService<Request = Request<U>, Response = Response<U>>,
C: Clone,
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -30,7 +31,7 @@ where
<U as Encoder>::Item: 'static,
<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 {
factory: factory.into_new_service(),
_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
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
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::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -64,48 +66,53 @@ where
type Response = FramedTransport<S::Service, T, U>;
type Error = 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>;
fn new_service(&self) -> Self::Future {
fn new_service(&self, cfg: &C) -> Self::Future {
ok(FramedService {
factory: self.factory.clone(),
config: cfg.clone(),
_t: PhantomData,
})
}
}
pub struct FramedService<S, T, U> {
pub struct FramedService<S, T, U, C> {
factory: S,
config: C,
_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
S: Clone,
C: Clone,
{
fn clone(&self) -> Self {
Self {
factory: self.factory.clone(),
config: self.config.clone(),
_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
S: NewService<Request = Request<U>, Response = Response<U>>,
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
C: Clone,
{
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>;
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> {
Ok(Async::Ready(()))
@ -113,17 +120,16 @@ where
fn call(&mut self, req: Framed<T, U>) -> Self::Future {
FramedServiceResponseFuture {
fut: self.factory.new_service(),
fut: self.factory.new_service(&self.config),
framed: Some(req),
}
}
}
#[doc(hidden)]
pub struct FramedServiceResponseFuture<S, T, U>
pub struct FramedServiceResponseFuture<S, T, U, C>
where
S: NewService<Request = Request<U>, Response = Response<U>>,
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -135,9 +141,9 @@ where
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
S: NewService<Request = Request<U>, Response = Response<U>>,
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service>::Future: 'static,
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
T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static,
@ -415,7 +421,7 @@ where
type Service = IntoFramedService<T, U, F>;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
fn new_service(&self, _: &()) -> Self::Future {
ok(IntoFramedService {
factory: self.factory.clone(),
_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
F: Fn() -> E + Clone,
{
@ -55,7 +55,7 @@ where
type Service = KeepAliveService<R, E, F>;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
fn new_service(&self, _: &()) -> Self::Future {
ok(KeepAliveService::new(
self.ka,
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>,
_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
C: Clone,
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::Service: '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 {
factory: Rc::new(factory.into_new_service()),
_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 {
Self {
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
C: Clone,
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::Service: 'static,
<T::Service as Service>::Future: 'static,
@ -71,29 +73,32 @@ where
type Response = ();
type Error = E;
type InitError = E;
type Service = StreamService<S, T, E>;
type Service = StreamService<S, T, E, C>;
type Future = FutureResult<Self::Service, E>;
fn new_service(&self) -> Self::Future {
fn new_service(&self, cfg: &C) -> Self::Future {
ok(StreamService {
factory: self.factory.clone(),
config: cfg.clone(),
_t: PhantomData,
})
}
}
pub struct StreamService<S, T, E> {
pub struct StreamService<S, T, E, C = ()> {
factory: Rc<T>,
config: C,
_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
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::Service: 'static,
<T::Service as Service>::Future: 'static,
C: Clone,
{
type Request = S;
type Response = ();
@ -107,7 +112,7 @@ where
fn call(&mut self, req: S) -> Self::Future {
Box::new(
self.factory
.new_service()
.new_service(&self.config)
.and_then(move |srv| StreamDispatcher::new(req, srv)),
)
}
@ -227,7 +232,7 @@ impl<T> Clone for TakeItem<T> {
}
}
impl<T: Stream> NewService for TakeItem<T> {
impl<T: Stream> NewService<()> for TakeItem<T> {
type Request = T;
type Response = (Option<T::Item>, T);
type Error = T::Error;
@ -235,7 +240,7 @@ impl<T: Stream> NewService for TakeItem<T> {
type Service = TakeItemService<T>;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
fn new_service(&self, _: &()) -> Self::Future {
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 Response = Instant;
type Error = Never;
@ -50,7 +50,7 @@ impl NewService for LowResTime {
type Service = LowResTimeService;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
fn new_service(&self, _: &()) -> Self::Future {
ok(self.timer())
}
}