1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-26 22:07:42 +02:00

Use associated type for NewService config

This commit is contained in:
Nikolay Kim
2019-05-12 06:03:50 -07:00
parent 76c317e0b2
commit f0776fca94
46 changed files with 810 additions and 1010 deletions

View File

@ -1,5 +1,20 @@
# Changes
## [0.4.0] - 2019-05-11
### Changed
* Use associated type for `NewService` config
* Change `apply_cfg` function
* Renamed helper functions
### Added
* Add `NewService::map_config` and `NewService::unit_config` combinators
## [0.3.6] - 2019-04-07
### Changed

View File

@ -23,8 +23,7 @@ name = "actix_service"
path = "src/lib.rs"
[dependencies]
futures = "0.1.24"
void = "1.0.2"
futures = "0.1.25"
[dev-dependencies]
actix-rt = "0.2"

View File

@ -1,5 +1,3 @@
use std::marker::PhantomData;
use futures::{Async, Future, Poll};
use super::{IntoNewService, NewService, Service};
@ -111,67 +109,75 @@ where
}
/// `AndThenNewService` new service combinator
pub struct AndThenNewService<A, B, C>
pub struct AndThenNewService<A, B>
where
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService,
B: NewService,
{
a: A,
b: B,
_t: PhantomData<C>,
}
impl<A, B, C> AndThenNewService<A, B, C>
impl<A, B> AndThenNewService<A, B>
where
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService,
B: NewService<
Config = A::Config,
Request = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{
/// Create new `AndThen` combinator
pub fn new<F: IntoNewService<B, C>>(a: A, f: F) -> Self {
pub fn new<F: IntoNewService<B>>(a: A, f: F) -> Self {
Self {
a,
b: f.into_new_service(),
_t: PhantomData,
}
}
}
impl<A, B, C> NewService<C> for AndThenNewService<A, B, C>
impl<A, B> NewService for AndThenNewService<A, B>
where
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService,
B: NewService<
Config = A::Config,
Request = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Config = A::Config;
type Service = AndThen<A::Service, B::Service>;
type InitError = A::InitError;
type Future = AndThenNewServiceFuture<A, B, C>;
type Future = AndThenNewServiceFuture<A, B>;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: &A::Config) -> Self::Future {
AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg))
}
}
impl<A, B, C> Clone for AndThenNewService<A, B, C>
impl<A, B> Clone for AndThenNewService<A, B>
where
A: NewService<C> + Clone,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError> + Clone,
A: NewService + Clone,
B: NewService + Clone,
{
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
b: self.b.clone(),
_t: PhantomData,
}
}
}
pub struct AndThenNewServiceFuture<A, B, C>
pub struct AndThenNewServiceFuture<A, B>
where
A: NewService<C>,
B: NewService<C, Request = A::Response>,
A: NewService,
B: NewService<Request = A::Response>,
{
fut_b: B::Future,
fut_a: A::Future,
@ -179,10 +185,10 @@ where
b: Option<B::Service>,
}
impl<A, B, C> AndThenNewServiceFuture<A, B, C>
impl<A, B> AndThenNewServiceFuture<A, B>
where
A: NewService<C>,
B: NewService<C, Request = A::Response>,
A: NewService,
B: NewService<Request = A::Response>,
{
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
AndThenNewServiceFuture {
@ -194,10 +200,10 @@ where
}
}
impl<A, B, C> Future for AndThenNewServiceFuture<A, B, C>
impl<A, B> Future for AndThenNewServiceFuture<A, B>
where
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService,
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>,
{
type Item = AndThen<A::Service, B::Service>;
type Error = A::InitError;

View File

@ -7,17 +7,16 @@ use crate::from_err::FromErr;
use crate::{NewService, Transform};
/// `Apply` new service combinator
pub struct AndThenTransform<T, A, B, C> {
pub struct AndThenTransform<T, A, B> {
a: A,
b: B,
t: Rc<T>,
_t: std::marker::PhantomData<C>,
}
impl<T, A, B, C> AndThenTransform<T, A, B, C>
impl<T, A, B> AndThenTransform<T, A, B>
where
A: NewService<C>,
B: NewService<C, InitError = A::InitError>,
A: NewService,
B: NewService<Config = A::Config, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
@ -27,12 +26,11 @@ where
a,
b,
t: Rc::new(t),
_t: std::marker::PhantomData,
}
}
}
impl<T, A, B, C> Clone for AndThenTransform<T, A, B, C>
impl<T, A, B> Clone for AndThenTransform<T, A, B>
where
A: Clone,
B: Clone,
@ -42,15 +40,14 @@ where
a: self.a.clone(),
b: self.b.clone(),
t: self.t.clone(),
_t: std::marker::PhantomData,
}
}
}
impl<T, A, B, C> NewService<C> for AndThenTransform<T, A, B, C>
impl<T, A, B> NewService for AndThenTransform<T, A, B>
where
A: NewService<C>,
B: NewService<C, InitError = A::InitError>,
A: NewService,
B: NewService<Config = A::Config, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
@ -58,11 +55,12 @@ where
type Response = T::Response;
type Error = T::Error;
type Config = A::Config;
type InitError = T::InitError;
type Service = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
type Future = AndThenTransformFuture<T, A, B, C>;
type Future = AndThenTransformFuture<T, A, B>;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: &A::Config) -> Self::Future {
AndThenTransformFuture {
a: None,
t: None,
@ -74,10 +72,10 @@ where
}
}
pub struct AndThenTransformFuture<T, A, B, C>
pub struct AndThenTransformFuture<T, A, B>
where
A: NewService<C>,
B: NewService<C, InitError = A::InitError>,
A: NewService,
B: NewService<InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
@ -89,10 +87,10 @@ where
t_cell: Rc<T>,
}
impl<T, A, B, C> Future for AndThenTransformFuture<T, A, B, C>
impl<T, A, B> Future for AndThenTransformFuture<T, A, B>
where
A: NewService<C>,
B: NewService<C, InitError = A::InitError>,
A: NewService,
B: NewService<InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{

View File

@ -133,27 +133,23 @@ where
}
/// `ApplyNewService` new service combinator
pub struct AndThenApplyNewService<A, B, F, Out, Cfg> {
pub struct AndThenApplyNewService<A, B, F, Out> {
a: A,
b: B,
f: Cell<F>,
r: PhantomData<(Out, Cfg)>,
r: PhantomData<Out>,
}
impl<A, B, F, Out, Cfg> AndThenApplyNewService<A, B, F, Out, Cfg>
impl<A, B, F, Out> AndThenApplyNewService<A, B, F, Out>
where
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService,
B: NewService<Config = A::Config, 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, Cfg>, B1: IntoNewService<B, Cfg>>(
a: A1,
b: B1,
f: F,
) -> Self {
pub fn new<A1: IntoNewService<A>, B1: IntoNewService<B>>(a: A1, b: B1, f: F) -> Self {
Self {
f: Cell::new(f),
a: a.into_new_service(),
@ -163,7 +159,7 @@ where
}
}
impl<A, B, F, Out, Cfg> Clone for AndThenApplyNewService<A, B, F, Out, Cfg>
impl<A, B, F, Out> Clone for AndThenApplyNewService<A, B, F, Out>
where
A: Clone,
B: Clone,
@ -178,10 +174,10 @@ where
}
}
impl<A, B, F, Out, Cfg> NewService<Cfg> for AndThenApplyNewService<A, B, F, Out, Cfg>
impl<A, B, F, Out> NewService for AndThenApplyNewService<A, B, F, Out>
where
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService,
B: NewService<Config = A::Config, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
@ -190,11 +186,11 @@ where
type Response = Out::Item;
type Error = A::Error;
type Service = AndThenApply<A::Service, B::Service, F, Out>;
type Config = A::Config;
type InitError = A::InitError;
type Future = AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>;
type Future = AndThenApplyNewServiceFuture<A, B, F, Out>;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
fn new_service(&self, cfg: &A::Config) -> Self::Future {
AndThenApplyNewServiceFuture {
a: None,
b: None,
@ -205,10 +201,10 @@ where
}
}
pub struct AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>
pub struct AndThenApplyNewServiceFuture<A, B, F, Out>
where
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService,
B: NewService<Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
@ -220,10 +216,10 @@ where
b: Option<B::Service>,
}
impl<A, B, F, Out, Cfg> Future for AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>
impl<A, B, F, Out> Future for AndThenApplyNewServiceFuture<A, B, F, Out>
where
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService,
B: NewService<Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,

