mirror of
https://github.com/tower-rs/tower.git
synced 2025-10-01 15:01:18 +00:00
Update service_fn
to std::future::Future
(#318)
This small PR ports service_fn to `std::future`.
This commit is contained in:
parent
84da777ad1
commit
ae611db665
@ -18,5 +18,5 @@ members = [
|
||||
# "tower-test",
|
||||
# "tower-timeout",
|
||||
"tower-make",
|
||||
# "tower-util",
|
||||
"tower-util",
|
||||
]
|
||||
|
@ -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" }
|
||||
|
@ -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;
|
||||
//}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
13
tower-util/tests/service_fn.rs
Normal file
13
tower-util/tests/service_fn.rs
Normal 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);
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user