Update service_fn to std::future::Future (#318)

This small PR ports service_fn to `std::future`.
This commit is contained in:
Stan Bondi 2019-09-03 16:12:33 +02:00 committed by Lucio Franco
parent 84da777ad1
commit ae611db665
5 changed files with 50 additions and 44 deletions

View File

@ -18,5 +18,5 @@ members = [
# "tower-test", # "tower-test",
# "tower-timeout", # "tower-timeout",
"tower-make", "tower-make",
# "tower-util", "tower-util",
] ]

View File

@ -8,8 +8,8 @@ name = "tower-util"
# - Cargo.toml # - Cargo.toml
# - README.md # - README.md
# - Update CHANGELOG.md. # - Update CHANGELOG.md.
# - Create "v0.1.x" git tag. # - Create "v0.3.x" git tag.
version = "0.1.0" version = "0.3.0-alpha.1"
authors = ["Tower Maintainers <team@tower-rs.com>"] authors = ["Tower Maintainers <team@tower-rs.com>"]
license = "MIT" license = "MIT"
readme = "README.md" readme = "README.md"
@ -26,7 +26,8 @@ edition = "2018"
tower-service = "=0.3.0-alpha.1" tower-service = "=0.3.0-alpha.1"
# tower-layer = "0.1.0" # 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" # tokio-mock-task = "0.1.1"
# tower = { version = "0.1.0", path = "../tower" } # tower = { version = "0.1.0", path = "../tower" }
# tower-test = { version = "0.1.0", path = "../tower-test" } # tower-test = { version = "0.1.0", path = "../tower-test" }

View File

@ -4,37 +4,28 @@
//! Various utility types and functions that are generally with Tower. //! Various utility types and functions that are generally with Tower.
mod boxed; //mod boxed;
mod call_all; //mod call_all;
mod either; //mod either;
pub mod layer; //pub mod layer;
mod oneshot; //mod oneshot;
mod optional; //mod optional;
mod ready; //mod ready;
mod sealed; //mod sealed;
mod service_fn; mod service_fn;
pub use crate::{ pub use crate::service_fn::{service_fn, ServiceFn};
boxed::{BoxService, UnsyncBoxService},
call_all::{CallAll, CallAllUnordered},
either::Either,
make_service::MakeService,
oneshot::Oneshot,
optional::Optional,
ready::Ready,
service_fn::{service_fn, ServiceFn},
};
pub mod error { //pub mod error {
//! Error types // //! Error types
//
pub use crate::optional::error as optional; // pub use crate::optional::error as optional;
} //}
//
pub mod future { //pub mod future {
//! Future types // //! Future types
//
#[cfg(feature = "either")] // #[cfg(feature = "either")]
pub use crate::either::future as either; // pub use crate::either::future as either;
pub use crate::optional::future as optional; // pub use crate::optional::future as optional;
} //}

View File

@ -1,4 +1,5 @@
use futures::{IntoFuture, Poll}; use std::future::Future;
use std::task::{Context, Poll};
use tower_service::Service; use tower_service::Service;
/// Returns a new `ServiceFn` with the given closure. /// Returns a new `ServiceFn` with the given closure.
@ -12,20 +13,20 @@ pub struct ServiceFn<T> {
f: T, f: T,
} }
impl<T, F, Request> Service<Request> for ServiceFn<T> impl<T, F, Request, R, E> Service<Request> for ServiceFn<T>
where where
T: FnMut(Request) -> F, T: FnMut(Request) -> F,
F: IntoFuture, F: Future<Output = Result<R, E>>,
{ {
type Response = F::Item; type Response = R;
type Error = F::Error; type Error = E;
type Future = F::Future; type Future = F;
fn poll_ready(&mut self) -> Poll<(), F::Error> { fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), E>> {
Ok(().into()) Ok(()).into()
} }
fn call(&mut self, req: Request) -> Self::Future { fn call(&mut self, req: Request) -> Self::Future {
(self.f)(req).into_future() (self.f)(req)
} }
} }

View File

@ -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);
});
}