From 5b6305812929766db51553eb30fc281af7e4cab1 Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Tue, 25 Oct 2016 10:52:46 -0700 Subject: [PATCH] Finalize tokio-service APIs for 0.1 A bit of tidying up ahead of the 0.1 release: - Drop `poll_ready` for now, given that we're uncertain about the overall backpressure story, and it can be added easily as a defaulted method later on. Backpressure should be a major goal in an upcoming release. - Rename `SimpleService` to `FnService`, matching the `Fn*` traits. - Rename `NewService::Item` to `NewService::Instance`, for greater clarity about their relationship. --- src/lib.rs | 46 +++++++++++++++------------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ae4e156..28f1e7b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ extern crate futures; -use futures::{Async, Future, IntoFuture}; +use futures::{Future, IntoFuture}; use std::io; use std::marker::PhantomData; @@ -153,14 +153,10 @@ pub trait Service { /// Process the request and return the response asynchronously. fn call(&self, req: Self::Request) -> Self::Future; - - /// Returns `Async::Ready` when the service is ready to accept a request. - fn poll_ready(&self) -> Async<()>; } /// Creates new `Service` values. pub trait NewService { - /// Requests handled by the service type Request; @@ -171,27 +167,27 @@ pub trait NewService { type Error; /// The `Service` value created by this factory - type Item: Service; + type Instance: Service; /// Create and return a new service value. - fn new_service(&self) -> io::Result; + fn new_service(&self) -> io::Result; } /// A service implemented by a closure. -pub struct SimpleService { +pub struct FnService { f: Arc, _ty: PhantomData R>, // don't impose Sync on R } /// Returns a `Service` backed by the given closure. -pub fn simple_service(f: F) -> SimpleService { - SimpleService::new(f) +pub fn fn_service(f: F) -> FnService { + FnService::new(f) } -impl SimpleService { - /// Create and return a new `SimpleService` backed by the given function. - pub fn new(f: F) -> SimpleService { - SimpleService { +impl FnService { + /// Create and return a new `FnService` backed by the given function. + pub fn new(f: F) -> FnService { + FnService { f: Arc::new(f), _ty: PhantomData, } @@ -201,7 +197,7 @@ impl SimpleService { impl NewService for T where T: Service + Clone, { - type Item = T; + type Instance = T; type Request = T::Request; type Response = T::Response; type Error = T::Error; @@ -211,7 +207,7 @@ impl NewService for T } } -impl Service for SimpleService +impl Service for FnService where F: Fn(R) -> S + Sync + Send + 'static, R: Send + 'static, S: IntoFuture + Send + 'static, @@ -227,15 +223,11 @@ impl Service for SimpleService fn call(&self, req: R) -> Self::Future { (self.f)(req).into_future() } - - fn poll_ready(&self) -> Async<()> { - Async::Ready(()) - } } -impl Clone for SimpleService { - fn clone(&self) -> SimpleService { - SimpleService { +impl Clone for FnService { + fn clone(&self) -> FnService { + FnService { f: self.f.clone(), _ty: PhantomData, } @@ -253,10 +245,6 @@ impl Service for Box F { (**self).call(request) } - - fn poll_ready(&self) -> Async<()> { - (**self).poll_ready() - } } impl Service for Box + Send + 'static> @@ -270,8 +258,4 @@ impl Service for Box F { (**self).call(request) } - - fn poll_ready(&self) -> Async<()> { - (**self).poll_ready() - } }