2019-03-02 21:16:30 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-02-04 19:55:39 +01:00
|
|
|
use futures::{Future, Poll};
|
2019-02-03 19:42:27 +01:00
|
|
|
|
2019-02-04 19:55:39 +01:00
|
|
|
use crate::transform_map_err::{TransformMapErr, TransformMapErrNewTransform};
|
2019-03-02 22:18:01 +01:00
|
|
|
use crate::transform_map_init_err::TransformMapInitErr;
|
2019-02-04 19:55:39 +01:00
|
|
|
use crate::Service;
|
2019-02-03 19:42:27 +01:00
|
|
|
|
|
|
|
/// An asynchronous function for transforming service call result.
|
|
|
|
pub trait Transform<Service> {
|
|
|
|
/// Requests handled by the service.
|
|
|
|
type Request;
|
|
|
|
|
|
|
|
/// Responses given by the service.
|
|
|
|
type Response;
|
|
|
|
|
|
|
|
/// Errors produced by the service.
|
|
|
|
type Error;
|
|
|
|
|
|
|
|
/// The future response value.
|
|
|
|
type Future: Future<Item = Self::Response, Error = Self::Error>;
|
|
|
|
|
|
|
|
/// Returns `Ready` when the service is able to process requests.
|
|
|
|
///
|
|
|
|
/// This method is similar to `Service::poll_ready` method.
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error>;
|
|
|
|
|
|
|
|
/// Process the request and apply it to provided service,
|
|
|
|
/// return the response asynchronously.
|
|
|
|
fn call(&mut self, request: Self::Request, service: &mut Service) -> Self::Future;
|
|
|
|
|
|
|
|
/// Map this transform's error to a different error, returning a new transform.
|
|
|
|
///
|
|
|
|
/// This function is similar to the `Result::map_err` where it will change
|
|
|
|
/// the error type of the underlying transform. This is useful for example to
|
|
|
|
/// ensure that services and transforms have the same error type.
|
|
|
|
///
|
|
|
|
/// Note that this function consumes the receiving transform and returns a
|
|
|
|
/// wrapped version of it.
|
|
|
|
fn map_err<F, E>(self, f: F) -> TransformMapErr<Self, Service, F, E>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: Fn(Self::Error) -> E,
|
|
|
|
{
|
|
|
|
TransformMapErr::new(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `Transform` service factory
|
2019-03-02 21:16:30 +01:00
|
|
|
///
|
|
|
|
/// `Config` is a service factory configuration type.
|
|
|
|
pub trait NewTransform<Service, Config = ()> {
|
2019-02-03 19:42:27 +01:00
|
|
|
/// Requests handled by the service.
|
|
|
|
type Request;
|
|
|
|
|
|
|
|
/// Responses given by the service.
|
|
|
|
type Response;
|
|
|
|
|
|
|
|
/// Errors produced by the service.
|
|
|
|
type Error;
|
|
|
|
|
|
|
|
/// The `TransformService` value created by this factory
|
|
|
|
type Transform: Transform<
|
|
|
|
Service,
|
|
|
|
Request = Self::Request,
|
|
|
|
Response = Self::Response,
|
|
|
|
Error = Self::Error,
|
|
|
|
>;
|
|
|
|
|
|
|
|
/// Errors produced while building a service.
|
|
|
|
type InitError;
|
|
|
|
|
|
|
|
/// The future response value.
|
|
|
|
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
|
|
|
|
|
|
|
|
/// Create and return a new service value asynchronously.
|
2019-03-02 21:16:30 +01:00
|
|
|
fn new_transform(&self, cfg: &Config) -> Self::Future;
|
2019-02-03 19:42:27 +01:00
|
|
|
|
|
|
|
/// Map this transforms's output to a different type, returning a new transform
|
|
|
|
/// of the resulting type.
|
2019-03-02 21:16:30 +01:00
|
|
|
fn map_err<F, E>(self, f: F) -> TransformMapErrNewTransform<Self, Service, Config, F, E>
|
2019-02-03 19:42:27 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: Fn(Self::Error) -> E,
|
|
|
|
{
|
|
|
|
TransformMapErrNewTransform::new(self, f)
|
|
|
|
}
|
2019-03-02 22:18:01 +01:00
|
|
|
|
|
|
|
/// Map this service's factory init error to a different error,
|
|
|
|
/// returning a new transform service factory.
|
|
|
|
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, Service, Config, F, E>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: Fn(Self::InitError) -> E,
|
|
|
|
{
|
|
|
|
TransformMapInitErr::new(self, f)
|
|
|
|
}
|
2019-02-03 19:42:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T, S> Transform<S> for &'a mut T
|
|
|
|
where
|
|
|
|
T: Transform<S> + 'a,
|
|
|
|
S: Service<Error = T::Error>,
|
|
|
|
{
|
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type Future = T::Future;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), T::Error> {
|
|
|
|
(**self).poll_ready()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, request: Self::Request, service: &mut S) -> T::Future {
|
|
|
|
(**self).call(request, service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, S> Transform<S> for Box<T>
|
|
|
|
where
|
|
|
|
T: Transform<S> + ?Sized,
|
|
|
|
S: Service<Error = T::Error>,
|
|
|
|
{
|
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type Future = T::Future;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), S::Error> {
|
|
|
|
(**self).poll_ready()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, request: Self::Request, service: &mut S) -> T::Future {
|
|
|
|
(**self).call(request, service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 21:16:30 +01:00
|
|
|
impl<S, C, T> NewTransform<S, C> for Rc<T>
|
|
|
|
where
|
|
|
|
T: NewTransform<S, C>,
|
|
|
|
{
|
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type Transform = T::Transform;
|
|
|
|
type InitError = T::InitError;
|
|
|
|
type Future = T::Future;
|
|
|
|
|
|
|
|
fn new_transform(&self, cfg: &C) -> T::Future {
|
|
|
|
self.as_ref().new_transform(cfg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, C, T> NewTransform<S, C> for Arc<T>
|
|
|
|
where
|
|
|
|
T: NewTransform<S, C>,
|
|
|
|
{
|
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type Transform = T::Transform;
|
|
|
|
type InitError = T::InitError;
|
|
|
|
type Future = T::Future;
|
|
|
|
|
|
|
|
fn new_transform(&self, cfg: &C) -> T::Future {
|
|
|
|
self.as_ref().new_transform(cfg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
/// Trait for types that can be converted to a `TransformService`
|
|
|
|
pub trait IntoTransform<T, S>
|
|
|
|
where
|
|
|
|
T: Transform<S>,
|
|
|
|
{
|
|
|
|
/// Convert to a `TransformService`
|
|
|
|
fn into_transform(self) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait for types that can be converted to a TransfromNewService
|
2019-03-02 21:16:30 +01:00
|
|
|
pub trait IntoNewTransform<T, S, C = ()>
|
2019-02-03 19:42:27 +01:00
|
|
|
where
|
2019-03-02 21:16:30 +01:00
|
|
|
T: NewTransform<S, C>,
|
2019-02-03 19:42:27 +01:00
|
|
|
{
|
|
|
|
/// Convert to an `TranformNewService`
|
|
|
|
fn into_new_transform(self) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, S> IntoTransform<T, S> for T
|
|
|
|
where
|
|
|
|
T: Transform<S>,
|
|
|
|
{
|
|
|
|
fn into_transform(self) -> T {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 21:16:30 +01:00
|
|
|
impl<T, S, C> IntoNewTransform<T, S, C> for T
|
2019-02-03 19:42:27 +01:00
|
|
|
where
|
2019-03-02 21:16:30 +01:00
|
|
|
T: NewTransform<S, C>,
|
2019-02-03 19:42:27 +01:00
|
|
|
{
|
|
|
|
fn into_new_transform(self) -> T {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|