refactor naming

This commit is contained in:
Nikolay Kim 2019-11-11 11:44:06 +06:00
parent daaae9d2f0
commit 68e00ec18c
18 changed files with 531 additions and 272 deletions

View File

@ -21,6 +21,6 @@ path = "src/lib.rs"
bytes = "0.4.12" bytes = "0.4.12"
pin-utils = "0.1.0-alpha.4" pin-utils = "0.1.0-alpha.4"
futures = "0.3.1" futures = "0.3.1"
tokio-io = "0.2.0-alpha.5" tokio-io = "0.2.0-alpha.6"
tokio-codec = "0.2.0-alpha.5" tokio-codec = "0.2.0-alpha.6"
log = "0.4" log = "0.4"

View File

@ -2,16 +2,16 @@
use std::fmt; use std::fmt;
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::BytesMut; use bytes::BytesMut;
use futures::{Poll, Sink, Stream}; use futures::{Sink, Stream};
use tokio_codec::{Decoder, Encoder}; use tokio_codec::{Decoder, Encoder};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use super::framed_read::{framed_read2, framed_read2_with_buffer, FramedRead2}; use super::framed_read::{framed_read2, framed_read2_with_buffer, FramedRead2};
use super::framed_write::{framed_write2, framed_write2_with_buffer, FramedWrite2}; use super::framed_write::{framed_write2, framed_write2_with_buffer, FramedWrite2};
use std::pin::Pin;
use std::task::Context;
const LW: usize = 1024; const LW: usize = 1024;
const HW: usize = 8 * 1024; const HW: usize = 8 * 1024;

View File