View File

@ -16,17 +16,14 @@ where
Apply::new(service.into_service(), f)
}
/// Create fractory for `apply_fn` service.
pub fn apply_fn_factory<T, F, In, Out, Cfg, U>(
service: U,
f: F,
) -> ApplyNewService<T, F, In, Out, Cfg>
/// Create factory for `apply` service.
pub fn new_apply_fn<T, F, In, Out, U>(service: U, f: F) -> ApplyNewService<T, F, In, Out>
where
T: NewService<Cfg>,
T: NewService,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
U: IntoNewService<T, Cfg>,
U: IntoNewService<T>,
{
ApplyNewService::new(service.into_new_service(), f)
}
@ -50,7 +47,7 @@ where
Out::Error: From<T::Error>,
{
/// Create new `Apply` combinator
pub fn new<I: IntoService<T>>(service: I, f: F) -> Self {
pub(crate) fn new<I: IntoService<T>>(service: I, f: F) -> Self {
Self {
service: service.into_service(),
f,
@ -95,24 +92,24 @@ where
}
/// `ApplyNewService` new service combinator
pub struct ApplyNewService<T, F, In, Out, Cfg>
pub struct ApplyNewService<T, F, In, Out>
where
T: NewService<Cfg>,
T: NewService,
{
service: T,
f: F,
r: PhantomData<(In, Out, Cfg)>,
r: PhantomData<(In, Out)>,
}
impl<T, F, In, Out, Cfg> ApplyNewService<T, F, In, Out, Cfg>
impl<T, F, In, Out> ApplyNewService<T, F, In, Out>
where
T: NewService<Cfg>,
T: NewService,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
/// Create new `ApplyNewService` new service instance
pub fn new<F1: IntoNewService<T, Cfg>>(service: F1, f: F) -> Self {
pub(crate) fn new<F1: IntoNewService<T>>(service: F1, f: F) -> Self {
Self {
f,
service: service.into_new_service(),
@ -121,9 +118,9 @@ where
}
}
impl<T, F, In, Out, Cfg> Clone for ApplyNewService<T, F, In, Out, Cfg>
impl<T, F, In, Out> Clone for ApplyNewService<T, F, In, Out>
where
T: NewService<Cfg> + Clone,
T: NewService + Clone,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
@ -136,9 +133,9 @@ where
}
}
impl<T, F, In, Out, Cfg> NewService<Cfg> for ApplyNewService<T, F, In, Out, Cfg>
impl<T, F, In, Out> NewService for ApplyNewService<T, F, In, Out>
where
T: NewService<Cfg>,
T: NewService,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
@ -146,19 +143,20 @@ where
type Request = In;
type Response = Out::Item;
type Error = Out::Error;
type Config = T::Config;
type Service = Apply<T::Service, F, In, Out>;
type InitError = T::InitError;
type Future = ApplyNewServiceFuture<T, F, In, Out, Cfg>;
type Future = ApplyNewServiceFuture<T, F, In, Out>;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
fn new_service(&self, cfg: &T::Config) -> Self::Future {
ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone())
}
}
pub struct ApplyNewServiceFuture<T, F, In, Out, Cfg>
pub struct ApplyNewServiceFuture<T, F, In, Out>
where
T: NewService<Cfg>,
T: NewService,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
@ -167,9 +165,9 @@ where
r: PhantomData<(In, Out)>,
}
impl<T, F, In, Out, Cfg> ApplyNewServiceFuture<T, F, In, Out, Cfg>
impl<T, F, In, Out> ApplyNewServiceFuture<T, F, In, Out>
where
T: NewService<Cfg>,
T: NewService,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
@ -182,9 +180,9 @@ where
}
}
impl<T, F, In, Out, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Cfg>
impl<T, F, In, Out> Future for ApplyNewServiceFuture<T, F, In, Out>
where
T: NewService<Cfg>,
T: NewService,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,

View File

