diff --git a/actix-connector/src/connector.rs b/actix-connector/src/connector.rs index b274586d..e3c12cc2 100644 --- a/actix-connector/src/connector.rs +++ b/actix-connector/src/connector.rs @@ -4,8 +4,8 @@ use std::net::{IpAddr, SocketAddr}; use std::time::Duration; use std::{fmt, io}; -use actix_service::{NewService, Service}; -use futures::future::{ok, Either, FutureResult}; +use actix_service::{fn_factory, NewService, Service}; +use futures::future::{ok, Either}; use futures::{try_ready, Async, Future, Poll}; use tokio_tcp::{ConnectFuture, TcpStream}; use trust_dns_resolver::config::{ResolverConfig, ResolverOpts}; @@ -183,7 +183,7 @@ impl Connector { Error = ConnectorError, InitError = E, > + Clone { - move |_: &()| -> FutureResult { ok(Connector::new(cfg.clone(), opts)) } + fn_factory(move || ok(Connector::new(cfg.clone(), opts))) } } diff --git a/actix-service/Cargo.toml b/actix-service/Cargo.toml index cb58980f..5a06e33a 100644 --- a/actix-service/Cargo.toml +++ b/actix-service/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-service" -version = "0.3.0" +version = "0.2.2" authors = ["Nikolay Kim "] description = "Actix Service" keywords = ["network", "framework", "async", "futures"] diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 9bdedcd0..ec2292b5 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -5,6 +5,35 @@ use futures::{Async, IntoFuture, Poll}; use super::{IntoNewService, IntoService, NewService, Service}; +/// Create `NewService` for function that can act as Service +pub fn fn_service(f: F) -> FnNewService +where + F: FnMut(Req) -> Out + Clone, + Out: IntoFuture, +{ + FnNewService::new(f) +} + +/// Create `NewService` for function that can produce services +pub fn fn_factory(f: F) -> FnNewServiceNoConfig +where + F: Fn() -> R, + R: IntoFuture, + S: Service, +{ + FnNewServiceNoConfig::new(f) +} + +/// Create `NewService` for function that can produce services with configuration +pub fn fn_cfg_factory(f: F) -> FnNewServiceConfig +where + F: Fn(&C) -> R, + R: IntoFuture, + S: Service, +{ + FnNewServiceConfig::new(f) +} + pub struct FnService where F: FnMut(Req) -> Out, @@ -66,15 +95,6 @@ where } } -/// Create `NewService` for function that can act as Service -pub fn fn_nservice(f: F) -> FnNewService -where - F: FnMut(Req) -> Out + Clone, - Out: IntoFuture, -{ - FnNewService::new(f) -} - pub struct FnNewService where F: FnMut(Req) -> Out, @@ -125,7 +145,7 @@ where /// Converter for `Fn() -> Future` fn pub struct FnNewServiceNoConfig where - F: Fn() -> R + Clone, + F: Fn() -> R, R: IntoFuture, S: Service, { @@ -134,7 +154,7 @@ where impl FnNewServiceNoConfig where - F: Fn() -> R + Clone, + F: Fn() -> R, R: IntoFuture, S: Service, { @@ -145,7 +165,7 @@ where impl NewService<()> for FnNewServiceNoConfig where - F: Fn() -> R + Clone, + F: Fn() -> R, R: IntoFuture, S: Service, { @@ -175,7 +195,7 @@ where impl IntoNewService, ()> for F where - F: Fn() -> R + Clone, + F: Fn() -> R, R: IntoFuture, S: Service, { @@ -183,3 +203,66 @@ where FnNewServiceNoConfig::new(self) } } + +/// Convert `Fn(&Config) -> Future` fn to NewService +pub struct FnNewServiceConfig +where + F: Fn(&C) -> R, + R: IntoFuture, + S: Service, +{ + f: F, + _t: PhantomData<(C, R, S, E)>, +} + +impl FnNewServiceConfig +where + F: Fn(&C) -> R, + R: IntoFuture, + S: Service, +{ + pub fn new(f: F) -> Self { + FnNewServiceConfig { f, _t: PhantomData } + } +} + +impl NewService for FnNewServiceConfig +where + F: Fn(&C) -> R, + R: IntoFuture, + 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, cfg: &C) -> Self::Future { + (self.f)(cfg).into_future() + } +} + +impl Clone for FnNewServiceConfig +where + F: Fn(&C) -> R + Clone, + R: IntoFuture, + S: Service, +{ + fn clone(&self) -> Self { + Self::new(self.f.clone()) + } +} + +impl IntoNewService, C> for F +where + F: Fn(&C) -> R, + R: IntoFuture, + S: Service, +{ + fn into_new_service(self) -> FnNewServiceConfig { + FnNewServiceConfig::new(self) + } +} diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index c1a94479..3e5cd52a 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -24,7 +24,7 @@ use self::and_then_apply::{AndThenTransform, AndThenTransformNewService}; use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService}; pub use self::apply::{Apply, ApplyNewService}; pub use self::blank::{Blank, BlankNewService}; -pub use self::fn_service::{fn_nservice, FnNewService, FnService}; +pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService}; pub use self::fn_transform::{FnNewTransform, FnTransform}; pub use self::from_err::{FromErr, FromErrNewService}; pub use self::map::{Map, MapNewService}; @@ -370,24 +370,6 @@ where } } -impl NewService for F -where - F: Fn(&C) -> R, - R: IntoFuture, - 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, cfg: &C) -> Self::Future { - (*self)(cfg).into_future() - } -} - impl NewService for Rc where S: NewService,