@ -1,14 +1,14 @@
use std::fmt; use std::fmt;
use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::BytesMut; use bytes::BytesMut;
use futures::{Poll, Sink, Stream}; use futures::{Sink, Stream};
use log::trace; use log::trace;
use tokio_codec::Decoder; use tokio_codec::Decoder;
use tokio_io::AsyncRead; use tokio_io::AsyncRead;
use super::framed::Fuse; use super::framed::Fuse;
use std::pin::Pin;
use std::task::Context;
/// A `Stream` of messages decoded from an `AsyncRead`. /// A `Stream` of messages decoded from an `AsyncRead`.
pub struct FramedRead<T, D> { pub struct FramedRead<T, D> {

View File

@ -1,15 +1,15 @@
use std::fmt; use std::fmt;
use std::io::{self, Read}; use std::io::{self, Read};
use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::BytesMut; use bytes::BytesMut;
use futures::{ready, Poll, Sink, Stream}; use futures::{ready, Sink, Stream};
use log::trace; use log::trace;
use tokio_codec::{Decoder, Encoder}; use tokio_codec::{Decoder, Encoder};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use super::framed::Fuse; use super::framed::Fuse;
use std::pin::Pin;
use std::task::Context;
/// A `Sink` of frames encoded to an `AsyncWrite`. /// A `Sink` of frames encoded to an `AsyncWrite`.
pub struct FramedWrite<T, E> { pub struct FramedWrite<T, E> {

View File

@ -4,7 +4,7 @@ use std::task::{Context, Poll};
use pin_project::pin_project; use pin_project::pin_project;
use super::{IntoNewService, NewService, Service}; use super::{Factory, Service};
use crate::cell::Cell; use crate::cell::Cell;
/// Service for the `and_then` combinator, chaining a computation onto the end /// Service for the `and_then` combinator, chaining a computation onto the end
@ -129,8 +129,8 @@ where
/// `AndThenNewService` new service combinator /// `AndThenNewService` new service combinator
pub struct AndThenNewService<A, B> pub struct AndThenNewService<A, B>
where where
A: NewService, A: Factory,
B: NewService, B: Factory,
{ {
a: A, a: A,
b: B, b: B,
@ -138,8 +138,8 @@ where
impl<A, B> AndThenNewService<A, B> impl<A, B> AndThenNewService<A, B>
where where
A: NewService, A: Factory,
B: NewService< B: Factory<
Config = A::Config, Config = A::Config,
Request = A::Response, Request = A::Response,
Error = A::Error, Error = A::Error,
@ -147,18 +147,15 @@ where
>, >,
{ {
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<F: IntoNewService<B>>(a: A, f: F) -> Self { pub fn new(a: A, b: B) -> Self {
Self { Self { a, b }
a,
b: f.into_new_service(),
}
} }
} }
impl<A, B> NewService for AndThenNewService<A, B> impl<A, B> Factory for AndThenNewService<A, B>
where where
A: NewService, A: Factory,
B: NewService< B: Factory<
Config = A::Config, Config = A::Config,
Request = A::Response, Request = A::Response,
Error = A::Error, Error = A::Error,
@ -181,8 +178,8 @@ where
impl<A, B> Clone for AndThenNewService<A, B> impl<A, B> Clone for AndThenNewService<A, B>
where where
A: NewService + Clone, A: Factory + Clone,
B: NewService + Clone, B: Factory + Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
@ -195,8 +192,8 @@ where
#[pin_project] #[pin_project]
pub struct AndThenNewServiceFuture<A, B> pub struct AndThenNewServiceFuture<A, B>
where where
A: NewService, A: Factory,
B: NewService<Request = A::Response>, B: Factory<Request = A::Response>,
{ {
#[pin] #[pin]
fut_b: B::Future, fut_b: B::Future,
@ -209,8 +206,8 @@ where
impl<A, B> AndThenNewServiceFuture<A, B> impl<A, B> AndThenNewServiceFuture<A, B>
where where
A: NewService, A: Factory,
B: NewService<Request = A::Response>, B: Factory<Request = A::Response>,
{ {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self { fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
AndThenNewServiceFuture { AndThenNewServiceFuture {
@ -224,8 +221,8 @@ where
impl<A, B> Future for AndThenNewServiceFuture<A, B> impl<A, B> Future for AndThenNewServiceFuture<A, B>
where where
A: NewService, A: Factory,
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>, B: Factory<Request = A::Response, Error = A::Error, InitError = A::InitError>,
{ {
type Output = Result<AndThen<A::Service, B::Service>, A::InitError>; type Output = Result<AndThen<A::Service, B::Service>, A::InitError>;

View File

@ -5,7 +5,7 @@ use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use super::IntoFuture; use super::IntoFuture;
use super::{IntoNewService, IntoService, NewService, Service}; use super::{Factory, IntoFactory, IntoService, Service};
/// Apply tranform function to a service /// Apply tranform function to a service
pub fn apply_fn<T, F, In, Out, U>(service: U, f: F) -> Apply<T, F, In, Out> pub fn apply_fn<T, F, In, Out, U>(service: U, f: F) -> Apply<T, F, In, Out>
@ -20,15 +20,15 @@ where
} }
/// Create factory for `apply` service. /// Create factory for `apply` service.
pub fn new_apply_fn<T, F, In, Out, U>(service: U, f: F) -> ApplyNewService<T, F, In, Out> pub fn apply_fn_factory<T, F, In, Out, U>(service: U, f: F) -> ApplyNewService<T, F, In, Out>
where where
T: NewService, T: Factory,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
Out::Error: From<T::Error>, Out::Error: From<T::Error>,
U: IntoNewService<T>, U: IntoFactory<T>,
{ {
ApplyNewService::new(service.into_new_service(), f) ApplyNewService::new(service.into_factory(), f)
} }
#[doc(hidden)] #[doc(hidden)]
@ -52,9 +52,9 @@ where
Out::Error: From<T::Error>, Out::Error: From<T::Error>,
{ {
/// Create new `Apply` combinator /// Create new `Apply` combinator
pub(crate) fn new<I: IntoService<T>>(service: I, f: F) -> Self { pub(crate) fn new(service: T, f: F) -> Self {
Self { Self {
service: service.into_service(), service,
f, f,
r: PhantomData, r: PhantomData,
} }
@ -99,7 +99,7 @@ where
/// `ApplyNewService` new service combinator /// `ApplyNewService` new service combinator
pub struct ApplyNewService<T, F, In, Out> pub struct ApplyNewService<T, F, In, Out>
where where
T: NewService, T: Factory,
{ {
service: T, service: T,
f: F, f: F,
@ -108,16 +108,16 @@ where
impl<T, F, In, Out> ApplyNewService<T, F, In, Out> impl<T, F, In, Out> ApplyNewService<T, F, In, Out>
where where
T: NewService, T: Factory,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
Out::Error: From<T::Error>, Out::Error: From<T::Error>,
{ {
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub(crate) fn new<F1: IntoNewService<T>>(service: F1, f: F) -> Self { pub(crate) fn new(service: T, f: F) -> Self {
Self { Self {
f, f,
service: service.into_new_service(), service,
r: PhantomData, r: PhantomData,
} }
} }
@ -125,7 +125,7 @@ where
impl<T, F, In, Out> Clone for ApplyNewService<T, F, In, Out> impl<T, F, In, Out> Clone for ApplyNewService<T, F, In, Out>
where where
T: NewService + Clone, T: Factory + Clone,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
@ -138,9 +138,9 @@ where
} }
} }
impl<T, F, In, Out> NewService for ApplyNewService<T, F, In, Out> impl<T, F, In, Out> Factory for ApplyNewService<T, F, In, Out>
where where
T: NewService, T: Factory,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
Out::Error: From<T::Error>, Out::Error: From<T::Error>,
@ -162,7 +162,7 @@ where
#[pin_project] #[pin_project]
pub struct ApplyNewServiceFuture<T, F, In, Out> pub struct ApplyNewServiceFuture<T, F, In, Out>
where where
T: NewService, T: Factory,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
@ -174,7 +174,7 @@ where
impl<T, F, In, Out> ApplyNewServiceFuture<T, F, In, Out> impl<T, F, In, Out> ApplyNewServiceFuture<T, F, In, Out>
where where
T: NewService, T: Factory,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
@ -189,7 +189,7 @@ where
impl<T, F, In, Out> Future for ApplyNewServiceFuture<T, F, In, Out> impl<T, F, In, Out> Future for ApplyNewServiceFuture<T, F, In, Out>
where where
T: NewService, T: Factory,
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
Out::Error: From<T::Error>, Out::Error: From<T::Error>,

View File

@ -7,13 +7,13 @@ use futures::ready;
use pin_project::pin_project; use pin_project::pin_project;
use crate::cell::Cell; use crate::cell::Cell;
use crate::{IntoFuture, IntoService, NewService, Service}; use crate::{Factory, IntoFuture, IntoService, Service};
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService /// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
pub fn apply_cfg<F, C, T, R, S>( pub fn apply_cfg<F, C, T, R, S>(
srv: T, srv: T,
f: F, f: F,
) -> impl NewService< ) -> impl Factory<
Config = C, Config = C,
Request = S::Request, Request = S::Request,
Response = S::Response, Response = S::Response,
@ -37,10 +37,10 @@ where
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService /// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
/// Service get constructor from NewService. /// Service get constructor from NewService.
pub fn new_apply_cfg<F, C, T, R, S>( pub fn apply_cfg_factory<F, C, T, R, S>(
srv: T, srv: T,
f: F, f: F,
) -> impl NewService< ) -> impl Factory<
Config = C, Config = C,
Request = S::Request, Request = S::Request,
Response = S::Response, Response = S::Response,
@ -51,7 +51,7 @@ pub fn new_apply_cfg<F, C, T, R, S>(
where where
C: Clone, C: Clone,
F: FnMut(&C, &mut T::Service) -> R, F: FnMut(&C, &mut T::Service) -> R,
T: NewService<Config = ()>, T: Factory<Config = ()>,
T::InitError: From<T::Error>, T::InitError: From<T::Error>,
R: IntoFuture<Error = T::InitError>, R: IntoFuture<Error = T::InitError>,
R::Item: IntoService<S>, R::Item: IntoService<S>,
@ -97,7 +97,7 @@ where
} }
} }
impl<F, C, T, R, S> NewService for ApplyConfigService<F, C, T, R, S> impl<F, C, T, R, S> Factory for ApplyConfigService<F, C, T, R, S>
where where
F: FnMut(&C, &mut T) -> R, F: FnMut(&C, &mut T) -> R,
T: Service, T: Service,
@ -153,7 +153,7 @@ struct ApplyConfigNewService<F, C, T, R, S>
where where
C: Clone, C: Clone,
F: FnMut(&C, &mut T::Service) -> R, F: FnMut(&C, &mut T::Service) -> R,
T: NewService<Config = ()>, T: Factory<Config = ()>,
R: IntoFuture<Error = T::InitError>, R: IntoFuture<Error = T::InitError>,
R::Item: IntoService<S>, R::Item: IntoService<S>,
S: Service, S: Service,
@ -167,7 +167,7 @@ impl<F, C, T, R, S> Clone for ApplyConfigNewService<F, C, T, R, S>
where where
C: Clone, C: Clone,
F: FnMut(&C, &mut T::Service) -> R, F: FnMut(&C, &mut T::Service) -> R,
T: NewService<Config = ()>, T: Factory<Config = ()>,
R: IntoFuture<Error = T::InitError>, R: IntoFuture<Error = T::InitError>,
R::Item: IntoService<S>, R::Item: IntoService<S>,
S: Service, S: Service,
@ -181,11 +181,11 @@ where
} }
} }
impl<F, C, T, R, S> NewService for ApplyConfigNewService<F, C, T, R, S> impl<F, C, T, R, S> Factory for ApplyConfigNewService<F, C, T, R, S>
where where
C: Clone, C: Clone,
F: FnMut(&C, &mut T::Service) -> R, F: FnMut(&C, &mut T::Service) -> R,
T: NewService<Config = ()>, T: Factory<Config = ()>,
T::InitError: From<T::Error>, T::InitError: From<T::Error>,
R: IntoFuture<Error = T::InitError>, R: IntoFuture<Error = T::InitError>,
R::Item: IntoService<S>, R::Item: IntoService<S>,
@ -217,7 +217,7 @@ struct ApplyConfigNewServiceFut<F, C, T, R, S>
where where
C: Clone, C: Clone,
F: FnMut(&C, &mut T::Service) -> R, F: FnMut(&C, &mut T::Service) -> R,
T: NewService<Config = ()>, T: Factory<Config = ()>,
T::InitError: From<T::Error>, T::InitError: From<T::Error>,
R: IntoFuture<Error = T::InitError>, R: IntoFuture<Error = T::InitError>,
R::Item: IntoService<S>, R::Item: IntoService<S>,
@ -237,7 +237,7 @@ impl<F, C, T, R, S> Future for ApplyConfigNewServiceFut<F, C, T, R, S>
where where
C: Clone, C: Clone,
F: FnMut(&C, &mut T::Service) -> R, F: FnMut(&C, &mut T::Service) -> R,
T: NewService<Config = ()>, T: Factory<Config = ()>,
T::InitError: From<T::Error>, T::InitError: From<T::Error>,
R: IntoFuture<Error = T::InitError>, R: IntoFuture<Error = T::InitError>,
R::Item: IntoService<S>, R::Item: IntoService<S>,

View File

@ -4,10 +4,10 @@ use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use crate::{IntoFuture, NewService, Service};
use futures::future::FutureExt;
use futures::future::LocalBoxFuture;
use futures::future::{err, ok, Either, Ready}; use futures::future::{err, ok, Either, Ready};
use futures::future::{FutureExt, LocalBoxFuture};
use crate::{Factory, IntoFuture, Service};
pub type BoxedService<Req, Res, Err> = Box< pub type BoxedService<Req, Res, Err> = Box<
dyn Service< dyn Service<
@ -24,11 +24,11 @@ pub type BoxedServiceResponse<Res, Err> =
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>); pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
/// Create boxed new service /// Create boxed new service
pub fn new_service<T>( pub fn factory<T>(
service: T, factory: T,
) -> BoxedNewService<T::Config, T::Request, T::Response, T::Error, T::InitError> ) -> BoxedNewService<T::Config, T::Request, T::Response, T::Error, T::InitError>
where where
T: NewService + 'static, T: Factory + 'static,
T::Request: 'static, T::Request: 'static,
T::Response: 'static, T::Response: 'static,
T::Service: 'static, T::Service: 'static,
@ -36,8 +36,8 @@ where
T::Error: 'static, T::Error: 'static,
T::InitError: 'static, T::InitError: 'static,
{ {
BoxedNewService(Box::new(NewServiceWrapper { BoxedNewService(Box::new(FactoryWrapper {
service, factory,
_t: std::marker::PhantomData, _t: std::marker::PhantomData,
})) }))
} }
@ -52,7 +52,7 @@ where
} }
type Inner<C, Req, Res, Err, InitErr> = Box< type Inner<C, Req, Res, Err, InitErr> = Box<
dyn NewService< dyn Factory<
Config = C, Config = C,
Request = Req, Request = Req,
Response = Res, Response = Res,
@ -63,7 +63,7 @@ type Inner<C, Req, Res, Err, InitErr> = Box<
>, >,
>; >;
impl<C, Req, Res, Err, InitErr> NewService for BoxedNewService<C, Req, Res, Err, InitErr> impl<C, Req, Res, Err, InitErr> Factory for BoxedNewService<C, Req, Res, Err, InitErr>
where where
Req: 'static, Req: 'static,
Res: 'static, Res: 'static,
@ -84,18 +84,18 @@ where
} }
} }
struct NewServiceWrapper<C, T: NewService> { struct FactoryWrapper<C, T: Factory> {
service: T, factory: T,
_t: std::marker::PhantomData<C>, _t: std::marker::PhantomData<C>,
} }
impl<C, T, Req, Res, Err, InitErr> NewService for NewServiceWrapper<C, T> impl<C, T, Req, Res, Err, InitErr> Factory for FactoryWrapper<C, T>
where where
Req: 'static, Req: 'static,
Res: 'static, Res: 'static,
Err: 'static, Err: 'static,
InitErr: 'static, InitErr: 'static,
T: NewService<Config = C, Request = Req, Response = Res, Error = Err, InitError = InitErr>, T: Factory<Config = C, Request = Req, Response = Res, Error = Err, InitError = InitErr>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
<T::Service as Service>::Future: 'static, <T::Service as Service>::Future: 'static,

View File

@ -7,10 +7,18 @@ use futures::future::{ok, Ready};
use pin_project::pin_project; use pin_project::pin_project;
use crate::IntoFuture; use crate::IntoFuture;
use crate::{IntoNewService, IntoService, NewService, Service}; use crate::{Factory, IntoFactory, IntoService, Service};
/// Create `NewService` for function that can act as a Service /// 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> pub fn service_fn<F, Req, Out, Cfg>(
f: F,
) -> impl Factory<
Config = Cfg,
Request = Req,
Response = Out::Item,
Error = Out::Error,
InitError = (),
>
where where
F: FnMut(Req) -> Out + Clone, F: FnMut(Req) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
@ -18,8 +26,17 @@ where
NewServiceFn::new(f) NewServiceFn::new(f)
} }
/// Create `NewService` for function that can produce services /// Create `Factory` for function that can produce services
pub fn new_service_fn<F, C, R, S, E>(f: F) -> FnNewServiceNoConfig<F, C, R, S, E> pub fn factory_fn<F, C, R, S, E>(
f: F,
) -> impl Factory<
Config = C,
Request = S::Request,
Response = S::Response,
Error = S::Error,
InitError = E,
Future = R::Future,
>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
@ -30,7 +47,15 @@ where
} }
/// Create `NewService` for function that can produce services with configuration /// Create `NewService` for function that can produce services with configuration
pub fn new_service_cfg<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,
) -> impl Factory<
Config = C,
Request = S::Request,
Response = S::Response,
Error = S::Error,
InitError = E,
>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
@ -40,7 +65,7 @@ where
FnNewServiceConfig::new(f) FnNewServiceConfig::new(f)
} }
pub struct ServiceFn<F, Req, Out> pub(crate) struct ServiceFn<F, Req, Out>
where where
F: FnMut(Req) -> Out, F: FnMut(Req) -> Out,
Out: IntoFuture, Out: IntoFuture,
@ -98,7 +123,7 @@ where
} }
} }
pub struct NewServiceFn<F, Req, Out, Cfg> pub(crate) struct NewServiceFn<F, Req, Out, Cfg>
where where
F: FnMut(Req) -> Out, F: FnMut(Req) -> Out,
Out: IntoFuture, Out: IntoFuture,
@ -127,7 +152,7 @@ where
} }
} }
impl<F, Req, Out, Cfg> NewService for NewServiceFn<F, Req, Out, Cfg> impl<F, Req, Out, Cfg> Factory for NewServiceFn<F, Req, Out, Cfg>
where where
F: FnMut(Req) -> Out + Clone, F: FnMut(Req) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
@ -156,18 +181,18 @@ where
} }
} }
impl<F, Req, Out, Cfg> IntoNewService<NewServiceFn<F, Req, Out, Cfg>> for F impl<F, Req, Out, Cfg> IntoFactory<NewServiceFn<F, Req, Out, Cfg>> for F
where where
F: Fn(Req) -> Out + Clone, F: Fn(Req) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
fn into_new_service(self) -> NewServiceFn<F, Req, Out, Cfg> { fn into_factory(self) -> NewServiceFn<F, Req, Out, Cfg> {
NewServiceFn::new(self) NewServiceFn::new(self)
} }
} }
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService /// Convert `Fn(&Config) -> Future<Service>` fn to NewService
pub struct FnNewServiceConfig<F, C, R, S, E> pub(crate) struct FnNewServiceConfig<F, C, R, S, E>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
@ -190,7 +215,7 @@ where
} }
} }
impl<F, C, R, S, E> NewService for FnNewServiceConfig<F, C, R, S, E> impl<F, C, R, S, E> Factory for FnNewServiceConfig<F, C, R, S, E>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
@ -215,7 +240,7 @@ where
} }
#[pin_project] #[pin_project]
pub struct FnNewServiceConfigFut<R, S, E> pub(crate) struct FnNewServiceConfigFut<R, S, E>
where where
R: IntoFuture<Error = E>, R: IntoFuture<Error = E>,
R::Item: IntoService<S>, R::Item: IntoService<S>,
@ -254,7 +279,7 @@ where
} }
/// Converter for `Fn() -> Future<Service>` fn /// Converter for `Fn() -> Future<Service>` fn
pub struct FnNewServiceNoConfig<F, C, R, S, E> pub(crate) struct FnNewServiceNoConfig<F, C, R, S, E>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
@ -275,7 +300,7 @@ where
} }
} }
impl<F, C, R, S, E> NewService for FnNewServiceNoConfig<F, C, R, S, E> impl<F, C, R, S, E> Factory for FnNewServiceNoConfig<F, C, R, S, E>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
@ -305,13 +330,13 @@ where
} }
} }
impl<F, C, R, S, E> IntoNewService<FnNewServiceNoConfig<F, C, R, S, E>> for F impl<F, C, R, S, E> IntoFactory<FnNewServiceNoConfig<F, C, R, S, E>> for F
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service,
{ {
fn into_new_service(self) -> FnNewServiceNoConfig<F, C, R, S, E> { fn into_factory(self) -> FnNewServiceNoConfig<F, C, R, S, E> {
FnNewServiceNoConfig::new(self) FnNewServiceNoConfig::new(self)
} }
} }