@ -1,112 +1,104 @@
use std::marker::PhantomData;
use crate::{IntoNewService, NewService};
use futures::future::Future;
use futures::{try_ready, Async, IntoFuture, Poll};
/// Create new ApplyConfig` service factory combinator
pub fn apply_cfg<F, S, C1, C2, U>(f: F, service: U) -> ApplyConfig<F, S, C1, C2>
use crate::cell::Cell;
use crate::{IntoService, NewService, Service};
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
pub fn apply_cfg<F, C, T, R, S>(srv: T, f: F) -> ApplyConfigService<F, C, T, R, S>
where
S: NewService<C2>,
F: Fn(&C1) -> C2,
U: IntoNewService<S, C2>,
F: FnMut(&C, &mut T) -> R,
T: Service,
R: IntoFuture,
R::Item: IntoService<S>,
S: Service,
{
ApplyConfig::new(service.into_new_service(), f)
}
/// `ApplyConfig` service factory combinator
pub struct ApplyConfig<F, S, C1, C2> {
s: S,
f: F,
r: PhantomData<(C1, C2)>,
}
impl<F, S, C1, C2> ApplyConfig<F, S, C1, C2>
where
S: NewService<C2>,
F: Fn(&C1) -> C2,
{
/// Create new ApplyConfig` service factory combinator
pub fn new<U: IntoNewService<S, C2>>(a: U, f: F) -> Self {
Self {
f,
s: a.into_new_service(),
r: PhantomData,
}
ApplyConfigService {
f: Cell::new(f),
srv: Cell::new(srv.into_service()),
_t: PhantomData,
}
}
impl<F, S, C1, C2> Clone for ApplyConfig<F, S, C1, C2>
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
pub struct ApplyConfigService<F, C, T, R, S>
where
S: Clone,
F: Clone,
F: FnMut(&C, &mut T) -> R,
T: Service,
R: IntoFuture,
R::Item: IntoService<S>,
S: Service,
{
f: Cell<F>,
srv: Cell<T>,
_t: PhantomData<(C, R, S)>,
}
impl<F, C, T, R, S> Clone for ApplyConfigService<F, C, T, R, S>
where
F: FnMut(&C, &mut T) -> R,
T: Service,
R: IntoFuture,
R::Item: IntoService<S>,
S: Service,
{
fn clone(&self) -> Self {
Self {
s: self.s.clone(),
ApplyConfigService {
f: self.f.clone(),
r: PhantomData,
srv: self.srv.clone(),
_t: PhantomData,
}
}
}
impl<F, S, C1, C2> NewService<C1> for ApplyConfig<F, S, C1, C2>
impl<F, C, T, R, S> NewService for ApplyConfigService<F, C, T, R, S>
where
S: NewService<C2>,
F: Fn(&C1) -> C2,
F: FnMut(&C, &mut T) -> R,
T: Service,
R: IntoFuture,
R::Item: IntoService<S>,
S: Service,
{
type Config = C;
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;
type Service = S;
type InitError = S::InitError;
type Future = S::Future;
type InitError = R::Error;
type Future = FnNewServiceConfigFut<R, S>;
fn new_service(&self, cfg: &C1) -> Self::Future {
let cfg2 = (self.f)(cfg);
self.s.new_service(&cfg2)
}
}
#[cfg(test)]
mod tests {
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
use crate::{fn_cfg_factory, NewService, Service};
#[derive(Clone)]
struct Srv;
impl Service for Srv {
type Request = ();
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, _: ()) -> Self::Future {
ok(())
}
}
#[test]
fn test_new_service() {
let new_srv = fn_cfg_factory(|_: &usize| Ok::<_, ()>(Srv)).apply_cfg(
fn_cfg_factory(|s: &String| {
assert_eq!(s, "test");
Ok::<_, ()>(Srv)
}),
|cfg: &usize| {
assert_eq!(*cfg, 1);
"test".to_string()
},
);
if let Async::Ready(mut srv) = new_srv.new_service(&1).poll().unwrap() {
assert!(srv.poll_ready().is_ok());
fn new_service(&self, cfg: &C) -> Self::Future {
FnNewServiceConfigFut {
fut: unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) }
.into_future(),
_t: PhantomData,
}
}
}
pub struct FnNewServiceConfigFut<R, S>
where
R: IntoFuture,
R::Item: IntoService<S>,
S: Service,
{
fut: R::Future,
_t: PhantomData<(S,)>,
}
impl<R, S> Future for FnNewServiceConfigFut<R, S>
where
R: IntoFuture,
R::Item: IntoService<S>,
S: Service,
{
type Item = S;
type Error = R::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
Ok(Async::Ready(try_ready!(self.fut.poll()).into_service()))
}
}

View File

@ -68,12 +68,13 @@ 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 Service = Blank<R, E1>;
type Config = ();
type Service = Blank<R, E1>;
type InitError = E2;
type Future = FutureResult<Self::Service, Self::InitError>;

View File

