Repository: tower-rs/tower
Pull request #318 (2019-09-03 11:22:31)

Update `service_fn` to `std::future::Future`
This small PR ports service_fn to `std::future`.


Patch:
diff --git a/Cargo.toml b/Cargo.toml
index b616646..c0da88b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,5 +18,5 @@ members = [
   # "tower-test",
   # "tower-timeout",
   "tower-make",
-  # "tower-util",
+  "tower-util",
 ]
diff --git a/tower-util/Cargo.toml b/tower-util/Cargo.toml
index 85bd56a..981c603 100644
--- a/tower-util/Cargo.toml
+++ b/tower-util/Cargo.toml
@@ -8,8 +8,8 @@ name = "tower-util"
 #   - Cargo.toml
 #   - README.md
 # - Update CHANGELOG.md.
-# - Create "v0.1.x" git tag.
-version = "0.1.0"
+# - Create "v0.3.x" git tag.
+version = "0.3.0-alpha.1"
 authors = ["Tower Maintainers <team@tower-rs.com>"]
 license = "MIT"
 readme = "README.md"
@@ -26,7 +26,8 @@ edition = "2018"
 tower-service = "=0.3.0-alpha.1"
 # tower-layer = "0.1.0"
 
-# [dev-dependencies]
+[dev-dependencies]
+futures = { version = "=0.3.0-alpha.18", package = "futures-preview"}
 # tokio-mock-task = "0.1.1"
 # tower = { version = "0.1.0", path = "../tower" }
 # tower-test = { version = "0.1.0", path = "../tower-test" }
diff --git a/tower-util/src/lib.rs b/tower-util/src/lib.rs
index f543ad1..4421bde 100644
--- a/tower-util/src/lib.rs
+++ b/tower-util/src/lib.rs
@@ -4,37 +4,28 @@
 
 //! Various utility types and functions that are generally with Tower.
 
-mod boxed;
-mod call_all;
-mod either;
-pub mod layer;
-mod oneshot;
-mod optional;
-mod ready;
-mod sealed;
+//mod boxed;
+//mod call_all;
+//mod either;
+//pub mod layer;
+//mod oneshot;
+//mod optional;
+//mod ready;
+//mod sealed;
 mod service_fn;
 
-pub use crate::{
-    boxed::{BoxService, UnsyncBoxService},
-    call_all::{CallAll, CallAllUnordered},
-    either::Either,
-    make_service::MakeService,
-    oneshot::Oneshot,
-    optional::Optional,
-    ready::Ready,
-    service_fn::{service_fn, ServiceFn},
-};
+pub use crate::service_fn::{service_fn, ServiceFn};
 
-pub mod error {
-    //! Error types
-
-    pub use crate::optional::error as optional;
-}
-
-pub mod future {
-    //! Future types
-
-    #[cfg(feature = "either")]
-    pub use crate::either::future as either;
-    pub use crate::optional::future as optional;
-}
+//pub mod error {
+//    //! Error types
+//
+//    pub use crate::optional::error as optional;
+//}
+//
+//pub mod future {
+//    //! Future types
+//
+//    #[cfg(feature = "either")]
+//    pub use crate::either::future as either;
+//    pub use crate::optional::future as optional;
+//}
diff --git a/tower-util/src/service_fn.rs b/tower-util/src/service_fn.rs
index a2472ae..f109d6b 100644
--- a/tower-util/src/service_fn.rs
+++ b/tower-util/src/service_fn.rs
@@ -1,4 +1,5 @@
-use futures::{IntoFuture, Poll};
+use std::future::Future;
+use std::task::{Context, Poll};
 use tower_service::Service;
 
 /// Returns a new `ServiceFn` with the given closure.
@@ -12,20 +13,20 @@ pub struct ServiceFn<T> {
     f: T,
 }
 
-impl<T, F, Request> Service<Request> for ServiceFn<T>
+impl<T, F, Request, R, E> Service<Request> for ServiceFn<T>
 where
     T: FnMut(Request) -> F,
-    F: IntoFuture,
+    F: Future<Output = Result<R, E>>,
 {
-    type Response = F::Item;
-    type Error = F::Error;
-    type Future = F::Future;
+    type Response = R;
+    type Error = E;
+    type Future = F;
 
-    fn poll_ready(&mut self) -> Poll<(), F::Error> {
-        Ok(().into())
+    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), E>> {
+        Ok(()).into()
     }
 
     fn call(&mut self, req: Request) -> Self::Future {
-        (self.f)(req).into_future()
+        (self.f)(req)
     }
 }


Test patch:
diff --git a/tower-util/tests/service_fn.rs b/tower-util/tests/service_fn.rs
new file mode 100644
index 0000000..b19f287
--- /dev/null
+++ b/tower-util/tests/service_fn.rs
@@ -0,0 +1,13 @@
+use futures::executor::block_on;
+use futures::future;
+use tower_service::Service;
+use tower_util::service_fn;
+
+#[test]
+fn simple() {
+    block_on(async {
+        let mut add_one = service_fn(|req| future::ok::<_, ()>(req + 1));
+        let answer = add_one.call(1).await.unwrap();
+        assert_eq!(answer, 2);
+    });
+}