204
actix-service/src/into.rs Normal file
View File

@ -0,0 +1,204 @@
use std::task::{Context, Poll};
use crate::map::{Map, MapNewService};
use crate::map_err::{MapErr, MapErrNewService};
use crate::map_init_err::MapInitErr;
use crate::{Factory, IntoFactory, IntoService, Service};
#[inline]
/// Convert object of type `U` to a service `T`
pub fn into_service<T, U>(service: U) -> ServiceMapper<T>
where
U: IntoService<T>,
T: Service,
{
ServiceMapper {
service: service.into_service(),
}
}
pub fn into_factory<T, F>(factory: F) -> FactoryMapper<T>
where
T: Factory,
F: IntoFactory<T>,
{
FactoryMapper {
factory: factory.into_factory(),
}
}
pub struct ServiceMapper<T> {
service: T,
}
pub struct FactoryMapper<T> {
factory: T,
}
impl<T: Service> ServiceMapper<T> {
/// Map this service's output to a different type, returning a new service
/// of the resulting type.
///
/// This function is similar to the `Option::map` or `Iterator::map` where
/// it will change the type of the underlying service.
///
/// Note that this function consumes the receiving service and returns a
/// wrapped version of it, similar to the existing `map` methods in the
/// standard library.
pub fn map<F, R>(
self,
f: F,
) -> ServiceMapper<impl Service<Request = T::Request, Response = R, Error = T::Error>>
where
Self: Sized,
F: FnMut(T::Response) -> R + Clone,
{
ServiceMapper {
service: Map::new(self.service, f),
}
}
/// Map this service's error to a different error, returning a new service.
///
/// This function is similar to the `Result::map_err` where it will change
/// the error type of the underlying service. This is useful for example to
/// ensure that services have the same error type.
///
/// Note that this function consumes the receiving service and returns a
/// wrapped version of it.
pub fn map_err<F, E>(
self,
f: F,
) -> ServiceMapper<impl Service<Request = T::Request, Response = T::Response, Error = E>>
where
Self: Sized,
F: Fn(T::Error) -> E + Clone,
{
ServiceMapper {
service: MapErr::new(self, f),
}
}
}
impl<T> Clone for ServiceMapper<T>
where
T: Clone,
{
fn clone(&self) -> Self {
ServiceMapper {
service: self.service.clone(),
}
}
}
impl<T: Service> Service for ServiceMapper<T> {
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;
#[inline]
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(ctx)
}
#[inline]
fn call(&mut self, req: T::Request) -> Self::Future {
self.service.call(req)
}
}
impl<T: Factory> FactoryMapper<T> {
/// Map this service's output to a different type, returning a new service
/// of the resulting type.
pub fn map<F, R>(
self,
f: F,
) -> FactoryMapper<
impl Factory<
Config = T::Config,
Request = T::Request,
Response = R,
Error = T::Error,
InitError = T::InitError,
>,
>
where
Self: Sized,
F: FnMut(T::Response) -> R + Clone,
{
FactoryMapper {
factory: MapNewService::new(self.factory, f),
}
}
/// Map this service's error to a different error, returning a new service.
pub fn map_err<F, E>(
self,
f: F,
) -> FactoryMapper<
impl Factory<
Config = T::Config,
Request = T::Request,
Response = T::Response,
Error = E,
InitError = T::InitError,
>,
>
where
Self: Sized,
F: Fn(T::Error) -> E + Clone,
{
FactoryMapper {
factory: MapErrNewService::new(self.factory, f),
}
}
/// Map this factory's init error to a different error, returning a new service.
pub fn map_init_err<F, E>(
self,
f: F,
) -> FactoryMapper<
impl Factory<
Config = T::Config,
Request = T::Request,
Response = T::Response,
Error = T::Error,
InitError = E,
>,
>
where
Self: Sized,
F: Fn(T::InitError) -> E + Clone,
{
FactoryMapper {
factory: MapInitErr::new(self.factory, f),
}
}
}
impl<T> Clone for FactoryMapper<T>
where
T: Clone,
{
fn clone(&self) -> Self {
FactoryMapper {
factory: self.factory.clone(),
}
}
}
impl<T: Factory> Factory for FactoryMapper<T> {
type Config = T::Config;
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Service = T::Service;
type InitError = T::InitError;
type Future = T::Future;
#[inline]
fn new_service(&self, cfg: &T::Config) -> Self::Future {
self.factory.new_service(cfg)
}
}