@ -15,13 +15,14 @@ pub type BoxedService<Req, Res, Err> = Box<
pub type BoxedServiceResponse<Res, Err> =
Either<FutureResult<Res, Err>, Box<Future<Item = Res, Error = Err>>>;
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
/// Create boxed new service
pub fn new_service<T, C>(
pub fn new_service<T>(
service: T,
) -> BoxedNewService<C, T::Request, T::Response, T::Error, T::InitError>
) -> BoxedNewService<T::Config, T::Request, T::Response, T::Error, T::InitError>
where
C: 'static,
T: NewService<C> + 'static,
T: NewService + 'static,
T::Request: 'static,
T::Response: 'static,
T::Service: 'static,
@ -46,7 +47,7 @@ where
type Inner<C, Req, Res, Err, InitErr> = Box<
NewService<
C,
Config = C,
Request = Req,
Response = Res,
Error = Err,
@ -56,9 +57,7 @@ type Inner<C, Req, Res, Err, InitErr> = Box<
>,
>;
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
impl<C, Req, Res, Err, InitErr> NewService<C> for BoxedNewService<C, Req, Res, Err, InitErr>
impl<C, Req, Res, Err, InitErr> NewService for BoxedNewService<C, Req, Res, Err, InitErr>
where
Req: 'static,
Res: 'static,
@ -69,6 +68,7 @@ where
type Response = Res;
type Error = Err;
type InitError = InitErr;
type Config = C;
type Service = BoxedService<Req, Res, Err>;
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
@ -77,18 +77,18 @@ where
}
}
struct NewServiceWrapper<C, T: NewService<C>> {
struct NewServiceWrapper<C, T: NewService> {
service: T,
_t: std::marker::PhantomData<C>,
}
impl<C, T, Req, Res, Err, InitErr> NewService<C> for NewServiceWrapper<C, T>
impl<C, T, Req, Res, Err, InitErr> NewService for NewServiceWrapper<C, T>
where
Req: 'static,
Res: 'static,
Err: 'static,
InitErr: 'static,
T: NewService<C, Request = Req, Response = Res, Error = Err, InitError = InitErr>,
T: NewService<Config = C, Request = Req, Response = Res, Error = Err, InitError = InitErr>,
T::Future: 'static,
T::Service: 'static,
<T::Service as Service>::Future: 'static,
@ -97,6 +97,7 @@ where
type Response = Res;
type Error = Err;
type InitError = InitErr;
type Config = C;
type Service = BoxedService<Req, Res, Err>;
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;

View File

@ -29,4 +29,8 @@ impl<T> Cell<T> {
pub(crate) fn get_mut(&mut self) -> &mut T {
unsafe { &mut *self.inner.as_ref().get() }
}
pub(crate) unsafe fn get_mut_unsafe(&self) -> &mut T {
&mut *self.inner.as_ref().get()
}
}

View File

@ -3,29 +3,30 @@ use std::marker::PhantomData;
use futures::future::{ok, Future, FutureResult};
use futures::{try_ready, Async, IntoFuture, Poll};
use crate::{IntoConfigurableNewService, IntoNewService, IntoService, NewService, Service};
use crate::{IntoNewService, IntoService, NewService, Service};
/// Create `NewService` for function that can act as Service
pub fn fn_service<F, Req, Out, Cfg>(f: F) -> FnNewService<F, Req, Out, Cfg>
/// Create `NewService` for function that can act as a Service
pub fn service_fn<F, Req, Out, Cfg>(f: F) -> NewServiceFn<F, Req, Out, Cfg>
where
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
{
FnNewService::new(f)
NewServiceFn::new(f)
}
/// Create `NewService` for function that can produce services
pub fn fn_factory<F, R, S, E>(f: F) -> FnNewServiceNoConfig<F, R, S, E>
pub fn new_service_fn<F, C, R, S, E>(f: F) -> FnNewServiceNoConfig<F, C, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
R::Item: IntoService<S>,
S: Service,
{
FnNewServiceNoConfig::new(f)
}
/// Create `NewService` for function that can produce services with configuration
pub fn fn_cfg_factory<F, C, R, S, E>(f: F) -> FnNewServiceConfig<F, C, R, S, E>
pub fn new_service_cfg<F, C, R, S, E>(f: F) -> FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Error = E>,
@ -35,39 +36,36 @@ where
FnNewServiceConfig::new(f)
}
pub struct FnService<F, Req, Out>
pub struct ServiceFn<F, Req, Out>
where
F: FnMut(Req) -> Out,
Out: IntoFuture,
{
f: F,
_t: PhantomData<(Req,)>,
_t: PhantomData<Req>,
}
impl<F, Req, Out> FnService<F, Req, Out>
impl<F, Req, Out> ServiceFn<F, Req, Out>
where
F: FnMut(Req) -> Out,
Out: IntoFuture,
{
pub fn new(f: F) -> Self {
FnService { f, _t: PhantomData }
pub(crate) fn new(f: F) -> Self {
ServiceFn { f, _t: PhantomData }
}
}
impl<F, Req, Out> Clone for FnService<F, Req, Out>
impl<F, Req, Out> Clone for ServiceFn<F, Req, Out>
where
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
{
fn clone(&self) -> Self {
FnService {
f: self.f.clone(),
_t: PhantomData,
}
ServiceFn::new(self.f.clone())
}
}
impl<F, Req, Out> Service for FnService<F, Req, Out>
impl<F, Req, Out> Service for ServiceFn<F, Req, Out>
where
F: FnMut(Req) -> Out,
Out: IntoFuture,
@ -86,17 +84,17 @@ where
}
}
impl<F, Req, Out> IntoService<FnService<F, Req, Out>> for F
impl<F, Req, Out> IntoService<ServiceFn<F, Req, Out>> for F
where
F: FnMut(Req) -> Out + 'static,
F: FnMut(Req) -> Out,
Out: IntoFuture,
{
fn into_service(self) -> FnService<F, Req, Out> {
FnService::new(self)
fn into_service(self) -> ServiceFn<F, Req, Out> {
ServiceFn::new(self)
}
}
pub struct FnNewService<F, Req, Out, Cfg>
pub struct NewServiceFn<F, Req, Out, Cfg>
where
F: FnMut(Req) -> Out,
Out: IntoFuture,
@ -105,17 +103,27 @@ where
_t: PhantomData<(Req, Cfg)>,
}
impl<F, Req, Out, Cfg> FnNewService<F, Req, Out, Cfg>
impl<F, Req, Out, Cfg> NewServiceFn<F, Req, Out, Cfg>
where
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
{
pub fn new(f: F) -> Self {
FnNewService { f, _t: PhantomData }
pub(crate) fn new(f: F) -> Self {
NewServiceFn { f, _t: PhantomData }
}
}
impl<F, Req, Out, Cfg> NewService<Cfg> for FnNewService<F, Req, Out, Cfg>
impl<F, Req, Out, Cfg> Clone for NewServiceFn<F, Req, Out, Cfg>
where
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
{
fn clone(&self) -> Self {
NewServiceFn::new(self.f.clone())
}
}
impl<F, Req, Out, Cfg> NewService for NewServiceFn<F, Req, Out, Cfg>
where
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
@ -123,95 +131,34 @@ where
type Request = Req;
type Response = Out::Item;
type Error = Out::Error;
type Service = FnService<F, Req, Out>;
type Config = Cfg;
type Service = ServiceFn<F, Req, Out>;
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &Cfg) -> Self::Future {
ok(FnService::new(self.f.clone()))
ok(ServiceFn::new(self.f.clone()))
}
}
impl<F, Req, Out, Cfg> Clone for FnNewService<F, Req, Out, Cfg>
impl<F, Req, Out, Cfg> IntoService<ServiceFn<F, Req, Out>> for NewServiceFn<F, Req, Out, Cfg>
where
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
fn into_service(self) -> ServiceFn<F, Req, Out> {
ServiceFn::new(self.f.clone())
}
}
impl<F, Req, Out, Cfg> IntoNewService<FnNewService<F, Req, Out, Cfg>, Cfg> for F
impl<F, Req, Out, Cfg> IntoNewService<NewServiceFn<F, Req, Out, Cfg>> for F
where
F: Fn(Req) -> Out + Clone,
Out: IntoFuture,
{
fn into_new_service(self) -> FnNewService<F, Req, Out, Cfg> {
FnNewService::new(self)
}
}
/// Converter for `Fn() -> Future<Service>` fn
pub struct FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
f: F,
}
impl<F, R, S, E> FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
pub fn new(f: F) -> Self {
FnNewServiceNoConfig { f }
}
}
impl<F, R, S, E> NewService<()> for FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S;
type InitError = E;
type Future = R::Future;
fn new_service(&self, _: &()) -> Self::Future {
(self.f)().into_future()
}
}
impl<F, R, S, E> Clone for FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
}
}
impl<F, R, S, E> IntoNewService<FnNewServiceNoConfig<F, R, S, E>, ()> for F
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E> {
FnNewServiceNoConfig::new(self)
fn into_new_service(self) -> NewServiceFn<F, Req, Out, Cfg> {
NewServiceFn::new(self)
}
}
@ -239,7 +186,7 @@ where
}
}
impl<F, C, R, S, E> NewService<C> for FnNewServiceConfig<F, C, R, S, E>
impl<F, C, R, S, E> NewService for FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Error = E>,
@ -249,8 +196,9 @@ where
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S;
type Config = C;
type Service = S;
type InitError = E;
type Future = FnNewServiceConfigFut<R, S, E>;
@ -298,14 +246,65 @@ where
}
}
impl<F, C, R, S, E> IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E>, C> for F
/// Converter for `Fn() -> Future<Service>` fn
pub struct FnNewServiceNoConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E> {
FnNewServiceConfig::new(self)
f: F,
_t: PhantomData<C>,
}
impl<F, C, R, S, E> FnNewServiceNoConfig<F, C, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
pub fn new(f: F) -> Self {
FnNewServiceNoConfig { f, _t: PhantomData }
}
}
impl<F, C, R, S, E> NewService for FnNewServiceNoConfig<F, C, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S;
type Config = C;
type InitError = E;
type Future = R::Future;
fn new_service(&self, _: &C) -> Self::Future {
(self.f)().into_future()
}
}
impl<F, C, R, S, E> Clone for FnNewServiceNoConfig<F, C, R, S, E>
where
F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
}
}
impl<F, C, R, S, E> IntoNewService<FnNewServiceNoConfig<F, C, R, S, E>> for F
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
fn into_new_service(self) -> FnNewServiceNoConfig<F, C, R, S, E> {
FnNewServiceNoConfig::new(self)
}
}

