From d1c891d1badb988ec1a2f20b4b1afe127a2f86c9 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 10 Apr 2019 13:17:16 -0700 Subject: [PATCH] Promote service_fn to tower crate (#248) --- tower-balance/examples/demo.rs | 6 ++---- tower-util/src/boxed/mod.rs | 38 +++++++--------------------------- tower-util/src/lib.rs | 2 +- tower-util/src/service_fn.rs | 12 +++++------ tower/src/lib.rs | 2 +- tower/src/util.rs | 3 +-- 6 files changed, 18 insertions(+), 45 deletions(-) diff --git a/tower-balance/examples/demo.rs b/tower-balance/examples/demo.rs index eb47f6f..18860ec 100644 --- a/tower-balance/examples/demo.rs +++ b/tower-balance/examples/demo.rs @@ -6,9 +6,7 @@ use hdrsample::Histogram; use rand::{self, Rng}; use std::time::{Duration, Instant}; use tokio::{runtime, timer}; -use tower::{ - discover::Discover, limit::concurrency::ConcurrencyLimit, util::ServiceFn, Service, ServiceExt, -}; +use tower::{discover::Discover, limit::concurrency::ConcurrencyLimit, Service, ServiceExt}; use tower_balance as lb; const REQUESTS: usize = 50_000; @@ -124,7 +122,7 @@ fn gen_disco() -> impl Discover< .zip(WEIGHTS.iter()) .enumerate() .map(|(instance, (latency, weight))| { - let svc = ServiceFn::new(move |_| { + let svc = tower::service_fn(move |_| { let start = Instant::now(); let maxms = u64::from(latency.subsec_nanos() / 1_000 / 1_000) diff --git a/tower-util/src/boxed/mod.rs b/tower-util/src/boxed/mod.rs index f56833f..8d46788 100644 --- a/tower-util/src/boxed/mod.rs +++ b/tower-util/src/boxed/mod.rs @@ -20,37 +20,15 @@ //! # use futures::*; //! # use futures::future::FutureResult; //! # use tower_service::Service; -//! # use tower_util::BoxService; -//! // Respond to requests using a closure. Since closures cannot be named, -//! // `ServiceFn` cannot be named either -//! pub struct ServiceFn { -//! f: F, -//! } +//! # use tower_util::{BoxService, service_fn}; +//! // Respond to requests using a closure, but closures cannot be named... +//! # pub fn main() { +//! let svc = service_fn(|mut request: String| { +//! request.push_str(" response"); +//! Ok(request) +//! }); //! -//! impl Service for ServiceFn -//! where F: Fn(String) -> String, -//! { -//! type Response = String; -//! type Error = (); -//! type Future = FutureResult; -//! -//! fn poll_ready(&mut self) -> Poll<(), ()> { -//! Ok(().into()) -//! } -//! -//! fn call(&mut self, request: String) -> FutureResult { -//! future::ok((self.f)(request)) -//! } -//! } -//! -//! pub fn main() { -//! let f = |mut request: String| { -//! request.push_str(" response"); -//! request -//! }; -//! -//! let service: BoxService = -//! BoxService::new(ServiceFn { f }); +//! let service: BoxService = BoxService::new(svc); //! # drop(service); //! } //! ``` diff --git a/tower-util/src/lib.rs b/tower-util/src/lib.rs index 14207f4..41d2bb6 100644 --- a/tower-util/src/lib.rs +++ b/tower-util/src/lib.rs @@ -25,7 +25,7 @@ pub use crate::{ oneshot::Oneshot, optional::Optional, ready::Ready, - service_fn::ServiceFn, + service_fn::{service_fn, ServiceFn}, }; pub mod error { diff --git a/tower-util/src/service_fn.rs b/tower-util/src/service_fn.rs index 9743a8e..3848164 100644 --- a/tower-util/src/service_fn.rs +++ b/tower-util/src/service_fn.rs @@ -1,19 +1,17 @@ use futures::{IntoFuture, Poll}; use tower_service::Service; +/// Returns a new `ServiceFn` with the given closure. +pub fn service_fn(f: T) -> ServiceFn { + ServiceFn { f } +} + /// A `Service` implemented by a closure. #[derive(Copy, Clone, Debug)] pub struct ServiceFn { f: T, } -impl ServiceFn { - /// Returns a new `ServiceFn` with the given closure. - pub fn new(f: T) -> Self { - ServiceFn { f } - } -} - impl Service for ServiceFn where T: Fn(Request) -> F, diff --git a/tower/src/lib.rs b/tower/src/lib.rs index 65a2bc3..a47ec4b 100644 --- a/tower/src/lib.rs +++ b/tower/src/lib.rs @@ -19,4 +19,4 @@ pub mod util; pub use crate::{builder::ServiceBuilder, util::ServiceExt}; pub use tower_service::Service; -pub use tower_util::{MakeConnection, MakeService}; +pub use tower_util::{service_fn, MakeConnection, MakeService}; diff --git a/tower/src/util.rs b/tower/src/util.rs index 720c719..1f5eeda 100644 --- a/tower/src/util.rs +++ b/tower/src/util.rs @@ -3,8 +3,7 @@ use futures::Stream; use tower_service::Service; pub use tower_util::{ - BoxService, CallAll, CallAllUnordered, Either, Oneshot, Optional, Ready, ServiceFn, - UnsyncBoxService, + BoxService, CallAll, CallAllUnordered, Either, Oneshot, Optional, Ready, UnsyncBoxService, }; impl ServiceExt for T where T: Service {}