View File

@ -10,6 +10,7 @@ mod apply_cfg;
pub mod boxed; pub mod boxed;
mod cell; mod cell;
mod fn_service; mod fn_service;
mod into;
mod map; mod map;
mod map_config; mod map_config;
mod map_err; mod map_err;
@ -19,15 +20,12 @@ mod then;
mod transform; mod transform;
mod transform_err; mod transform_err;
pub use self::apply::{apply_fn, new_apply_fn}; pub use self::apply::{apply_fn, apply_fn_factory};
pub use self::apply_cfg::{apply_cfg, new_apply_cfg}; pub use self::apply_cfg::{apply_cfg, apply_cfg_factory};
pub use self::fn_service::{new_service_cfg, new_service_fn, service_fn, ServiceFn}; pub use self::fn_service::{factory_fn, new_service_cfg, service_fn};
pub use self::map::{Map, MapNewService}; pub use self::into::{into_factory, into_service, FactoryMapper, ServiceMapper};
pub use self::map_config::{MapConfig, MappedConfig, UnitConfig}; pub use self::map_config::{map_config, unit_config, MappedConfig};
pub use self::map_err::{MapErr, MapErrNewService}; pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory};
pub use self::map_init_err::MapInitErr;
pub use self::pipeline::{new_pipeline, pipeline, NewPipeline, Pipeline};
pub use self::then::{Then, ThenNewService};
pub use self::transform::{apply_transform, IntoTransform, Transform}; pub use self::transform::{apply_transform, IntoTransform, Transform};
pub trait IntoFuture { pub trait IntoFuture {
@ -47,22 +45,6 @@ impl<F: Future<Output = Result<I, E>>, I, E> IntoFuture for F {
} }
} }
pub fn service<T, U>(factory: U) -> T
where
T: Service,
U: IntoService<T>,
{
factory.into_service()
}
pub fn new_service<T, U>(factory: U) -> T
where
T: NewService,
U: IntoNewService<T>,
{
factory.into_new_service()
}
/// An asynchronous function from `Request` to a `Response`. /// An asynchronous function from `Request` to a `Response`.
pub trait Service { pub trait Service {
/// Requests handled by the service. /// Requests handled by the service.
@ -130,11 +112,11 @@ pub trait Service {
/// Acts as a service factory. This is useful for cases where new `Service` /// Acts as a service factory. This is useful for cases where new `Service`
/// values must be produced. One case is a TCP server listener. The listener /// values must be produced. One case is a TCP server listener. The listener
/// accepts new TCP streams, obtains a new `Service` value using the /// accepts new TCP streams, obtains a new `Service` value using the
/// `NewService` trait, and uses that new `Service` value to process inbound /// `ServiceFactory` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream. /// requests on that new TCP stream.
/// ///
/// `Config` is a service factory configuration type. /// `Config` is a service factory configuration type.
pub trait NewService { pub trait Factory {
/// Requests handled by the service. /// Requests handled by the service.
type Request; type Request;
@ -218,9 +200,9 @@ where
} }
} }
impl<S> NewService for Rc<S> impl<S> Factory for Rc<S>
where where
S: NewService, S: Factory,
{ {
type Request = S::Request; type Request = S::Request;
type Response = S::Response; type Response = S::Response;
@ -235,9 +217,9 @@ where
} }
} }
impl<S> NewService for Arc<S> impl<S> Factory for Arc<S>
where where
S: NewService, S: Factory,
{ {
type Request = S::Request; type Request = S::Request;
type Response = S::Response; type Response = S::Response;
@ -261,13 +243,13 @@ where
fn into_service(self) -> T; fn into_service(self) -> T;
} }
/// Trait for types that can be converted to a `NewService` /// Trait for types that can be converted to a `ServiceFactory`
pub trait IntoNewService<T> pub trait IntoFactory<T>
where where
T: NewService, T: Factory,
{ {
/// Convert to an `NewService` /// Convert `Self` an `ServiceFactory`
fn into_new_service(self) -> T; fn into_factory(self) -> T;
} }
impl<T> IntoService<T> for T impl<T> IntoService<T> for T
@ -279,11 +261,11 @@ where
} }
} }
impl<T> IntoNewService<T> for T impl<T> IntoFactory<T> for T
where where
T: NewService, T: Factory,
{ {
fn into_new_service(self) -> T { fn into_factory(self) -> T {
self self
} }
} }