View File

@ -3,10 +3,11 @@ use std::marker::PhantomData;
use futures::future::{ok, FutureResult};
use futures::IntoFuture;
use crate::{Apply, IntoTransform, Service, Transform};
use crate::apply::Apply;
use crate::{IntoTransform, Service, Transform};
/// Use function as transform service
pub fn fn_transform<F, S, In, Out, Err>(
pub fn transform_fn<F, S, In, Out, Err>(
f: F,
) -> impl Transform<S, Request = In, Response = Out::Item, Error = Out::Error, InitError = Err>
where

View File

@ -81,23 +81,23 @@ where
/// service's error.
///
/// This is created by the `NewServiceExt::from_err` method.
pub struct FromErrNewService<A, E, C> {
pub struct FromErrNewService<A, E> {
a: A,
e: PhantomData<(E, C)>,
e: PhantomData<E>,
}
impl<A, E, C> FromErrNewService<A, E, C> {
impl<A, E> FromErrNewService<A, E> {
/// Create new `FromErr` new service instance
pub fn new(a: A) -> Self
where
A: NewService<C>,
A: NewService,
E: From<A::Error>,
{
Self { a, e: PhantomData }
}
}
impl<A, E, C> Clone for FromErrNewService<A, E, C>
impl<A, E> Clone for FromErrNewService<A, E>
where
A: Clone,
{
@ -109,20 +109,21 @@ where
}
}
impl<A, E, C> NewService<C> for FromErrNewService<A, E, C>
impl<A, E> NewService for FromErrNewService<A, E>
where
A: NewService<C>,
A: NewService,
E: From<A::Error>,
{
type Request = A::Request;
type Response = A::Response;
type Error = E;
type Config = A::Config;
type Service = FromErr<A::Service, E>;
type InitError = A::InitError;
type Future = FromErrNewServiceFuture<A, E, C>;
type Future = FromErrNewServiceFuture<A, E>;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: &A::Config) -> Self::Future {
FromErrNewServiceFuture {
fut: self.a.new_service(cfg),
e: PhantomData,
@ -130,18 +131,18 @@ where
}
}
pub struct FromErrNewServiceFuture<A, E, C>
pub struct FromErrNewServiceFuture<A, E>
where
A: NewService<C>,
A: NewService,
E: From<A::Error>,
{
fut: A::Future,
e: PhantomData<E>,
}
impl<A, E, C> Future for FromErrNewServiceFuture<A, E, C>
impl<A, E> Future for FromErrNewServiceFuture<A, E>
where
A: NewService<C>,
A: NewService,
E: From<A::Error>,
{
type Item = FromErr<A::Service, E>;

View File

@ -4,8 +4,6 @@ use std::sync::Arc;
use futures::{Future, IntoFuture, Poll};
pub use void::Void;
mod and_then;
mod and_then_apply;
mod and_then_apply_fn;
@ -18,40 +16,29 @@ mod fn_service;
mod fn_transform;
mod from_err;
mod map;
mod map_config;
mod map_err;
mod map_init_err;
mod then;
mod transform;
mod transform_err;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `apply_fn` instead")]
pub use self::apply::Apply;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `apply_fn_factory` instead")]
pub use self::apply::ApplyNewService;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `fn_transform` instead")]
pub use self::fn_transform::FnTransform;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `apply_transform` instead")]
pub use self::transform::ApplyTransform;
pub use self::and_then::{AndThen, AndThenNewService};
use self::and_then_apply::AndThenTransform;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
pub use self::apply::{apply_fn, apply_fn_factory};
pub use self::apply::{apply_fn, new_apply_fn, Apply, ApplyNewService};
pub use self::apply_cfg::apply_cfg;
use self::apply_cfg::ApplyConfig;
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
pub use self::fn_transform::fn_transform;
pub use self::fn_service::{new_service_cfg, new_service_fn, service_fn, ServiceFn};
pub use self::fn_transform::transform_fn;
pub use self::from_err::{FromErr, FromErrNewService};
pub use self::map::{Map, MapNewService};
pub use self::map_config::{MapConfig, MappedConfig, UnitConfig};
pub use self::map_err::{MapErr, MapErrNewService};
pub use self::map_init_err::MapInitErr;
pub use self::then::{Then, ThenNewService};
pub use self::transform::{apply_transform, IntoTransform, Transform};
use self::and_then_apply::AndThenTransform;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
/// An asynchronous function from `Request` to a `Response`.
pub trait Service {
/// Requests handled by the service.
@ -195,7 +182,7 @@ impl<T: ?Sized> ServiceExt for T where T: Service {}
/// requests on that new TCP stream.
///
/// `Config` is a service factory configuration type.
pub trait NewService<Config = ()> {
pub trait NewService {
/// Requests handled by the service.
type Request;
@ -205,6 +192,9 @@ pub trait NewService<Config = ()> {
/// Errors produced by the service
type Error;
/// Service factory configuration
type Config;
/// The `Service` value created by this factory
type Service: Service<
Request = Self::Request,
@ -219,37 +209,29 @@ pub trait NewService<Config = ()> {
type Future: Future<Item = Self::Service, Error = Self::InitError>;
/// Create and return a new service value asynchronously.
fn new_service(&self, cfg: &Config) -> Self::Future;
fn new_service(&self, cfg: &Self::Config) -> Self::Future;
/// Apply transform service to specified service and use it as a next service in
/// chain.
fn apply<T, T1, B, B1>(
self,
transform: T1,
service: B1,
) -> AndThenTransform<T, Self, B, Config>
fn apply<T, T1, B, B1>(self, transform: T1, service: B1) -> AndThenTransform<T, Self, B>
where
Self: Sized,
T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>,
T::Error: From<Self::Error>,
T1: IntoTransform<T, B::Service>,
B: NewService<Config, InitError = Self::InitError>,
B1: IntoNewService<B, Config>,
B: NewService<Config = Self::Config, InitError = Self::InitError>,
B1: IntoNewService<B>,
{
AndThenTransform::new(transform.into_transform(), self, service.into_new_service())
}
/// 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, Config>
fn apply_fn<B, I, F, Out>(self, service: I, f: F) -> AndThenApplyNewService<Self, B, F, Out>
where
Self: Sized,
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Config>,
B: NewService<Config = Self::Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B>,
F: FnMut(Self::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<Self::Error>,
@ -257,34 +239,13 @@ pub trait NewService<Config = ()> {
AndThenApplyNewService::new(self, service, f)
}
/// Map this service's config type to a different config,
/// and use for nested service
fn apply_cfg<F, C, S, U>(
self,
service: U,
f: F,
) -> AndThenNewService<Self, ApplyConfig<F, S, Config, C>, Config>
where
Self: Sized,
F: Fn(&Config) -> C,
U: IntoNewService<S, C>,
S: NewService<
C,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
{
self.and_then(ApplyConfig::new(service, f))
}
/// Call another service after call to this one has resolved successfully.
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B, Config>
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
where
Self: Sized,
F: IntoNewService<B, Config>,
F: IntoNewService<B>,
B: NewService<
Config,
Config = Self::Config,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
@ -299,7 +260,7 @@ pub trait NewService<Config = ()> {
///
/// Note that this function consumes the receiving new service and returns a
/// wrapped version of it.
fn from_err<E>(self) -> FromErrNewService<Self, E, Config>
fn from_err<E>(self) -> FromErrNewService<Self, E>
where
Self: Sized,
E: From<Self::Error>,
@ -313,12 +274,12 @@ pub trait NewService<Config = ()> {
///
/// 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, Config>
fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B>
where
Self: Sized,
F: IntoNewService<B, Config>,
F: IntoNewService<B>,
B: NewService<
Config,
Config = Self::Config,
Request = Result<Self::Response, Self::Error>,
Error = Self::Error,
InitError = Self::InitError,
@ -329,7 +290,7 @@ pub trait NewService<Config = ()> {
/// 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, Config>
fn map<F, R>(self, f: F) -> MapNewService<Self, F, R>
where
Self: Sized,
F: FnMut(Self::Response) -> R,
@ -338,7 +299,7 @@ pub trait NewService<Config = ()> {
}
/// 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, Config>
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E + Clone,
@ -346,14 +307,31 @@ pub trait NewService<Config = ()> {
MapErrNewService::new(self, f)
}
/// 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, Config>
/// Map this factory's init error to a different error, returning a new service.
fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, F, E>
where
Self: Sized,
F: Fn(Self::InitError) -> E,
{
MapInitErr::new(self, f)
}
/// Map config to a different error, returning a new service.
fn map_config<F, C>(self, f: F) -> MapConfig<Self, F, C>
where
Self: Sized,
F: Fn(&C) -> MappedConfig<Self::Config>,
{
MapConfig::new(self, f)
}
/// Replace config with unit
fn unit_config<C>(self) -> UnitConfig<Self, C>
where
Self: NewService<Config = ()> + Sized,
{
UnitConfig::new(self)
}
}
impl<'a, S> Service for &'a mut S
@ -410,34 +388,36 @@ where
}
}
impl<S, C> NewService<C> for Rc<S>
impl<S> NewService for Rc<S>
where
S: NewService<C>,
S: NewService,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Config = S::Config;
type Service = S::Service;
type InitError = S::InitError;
type Future = S::Future;
fn new_service(&self, cfg: &C) -> S::Future {
fn new_service(&self, cfg: &S::Config) -> S::Future {
self.as_ref().new_service(cfg)
}
}
impl<S, C> NewService<C> for Arc<S>
impl<S> NewService for Arc<S>
where
S: NewService<C>,
S: NewService,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Config = S::Config;
type Service = S::Service;
type InitError = S::InitError;
type Future = S::Future;
fn new_service(&self, cfg: &C) -> S::Future {
fn new_service(&self, cfg: &S::Config) -> S::Future {
self.as_ref().new_service(cfg)
}
}
@ -452,9 +432,9 @@ where
}
/// Trait for types that can be converted to a `NewService`
pub trait IntoNewService<T, C = ()>
pub trait IntoNewService<T>
where
T: NewService<C>,
T: NewService,
{
/// Convert to an `NewService`
fn into_new_service(self) -> T;
@ -469,27 +449,9 @@ where
}
}
impl<T, C> IntoNewService<T, C> for T
impl<T> IntoNewService<T> for T
where
T: NewService<C>,
{
fn into_new_service(self) -> T {
self
}
}
/// Trait for types that can be converted to a configurable `NewService`
pub trait IntoConfigurableNewService<T, C>
where
T: NewService<C>,
{
/// Convert to an `NewService`
fn into_new_service(self) -> T;
}
impl<T, C> IntoConfigurableNewService<T, C> for T
where
T: NewService<C>,
T: NewService,
{
fn into_new_service(self) -> T {
self

View File

@ -97,17 +97,17 @@ where
}
/// `MapNewService` new service combinator
pub struct MapNewService<A, F, Res, Cfg> {
pub struct MapNewService<A, F, Res> {
a: A,
f: F,
r: PhantomData<(Res, Cfg)>,
r: PhantomData<Res>,
}
impl<A, F, Res, Cfg> MapNewService<A, F, Res, Cfg> {
impl<A, F, Res> MapNewService<A, F, Res> {
/// Create new `Map` new service instance
pub fn new(a: A, f: F) -> Self
where
A: NewService<Cfg>,
A: NewService,
F: FnMut(A::Response) -> Res,
{
Self {
@ -118,7 +118,7 @@ impl<A, F, Res, Cfg> MapNewService<A, F, Res, Cfg> {
}
}
impl<A, F, Res, Cfg> Clone for MapNewService<A, F, Res, Cfg>
impl<A, F, Res> Clone for MapNewService<A, F, Res>
where
A: Clone,
F: Clone,
@ -132,36 +132,37 @@ where
}
}
impl<A, F, Res, Cfg> NewService<Cfg> for MapNewService<A, F, Res, Cfg>
impl<A, F, Res> NewService for MapNewService<A, F, Res>
where
A: NewService<Cfg>,
A: NewService,
F: FnMut(A::Response) -> Res + Clone,
{
type Request = A::Request;
type Response = Res;
type Error = A::Error;
type Config = A::Config;
type Service = Map<A::Service, F, Res>;
type InitError = A::InitError;
type Future = MapNewServiceFuture<A, F, Res, Cfg>;
type Future = MapNewServiceFuture<A, F, Res>;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
fn new_service(&self, cfg: &A::Config) -> Self::Future {
MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
}
}
pub struct MapNewServiceFuture<A, F, Res, Cfg>
pub struct MapNewServiceFuture<A, F, Res>
where
A: NewService<Cfg>,
A: NewService,
F: FnMut(A::Response) -> Res,
{
fut: A::Future,
f: Option<F>,
}
impl<A, F, Res, Cfg> MapNewServiceFuture<A, F, Res, Cfg>
impl<A, F, Res> MapNewServiceFuture<A, F, Res>
where
A: NewService<Cfg>,
A: NewService,
F: FnMut(A::Response) -> Res,
{
fn new(fut: A::Future, f: F) -> Self {
@ -169,9 +170,9 @@ where
}
}
impl<A, F, Res, Cfg> Future for MapNewServiceFuture<A, F, Res, Cfg>
impl<A, F, Res> Future for MapNewServiceFuture<A, F, Res>
where
A: NewService<Cfg>,
A: NewService,
F: FnMut(A::Response) -> Res,
{
type Item = Map<A::Service, F, Res>;

View File

@ -0,0 +1,112 @@
use std::marker::PhantomData;
use super::NewService;
pub enum MappedConfig<'a, T> {
Ref(&'a T),
Owned(T),
}
/// `MapInitErr` service combinator
pub struct MapConfig<A, F, C> {
a: A,
f: F,
e: PhantomData<C>,
}
impl<A, F, C> MapConfig<A, F, C> {
/// Create new `MapConfig` combinator
pub fn new(a: A, f: F) -> Self
where
A: NewService,
F: Fn(&C) -> MappedConfig<A::Config>,
{
Self {
a,
f,
e: PhantomData,
}
}
}
impl<A, F, C> Clone for MapConfig<A, F, C>
where
A: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
f: self.f.clone(),
e: PhantomData,
}
}
}
impl<A, F, C> NewService for MapConfig<A, F, C>
where
A: NewService,
F: Fn(&C) -> MappedConfig<A::Config>,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type Config = C;
type Service = A::Service;
type InitError = A::InitError;
type Future = A::Future;
fn new_service(&self, cfg: &C) -> Self::Future {
match (self.f)(cfg) {
MappedConfig::Ref(cfg) => self.a.new_service(cfg),
MappedConfig::Owned(cfg) => self.a.new_service(&cfg),
}
}
}
/// `MapInitErr` service combinator
pub struct UnitConfig<A, C> {
a: A,
e: PhantomData<C>,
}
impl<A, C> UnitConfig<A, C> {
/// Create new `UnitConfig` combinator
pub fn new(a: A) -> Self
where
A: NewService<Config = ()>,
{
Self { a, e: PhantomData }
}
}
impl<A, C> Clone for UnitConfig<A, C>
where
A: Clone,
{
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
e: PhantomData,
}
}
}
impl<A, C> NewService for UnitConfig<A, C>
where
A: NewService<Config = ()>,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type Config = C;
type Service = A::Service;
type InitError = A::InitError;
type Future = A::Future;
fn new_service(&self, _: &C) -> Self::Future {
self.a.new_service(&())
}
}

View File

@ -98,19 +98,19 @@ where
/// service's error.
///
/// This is created by the `NewServiceExt::map_err` method.
pub struct MapErrNewService<A, F, E, C>
pub struct MapErrNewService<A, F, E>
where
A: NewService<C>,
A: NewService,
F: Fn(A::Error) -> E + Clone,
{
a: A,
f: F,
e: PhantomData<(E, C)>,
e: PhantomData<E>,
}
impl<A, F, E, C> MapErrNewService<A, F, E, C>
impl<A, F, E> MapErrNewService<A, F, E>
where
A: NewService<C>,
A: NewService,
F: Fn(A::Error) -> E + Clone,
{
/// Create new `MapErr` new service instance
@ -123,9 +123,9 @@ where
}
}
impl<A, F, E, C> Clone for MapErrNewService<A, F, E, C>
impl<A, F, E> Clone for MapErrNewService<A, F, E>
where
A: NewService<C> + Clone,
A: NewService + Clone,
F: Fn(A::Error) -> E + Clone,
{
fn clone(&self) -> Self {
@ -137,36 +137,37 @@ where
}
}
impl<A, F, E, C> NewService<C> for MapErrNewService<A, F, E, C>
impl<A, F, E> NewService for MapErrNewService<A, F, E>
where
A: NewService<C>,
A: NewService,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = E;
type Config = A::Config;
type Service = MapErr<A::Service, F, E>;
type InitError = A::InitError;
type Future = MapErrNewServiceFuture<A, F, E, C>;
type Future = MapErrNewServiceFuture<A, F, E>;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: &A::Config) -> Self::Future {
MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
}
}
pub struct MapErrNewServiceFuture<A, F, E, C>
pub struct MapErrNewServiceFuture<A, F, E>
where
A: NewService<C>,
A: NewService,
F: Fn(A::Error) -> E,
{
fut: A::Future,
f: F,
}
impl<A, F, E, C> MapErrNewServiceFuture<A, F, E, C>
impl<A, F, E> MapErrNewServiceFuture<A, F, E>
where
A: NewService<C>,
A: NewService,
F: Fn(A::Error) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
@ -174,9 +175,9 @@ where
}
}
impl<A, F, E, C> Future for MapErrNewServiceFuture<A, F, E, C>
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E>
where
A: NewService<C>,
A: NewService,
F: Fn(A::Error) -> E + Clone,
{
type Item = MapErr<A::Service, F, E>;

View File

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

View File

@ -1,5 +1,3 @@
use std::marker::PhantomData;
use futures::{Async, Future, Poll};
use super::{IntoNewService, NewService, Service};
@ -115,38 +113,36 @@ where
}
/// `ThenNewService` new service combinator
pub struct ThenNewService<A, B, C> {
pub struct ThenNewService<A, B> {
a: A,
b: B,
_t: PhantomData<C>,
}
impl<A, B, C> ThenNewService<A, B, C> {
impl<A, B> ThenNewService<A, B> {
/// Create new `AndThen` combinator
pub fn new<F>(a: A, f: F) -> Self
where
A: NewService<C>,
A: NewService,
B: NewService<
C,
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
F: IntoNewService<B, C>,
F: IntoNewService<B>,
{
Self {
a,
b: f.into_new_service(),
_t: PhantomData,
}
}
}
impl<A, B, C> NewService<C> for ThenNewService<A, B, C>
impl<A, B> NewService for ThenNewService<A, B>
where
A: NewService<C>,
A: NewService,
B: NewService<
C,
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
@ -155,17 +151,18 @@ where
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Config = A::Config;
type Service = Then<A::Service, B::Service>;
type InitError = A::InitError;
type Future = ThenNewServiceFuture<A, B, C>;
type Future = ThenNewServiceFuture<A, B>;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: &A::Config) -> Self::Future {
ThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg))
}
}
impl<A, B, C> Clone for ThenNewService<A, B, C>
impl<A, B> Clone for ThenNewService<A, B>
where
A: Clone,
B: Clone,
@ -174,16 +171,15 @@ where
Self {
a: self.a.clone(),
b: self.b.clone(),
_t: PhantomData,
}
}
}
pub struct ThenNewServiceFuture<A, B, C>
pub struct ThenNewServiceFuture<A, B>
where
A: NewService<C>,
A: NewService,
B: NewService<
C,
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
@ -195,11 +191,11 @@ where
b: Option<B::Service>,
}
impl<A, B, C> ThenNewServiceFuture<A, B, C>
impl<A, B> ThenNewServiceFuture<A, B>
where
A: NewService<C>,
A: NewService,
B: NewService<
C,
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
@ -215,11 +211,11 @@ where
}
}
impl<A, B, C> Future for ThenNewServiceFuture<A, B, C>
impl<A, B> Future for ThenNewServiceFuture<A, B>
where
A: NewService<C>,
A: NewService,
B: NewService<
C,
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,

