diff --git a/actix-codec/Cargo.toml b/actix-codec/Cargo.toml index 1eb604d6..ff672051 100644 --- a/actix-codec/Cargo.toml +++ b/actix-codec/Cargo.toml @@ -21,6 +21,6 @@ path = "src/lib.rs" bytes = "0.4.12" pin-utils = "0.1.0-alpha.4" futures = "0.3.1" -tokio-io = "0.2.0-alpha.5" -tokio-codec = "0.2.0-alpha.5" +tokio-io = "0.2.0-alpha.6" +tokio-codec = "0.2.0-alpha.6" log = "0.4" \ No newline at end of file diff --git a/actix-codec/src/framed.rs b/actix-codec/src/framed.rs index edd68e4b..39704e96 100644 --- a/actix-codec/src/framed.rs +++ b/actix-codec/src/framed.rs @@ -2,16 +2,16 @@ use std::fmt; use std::io::{self, Read, Write}; +use std::pin::Pin; +use std::task::{Context, Poll}; use bytes::BytesMut; -use futures::{Poll, Sink, Stream}; +use futures::{Sink, Stream}; use tokio_codec::{Decoder, Encoder}; use tokio_io::{AsyncRead, AsyncWrite}; use super::framed_read::{framed_read2, framed_read2_with_buffer, FramedRead2}; use super::framed_write::{framed_write2, framed_write2_with_buffer, FramedWrite2}; -use std::pin::Pin; -use std::task::Context; const LW: usize = 1024; const HW: usize = 8 * 1024; diff --git a/actix-codec/src/framed_read.rs b/actix-codec/src/framed_read.rs index 9d33e262..e4dc206e 100644 --- a/actix-codec/src/framed_read.rs +++ b/actix-codec/src/framed_read.rs @@ -1,14 +1,14 @@ use std::fmt; +use std::pin::Pin; +use std::task::{Context, Poll}; use bytes::BytesMut; -use futures::{Poll, Sink, Stream}; +use futures::{Sink, Stream}; use log::trace; use tokio_codec::Decoder; use tokio_io::AsyncRead; use super::framed::Fuse; -use std::pin::Pin; -use std::task::Context; /// A `Stream` of messages decoded from an `AsyncRead`. pub struct FramedRead { diff --git a/actix-codec/src/framed_write.rs b/actix-codec/src/framed_write.rs index 278b9995..542bb195 100644 --- a/actix-codec/src/framed_write.rs +++ b/actix-codec/src/framed_write.rs @@ -1,15 +1,15 @@ use std::fmt; use std::io::{self, Read}; +use std::pin::Pin; +use std::task::{Context, Poll}; use bytes::BytesMut; -use futures::{ready, Poll, Sink, Stream}; +use futures::{ready, Sink, Stream}; use log::trace; use tokio_codec::{Decoder, Encoder}; use tokio_io::{AsyncRead, AsyncWrite}; use super::framed::Fuse; -use std::pin::Pin; -use std::task::Context; /// A `Sink` of frames encoded to an `AsyncWrite`. pub struct FramedWrite { diff --git a/actix-service/src/and_then.rs b/actix-service/src/and_then.rs index 24e4dc2a..314c08b8 100644 --- a/actix-service/src/and_then.rs +++ b/actix-service/src/and_then.rs @@ -4,7 +4,7 @@ use std::task::{Context, Poll}; use pin_project::pin_project; -use super::{IntoNewService, NewService, Service}; +use super::{Factory, Service}; use crate::cell::Cell; /// Service for the `and_then` combinator, chaining a computation onto the end @@ -129,8 +129,8 @@ where /// `AndThenNewService` new service combinator pub struct AndThenNewService where - A: NewService, - B: NewService, + A: Factory, + B: Factory, { a: A, b: B, @@ -138,8 +138,8 @@ where impl AndThenNewService where - A: NewService, - B: NewService< + A: Factory, + B: Factory< Config = A::Config, Request = A::Response, Error = A::Error, @@ -147,18 +147,15 @@ where >, { /// Create new `AndThen` combinator - pub fn new>(a: A, f: F) -> Self { - Self { - a, - b: f.into_new_service(), - } + pub fn new(a: A, b: B) -> Self { + Self { a, b } } } -impl NewService for AndThenNewService +impl Factory for AndThenNewService where - A: NewService, - B: NewService< + A: Factory, + B: Factory< Config = A::Config, Request = A::Response, Error = A::Error, @@ -181,8 +178,8 @@ where impl Clone for AndThenNewService where - A: NewService + Clone, - B: NewService + Clone, + A: Factory + Clone, + B: Factory + Clone, { fn clone(&self) -> Self { Self { @@ -195,8 +192,8 @@ where #[pin_project] pub struct AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: Factory, + B: Factory, { #[pin] fut_b: B::Future, @@ -209,8 +206,8 @@ where impl AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: Factory, + B: Factory, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { AndThenNewServiceFuture { @@ -224,8 +221,8 @@ where impl Future for AndThenNewServiceFuture where - A: NewService, - B: NewService, + A: Factory, + B: Factory, { type Output = Result, A::InitError>; diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index eb0389da..2a1b93f3 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -5,7 +5,7 @@ use std::pin::Pin; use std::task::{Context, Poll}; use super::IntoFuture; -use super::{IntoNewService, IntoService, NewService, Service}; +use super::{Factory, IntoFactory, IntoService, Service}; /// Apply tranform function to a service pub fn apply_fn(service: U, f: F) -> Apply @@ -20,15 +20,15 @@ where } /// Create factory for `apply` service. -pub fn new_apply_fn(service: U, f: F) -> ApplyNewService +pub fn apply_fn_factory(service: U, f: F) -> ApplyNewService where - T: NewService, + T: Factory, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From, - U: IntoNewService, + U: IntoFactory, { - ApplyNewService::new(service.into_new_service(), f) + ApplyNewService::new(service.into_factory(), f) } #[doc(hidden)] @@ -52,9 +52,9 @@ where Out::Error: From, { /// Create new `Apply` combinator - pub(crate) fn new>(service: I, f: F) -> Self { + pub(crate) fn new(service: T, f: F) -> Self { Self { - service: service.into_service(), + service, f, r: PhantomData, } @@ -99,7 +99,7 @@ where /// `ApplyNewService` new service combinator pub struct ApplyNewService where - T: NewService, + T: Factory, { service: T, f: F, @@ -108,16 +108,16 @@ where impl ApplyNewService where - T: NewService, + T: Factory, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From, { /// Create new `ApplyNewService` new service instance - pub(crate) fn new>(service: F1, f: F) -> Self { + pub(crate) fn new(service: T, f: F) -> Self { Self { f, - service: service.into_new_service(), + service, r: PhantomData, } } @@ -125,7 +125,7 @@ where impl Clone for ApplyNewService where - T: NewService + Clone, + T: Factory + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, { @@ -138,9 +138,9 @@ where } } -impl NewService for ApplyNewService +impl Factory for ApplyNewService where - T: NewService, + T: Factory, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From, @@ -162,7 +162,7 @@ where #[pin_project] pub struct ApplyNewServiceFuture where - T: NewService, + T: Factory, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, { @@ -174,7 +174,7 @@ where impl ApplyNewServiceFuture where - T: NewService, + T: Factory, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, { @@ -189,7 +189,7 @@ where impl Future for ApplyNewServiceFuture where - T: NewService, + T: Factory, F: FnMut(In, &mut T::Service) -> Out + Clone, Out: IntoFuture, Out::Error: From, diff --git a/actix-service/src/apply_cfg.rs b/actix-service/src/apply_cfg.rs index e7592c2e..5a79d766 100644 --- a/actix-service/src/apply_cfg.rs +++ b/actix-service/src/apply_cfg.rs @@ -7,13 +7,13 @@ use futures::ready; use pin_project::pin_project; use crate::cell::Cell; -use crate::{IntoFuture, IntoService, NewService, Service}; +use crate::{Factory, IntoFuture, IntoService, Service}; /// Convert `Fn(&Config, &mut Service) -> Future` fn to a NewService pub fn apply_cfg( srv: T, f: F, -) -> impl NewService< +) -> impl Factory< Config = C, Request = S::Request, Response = S::Response, @@ -37,10 +37,10 @@ where /// Convert `Fn(&Config, &mut Service) -> Future` fn to a NewService /// Service get constructor from NewService. -pub fn new_apply_cfg( +pub fn apply_cfg_factory( srv: T, f: F, -) -> impl NewService< +) -> impl Factory< Config = C, Request = S::Request, Response = S::Response, @@ -51,7 +51,7 @@ pub fn new_apply_cfg( where C: Clone, F: FnMut(&C, &mut T::Service) -> R, - T: NewService, + T: Factory, T::InitError: From, R: IntoFuture, R::Item: IntoService, @@ -97,7 +97,7 @@ where } } -impl NewService for ApplyConfigService +impl Factory for ApplyConfigService where F: FnMut(&C, &mut T) -> R, T: Service, @@ -153,7 +153,7 @@ struct ApplyConfigNewService where C: Clone, F: FnMut(&C, &mut T::Service) -> R, - T: NewService, + T: Factory, R: IntoFuture, R::Item: IntoService, S: Service, @@ -167,7 +167,7 @@ impl Clone for ApplyConfigNewService where C: Clone, F: FnMut(&C, &mut T::Service) -> R, - T: NewService, + T: Factory, R: IntoFuture, R::Item: IntoService, S: Service, @@ -181,11 +181,11 @@ where } } -impl NewService for ApplyConfigNewService +impl Factory for ApplyConfigNewService where C: Clone, F: FnMut(&C, &mut T::Service) -> R, - T: NewService, + T: Factory, T::InitError: From, R: IntoFuture, R::Item: IntoService, @@ -217,7 +217,7 @@ struct ApplyConfigNewServiceFut where C: Clone, F: FnMut(&C, &mut T::Service) -> R, - T: NewService, + T: Factory, T::InitError: From, R: IntoFuture, R::Item: IntoService, @@ -237,7 +237,7 @@ impl Future for ApplyConfigNewServiceFut where C: Clone, F: FnMut(&C, &mut T::Service) -> R, - T: NewService, + T: Factory, T::InitError: From, R: IntoFuture, R::Item: IntoService, diff --git a/actix-service/src/boxed.rs b/actix-service/src/boxed.rs index d81f9722..784678a2 100644 --- a/actix-service/src/boxed.rs +++ b/actix-service/src/boxed.rs @@ -4,10 +4,10 @@ use std::future::Future; use std::pin::Pin; 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::{FutureExt, LocalBoxFuture}; + +use crate::{Factory, IntoFuture, Service}; pub type BoxedService = Box< dyn Service< @@ -24,11 +24,11 @@ pub type BoxedServiceResponse = pub struct BoxedNewService(Inner); /// Create boxed new service -pub fn new_service( - service: T, +pub fn factory( + factory: T, ) -> BoxedNewService where - T: NewService + 'static, + T: Factory + 'static, T::Request: 'static, T::Response: 'static, T::Service: 'static, @@ -36,8 +36,8 @@ where T::Error: 'static, T::InitError: 'static, { - BoxedNewService(Box::new(NewServiceWrapper { - service, + BoxedNewService(Box::new(FactoryWrapper { + factory, _t: std::marker::PhantomData, })) } @@ -52,7 +52,7 @@ where } type Inner = Box< - dyn NewService< + dyn Factory< Config = C, Request = Req, Response = Res, @@ -63,7 +63,7 @@ type Inner = Box< >, >; -impl NewService for BoxedNewService +impl Factory for BoxedNewService where Req: 'static, Res: 'static, @@ -84,18 +84,18 @@ where } } -struct NewServiceWrapper { - service: T, +struct FactoryWrapper { + factory: T, _t: std::marker::PhantomData, } -impl NewService for NewServiceWrapper +impl Factory for FactoryWrapper where Req: 'static, Res: 'static, Err: 'static, InitErr: 'static, - T: NewService, + T: Factory, T::Future: 'static, T::Service: 'static, ::Future: 'static, diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index aebce5bf..743a835b 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -7,10 +7,18 @@ use futures::future::{ok, Ready}; use pin_project::pin_project; 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 -pub fn service_fn(f: F) -> NewServiceFn +pub fn service_fn( + f: F, +) -> impl Factory< + Config = Cfg, + Request = Req, + Response = Out::Item, + Error = Out::Error, + InitError = (), +> where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, @@ -18,8 +26,17 @@ where NewServiceFn::new(f) } -/// Create `NewService` for function that can produce services -pub fn new_service_fn(f: F) -> FnNewServiceNoConfig +/// Create `Factory` for function that can produce services +pub fn factory_fn( + f: F, +) -> impl Factory< + Config = C, + Request = S::Request, + Response = S::Response, + Error = S::Error, + InitError = E, + Future = R::Future, +> where F: Fn() -> R, R: IntoFuture, @@ -30,7 +47,15 @@ where } /// Create `NewService` for function that can produce services with configuration -pub fn new_service_cfg(f: F) -> FnNewServiceConfig +pub fn new_service_cfg( + f: F, +) -> impl Factory< + Config = C, + Request = S::Request, + Response = S::Response, + Error = S::Error, + InitError = E, +> where F: Fn(&C) -> R, R: IntoFuture, @@ -40,7 +65,7 @@ where FnNewServiceConfig::new(f) } -pub struct ServiceFn +pub(crate) struct ServiceFn where F: FnMut(Req) -> Out, Out: IntoFuture, @@ -98,7 +123,7 @@ where } } -pub struct NewServiceFn +pub(crate) struct NewServiceFn where F: FnMut(Req) -> Out, Out: IntoFuture, @@ -127,7 +152,7 @@ where } } -impl NewService for NewServiceFn +impl Factory for NewServiceFn where F: FnMut(Req) -> Out + Clone, Out: IntoFuture, @@ -156,18 +181,18 @@ where } } -impl IntoNewService> for F +impl IntoFactory> for F where F: Fn(Req) -> Out + Clone, Out: IntoFuture, { - fn into_new_service(self) -> NewServiceFn { + fn into_factory(self) -> NewServiceFn { NewServiceFn::new(self) } } /// Convert `Fn(&Config) -> Future` fn to NewService -pub struct FnNewServiceConfig +pub(crate) struct FnNewServiceConfig where F: Fn(&C) -> R, R: IntoFuture, @@ -190,7 +215,7 @@ where } } -impl NewService for FnNewServiceConfig +impl Factory for FnNewServiceConfig where F: Fn(&C) -> R, R: IntoFuture, @@ -215,7 +240,7 @@ where } #[pin_project] -pub struct FnNewServiceConfigFut +pub(crate) struct FnNewServiceConfigFut where R: IntoFuture, R::Item: IntoService, @@ -254,7 +279,7 @@ where } /// Converter for `Fn() -> Future` fn -pub struct FnNewServiceNoConfig +pub(crate) struct FnNewServiceNoConfig where F: Fn() -> R, R: IntoFuture, @@ -275,7 +300,7 @@ where } } -impl NewService for FnNewServiceNoConfig +impl Factory for FnNewServiceNoConfig where F: Fn() -> R, R: IntoFuture, @@ -305,13 +330,13 @@ where } } -impl IntoNewService> for F +impl IntoFactory> for F where F: Fn() -> R, R: IntoFuture, S: Service, { - fn into_new_service(self) -> FnNewServiceNoConfig { + fn into_factory(self) -> FnNewServiceNoConfig { FnNewServiceNoConfig::new(self) } } diff --git a/actix-service/src/into.rs b/actix-service/src/into.rs new file mode 100644 index 00000000..b36761ad --- /dev/null +++ b/actix-service/src/into.rs @@ -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(service: U) -> ServiceMapper +where + U: IntoService, + T: Service, +{ + ServiceMapper { + service: service.into_service(), + } +} + +pub fn into_factory(factory: F) -> FactoryMapper +where + T: Factory, + F: IntoFactory, +{ + FactoryMapper { + factory: factory.into_factory(), + } +} + +pub struct ServiceMapper { + service: T, +} + +pub struct FactoryMapper { + factory: T, +} + +impl ServiceMapper { + /// 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( + self, + f: F, + ) -> ServiceMapper> + 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( + self, + f: F, + ) -> ServiceMapper> + where + Self: Sized, + F: Fn(T::Error) -> E + Clone, + { + ServiceMapper { + service: MapErr::new(self, f), + } + } +} + +impl Clone for ServiceMapper +where + T: Clone, +{ + fn clone(&self) -> Self { + ServiceMapper { + service: self.service.clone(), + } + } +} + +impl Service for ServiceMapper { + 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> { + self.service.poll_ready(ctx) + } + + #[inline] + fn call(&mut self, req: T::Request) -> Self::Future { + self.service.call(req) + } +} + +impl FactoryMapper { + /// Map this service's output to a different type, returning a new service + /// of the resulting type. + pub fn map( + 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( + 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( + 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 Clone for FactoryMapper +where + T: Clone, +{ + fn clone(&self) -> Self { + FactoryMapper { + factory: self.factory.clone(), + } + } +} + +impl Factory for FactoryMapper { + 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) + } +} diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index 25e3dfb9..db5673f7 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -10,6 +10,7 @@ mod apply_cfg; pub mod boxed; mod cell; mod fn_service; +mod into; mod map; mod map_config; mod map_err; @@ -19,15 +20,12 @@ mod then; mod transform; mod transform_err; -pub use self::apply::{apply_fn, new_apply_fn}; -pub use self::apply_cfg::{apply_cfg, new_apply_cfg}; -pub use self::fn_service::{new_service_cfg, new_service_fn, service_fn, ServiceFn}; -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::pipeline::{new_pipeline, pipeline, NewPipeline, Pipeline}; -pub use self::then::{Then, ThenNewService}; +pub use self::apply::{apply_fn, apply_fn_factory}; +pub use self::apply_cfg::{apply_cfg, apply_cfg_factory}; +pub use self::fn_service::{factory_fn, new_service_cfg, service_fn}; +pub use self::into::{into_factory, into_service, FactoryMapper, ServiceMapper}; +pub use self::map_config::{map_config, unit_config, MappedConfig}; +pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory}; pub use self::transform::{apply_transform, IntoTransform, Transform}; pub trait IntoFuture { @@ -47,22 +45,6 @@ impl>, I, E> IntoFuture for F { } } -pub fn service(factory: U) -> T -where - T: Service, - U: IntoService, -{ - factory.into_service() -} - -pub fn new_service(factory: U) -> T -where - T: NewService, - U: IntoNewService, -{ - factory.into_new_service() -} - /// An asynchronous function from `Request` to a `Response`. pub trait 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` /// values must be produced. One case is a TCP server listener. The listener /// 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. /// /// `Config` is a service factory configuration type. -pub trait NewService { +pub trait Factory { /// Requests handled by the service. type Request; @@ -218,9 +200,9 @@ where } } -impl NewService for Rc +impl Factory for Rc where - S: NewService, + S: Factory, { type Request = S::Request; type Response = S::Response; @@ -235,9 +217,9 @@ where } } -impl NewService for Arc +impl Factory for Arc where - S: NewService, + S: Factory, { type Request = S::Request; type Response = S::Response; @@ -261,13 +243,13 @@ where fn into_service(self) -> T; } -/// Trait for types that can be converted to a `NewService` -pub trait IntoNewService +/// Trait for types that can be converted to a `ServiceFactory` +pub trait IntoFactory where - T: NewService, + T: Factory, { - /// Convert to an `NewService` - fn into_new_service(self) -> T; + /// Convert `Self` an `ServiceFactory` + fn into_factory(self) -> T; } impl IntoService for T @@ -279,11 +261,11 @@ where } } -impl IntoNewService for T +impl IntoFactory for T where - T: NewService, + T: Factory, { - fn into_new_service(self) -> T { + fn into_factory(self) -> T { self } } diff --git a/actix-service/src/map.rs b/actix-service/src/map.rs index abc83bff..e42ac81f 100644 --- a/actix-service/src/map.rs +++ b/actix-service/src/map.rs @@ -5,12 +5,12 @@ use std::task::{Context, Poll}; 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. /// /// This is created by the `ServiceExt::map` method. -pub struct Map { +pub(crate) struct Map { service: A, f: F, _t: PhantomData, @@ -65,7 +65,7 @@ where } #[pin_project] -pub struct MapFuture +pub(crate) struct MapFuture where A: Service, F: FnMut(A::Response) -> Response, @@ -103,7 +103,7 @@ where } /// `MapNewService` new service combinator -pub struct MapNewService { +pub(crate) struct MapNewService { a: A, f: F, r: PhantomData, @@ -113,7 +113,7 @@ impl MapNewService { /// Create new `Map` new service instance pub fn new(a: A, f: F) -> Self where - A: NewService, + A: Factory, F: FnMut(A::Response) -> Res, { Self { @@ -138,9 +138,9 @@ where } } -impl NewService for MapNewService +impl Factory for MapNewService where - A: NewService, + A: Factory, F: FnMut(A::Response) -> Res + Clone, { type Request = A::Request; @@ -158,9 +158,9 @@ where } #[pin_project] -pub struct MapNewServiceFuture +pub(crate) struct MapNewServiceFuture where - A: NewService, + A: Factory, F: FnMut(A::Response) -> Res, { #[pin] @@ -170,7 +170,7 @@ where impl MapNewServiceFuture where - A: NewService, + A: Factory, F: FnMut(A::Response) -> Res, { fn new(fut: A::Future, f: F) -> Self { @@ -180,7 +180,7 @@ where impl Future for MapNewServiceFuture where - A: NewService, + A: Factory, F: FnMut(A::Response) -> Res, { type Output = Result, A::InitError>; @@ -200,7 +200,7 @@ mod tests { use futures::future::{ok, Ready}; use super::*; - use crate::{IntoNewService, Service}; + use crate::{IntoFactory, Service}; struct Srv; diff --git a/actix-service/src/map_config.rs b/actix-service/src/map_config.rs index 16830fc0..912a30bd 100644 --- a/actix-service/src/map_config.rs +++ b/actix-service/src/map_config.rs @@ -1,14 +1,48 @@ use std::marker::PhantomData; -use super::NewService; +use super::Factory; pub enum MappedConfig<'a, T> { Ref(&'a T), Owned(T), } +/// Adapt external config to a config for provided new service +pub fn map_config( + 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, +{ + MapConfig::new(new_service, f) +} + +/// Replace config with unit +pub fn unit_config( + new_service: T, +) -> impl Factory< + Config = C, + Request = T::Request, + Response = T::Response, + Error = T::Error, + InitError = T::InitError, +> +where + T: Factory, +{ + UnitConfig::new(new_service) +} + /// `MapInitErr` service combinator -pub struct MapConfig { +pub(crate) struct MapConfig { a: A, f: F, e: PhantomData, @@ -18,7 +52,7 @@ impl MapConfig { /// Create new `MapConfig` combinator pub fn new(a: A, f: F) -> Self where - A: NewService, + A: Factory, F: Fn(&C) -> MappedConfig, { Self { @@ -43,9 +77,9 @@ where } } -impl NewService for MapConfig +impl Factory for MapConfig where - A: NewService, + A: Factory, F: Fn(&C) -> MappedConfig, { type Request = A::Request; @@ -66,17 +100,17 @@ where } /// `MapInitErr` service combinator -pub struct UnitConfig { +pub(crate) struct UnitConfig { a: A, e: PhantomData, } -impl UnitConfig { +impl UnitConfig +where + A: Factory, +{ /// Create new `UnitConfig` combinator - pub fn new(a: A) -> Self - where - A: NewService, - { + pub(crate) fn new(a: A) -> Self { Self { a, e: PhantomData } } } @@ -93,9 +127,9 @@ where } } -impl NewService for UnitConfig +impl Factory for UnitConfig where - A: NewService, + A: Factory, { type Request = A::Request; type Response = A::Response; diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index fd51d202..b7f2dd24 100644 --- a/actix-service/src/map_err.rs +++ b/actix-service/src/map_err.rs @@ -5,13 +5,13 @@ use std::task::{Context, Poll}; 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 /// error. /// /// This is created by the `ServiceExt::map_err` method. -pub struct MapErr { +pub(crate) struct MapErr { service: A, f: F, _t: PhantomData, @@ -66,7 +66,7 @@ where } #[pin_project] -pub struct MapErrFuture +pub(crate) struct MapErrFuture where A: Service, 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. /// /// This is created by the `NewServiceExt::map_err` method. -pub struct MapErrNewService +pub(crate) struct MapErrNewService where - A: NewService, + A: Factory, F: Fn(A::Error) -> E + Clone, { a: A, @@ -115,11 +115,11 @@ where impl MapErrNewService where - A: NewService, + A: Factory, F: Fn(A::Error) -> E + Clone, { /// Create new `MapErr` new service instance - pub fn new(a: A, f: F) -> Self { + pub(crate) fn new(a: A, f: F) -> Self { Self { a, f, @@ -130,7 +130,7 @@ where impl Clone for MapErrNewService where - A: NewService + Clone, + A: Factory + Clone, F: Fn(A::Error) -> E + Clone, { fn clone(&self) -> Self { @@ -142,9 +142,9 @@ where } } -impl NewService for MapErrNewService +impl Factory for MapErrNewService where - A: NewService, + A: Factory, F: Fn(A::Error) -> E + Clone, { type Request = A::Request; @@ -162,9 +162,9 @@ where } #[pin_project] -pub struct MapErrNewServiceFuture +pub(crate) struct MapErrNewServiceFuture where - A: NewService, + A: Factory, F: Fn(A::Error) -> E, { #[pin] @@ -174,7 +174,7 @@ where impl MapErrNewServiceFuture where - A: NewService, + A: Factory, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -184,7 +184,7 @@ where impl Future for MapErrNewServiceFuture where - A: NewService, + A: Factory, F: Fn(A::Error) -> E + Clone, { type Output = Result, A::InitError>; @@ -201,28 +201,28 @@ where #[cfg(test)] mod tests { - use futures::future::{err, Ready}; + // use futures::future::{err, Ready}; - use super::*; - use crate::{IntoNewService, NewService, Service}; - use tokio::future::ok; + // use super::*; + // use crate::{IntoNewService, NewService, Service}; + // use tokio::future::ok; - struct Srv; + // struct Srv; - impl Service for Srv { - type Request = (); - type Response = (); - type Error = (); - type Future = Ready>; + // impl Service for Srv { + // type Request = (); + // type Response = (); + // type Error = (); + // type Future = Ready>; - fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { - Poll::Ready(Err(())) - } + // fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { + // Poll::Ready(Err(())) + // } - fn call(&mut self, _: ()) -> Self::Future { - err(()) - } - } + // fn call(&mut self, _: ()) -> Self::Future { + // err(()) + // } + // } // #[tokio::test] // async fn test_poll_ready() { diff --git a/actix-service/src/map_init_err.rs b/actix-service/src/map_init_err.rs index 9adafeb3..52e870dc 100644 --- a/actix-service/src/map_init_err.rs +++ b/actix-service/src/map_init_err.rs @@ -5,22 +5,22 @@ use std::task::{Context, Poll}; use pin_project::pin_project; -use super::NewService; +use super::Factory; /// `MapInitErr` service combinator -pub struct MapInitErr { +pub(crate) struct MapInitErr { a: A, f: F, e: PhantomData, } -impl MapInitErr { +impl MapInitErr +where + A: Factory, + F: Fn(A::InitError) -> E, +{ /// Create new `MapInitErr` combinator - pub fn new(a: A, f: F) -> Self - where - A: NewService, - F: Fn(A::InitError) -> E, - { + pub(crate) fn new(a: A, f: F) -> Self { Self { a, f, @@ -43,9 +43,9 @@ where } } -impl NewService for MapInitErr +impl Factory for MapInitErr where - A: NewService, + A: Factory, F: Fn(A::InitError) -> E + Clone, { type Request = A::Request; @@ -62,9 +62,9 @@ where } } #[pin_project] -pub struct MapInitErrFuture +pub(crate) struct MapInitErrFuture where - A: NewService, + A: Factory, F: Fn(A::InitError) -> E, { f: F, @@ -74,7 +74,7 @@ where impl MapInitErrFuture where - A: NewService, + A: Factory, F: Fn(A::InitError) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -84,7 +84,7 @@ where impl Future for MapInitErrFuture where - A: NewService, + A: Factory, F: Fn(A::InitError) -> E, { type Output = Result; diff --git a/actix-service/src/pipeline.rs b/actix-service/src/pipeline.rs index 42d7b151..8e033b6c 100644 --- a/actix-service/src/pipeline.rs +++ b/actix-service/src/pipeline.rs @@ -2,7 +2,7 @@ use std::task::{Context, Poll}; use crate::and_then::{AndThen, AndThenNewService}; use crate::then::{Then, ThenNewService}; -use crate::{IntoNewService, IntoService, NewService, Service}; +use crate::{Factory, IntoFactory, IntoService, Service}; pub fn pipeline(service: F) -> Pipeline where @@ -14,13 +14,13 @@ where } } -pub fn new_pipeline(new_service: F) -> NewPipeline +pub fn pipeline_factory(factory: F) -> PipelineFactory where - F: IntoNewService, - T: NewService, + T: Factory, + F: IntoFactory, { - NewPipeline { - service: new_service.into_new_service(), + PipelineFactory { + factory: factory.into_factory(), } } @@ -102,25 +102,36 @@ impl Service for Pipeline { } /// Pipeline constructor -pub struct NewPipeline { - service: T, +pub struct PipelineFactory { + factory: T, } -impl NewPipeline { +impl PipelineFactory { /// Call another service after call to this one has resolved successfully. - pub fn and_then(self, new_service: U) -> NewPipeline> + pub fn and_then( + self, + factory: U, + ) -> PipelineFactory< + impl Factory< + Config = T::Config, + Request = T::Request, + Response = U::Response, + Error = T::Error, + InitError = T::InitError, + >, + > where Self: Sized, - F: IntoNewService, - U: NewService< + F: IntoFactory, + U: Factory< Config = T::Config, Request = T::Response, Error = T::Error, InitError = T::InitError, >, { - NewPipeline { - service: AndThenNewService::new(self.service, new_service.into_new_service()), + PipelineFactory { + factory: AndThenNewService::new(self.factory, factory.into_factory()), } } @@ -130,35 +141,46 @@ impl NewPipeline { /// /// Note that this function consumes the receiving pipeline and returns a /// wrapped version of it. - pub fn then(self, new_service: F) -> NewPipeline> + pub fn then( + self, + factory: F, + ) -> PipelineFactory< + impl Factory< + Config = T::Config, + Request = T::Request, + Response = U::Response, + Error = T::Error, + InitError = T::InitError, + >, + > where Self: Sized, - F: IntoNewService, - U: NewService< + F: IntoFactory, + U: Factory< Config = T::Config, Request = Result, Error = T::Error, InitError = T::InitError, >, { - NewPipeline { - service: ThenNewService::new(self.service, new_service.into_new_service()), + PipelineFactory { + factory: ThenNewService::new(self.factory, factory.into_factory()), } } } -impl Clone for NewPipeline +impl Clone for PipelineFactory where T: Clone, { fn clone(&self) -> Self { - NewPipeline { - service: self.service.clone(), + PipelineFactory { + factory: self.factory.clone(), } } } -impl NewService for NewPipeline { +impl Factory for PipelineFactory { type Config = T::Config; type Request = T::Request; type Response = T::Response; @@ -169,6 +191,6 @@ impl NewService for NewPipeline { #[inline] fn new_service(&self, cfg: &T::Config) -> Self::Future { - self.service.new_service(cfg) + self.factory.new_service(cfg) } } diff --git a/actix-service/src/then.rs b/actix-service/src/then.rs index 7093358f..bb6fb2f4 100644 --- a/actix-service/src/then.rs +++ b/actix-service/src/then.rs @@ -4,14 +4,14 @@ use std::task::{Context, Poll}; use pin_project::pin_project; -use super::{IntoNewService, NewService, Service}; +use super::{Factory, Service}; use crate::cell::Cell; /// Service for the `then` combinator, chaining a computation onto the end of /// another service. /// /// This is created by the `ServiceExt::then` method. -pub struct Then { +pub(crate) struct Then { a: A, b: Cell, } @@ -64,7 +64,7 @@ where } #[pin_project] -pub struct ThenFuture +pub(crate) struct ThenFuture where A: Service, B: Service>, @@ -126,36 +126,32 @@ where } } -/// `ThenNewService` new service combinator -pub struct ThenNewService { +/// `.then()` service factory combinator +pub(crate) struct ThenNewService { a: A, b: B, } -impl ThenNewService { +impl ThenNewService +where + A: Factory, + B: Factory< + Config = A::Config, + Request = Result, + Error = A::Error, + InitError = A::InitError, + >, +{ /// Create new `AndThen` combinator - pub fn new(a: A, f: F) -> Self - where - A: NewService, - B: NewService< - Config = A::Config, - Request = Result, - Error = A::Error, - InitError = A::InitError, - >, - F: IntoNewService, - { - Self { - a, - b: f.into_new_service(), - } + pub fn new(a: A, b: B) -> Self { + Self { a, b } } } -impl NewService for ThenNewService +impl Factory for ThenNewService where - A: NewService, - B: NewService< + A: Factory, + B: Factory< Config = A::Config, Request = Result, Error = A::Error, @@ -190,10 +186,10 @@ where } #[pin_project] -pub struct ThenNewServiceFuture +pub(crate) struct ThenNewServiceFuture where - A: NewService, - B: NewService< + A: Factory, + B: Factory< Config = A::Config, Request = Result, Error = A::Error, @@ -210,8 +206,8 @@ where impl ThenNewServiceFuture where - A: NewService, - B: NewService< + A: Factory, + B: Factory< Config = A::Config, Request = Result, Error = A::Error, @@ -230,8 +226,8 @@ where impl Future for ThenNewServiceFuture where - A: NewService, - B: NewService< + A: Factory, + B: Factory< Config = A::Config, Request = Result, Error = A::Error, @@ -271,7 +267,7 @@ mod tests { 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)] struct Srv1(Rc>); @@ -340,13 +336,12 @@ mod tests { } #[tokio::test] - async fn test_new_service() { + async fn test_factory() { let cnt = Rc::new(Cell::new(0)); let cnt2 = cnt.clone(); let blank = move || ready(Ok::<_, ()>(Srv1(cnt2.clone()))); - let new_srv = - new_pipeline(new_service(blank)).then(move || ready(Ok(Srv2(cnt.clone())))); - let mut srv = new_srv.clone().new_service(&()).await.unwrap(); + let factory = pipeline_factory(blank).then(move || ready(Ok(Srv2(cnt.clone())))); + let mut srv = factory.new_service(&()).await.unwrap(); let res = srv.call(Ok("srv1")).await; assert!(res.is_ok()); assert_eq!(res.unwrap(), (("srv1", "ok"))); diff --git a/actix-service/src/transform.rs b/actix-service/src/transform.rs index 8c399c74..d2144c34 100644 --- a/actix-service/src/transform.rs +++ b/actix-service/src/transform.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use std::task::{Context, Poll}; use crate::transform_err::{TransformFromErr, TransformMapInitErr}; -use crate::{IntoNewService, NewService, Service}; +use crate::{Factory, IntoFactory, Service}; use pin_project::pin_project; @@ -133,7 +133,7 @@ where pub fn apply_transform( t: F, service: U, -) -> impl NewService< +) -> impl Factory< Config = S::Config, Request = T::Request, Response = T::Response, @@ -142,12 +142,12 @@ pub fn apply_transform( InitError = S::InitError, > + Clone where - S: NewService, + S: Factory, T: Transform, F: IntoTransform, - U: IntoNewService, + U: IntoFactory, { - ApplyTransform::new(t.into_transform(), service.into_new_service()) + ApplyTransform::new(t.into_transform(), service.into_factory()) } /// `Apply` transform to new service @@ -158,7 +158,7 @@ pub struct ApplyTransform { impl ApplyTransform where - S: NewService, + S: Factory, T: Transform, { /// Create new `ApplyTransform` new service instance @@ -179,9 +179,9 @@ impl Clone for ApplyTransform { } } -impl NewService for ApplyTransform +impl Factory for ApplyTransform where - S: NewService, + S: Factory, T: Transform, { type Request = T::Request; @@ -204,7 +204,7 @@ where #[pin_project] pub struct ApplyTransformFuture where - S: NewService, + S: Factory, T: Transform, { #[pin] @@ -216,7 +216,7 @@ where impl Future for ApplyTransformFuture where - S: NewService, + S: Factory, T: Transform, { type Output = Result;