View File

@ -5,12 +5,12 @@ use std::task::{Context, Poll};
use pin_project::pin_project; use pin_project::pin_project;
use super::{NewService, Service}; use super::{Factory, Service};
/// Service for the `map` combinator, changing the type of a service's response. /// Service for the `map` combinator, changing the type of a service's response.
/// ///
/// This is created by the `ServiceExt::map` method. /// This is created by the `ServiceExt::map` method.
pub struct Map<A, F, Response> { pub(crate) struct Map<A, F, Response> {
service: A, service: A,
f: F, f: F,
_t: PhantomData<Response>, _t: PhantomData<Response>,
@ -65,7 +65,7 @@ where
} }
#[pin_project] #[pin_project]
pub struct MapFuture<A, F, Response> pub(crate) struct MapFuture<A, F, Response>
where where
A: Service, A: Service,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Response,
@ -103,7 +103,7 @@ where
} }
/// `MapNewService` new service combinator /// `MapNewService` new service combinator
pub struct MapNewService<A, F, Res> { pub(crate) struct MapNewService<A, F, Res> {
a: A, a: A,
f: F, f: F,
r: PhantomData<Res>, r: PhantomData<Res>,
@ -113,7 +113,7 @@ impl<A, F, Res> MapNewService<A, F, Res> {
/// Create new `Map` new service instance /// Create new `Map` new service instance
pub fn new(a: A, f: F) -> Self pub fn new(a: A, f: F) -> Self
where where
A: NewService, A: Factory,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
Self { Self {
@ -138,9 +138,9 @@ where
} }
} }
impl<A, F, Res> NewService for MapNewService<A, F, Res> impl<A, F, Res> Factory for MapNewService<A, F, Res>
where where
A: NewService, A: Factory,
F: FnMut(A::Response) -> Res + Clone, F: FnMut(A::Response) -> Res + Clone,
{ {
type Request = A::Request; type Request = A::Request;
@ -158,9 +158,9 @@ where
} }
#[pin_project] #[pin_project]
pub struct MapNewServiceFuture<A, F, Res> pub(crate) struct MapNewServiceFuture<A, F, Res>
where where
A: NewService, A: Factory,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
#[pin] #[pin]
@ -170,7 +170,7 @@ where
impl<A, F, Res> MapNewServiceFuture<A, F, Res> impl<A, F, Res> MapNewServiceFuture<A, F, Res>
where where
A: NewService, A: Factory,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@ -180,7 +180,7 @@ where
impl<A, F, Res> Future for MapNewServiceFuture<A, F, Res> impl<A, F, Res> Future for MapNewServiceFuture<A, F, Res>
where where
A: NewService, A: Factory,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
type Output = Result<Map<A::Service, F, Res>, A::InitError>; type Output = Result<Map<A::Service, F, Res>, A::InitError>;
@ -200,7 +200,7 @@ mod tests {
use futures::future::{ok, Ready}; use futures::future::{ok, Ready};
use super::*; use super::*;
use crate::{IntoNewService, Service}; use crate::{IntoFactory, Service};
struct Srv; struct Srv;

View File

@ -1,14 +1,48 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use super::NewService; use super::Factory;
pub enum MappedConfig<'a, T> { pub enum MappedConfig<'a, T> {
Ref(&'a T), Ref(&'a T),
Owned(T), Owned(T),
} }
/// Adapt external config to a config for provided new service
pub fn map_config<T, F, C>(
new_service: T,
f: F,
) -> impl Factory<
Config = C,
Request = T::Request,
Response = T::Response,
Error = T::Error,
InitError = T::InitError,
>
where
T: Factory,
F: Fn(&C) -> MappedConfig<T::Config>,
{
MapConfig::new(new_service, f)
}
/// Replace config with unit
pub fn unit_config<T, C>(
new_service: T,
) -> impl Factory<
Config = C,
Request = T::Request,
Response = T::Response,
Error = T::Error,
InitError = T::InitError,
>
where
T: Factory<Config = ()>,
{
UnitConfig::new(new_service)
}
/// `MapInitErr` service combinator /// `MapInitErr` service combinator
pub struct MapConfig<A, F, C> { pub(crate) struct MapConfig<A, F, C> {
a: A, a: A,
f: F, f: F,
e: PhantomData<C>, e: PhantomData<C>,
@ -18,7 +52,7 @@ impl<A, F, C> MapConfig<A, F, C> {
/// Create new `MapConfig` combinator /// Create new `MapConfig` combinator
pub fn new(a: A, f: F) -> Self pub fn new(a: A, f: F) -> Self
where where
A: NewService, A: Factory,
F: Fn(&C) -> MappedConfig<A::Config>, F: Fn(&C) -> MappedConfig<A::Config>,
{ {
Self { Self {
@ -43,9 +77,9 @@ where
} }
} }
impl<A, F, C> NewService for MapConfig<A, F, C> impl<A, F, C> Factory for MapConfig<A, F, C>
where where
A: NewService, A: Factory,
F: Fn(&C) -> MappedConfig<A::Config>, F: Fn(&C) -> MappedConfig<A::Config>,
{ {
type Request = A::Request; type Request = A::Request;
@ -66,17 +100,17 @@ where
} }
/// `MapInitErr` service combinator /// `MapInitErr` service combinator
pub struct UnitConfig<A, C> { pub(crate) struct UnitConfig<A, C> {
a: A, a: A,
e: PhantomData<C>, e: PhantomData<C>,
} }
impl<A, C> UnitConfig<A, C> { impl<A, C> UnitConfig<A, C>
where
A: Factory<Config = ()>,
{
/// Create new `UnitConfig` combinator /// Create new `UnitConfig` combinator
pub fn new(a: A) -> Self pub(crate) fn new(a: A) -> Self {
where
A: NewService<Config = ()>,
{
Self { a, e: PhantomData } Self { a, e: PhantomData }
} }
} }
@ -93,9 +127,9 @@ where
} }
} }
impl<A, C> NewService for UnitConfig<A, C> impl<A, C> Factory for UnitConfig<A, C>
where where
A: NewService<Config = ()>, A: Factory<Config = ()>,
{ {
type Request = A::Request; type Request = A::Request;
type Response = A::Response; type Response = A::Response;

View File

@ -5,13 +5,13 @@ use std::task::{Context, Poll};
use pin_project::pin_project; use pin_project::pin_project;
use super::{NewService, Service}; use super::{Factory, Service};
/// Service for the `map_err` combinator, changing the type of a service's /// Service for the `map_err` combinator, changing the type of a service's
/// error. /// error.
/// ///
/// This is created by the `ServiceExt::map_err` method. /// This is created by the `ServiceExt::map_err` method.
pub struct MapErr<A, F, E> { pub(crate) struct MapErr<A, F, E> {
service: A, service: A,
f: F, f: F,
_t: PhantomData<E>, _t: PhantomData<E>,
@ -66,7 +66,7 @@ where
} }
#[pin_project] #[pin_project]
pub struct MapErrFuture<A, F, E> pub(crate) struct MapErrFuture<A, F, E>
where where
A: Service, A: Service,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
@ -99,13 +99,13 @@ where
} }
} }
/// NewService for the `map_err` combinator, changing the type of a new /// Factory for the `map_err` combinator, changing the type of a new
/// service's error. /// service's error.
/// ///
/// This is created by the `NewServiceExt::map_err` method. /// This is created by the `NewServiceExt::map_err` method.
pub struct MapErrNewService<A, F, E> pub(crate) struct MapErrNewService<A, F, E>
where where
A: NewService, A: Factory,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
a: A, a: A,
@ -115,11 +115,11 @@ where
impl<A, F, E> MapErrNewService<A, F, E> impl<A, F, E> MapErrNewService<A, F, E>
where where
A: NewService, A: Factory,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
/// Create new `MapErr` new service instance /// Create new `MapErr` new service instance
pub fn new(a: A, f: F) -> Self { pub(crate) fn new(a: A, f: F) -> Self {
Self { Self {
a, a,
f, f,
@ -130,7 +130,7 @@ where
impl<A, F, E> Clone for MapErrNewService<A, F, E> impl<A, F, E> Clone for MapErrNewService<A, F, E>
where where
A: NewService + Clone, A: Factory + Clone,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
@ -142,9 +142,9 @@ where
} }
} }
impl<A, F, E> NewService for MapErrNewService<A, F, E> impl<A, F, E> Factory for MapErrNewService<A, F, E>
where where
A: NewService, A: Factory,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Request = A::Request; type Request = A::Request;
@ -162,9 +162,9 @@ where
} }
#[pin_project] #[pin_project]
pub struct MapErrNewServiceFuture<A, F, E> pub(crate) struct MapErrNewServiceFuture<A, F, E>
where where
A: NewService, A: Factory,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
#[pin] #[pin]
@ -174,7 +174,7 @@ where
impl<A, F, E> MapErrNewServiceFuture<A, F, E> impl<A, F, E> MapErrNewServiceFuture<A, F, E>
where where
A: NewService, A: Factory,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@ -184,7 +184,7 @@ where
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E> impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E>
where where
A: NewService, A: Factory,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Output = Result<MapErr<A::Service, F, E>, A::InitError>; type Output = Result<MapErr<A::Service, F, E>, A::InitError>;
@ -201,28 +201,28 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use futures::future::{err, Ready}; // use futures::future::{err, Ready};
use super::*; // use super::*;
use crate::{IntoNewService, NewService, Service}; // use crate::{IntoNewService, NewService, Service};
use tokio::future::ok; // use tokio::future::ok;
struct Srv; // struct Srv;
impl Service for Srv { // impl Service for Srv {
type Request = (); // type Request = ();
type Response = (); // type Response = ();
type Error = (); // type Error = ();
type Future = Ready<Result<(), ()>>; // type Future = Ready<Result<(), ()>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { // fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Err(())) // Poll::Ready(Err(()))
} // }
fn call(&mut self, _: ()) -> Self::Future { // fn call(&mut self, _: ()) -> Self::Future {
err(()) // err(())
} // }
} // }
// #[tokio::test] // #[tokio::test]
// async fn test_poll_ready() { // async fn test_poll_ready() {

View File

@ -5,22 +5,22 @@ use std::task::{Context, Poll};
use pin_project::pin_project; use pin_project::pin_project;
use super::NewService; use super::Factory;
/// `MapInitErr` service combinator /// `MapInitErr` service combinator
pub struct MapInitErr<A, F, E> { pub(crate) struct MapInitErr<A, F, E> {
a: A, a: A,
f: F, f: F,
e: PhantomData<E>, e: PhantomData<E>,
} }
impl<A, F, E> MapInitErr<A, F, E> { impl<A, F, E> MapInitErr<A, F, E>
/// Create new `MapInitErr` combinator where
pub fn new(a: A, f: F) -> Self A: Factory,
where
A: NewService,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
/// Create new `MapInitErr` combinator
pub(crate) fn new(a: A, f: F) -> Self {
Self { Self {
a, a,
f, f,
@ -43,9 +43,9 @@ where
} }
} }
impl<A, F, E> NewService for MapInitErr<A, F, E> impl<A, F, E> Factory for MapInitErr<A, F, E>
where where
A: NewService, A: Factory,
F: Fn(A::InitError) -> E + Clone, F: Fn(A::InitError) -> E + Clone,
{ {
type Request = A::Request; type Request = A::Request;
@ -62,9 +62,9 @@ where
} }
} }
#[pin_project] #[pin_project]
pub struct MapInitErrFuture<A, F, E> pub(crate) struct MapInitErrFuture<A, F, E>
where where
A: NewService, A: Factory,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
f: F, f: F,
@ -74,7 +74,7 @@ where
impl<A, F, E> MapInitErrFuture<A, F, E> impl<A, F, E> MapInitErrFuture<A, F, E>
where where
A: NewService, A: Factory,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@ -84,7 +84,7 @@ where
impl<A, F, E> Future for MapInitErrFuture<A, F, E> impl<A, F, E> Future for MapInitErrFuture<A, F, E>
where where
A: NewService, A: Factory,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
type Output = Result<A::Service, E>; type Output = Result<A::Service, E>;