View File

@ -133,11 +133,11 @@ where
/// Apply transform to service factory. Function returns
/// services factory that in initialization creates
/// service and applies transform to this service.
pub fn apply_transform<T, S, C, F, U>(
pub fn apply_transform<T, S, F, U>(
t: F,
service: U,
) -> impl NewService<
C,
Config = S::Config,
Request = T::Request,
Response = T::Response,
Error = T::Error,
@ -145,24 +145,23 @@ pub fn apply_transform<T, S, C, F, U>(
InitError = S::InitError,
> + Clone
where
S: NewService<C>,
S: NewService,
T: Transform<S::Service, InitError = S::InitError>,
F: IntoTransform<T, S::Service>,
U: IntoNewService<S, C>,
U: IntoNewService<S>,
{
ApplyTransform::new(t.into_transform(), service.into_new_service())
}
/// `Apply` transform to new service
pub struct ApplyTransform<T, S, C> {
pub struct ApplyTransform<T, S> {
s: Rc<S>,
t: Rc<T>,
_t: std::marker::PhantomData<C>,
}
impl<T, S, C> ApplyTransform<T, S, C>
impl<T, S> ApplyTransform<T, S>
where
S: NewService<C>,
S: NewService,
T: Transform<S::Service, InitError = S::InitError>,
{
/// Create new `ApplyTransform` new service instance
@ -170,35 +169,34 @@ where
Self {
s: Rc::new(service),
t: Rc::new(t.into_transform()),
_t: std::marker::PhantomData,
}
}
}
impl<T, S, C> Clone for ApplyTransform<T, S, C> {
impl<T, S> Clone for ApplyTransform<T, S> {
fn clone(&self) -> Self {
ApplyTransform {
s: self.s.clone(),
t: self.t.clone(),
_t: std::marker::PhantomData,
}
}
}
impl<T, S, C> NewService<C> for ApplyTransform<T, S, C>
impl<T, S> NewService for ApplyTransform<T, S>
where
S: NewService<C>,
S: NewService,
T: Transform<S::Service, InitError = S::InitError>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Config = S::Config;
type Service = T::Transform;
type InitError = T::InitError;
type Future = ApplyTransformFuture<T, S, C>;
type Future = ApplyTransformFuture<T, S>;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: &S::Config) -> Self::Future {
ApplyTransformFuture {
t_cell: self.t.clone(),
fut_a: self.s.new_service(cfg).into_future(),
@ -207,9 +205,9 @@ where
}
}
pub struct ApplyTransformFuture<T, S, C>
pub struct ApplyTransformFuture<T, S>
where
S: NewService<C>,
S: NewService,
T: Transform<S::Service, InitError = S::InitError>,
{
fut_a: S::Future,
@ -217,9 +215,9 @@ where
t_cell: Rc<T>,
}
impl<T, S, C> Future for ApplyTransformFuture<T, S, C>
impl<T, S> Future for ApplyTransformFuture<T, S>
where
S: NewService<C>,
S: NewService,
T: Transform<S::Service, InitError = S::InitError>,
{
type Item = T::Transform;