2019-12-02 17:30:09 +01:00
|
|
|
#![deny(rust_2018_idioms, warnings)]
|
|
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
|
2019-03-29 18:21:17 +01:00
|
|
|
use std::cell::RefCell;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
2019-02-19 20:31:54 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::task::{self, Context, Poll};
|
2019-01-25 04:19:44 +01:00
|
|
|
|
2018-08-25 18:02:14 +02:00
|
|
|
mod and_then;
|
2019-12-03 14:59:28 +01:00
|
|
|
mod and_then_apply_fn;
|
2018-08-30 18:17:17 +02:00
|
|
|
mod apply;
|
2019-03-09 18:01:02 +01:00
|
|
|
mod apply_cfg;
|
2019-02-23 03:20:54 +01:00
|
|
|
pub mod boxed;
|
2018-08-25 18:02:14 +02:00
|
|
|
mod fn_service;
|
2018-08-28 19:39:27 +02:00
|
|
|
mod map;
|
2019-05-12 15:03:50 +02:00
|
|
|
mod map_config;
|
2018-08-25 18:02:14 +02:00
|
|
|
mod map_err;
|
|
|
|
mod map_init_err;
|
2019-11-14 13:38:24 +01:00
|
|
|
mod pipeline;
|
2018-10-03 06:47:50 +02:00
|
|
|
mod then;
|
2019-02-03 19:42:27 +01:00
|
|
|
mod transform;
|
2019-12-03 13:32:02 +01:00
|
|
|
mod transform_err;
|
2019-03-12 20:53:08 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
pub use self::apply::{apply_fn, apply_fn_factory};
|
|
|
|
pub use self::apply_cfg::{apply_cfg, apply_cfg_factory};
|
2019-12-08 14:05:05 +01:00
|
|
|
pub use self::fn_service::{fn_factory, fn_factory_with_config, fn_service};
|
2019-12-02 16:27:48 +01:00
|
|
|
pub use self::map_config::{map_config, unit_config};
|
2019-11-14 13:38:24 +01:00
|
|
|
pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory};
|
|
|
|
pub use self::transform::{apply, Transform};
|
2019-05-12 15:03:50 +02:00
|
|
|
|
2018-12-09 18:56:23 +01:00
|
|
|
/// An asynchronous function from `Request` to a `Response`.
|
2019-12-10 16:11:39 +01:00
|
|
|
///
|
|
|
|
/// `Service` represents a service that represanting interation, taking requests and giving back
|
|
|
|
/// replies. You can think about service as a function with one argument and result as a return
|
|
|
|
/// type. In general form it looks like `async fn(Req) -> Result<Res, Err>`. `Service`
|
|
|
|
/// trait just generalizing form of this function. Each parameter described as an assotiated type.
|
|
|
|
///
|
|
|
|
/// Services provides a symmetric and uniform API, same abstractions represents
|
|
|
|
/// clients and servers. Services describe only `transforamtion` operation
|
|
|
|
/// which encorouge to simplify api surface and phrases `value transformation`.
|
|
|
|
/// That leads to simplier design of each service. That also allows better testability
|
|
|
|
/// and better composition.
|
|
|
|
///
|
|
|
|
/// Services could be represented in several different forms. In general,
|
|
|
|
/// Service is a type that implements `Service` trait.
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// struct MyService;
|
|
|
|
///
|
|
|
|
/// impl Service for MyService {
|
|
|
|
/// type Request = u8;
|
|
|
|
/// type Response = u64;
|
|
|
|
/// type Error = MyError;
|
2020-08-06 12:21:51 +02:00
|
|
|
/// type Future = Pin<Box<Future<Output=Result<Self::Response, Self::Error>>>>;
|
2019-12-10 16:11:39 +01:00
|
|
|
///
|
|
|
|
/// fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { ... }
|
|
|
|
///
|
2020-01-11 23:44:01 +01:00
|
|
|
/// fn call(&mut self, req: Self::Request) -> Self::Future { ... }
|
2019-12-10 16:11:39 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Service can have mutable state that influence computation.
|
|
|
|
/// This service could be rewritten as a simple function:
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// async fn my_service(req: u8) -> Result<u64, MyError>;
|
|
|
|
/// ```
|
2019-03-09 15:36:23 +01:00
|
|
|
pub trait Service {
|
|
|
|
/// Requests handled by the service.
|
|
|
|
type Request;
|
|
|
|
|
2018-12-09 18:56:23 +01:00
|
|
|
/// Responses given by the service.
|
|
|
|
type Response;
|
|
|
|
|
|
|
|
/// Errors produced by the service.
|
|
|
|
type Error;
|
|
|
|
|
|
|
|
/// The future response value.
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future: Future<Output = Result<Self::Response, Self::Error>>;
|
2018-12-09 18:56:23 +01:00
|
|
|
|
|
|
|
/// Returns `Ready` when the service is able to process requests.
|
|
|
|
///
|
2020-01-11 23:44:01 +01:00
|
|
|
/// If the service is at capacity, then `Pending` is returned and the task
|
2018-12-09 18:56:23 +01:00
|
|
|
/// 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.
|
2019-12-10 16:11:39 +01:00
|
|
|
///
|
|
|
|
/// There are several notes to consider:
|
|
|
|
///
|
|
|
|
/// 1. `.poll_ready()` might be called on different task from actual service call.
|
|
|
|
///
|
|
|
|
/// 2. In case of chained services, `.poll_ready()` get called for all services at once.
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, ctx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>>;
|
2018-12-09 18:56:23 +01:00
|
|
|
|
|
|
|
/// Process the request and return the response asynchronously.
|
|
|
|
///
|
|
|
|
/// This function is expected to be callable off task. As such,
|
|
|
|
/// implementations should take care to not call `poll_ready`. If the
|
|
|
|
/// service is at capacity and the request is unable to be handled, the
|
|
|
|
/// returned `Future` should resolve to an error.
|
|
|
|
///
|
|
|
|
/// Calling `call` without calling `poll_ready` is permitted. The
|
|
|
|
/// implementation must be resilient to this fact.
|
2019-03-09 15:36:23 +01:00
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future;
|
2019-11-18 09:30:04 +01:00
|
|
|
|
|
|
|
/// Map this service's output to a different type, returning a new service
|
|
|
|
/// of the resulting type.
|
|
|
|
///
|
|
|
|
/// This function is similar to the `Option::map` or `Iterator::map` where
|
|
|
|
/// it will change the type of the underlying service.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving service and returns a
|
|
|
|
/// wrapped version of it, similar to the existing `map` methods in the
|
|
|
|
/// standard library.
|
|
|
|
fn map<F, R>(self, f: F) -> crate::dev::Map<Self, F, R>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: FnMut(Self::Response) -> R,
|
2019-11-18 09:30:04 +01:00
|
|
|
{
|
|
|
|
crate::dev::Map::new(self, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map this service's error to a different error, returning a new service.
|
|
|
|
///
|
|
|
|
/// This function is similar to the `Result::map_err` where it will change
|
|
|
|
/// the error type of the underlying service. This is useful for example to
|
|
|
|
/// ensure that services have the same error type.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving service and returns a
|
|
|
|
/// wrapped version of it.
|
|
|
|
fn map_err<F, E>(self, f: F) -> crate::dev::MapErr<Self, F, E>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: Fn(Self::Error) -> E,
|
|
|
|
{
|
|
|
|
crate::dev::MapErr::new(self, f)
|
|
|
|
}
|
2018-12-13 03:32:19 +01:00
|
|
|
}
|
2018-12-09 18:56:23 +01:00
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
/// Creates new `Service` values.
|
|
|
|
///
|
|
|
|
/// Acts as a service factory. This is useful for cases where new `Service`
|
2019-07-25 07:46:11 +02:00
|
|
|
/// values must be produced. One case is a TCP server listener. The listener
|
2018-11-30 03:56:15 +01:00
|
|
|
/// accepts new TCP streams, obtains a new `Service` value using the
|
2019-11-14 13:38:24 +01:00
|
|
|
/// `ServiceFactory` trait, and uses that new `Service` value to process inbound
|
2018-11-30 03:56:15 +01:00
|
|
|
/// requests on that new TCP stream.
|
2019-02-22 21:44:37 +01:00
|
|
|
///
|
2019-03-09 15:36:23 +01:00
|
|
|
/// `Config` is a service factory configuration type.
|
2019-11-14 13:38:24 +01:00
|
|
|
pub trait ServiceFactory {
|
2019-03-09 15:36:23 +01:00
|
|
|
/// Requests handled by the service.
|
|
|
|
type Request;
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
/// Responses given by the service
|
|
|
|
type Response;
|
|
|
|
|
|
|
|
/// Errors produced by the service
|
|
|
|
type Error;
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
/// Service factory configuration
|
|
|
|
type Config;
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
/// The `Service` value created by this factory
|
2019-03-09 15:36:23 +01:00
|
|
|
type Service: Service<
|
|
|
|
Request = Self::Request,
|
|
|
|
Response = Self::Response,
|
|
|
|
Error = Self::Error,
|
|
|
|
>;
|
2018-11-30 03:56:15 +01:00
|
|
|
|
|
|
|
/// Errors produced while building a service.
|
|
|
|
type InitError;
|
|
|
|
|
|
|
|
/// The future of the `Service` instance.
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future: Future<Output = Result<Self::Service, Self::InitError>>;
|
2018-11-30 03:56:15 +01:00
|
|
|
|
|
|
|
/// Create and return a new service value asynchronously.
|
2019-12-02 16:27:48 +01:00
|
|
|
fn new_service(&self, cfg: Self::Config) -> Self::Future;
|
2019-11-18 09:30:04 +01:00
|
|
|
|
|
|
|
/// Map this service's output to a different type, returning a new service
|
|
|
|
/// of the resulting type.
|
|
|
|
fn map<F, R>(self, f: F) -> crate::map::MapServiceFactory<Self, F, R>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: FnMut(Self::Response) -> R + Clone,
|
2019-11-18 09:30:04 +01:00
|
|
|
{
|
|
|
|
crate::map::MapServiceFactory::new(self, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map this service's error to a different error, returning a new service.
|
|
|
|
fn map_err<F, E>(self, f: F) -> crate::map_err::MapErrServiceFactory<Self, F, E>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: Fn(Self::Error) -> E + Clone,
|
2019-11-18 09:30:04 +01:00
|
|
|
{
|
|
|
|
crate::map_err::MapErrServiceFactory::new(self, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map this factory's init error to a different error, returning a new service.
|
|
|
|
fn map_init_err<F, E>(self, f: F) -> crate::map_init_err::MapInitErr<Self, F, E>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: Fn(Self::InitError) -> E + Clone,
|
2019-11-18 09:30:04 +01:00
|
|
|
{
|
|
|
|
crate::map_init_err::MapInitErr::new(self, f)
|
|
|
|
}
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<'a, S> Service for &'a mut S
|
2018-12-09 18:56:23 +01:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
S: Service + 'a,
|
2018-12-09 18:56:23 +01:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = S::Request;
|
2018-12-09 18:56:23 +01:00
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
(**self).poll_ready(ctx)
|
2018-12-09 18:56:23 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
fn call(&mut self, request: Self::Request) -> S::Future {
|
2018-12-09 18:56:23 +01:00
|
|
|
(**self).call(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<S> Service for Box<S>
|
2018-12-09 19:14:08 +01:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
S: Service + ?Sized,
|
2018-12-09 19:14:08 +01:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = S::Request;
|
2018-12-09 19:14:08 +01:00
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
|
|
|
|
(**self).poll_ready(ctx)
|
2018-12-09 19:14:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
fn call(&mut self, request: Self::Request) -> S::Future {
|
2018-12-09 19:14:08 +01:00
|
|
|
(**self).call(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 03:45:09 +01:00
|
|
|
impl<S> Service for RefCell<S>
|
|
|
|
where
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
type Request = S::Request;
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.borrow_mut().poll_ready(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, request: Self::Request) -> S::Future {
|
|
|
|
self.borrow_mut().call(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:21:17 +01:00
|
|
|
impl<S> Service for Rc<RefCell<S>>
|
|
|
|
where
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
type Request = S::Request;
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.borrow_mut().poll_ready(ctx)
|
2019-03-29 18:21:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, request: Self::Request) -> S::Future {
|
2019-11-14 13:38:24 +01:00
|
|
|
(&mut (**self).borrow_mut()).call(request)
|
2019-03-29 18:21:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<S> ServiceFactory for Rc<S>
|
2019-02-19 20:31:54 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
S: ServiceFactory,
|
2019-02-19 20:31:54 +01:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = S::Request;
|
2019-02-19 20:31:54 +01:00
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = S::Config;
|
2019-02-19 20:31:54 +01:00
|
|
|
type Service = S::Service;
|
|
|
|
type InitError = S::InitError;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2019-12-02 16:27:48 +01:00
|
|
|
fn new_service(&self, cfg: S::Config) -> S::Future {
|
2019-02-22 21:44:37 +01:00
|
|
|
self.as_ref().new_service(cfg)
|
2019-02-19 20:31:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<S> ServiceFactory for Arc<S>
|
2019-02-19 20:31:54 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
S: ServiceFactory,
|
2019-02-19 20:31:54 +01:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = S::Request;
|
2019-02-19 20:31:54 +01:00
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = S::Config;
|
2019-02-19 20:31:54 +01:00
|
|
|
type Service = S::Service;
|
|
|
|
type InitError = S::InitError;
|
|
|
|
type Future = S::Future;
|
|
|
|
|
2019-12-02 16:27:48 +01:00
|
|
|
fn new_service(&self, cfg: S::Config) -> S::Future {
|
2019-02-22 21:44:37 +01:00
|
|
|
self.as_ref().new_service(cfg)
|
2019-02-19 20:31:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:57:47 +02:00
|
|
|
/// Trait for types that can be converted to a `Service`
|
2019-03-09 15:36:23 +01:00
|
|
|
pub trait IntoService<T>
|
2018-09-04 18:49:21 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
T: Service,
|
2018-09-04 18:49:21 +02:00
|
|
|
{
|
2018-09-04 18:57:47 +02:00
|
|
|
/// Convert to a `Service`
|
2018-09-04 18:49:21 +02:00
|
|
|
fn into_service(self) -> T;
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Trait for types that can be converted to a `ServiceFactory`
|
|
|
|
pub trait IntoServiceFactory<T>
|
2018-09-04 18:49:21 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
T: ServiceFactory,
|
2018-09-04 18:49:21 +02:00
|
|
|
{
|
2019-12-10 16:11:39 +01:00
|
|
|
/// Convert `Self` to a `ServiceFactory`
|
2019-11-14 13:38:24 +01:00
|
|
|
fn into_factory(self) -> T;
|
2018-09-04 18:49:21 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<T> IntoService<T> for T
|
2018-09-04 18:49:21 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
T: Service,
|
2018-09-04 18:49:21 +02:00
|
|
|
{
|
|
|
|
fn into_service(self) -> T {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<T> IntoServiceFactory<T> for T
|
2019-02-22 23:30:00 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
T: ServiceFactory,
|
2019-02-22 23:30:00 +01:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
fn into_factory(self) -> T {
|
2019-02-22 23:30:00 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 09:30:04 +01:00
|
|
|
|
2020-01-08 13:31:50 +01:00
|
|
|
/// Convert object of type `T` to a service `S`
|
|
|
|
pub fn into_service<T, S>(tp: T) -> S
|
|
|
|
where
|
|
|
|
S: Service,
|
|
|
|
T: IntoService<S>,
|
|
|
|
{
|
|
|
|
tp.into_service()
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
pub mod dev {
|
|
|
|
pub use crate::apply::{Apply, ApplyServiceFactory};
|
|
|
|
pub use crate::fn_service::{
|
|
|
|
FnService, FnServiceConfig, FnServiceFactory, FnServiceNoConfig,
|
|
|
|
};
|
|
|
|
pub use crate::map::{Map, MapServiceFactory};
|
2019-11-18 13:28:54 +01:00
|
|
|
pub use crate::map_config::{MapConfig, UnitConfig};
|
2019-11-18 09:30:04 +01:00
|
|
|
pub use crate::map_err::{MapErr, MapErrServiceFactory};
|
|
|
|
pub use crate::map_init_err::MapInitErr;
|
2019-11-19 01:51:43 +01:00
|
|
|
pub use crate::transform::ApplyTransform;
|
2019-12-03 13:32:02 +01:00
|
|
|
pub use crate::transform_err::TransformMapInitErr;
|
2019-11-18 09:30:04 +01:00
|
|
|
}
|