move MakeService to tower-util

This commit is contained in:
Sean McArthur 2018-11-27 12:11:07 -08:00
parent 787c10b7c4
commit ac1586748f
5 changed files with 76 additions and 70 deletions

View File

@ -8,3 +8,4 @@ publish = false
log = "0.4.1"
futures = "0.1"
tower-service = { version = "0.1", path = "../tower-service" }
tower-util = { version = "0.1", path = "../tower-util" }

View File

@ -2,9 +2,11 @@ extern crate futures;
#[macro_use]
extern crate log;
extern crate tower_service;
extern crate tower_util;
use futures::{Future, Async, Poll};
use tower_service::{MakeService, Service};
use tower_service::Service;
use tower_util::MakeService;
use std::{error, fmt, marker::PhantomData};

View File

@ -9,8 +9,6 @@
//!
//! * [`Service`](trait.Service.html) is the primary trait and defines the request
//! / response exchange. See that trait for more details.
//! * [`MakeService`](trait.MakeService.html) is essentially a service factory. It
//! is responsible for generating `Service` values on demand.
extern crate futures;
@ -191,70 +189,6 @@ pub trait Service<Request> {
fn call(&mut self, req: Request) -> Self::Future;
}
/// Creates new `Service` values.
///
/// Acts as a service factory. This is useful for cases where new `Service`
/// values must be produced. One case is a TCP servier listener. The listner
/// accepts new TCP streams, obtains a new `Service` value using the
/// `MakeService` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream.
///
/// This is essentially a trait alias for a `Service` of `Service`s.
pub trait MakeService<Target, Request>: self::sealed::Sealed<Target, Request> {
/// Responses given by the service
type Response;
/// Errors produced by the service
type Error;
/// The `Service` value created by this factory
type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
/// Errors produced while building a service.
type MakeError;
/// The future of the `Service` instance.
type Future: Future<Item = Self::Service, Error = Self::MakeError>;
/// Returns `Ready` when the factory is able to process create more services.
///
/// If the service is at capacity, then `NotReady` is returned and the task
/// is notified when the service becomes ready again. This function is
/// expected to be called while on a task.
///
/// This is a **best effort** implementation. False positives are permitted.
/// It is permitted for the service to return `Ready` from a `poll_ready`
/// call and the next invocation of `call` results in an error.
fn poll_ready(&mut self) -> Poll<(), Self::MakeError>;
/// Create and return a new service value asynchronously.
fn make_service(&mut self, target: Target) -> Self::Future;
}
impl<M, S, Target, Request> self::sealed::Sealed<Target, Request> for M
where M: Service<Target, Response=S>,
S: Service<Request>,
{}
impl<M, S, Target, Request> MakeService<Target, Request> for M
where M: Service<Target, Response=S>,
S: Service<Request>,
{
type Response = S::Response;
type Error = S::Error;
type Service = S;
type MakeError = M::Error;
type Future = M::Future;
fn poll_ready(&mut self) -> Poll<(), Self::MakeError> {
Service::poll_ready(self)
}
fn make_service(&mut self, target: Target) -> Self::Future {
Service::call(self, target)
}
}
impl<'a, S, Request> Service<Request> for &'a mut S
where
S: Service<Request> + 'a
@ -289,6 +223,3 @@ where
}
}
mod sealed {
pub trait Sealed<A, B> {}
}

View File

@ -8,11 +8,13 @@ extern crate tower_service;
pub mod boxed;
pub mod either;
pub mod ext;
mod make_service;
pub mod option;
mod service_fn;
pub use boxed::BoxService;
pub use either::EitherService;
pub use ext::ServiceExt;
pub use make_service::MakeService;
pub use option::OptionService;
pub use service_fn::ServiceFn;

View File

@ -0,0 +1,70 @@
use futures::{Future, Poll};
use tower_service::Service;
/// Creates new `Service` values.
///
/// Acts as a service factory. This is useful for cases where new `Service`
/// values must be produced. One case is a TCP servier listener. The listner
/// accepts new TCP streams, obtains a new `Service` value using the
/// `MakeService` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream.
///
/// This is essentially a trait alias for a `Service` of `Service`s.
pub trait MakeService<Target, Request>: self::sealed::Sealed<Target, Request> {
/// Responses given by the service
type Response;
/// Errors produced by the service
type Error;
/// The `Service` value created by this factory
type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
/// Errors produced while building a service.
type MakeError;
/// The future of the `Service` instance.
type Future: Future<Item = Self::Service, Error = Self::MakeError>;
/// Returns `Ready` when the factory is able to process create more services.
///
/// If the service is at capacity, then `NotReady` is returned and the task
/// is notified when the service becomes ready again. This function is
/// expected to be called while on a task.
///
/// This is a **best effort** implementation. False positives are permitted.
/// It is permitted for the service to return `Ready` from a `poll_ready`
/// call and the next invocation of `call` results in an error.
fn poll_ready(&mut self) -> Poll<(), Self::MakeError>;
/// Create and return a new service value asynchronously.
fn make_service(&mut self, target: Target) -> Self::Future;
}
impl<M, S, Target, Request> self::sealed::Sealed<Target, Request> for M
where M: Service<Target, Response=S>,
S: Service<Request>,
{}
impl<M, S, Target, Request> MakeService<Target, Request> for M
where M: Service<Target, Response=S>,
S: Service<Request>,
{
type Response = S::Response;
type Error = S::Error;
type Service = S;
type MakeError = M::Error;
type Future = M::Future;
fn poll_ready(&mut self) -> Poll<(), Self::MakeError> {
Service::poll_ready(self)
}
fn make_service(&mut self, target: Target) -> Self::Future {
Service::call(self, target)
}
}
mod sealed {
pub trait Sealed<A, B> {}
}