diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 83b602fc..14db4dea 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -3,7 +3,7 @@ use std::marker::PhantomData; use futures::future::{ok, FutureResult}; use futures::{Async, IntoFuture, Poll}; -use super::{IntoService, NewService, Service}; +use super::{IntoNewService, IntoService, NewService, Service}; pub struct FnService where @@ -112,3 +112,64 @@ where Self::new(self.f.clone()) } } + +impl IntoNewService, ()> for F +where + F: Fn() -> R + Clone, + R: IntoFuture, + S: Service, +{ + fn into_new_service(self) -> FnNewServiceNoConfig { + FnNewServiceNoConfig::new(self) + } +} + +pub struct FnNewServiceNoConfig +where + F: Fn() -> R + Clone, + R: IntoFuture, + S: Service, +{ + f: F, +} + +impl FnNewServiceNoConfig +where + F: Fn() -> R + Clone, + R: IntoFuture, + S: Service, +{ + pub fn new(f: F) -> Self { + FnNewServiceNoConfig { f } + } +} + +impl NewService<()> for FnNewServiceNoConfig +where + F: Fn() -> R + Clone, + 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, _: &()) -> Self::Future { + (self.f)().into_future() + } +} + +impl Clone for FnNewServiceNoConfig +where + F: Fn() -> R + Clone, + R: IntoFuture, + S: Service, +{ + fn clone(&self) -> Self { + Self::new(self.f.clone()) + } +} diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index cd15d239..4e519976 100644 --- a/actix-service/src/map_err.rs +++ b/actix-service/src/map_err.rs @@ -229,7 +229,7 @@ mod tests { #[test] fn test_new_service() { - let blank = || Ok::<_, ()>(Srv); + let blank = |_: &()| Ok::<_, ()>(Srv); let new_srv = blank.into_new_service().map_err(|_| "error"); if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() { let res = srv.call(()).poll(); diff --git a/actix-utils/src/inflight.rs b/actix-utils/src/inflight.rs index e714c422..934a0659 100644 --- a/actix-utils/src/inflight.rs +++ b/actix-utils/src/inflight.rs @@ -142,7 +142,7 @@ mod tests { let srv = BlankNewService::new().apply(InFlight::new(1), || Ok(SleepService(wait_time))); - if let Async::Ready(mut srv) = srv.new_service().poll().unwrap() { + if let Async::Ready(mut srv) = srv.new_service(&()).poll().unwrap() { assert_eq!(srv.poll_ready(), Ok(Async::Ready(()))); let mut res = srv.call(()); diff --git a/actix-utils/src/timeout.rs b/actix-utils/src/timeout.rs index 49689030..9721156a 100644 --- a/actix-utils/src/timeout.rs +++ b/actix-utils/src/timeout.rs @@ -223,7 +223,7 @@ mod tests { let res = actix_rt::System::new("test").block_on(lazy(|| { let timeout = BlankNewService::<_, _, ()>::default() .apply(Timeout::new(resolution), || Ok(SleepService(wait_time))); - if let Async::Ready(mut to) = timeout.new_service().poll().unwrap() { + if let Async::Ready(mut to) = timeout.new_service(&()).poll().unwrap() { to.call(()) } else { panic!()