1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-26 22:07:42 +02: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

@ -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")));