View File

@ -2,7 +2,7 @@ use std::task::{Context, Poll};
use crate::and_then::{AndThen, AndThenNewService}; use crate::and_then::{AndThen, AndThenNewService};
use crate::then::{Then, ThenNewService}; use crate::then::{Then, ThenNewService};
use crate::{IntoNewService, IntoService, NewService, Service}; use crate::{Factory, IntoFactory, IntoService, Service};
pub fn pipeline<F, T>(service: F) -> Pipeline<T> pub fn pipeline<F, T>(service: F) -> Pipeline<T>
where where
@ -14,13 +14,13 @@ where
} }
} }
pub fn new_pipeline<F, T>(new_service: F) -> NewPipeline<T> pub fn pipeline_factory<T, F>(factory: F) -> PipelineFactory<T>
where where
F: IntoNewService<T>, T: Factory,
T: NewService, F: IntoFactory<T>,
{ {
NewPipeline { PipelineFactory {
service: new_service.into_new_service(), factory: factory.into_factory(),
} }
} }
@ -102,25 +102,36 @@ impl<T: Service> Service for Pipeline<T> {
} }
/// Pipeline constructor /// Pipeline constructor
pub struct NewPipeline<T> { pub struct PipelineFactory<T> {
service: T, factory: T,
} }
impl<T: NewService> NewPipeline<T> { impl<T: Factory> PipelineFactory<T> {
/// Call another service after call to this one has resolved successfully. /// Call another service after call to this one has resolved successfully.
pub fn and_then<F, U>(self, new_service: U) -> NewPipeline<AndThenNewService<T, U>> pub fn and_then<F, U>(
self,
factory: U,
) -> PipelineFactory<
impl Factory<
Config = T::Config,
Request = T::Request,
Response = U::Response,
Error = T::Error,
InitError = T::InitError,
>,
>
where where
Self: Sized, Self: Sized,
F: IntoNewService<U>, F: IntoFactory<U>,
U: NewService< U: Factory<
Config = T::Config, Config = T::Config,
Request = T::Response, Request = T::Response,
Error = T::Error, Error = T::Error,
InitError = T::InitError, InitError = T::InitError,
>, >,
{ {
NewPipeline { PipelineFactory {
service: AndThenNewService::new(self.service, new_service.into_new_service()), factory: AndThenNewService::new(self.factory, factory.into_factory()),
} }
} }
@ -130,35 +141,46 @@ impl<T: NewService> NewPipeline<T> {
/// ///
/// Note that this function consumes the receiving pipeline and returns a /// Note that this function consumes the receiving pipeline and returns a
/// wrapped version of it. /// wrapped version of it.
pub fn then<F, U>(self, new_service: F) -> NewPipeline<ThenNewService<T, U>> pub fn then<F, U>(
self,
factory: F,
) -> PipelineFactory<
impl Factory<
Config = T::Config,
Request = T::Request,
Response = U::Response,
Error = T::Error,
InitError = T::InitError,
>,
>
where where
Self: Sized, Self: Sized,
F: IntoNewService<U>, F: IntoFactory<U>,
U: NewService< U: Factory<
Config = T::Config, Config = T::Config,
Request = Result<T::Response, T::Error>, Request = Result<T::Response, T::Error>,
Error = T::Error, Error = T::Error,
InitError = T::InitError, InitError = T::InitError,
>, >,
{ {
NewPipeline { PipelineFactory {
service: ThenNewService::new(self.service, new_service.into_new_service()), factory: ThenNewService::new(self.factory, factory.into_factory()),
} }
} }
} }
impl<T> Clone for NewPipeline<T> impl<T> Clone for PipelineFactory<T>
where where
T: Clone, T: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
NewPipeline { PipelineFactory {
service: self.service.clone(), factory: self.factory.clone(),
} }
} }
} }
impl<T: NewService> NewService for NewPipeline<T> { impl<T: Factory> Factory for PipelineFactory<T> {
type Config = T::Config; type Config = T::Config;
type Request = T::Request; type Request = T::Request;
type Response = T::Response; type Response = T::Response;
@ -169,6 +191,6 @@ impl<T: NewService> NewService for NewPipeline<T> {
#[inline] #[inline]
fn new_service(&self, cfg: &T::Config) -> Self::Future { fn new_service(&self, cfg: &T::Config) -> Self::Future {
self.service.new_service(cfg) self.factory.new_service(cfg)
} }
} }

View File

@ -4,14 +4,14 @@ use std::task::{Context, Poll};
use pin_project::pin_project; use pin_project::pin_project;
use super::{IntoNewService, NewService, Service}; use super::{Factory, Service};
use crate::cell::Cell; use crate::cell::Cell;
/// Service for the `then` combinator, chaining a computation onto the end of /// Service for the `then` combinator, chaining a computation onto the end of
/// another service. /// another service.
/// ///
/// This is created by the `ServiceExt::then` method. /// This is created by the `ServiceExt::then` method.
pub struct Then<A, B> { pub(crate) struct Then<A, B> {
a: A, a: A,
b: Cell<B>, b: Cell<B>,
} }
@ -64,7 +64,7 @@ where
} }
#[pin_project] #[pin_project]
pub struct ThenFuture<A, B> pub(crate) struct ThenFuture<A, B>
where where
A: Service, A: Service,
B: Service<Request = Result<A::Response, A::Error>>, B: Service<Request = Result<A::Response, A::Error>>,
@ -126,36 +126,32 @@ where
} }
} }
/// `ThenNewService` new service combinator /// `.then()` service factory combinator
pub struct ThenNewService<A, B> { pub(crate) struct ThenNewService<A, B> {
a: A, a: A,
b: B, b: B,
} }
impl<A, B> ThenNewService<A, B> { impl<A, B> ThenNewService<A, B>
/// Create new `AndThen` combinator where
pub fn new<F>(a: A, f: F) -> Self A: Factory,
where B: Factory<
A: NewService,
B: NewService<
Config = A::Config, Config = A::Config,
Request = Result<A::Response, A::Error>, Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
InitError = A::InitError, InitError = A::InitError,
>, >,
F: IntoNewService<B>, {
{ /// Create new `AndThen` combinator
Self { pub fn new(a: A, b: B) -> Self {
a, Self { a, b }
b: f.into_new_service(),
}
} }
} }
impl<A, B> NewService for ThenNewService<A, B> impl<A, B> Factory for ThenNewService<A, B>
where where
A: NewService, A: Factory,
B: NewService< B: Factory<
Config = A::Config, Config = A::Config,
Request = Result<A::Response, A::Error>, Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
@ -190,10 +186,10 @@ where
} }
#[pin_project] #[pin_project]
pub struct ThenNewServiceFuture<A, B> pub(crate) struct ThenNewServiceFuture<A, B>
where where
A: NewService, A: Factory,
B: NewService< B: Factory<
Config = A::Config, Config = A::Config,
Request = Result<A::Response, A::Error>, Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
@ -210,8 +206,8 @@ where
impl<A, B> ThenNewServiceFuture<A, B> impl<A, B> ThenNewServiceFuture<A, B>
where where
A: NewService, A: Factory,
B: NewService< B: Factory<
Config = A::Config, Config = A::Config,
Request = Result<A::Response, A::Error>, Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
@ -230,8 +226,8 @@ where
impl<A, B> Future for ThenNewServiceFuture<A, B> impl<A, B> Future for ThenNewServiceFuture<A, B>
where where
A: NewService, A: Factory,
B: NewService< B: Factory<
Config = A::Config, Config = A::Config,
Request = Result<A::Response, A::Error>, Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
@ -271,7 +267,7 @@ mod tests {
use futures::future::{err, ok, ready, Ready}; use futures::future::{err, ok, ready, Ready};
use crate::{new_pipeline, new_service, pipeline, NewService, Service}; use crate::{pipeline, pipeline_factory, Factory, Service};
#[derive(Clone)] #[derive(Clone)]
struct Srv1(Rc<Cell<usize>>); struct Srv1(Rc<Cell<usize>>);
@ -340,13 +336,12 @@ mod tests {
} }
#[tokio::test] #[tokio::test]
async fn test_new_service() { async fn test_factory() {
let cnt = Rc::new(Cell::new(0)); let cnt = Rc::new(Cell::new(0));
let cnt2 = cnt.clone(); let cnt2 = cnt.clone();
let blank = move || ready(Ok::<_, ()>(Srv1(cnt2.clone()))); let blank = move || ready(Ok::<_, ()>(Srv1(cnt2.clone())));
let new_srv = let factory = pipeline_factory(blank).then(move || ready(Ok(Srv2(cnt.clone()))));
new_pipeline(new_service(blank)).then(move || ready(Ok(Srv2(cnt.clone())))); let mut srv = factory.new_service(&()).await.unwrap();
let mut srv = new_srv.clone().new_service(&()).await.unwrap();
let res = srv.call(Ok("srv1")).await; let res = srv.call(Ok("srv1")).await;
assert!(res.is_ok()); assert!(res.is_ok());
assert_eq!(res.unwrap(), (("srv1", "ok"))); assert_eq!(res.unwrap(), (("srv1", "ok")));

View File

@ -5,7 +5,7 @@ use std::sync::Arc;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use crate::transform_err::{TransformFromErr, TransformMapInitErr}; use crate::transform_err::{TransformFromErr, TransformMapInitErr};
use crate::{IntoNewService, NewService, Service}; use crate::{Factory, IntoFactory, Service};
use pin_project::pin_project; use pin_project::pin_project;
@ -133,7 +133,7 @@ where
pub fn apply_transform<T, S, F, U>( pub fn apply_transform<T, S, F, U>(
t: F, t: F,
service: U, service: U,
) -> impl NewService< ) -> impl Factory<
Config = S::Config, Config = S::Config,
Request = T::Request, Request = T::Request,
Response = T::Response, Response = T::Response,
@ -142,12 +142,12 @@ pub fn apply_transform<T, S, F, U>(
InitError = S::InitError, InitError = S::InitError,
> + Clone > + Clone
where where
S: NewService, S: Factory,
T: Transform<S::Service, InitError = S::InitError>, T: Transform<S::Service, InitError = S::InitError>,
F: IntoTransform<T, S::Service>, F: IntoTransform<T, S::Service>,
U: IntoNewService<S>, U: IntoFactory<S>,
{ {
ApplyTransform::new(t.into_transform(), service.into_new_service()) ApplyTransform::new(t.into_transform(), service.into_factory())
} }
/// `Apply` transform to new service /// `Apply` transform to new service
@ -158,7 +158,7 @@ pub struct ApplyTransform<T, S> {
impl<T, S> ApplyTransform<T, S> impl<T, S> ApplyTransform<T, S>
where where
S: NewService, S: Factory,
T: Transform<S::Service, InitError = S::InitError>, T: Transform<S::Service, InitError = S::InitError>,
{ {
/// Create new `ApplyTransform` new service instance /// Create new `ApplyTransform` new service instance
@ -179,9 +179,9 @@ impl<T, S> Clone for ApplyTransform<T, S> {
} }
} }
impl<T, S> NewService for ApplyTransform<T, S> impl<T, S> Factory for ApplyTransform<T, S>
where where
S: NewService, S: Factory,
T: Transform<S::Service, InitError = S::InitError>, T: Transform<S::Service, InitError = S::InitError>,
{ {
type Request = T::Request; type Request = T::Request;
@ -204,7 +204,7 @@ where
#[pin_project] #[pin_project]
pub struct ApplyTransformFuture<T, S> pub struct ApplyTransformFuture<T, S>
where where
S: NewService, S: Factory,
T: Transform<S::Service, InitError = S::InitError>, T: Transform<S::Service, InitError = S::InitError>,
{ {
#[pin] #[pin]
@ -216,7 +216,7 @@ where
impl<T, S> Future for ApplyTransformFuture<T, S> impl<T, S> Future for ApplyTransformFuture<T, S>
where where
S: NewService, S: Factory,
T: Transform<S::Service, InitError = S::InitError>, T: Transform<S::Service, InitError = S::InitError>,
{ {
type Output = Result<T::Transform, T::InitError>; type Output = Result<T::Transform, T::InitError>;