move Service::ready() to ServiceExt::ready() (#128)
This commit is contained in:
parent
c5cb47d612
commit
787c10b7c4
|
@ -10,3 +10,4 @@ tower-service = { version = "0.1", path = "../tower-service" }
|
|||
|
||||
[dev-dependencies]
|
||||
tower-mock = { version = "0.1", path = "../tower-mock" }
|
||||
tower-util = { version = "0.1", path = "../tower-util" }
|
||||
|
|
|
@ -2,10 +2,12 @@ extern crate futures;
|
|||
extern crate tower_mock;
|
||||
extern crate tower_filter;
|
||||
extern crate tower_service;
|
||||
extern crate tower_util;
|
||||
|
||||
use futures::*;
|
||||
use tower_filter::*;
|
||||
use tower_service::*;
|
||||
use tower_util::ServiceExt;
|
||||
|
||||
use std::thread;
|
||||
use std::sync::mpsc;
|
||||
|
|
|
@ -12,13 +12,10 @@
|
|||
//! * [`MakeService`](trait.MakeService.html) is essentially a service factory. It
|
||||
//! is responsible for generating `Service` values on demand.
|
||||
|
||||
#[macro_use]
|
||||
extern crate futures;
|
||||
|
||||
use futures::{Future, Poll};
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
/// An asynchronous function from `Request` to a `Response`.
|
||||
///
|
||||
/// The `Service` trait is a simplified interface making it easy to write
|
||||
|
@ -171,14 +168,6 @@ pub trait Service<Request> {
|
|||
/// The future response value.
|
||||
type Future: Future<Item = Self::Response, Error = Self::Error>;
|
||||
|
||||
/// A future yielding the service when it is ready to accept a request.
|
||||
fn ready(self) -> Ready<Self, Request> where Self: Sized {
|
||||
Ready {
|
||||
inner: Some(self),
|
||||
_p: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `Ready` when the service is able to process requests.
|
||||
///
|
||||
/// If the service is at capacity, then `NotReady` is returned and the task
|
||||
|
@ -202,32 +191,6 @@ pub trait Service<Request> {
|
|||
fn call(&mut self, req: Request) -> Self::Future;
|
||||
}
|
||||
|
||||
/// Future yielding a `Service` once the service is ready to process a request
|
||||
///
|
||||
/// `Ready` values are produced by `Service::ready`.
|
||||
pub struct Ready<T, Request> {
|
||||
inner: Option<T>,
|
||||
_p: PhantomData<fn() -> Request>,
|
||||
}
|
||||
|
||||
impl<T, Request> Future for Ready<T, Request>
|
||||
where T: Service<Request>,
|
||||
{
|
||||
type Item = T;
|
||||
type Error = T::Error;
|
||||
|
||||
fn poll(&mut self) -> Poll<T, T::Error> {
|
||||
match self.inner {
|
||||
Some(ref mut service) => {
|
||||
let _ = try_ready!(service.poll_ready());
|
||||
}
|
||||
None => panic!("called `poll` after future completed"),
|
||||
}
|
||||
|
||||
Ok(self.inner.take().unwrap().into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates new `Service` values.
|
||||
///
|
||||
/// Acts as a service factory. This is useful for cases where new `Service`
|
||||
|
|
|
@ -8,6 +8,7 @@ mod apply;
|
|||
mod from_err;
|
||||
mod map;
|
||||
mod map_err;
|
||||
mod ready;
|
||||
mod then;
|
||||
|
||||
pub use self::and_then::AndThen;
|
||||
|
@ -15,6 +16,7 @@ pub use self::apply::Apply;
|
|||
pub use self::from_err::FromErr;
|
||||
pub use self::map::Map;
|
||||
pub use self::map_err::MapErr;
|
||||
pub use self::ready::Ready;
|
||||
pub use self::then::Then;
|
||||
|
||||
impl<T: ?Sized, Request> ServiceExt<Request> for T
|
||||
|
@ -25,6 +27,14 @@ where
|
|||
/// An extension trait for `Service`s that provides a variety of convenient
|
||||
/// adapters
|
||||
pub trait ServiceExt<Request>: Service<Request> {
|
||||
/// A future yielding the service when it is ready to accept a request.
|
||||
fn ready(self) -> Ready<Self, Request>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Ready::new(self)
|
||||
}
|
||||
|
||||
fn apply<F, In, Out>(self, f: F) -> Apply<Self, F, In, Out, Request>
|
||||
where
|
||||
Self: Service<Request> + Clone + Sized,
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{Future, Poll};
|
||||
use tower_service::Service;
|
||||
|
||||
/// Future yielding a `Service` once the service is ready to process a request
|
||||
///
|
||||
/// `Ready` values are produced by `ServiceExt::ready`.
|
||||
pub struct Ready<T, Request> {
|
||||
inner: Option<T>,
|
||||
_p: PhantomData<fn() -> Request>,
|
||||
}
|
||||
|
||||
impl<T, Request> Ready<T, Request>
|
||||
where
|
||||
T: Service<Request>,
|
||||
{
|
||||
pub(super) fn new(service: T) -> Self {
|
||||
Ready {
|
||||
inner: Some(service),
|
||||
_p: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Request> Future for Ready<T, Request>
|
||||
where
|
||||
T: Service<Request>,
|
||||
{
|
||||
type Item = T;
|
||||
type Error = T::Error;
|
||||
|
||||
fn poll(&mut self) -> Poll<T, T::Error> {
|
||||
match self.inner {
|
||||
Some(ref mut service) => {
|
||||
let _ = try_ready!(service.poll_ready());
|
||||
}
|
||||
None => panic!("called `poll` after future completed"),
|
||||
}
|
||||
|
||||
Ok(self.inner.take().unwrap().into())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T, Request> fmt::Debug for Ready<T, Request>
|
||||
where
|
||||
T: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_struct("Ready")
|
||||
.field("inner", &self.inner)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue