diff --git a/actix-connect/tests/test_connect.rs b/actix-connect/tests/test_connect.rs index 994daeb1..4e9fb40a 100644 --- a/actix-connect/tests/test_connect.rs +++ b/actix-connect/tests/test_connect.rs @@ -2,7 +2,7 @@ use std::io; use actix_codec::{BytesCodec, Framed}; use actix_rt::net::TcpStream; -use actix_service::{service_fn, Service, ServiceFactory}; +use actix_service::{fn_service, Service, ServiceFactory}; use actix_testing::TestServer; use bytes::Bytes; use futures::SinkExt; @@ -14,7 +14,7 @@ use actix_connect::Connect; #[actix_rt::test] async fn test_string() { let srv = TestServer::with(|| { - service_fn(|io: TcpStream| { + fn_service(|io: TcpStream| { async { let mut framed = Framed::new(io, BytesCodec); framed.send(Bytes::from_static(b"test")).await?; @@ -33,7 +33,7 @@ async fn test_string() { #[actix_rt::test] async fn test_rustls_string() { let srv = TestServer::with(|| { - service_fn(|io: TcpStream| { + fn_service(|io: TcpStream| { async { let mut framed = Framed::new(io, BytesCodec); framed.send(Bytes::from_static(b"test")).await?; @@ -51,7 +51,7 @@ async fn test_rustls_string() { #[actix_rt::test] async fn test_static_str() { let srv = TestServer::with(|| { - service_fn(|io: TcpStream| { + fn_service(|io: TcpStream| { async { let mut framed = Framed::new(io, BytesCodec); framed.send(Bytes::from_static(b"test")).await?; @@ -75,7 +75,7 @@ async fn test_static_str() { #[actix_rt::test] async fn test_new_service() { let srv = TestServer::with(|| { - service_fn(|io: TcpStream| { + fn_service(|io: TcpStream| { async { let mut framed = Framed::new(io, BytesCodec); framed.send(Bytes::from_static(b"test")).await?; @@ -100,7 +100,7 @@ async fn test_uri() { use std::convert::TryFrom; let srv = TestServer::with(|| { - service_fn(|io: TcpStream| { + fn_service(|io: TcpStream| { async { let mut framed = Framed::new(io, BytesCodec); framed.send(Bytes::from_static(b"test")).await?; @@ -121,7 +121,7 @@ async fn test_rustls_uri() { use std::convert::TryFrom; let srv = TestServer::with(|| { - service_fn(|io: TcpStream| { + fn_service(|io: TcpStream| { async { let mut framed = Framed::new(io, BytesCodec); framed.send(Bytes::from_static(b"test")).await?; diff --git a/actix-ioframe/tests/test_server.rs b/actix-ioframe/tests/test_server.rs index 7a0e55c4..81da99c0 100644 --- a/actix-ioframe/tests/test_server.rs +++ b/actix-ioframe/tests/test_server.rs @@ -4,7 +4,7 @@ use std::time::Duration; use actix_codec::BytesCodec; use actix_rt::time::delay_for; -use actix_service::{service_fn, Service}; +use actix_service::{fn_service, Service}; use actix_testing::TestServer; use futures::future::ok; @@ -22,13 +22,13 @@ async fn test_disconnect() -> std::io::Result<()> { let disconnect1 = disconnect1.clone(); Builder::new() - .factory(service_fn(|conn: Connect<_>| { + .factory(fn_service(|conn: Connect<_>| { ok(conn.codec(BytesCodec).state(State)) })) .disconnect(move |_, _| { disconnect1.store(true, Ordering::Relaxed); }) - .finish(service_fn(|_t| ok(None))) + .finish(fn_service(|_t| ok(None))) }); let mut client = Builder::new() @@ -37,7 +37,7 @@ async fn test_disconnect() -> std::io::Result<()> { conn.sink().close(); ok(conn) }) - .finish(service_fn(|_t| ok(None))); + .finish(fn_service(|_t| ok(None))); let conn = actix_connect::default_connector() .call(actix_connect::Connect::with(String::new(), srv.addr())) diff --git a/actix-server/src/config.rs b/actix-server/src/config.rs index df5fa805..15352175 100644 --- a/actix-server/src/config.rs +++ b/actix-server/src/config.rs @@ -142,7 +142,7 @@ impl InternalServiceFactory for ConfiguredService { let name = names.remove(&token).unwrap().0; res.push(( token, - Box::new(StreamService::new(actix::service_fn2( + Box::new(StreamService::new(actix::fn_service( move |_: TcpStream| { error!("Service {:?} is not configured", name); ok::<_, ()>(()) diff --git a/actix-server/tests/test_server.rs b/actix-server/tests/test_server.rs index f8629a2b..a1cf1928 100644 --- a/actix-server/tests/test_server.rs +++ b/actix-server/tests/test_server.rs @@ -6,7 +6,7 @@ use std::{net, thread, time}; use actix_codec::{BytesCodec, Framed}; use actix_rt::net::TcpStream; use actix_server::Server; -use actix_service::service_fn; +use actix_service::fn_service; use bytes::Bytes; use futures::future::{lazy, ok}; use futures::SinkExt; @@ -31,7 +31,7 @@ fn test_bind() { let srv = Server::build() .workers(1) .disable_signals() - .bind("test", addr, move || service_fn(|_| ok::<_, ()>(()))) + .bind("test", addr, move || fn_service(|_| ok::<_, ()>(()))) .unwrap() .start(); let _ = tx.send((srv, actix_rt::System::current())); @@ -56,7 +56,7 @@ fn test_listen() { Server::build() .disable_signals() .workers(1) - .listen("test", lst, move || service_fn(|_| ok::<_, ()>(()))) + .listen("test", lst, move || fn_service(|_| ok::<_, ()>(()))) .unwrap() .start(); let _ = tx.send(actix_rt::System::current()); @@ -82,7 +82,7 @@ fn test_start() { .backlog(100) .disable_signals() .bind("test", addr, move || { - service_fn(|io: TcpStream| { + fn_service(|io: TcpStream| { async move { let mut f = Framed::new(io, BytesCodec); f.send(Bytes::from_static(b"test")).await.unwrap(); @@ -158,8 +158,8 @@ fn test_configure() { .listen("addr3", lst) .apply(move |rt| { let num = num.clone(); - rt.service("addr1", service_fn(|_| ok::<_, ()>(()))); - rt.service("addr3", service_fn(|_| ok::<_, ()>(()))); + rt.service("addr1", fn_service(|_| ok::<_, ()>(()))); + rt.service("addr3", fn_service(|_| ok::<_, ()>(()))); rt.on_start(lazy(move |_| { let _ = num.fetch_add(1, Relaxed); })) diff --git a/actix-service/CHANGES.md b/actix-service/CHANGES.md index 9af9cfdf..8a7aadba 100644 --- a/actix-service/CHANGES.md +++ b/actix-service/CHANGES.md @@ -1,5 +1,16 @@ # Changes +## [1.0.0-alpha.4] - 2019-12-xx + +### Changed + +* Renamed `service_fn` to `fn_service` + +* Renamed `factory_fn` to `fn_factory` + +* Renamed `factory_fn_cfg` to `fn_factory_with_config` + + ## [1.0.0-alpha.3] - 2019-12-06 ### Changed diff --git a/actix-service/src/and_then.rs b/actix-service/src/and_then.rs index 690d0cb4..44f6101d 100644 --- a/actix-service/src/and_then.rs +++ b/actix-service/src/and_then.rs @@ -258,7 +258,7 @@ mod tests { use futures_util::future::{lazy, ok, ready, Ready}; - use crate::{factory_fn, pipeline, pipeline_factory, Service, ServiceFactory}; + use crate::{fn_factory, pipeline, pipeline_factory, Service, ServiceFactory}; struct Srv1(Rc>); @@ -320,7 +320,7 @@ mod tests { let cnt = Rc::new(Cell::new(0)); let cnt2 = cnt.clone(); let new_srv = - pipeline_factory(factory_fn(move || ready(Ok::<_, ()>(Srv1(cnt2.clone()))))) + pipeline_factory(fn_factory(move || ready(Ok::<_, ()>(Srv1(cnt2.clone()))))) .and_then(move || ready(Ok(Srv2(cnt.clone())))); let mut srv = new_srv.new_service(()).await.unwrap(); diff --git a/actix-service/src/and_then_apply_fn.rs b/actix-service/src/and_then_apply_fn.rs index 2ac7474d..b0aba072 100644 --- a/actix-service/src/and_then_apply_fn.rs +++ b/actix-service/src/and_then_apply_fn.rs @@ -283,7 +283,7 @@ mod tests { use futures_util::future::{lazy, ok, Ready, TryFutureExt}; - use crate::{pipeline, pipeline_factory, service_fn2, Service, ServiceFactory}; + use crate::{fn_service, pipeline, pipeline_factory, Service, ServiceFactory}; #[derive(Clone)] struct Srv; @@ -318,7 +318,7 @@ mod tests { #[actix_rt::test] async fn test_service_factory() { - let new_srv = pipeline_factory(|| ok::<_, ()>(service_fn2(|r: &'static str| ok(r)))) + let new_srv = pipeline_factory(|| ok::<_, ()>(fn_service(|r: &'static str| ok(r)))) .and_then_apply_fn( || ok(Srv), |req: &'static str, s| s.call(()).map_ok(move |res| (req, res)), diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index c3d47d00..698cd052 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -7,7 +7,7 @@ use futures_util::future::{ok, Ready}; use crate::{IntoService, IntoServiceFactory, Service, ServiceFactory}; /// Create `ServiceFactory` for function that can act as a `Service` -pub fn service_fn( +pub fn fn_service( f: F, ) -> FnServiceFactory where @@ -17,16 +17,8 @@ where FnServiceFactory::new(f) } -pub fn service_fn2(f: F) -> FnService -where - F: FnMut(Req) -> Fut, - Fut: Future>, -{ - FnService::new(f) -} - /// Create `ServiceFactory` for function that can produce services -pub fn factory_fn(f: F) -> FnServiceNoConfig +pub fn fn_factory(f: F) -> FnServiceNoConfig where Srv: Service, F: Fn() -> Fut, @@ -35,8 +27,10 @@ where FnServiceNoConfig::new(f) } -/// Create `ServiceFactory` for function that can produce services with configuration -pub fn factory_fn_cfg(f: F) -> FnServiceConfig +/// Create `ServiceFactory` for function that accepts config and can produce services +pub fn fn_factory_with_config( + f: F, +) -> FnServiceConfig where F: Fn(Cfg) -> Fut, Fut: Future>, @@ -132,6 +126,25 @@ where } } +impl Service for FnServiceFactory +where + F: FnMut(Req) -> Fut + Clone, + Fut: Future>, +{ + type Request = Req; + type Response = Res; + type Error = Err; + type Future = Fut; + + fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, req: Self::Request) -> Self::Future { + (self.f)(req) + } +} + impl ServiceFactory for FnServiceFactory where F: FnMut(Req) -> Fut + Clone, @@ -280,3 +293,47 @@ where FnServiceNoConfig::new(self) } } + +#[cfg(test)] +mod tests { + use std::task::Poll; + + use futures_util::future::{lazy, ok}; + + use super::*; + use crate::{Service, ServiceFactory}; + + #[actix_rt::test] + async fn test_fn_service() { + let new_srv = fn_service(|()| ok::<_, ()>("srv")); + + let mut srv = new_srv.new_service(()).await.unwrap(); + let res = srv.call(()).await; + assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(()))); + assert!(res.is_ok()); + assert_eq!(res.unwrap(), "srv"); + } + + #[actix_rt::test] + async fn test_fn_service_service() { + let mut srv = fn_service(|()| ok::<_, ()>("srv")); + + let res = srv.call(()).await; + assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(()))); + assert!(res.is_ok()); + assert_eq!(res.unwrap(), "srv"); + } + + #[actix_rt::test] + async fn test_fn_service_with_config() { + let new_srv = fn_factory_with_config(|cfg: usize| { + ok::<_, ()>(fn_service(move |()| ok::<_, ()>(("srv", cfg)))) + }); + + let mut srv = new_srv.new_service(1).await.unwrap(); + let res = srv.call(()).await; + assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(()))); + assert!(res.is_ok()); + assert_eq!(res.unwrap(), ("srv", 1)); + } +} diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index bd127063..bcd2dd06 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -25,7 +25,7 @@ mod transform_err; 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, factory_fn_cfg, service_fn, service_fn2}; +pub use self::fn_service::{fn_factory, fn_factory_with_config, fn_service}; pub use self::map_config::{map_config, unit_config}; pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory}; pub use self::transform::{apply, Transform}; diff --git a/actix-testing/src/lib.rs b/actix-testing/src/lib.rs index 42452d73..4fd68f9a 100644 --- a/actix-testing/src/lib.rs +++ b/actix-testing/src/lib.rs @@ -20,12 +20,12 @@ pub use actix_macros::test; /// # Examples /// /// ```rust -/// use actix_service::{service_fn}; +/// use actix_service::fn_service; /// use actix_testing::TestServer; /// /// #[actix_rt::main] /// async fn main() { -/// let srv = TestServer::with(|| service_fn( +/// let srv = TestServer::with(|| fn_service( /// |sock| async move { /// println!("New connection: {:?}", sock); /// Ok::<_, ()>(()) diff --git a/actix-utils/src/inflight.rs b/actix-utils/src/inflight.rs index c64c7b56..3f547db2 100644 --- a/actix-utils/src/inflight.rs +++ b/actix-utils/src/inflight.rs @@ -115,7 +115,7 @@ mod tests { use std::time::Duration; use super::*; - use actix_service::{apply, factory_fn, Service, ServiceFactory}; + use actix_service::{apply, fn_factory, Service, ServiceFactory}; use futures::future::{lazy, ok, FutureExt, LocalBoxFuture}; struct SleepService(Duration); @@ -155,7 +155,7 @@ mod tests { async fn test_newtransform() { let wait_time = Duration::from_millis(50); - let srv = apply(InFlight::new(1), factory_fn(|| ok(SleepService(wait_time)))); + let srv = apply(InFlight::new(1), fn_factory(|| ok(SleepService(wait_time)))); let mut srv = srv.new_service(&()).await.unwrap(); assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(()))); diff --git a/actix-utils/src/timeout.rs b/actix-utils/src/timeout.rs index 4ea088f8..cb8c24b4 100644 --- a/actix-utils/src/timeout.rs +++ b/actix-utils/src/timeout.rs @@ -182,7 +182,7 @@ mod tests { use std::time::Duration; use super::*; - use actix_service::{apply, factory_fn, Service, ServiceFactory}; + use actix_service::{apply, fn_factory, Service, ServiceFactory}; use futures::future::{ok, FutureExt, LocalBoxFuture}; struct SleepService(Duration); @@ -229,7 +229,7 @@ mod tests { let timeout = apply( Timeout::new(resolution), - factory_fn(|| ok::<_, ()>(SleepService(wait_time))), + fn_factory(|| ok::<_, ()>(SleepService(wait_time))), ); let mut srv = timeout.new_service(&()).await.unwrap();