2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2019-03-02 21:16:30 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::task::{Context, Poll};
|
2019-03-02 21:16:30 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use crate::{IntoServiceFactory, Service, ServiceFactory};
|
2019-02-03 19:42:27 +01:00
|
|
|
|
2019-11-20 19:35:44 +01:00
|
|
|
/// Apply transform to a service. Function returns
|
|
|
|
/// services factory that in initialization creates
|
|
|
|
/// service and applies transform to this service.
|
|
|
|
pub fn apply<T, S, U>(t: T, service: U) -> ApplyTransform<T, S>
|
|
|
|
where
|
|
|
|
S: ServiceFactory,
|
|
|
|
T: Transform<S::Service, InitError = S::InitError>,
|
|
|
|
U: IntoServiceFactory<S>,
|
|
|
|
{
|
|
|
|
ApplyTransform::new(t, service.into_factory())
|
|
|
|
}
|
|
|
|
|
2019-07-17 06:30:59 +02:00
|
|
|
/// The `Transform` trait defines the interface of a Service factory. `Transform`
|
|
|
|
/// is often implemented for middleware, defining how to construct a
|
|
|
|
/// middleware Service. A Service that is constructed by the factory takes
|
2019-04-04 17:40:28 +02:00
|
|
|
/// the Service that follows it during execution as a parameter, assuming
|
2019-07-17 06:30:59 +02:00
|
|
|
/// ownership of the next Service.
|
2019-03-09 15:36:23 +01:00
|
|
|
pub trait Transform<S> {
|
|
|
|
/// Requests handled by the service.
|
|
|
|
type Request;
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
/// Responses given by the service.
|
|
|
|
type Response;
|
|
|
|
|
|
|
|
/// Errors produced by the service.
|
|
|
|
type Error;
|
|
|
|
|
|
|
|
/// The `TransformService` value created by this factory
|
2019-03-09 15:36:23 +01:00
|
|
|
type Transform: Service<
|
|
|
|
Request = Self::Request,
|
|
|
|
Response = Self::Response,
|
|
|
|
Error = Self::Error,
|
|
|
|
>;
|
2019-02-03 19:42:27 +01:00
|
|
|
|
|
|
|
/// Errors produced while building a service.
|
|
|
|
type InitError;
|
|
|
|
|
|
|
|
/// The future response value.
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future: Future<Output = Result<Self::Transform, Self::InitError>>;
|
2019-02-03 19:42:27 +01:00
|
|
|
|
2019-04-04 20:02:53 +02:00
|
|
|
/// Creates and returns a new Service component, asynchronously
|
2019-03-05 04:38:11 +01:00
|
|
|
fn new_transform(&self, service: S) -> Self::Future;
|
2019-02-03 19:42:27 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<T, S> Transform<S> for Rc<T>
|
2019-02-03 19:42:27 +01:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
T: Transform<S>,
|
2019-03-02 21:16:30 +01:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = T::Request;
|
2019-03-02 21:16:30 +01:00
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type InitError = T::InitError;
|
2019-03-05 04:38:11 +01:00
|
|
|
type Transform = T::Transform;
|
2019-03-02 21:16:30 +01:00
|
|
|
type Future = T::Future;
|
|
|
|
|
2019-03-05 04:38:11 +01:00
|
|
|
fn new_transform(&self, service: S) -> T::Future {
|
|
|
|
self.as_ref().new_transform(service)
|
2019-03-02 21:16:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<T, S> Transform<S> for Arc<T>
|
2019-03-02 21:16:30 +01:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
T: Transform<S>,
|
2019-03-02 21:16:30 +01:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = T::Request;
|
2019-03-02 21:16:30 +01:00
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type InitError = T::InitError;
|
2019-03-05 04:38:11 +01:00
|
|
|
type Transform = T::Transform;
|
2019-03-02 21:16:30 +01:00
|
|
|
type Future = T::Future;
|
|
|
|
|
2019-03-05 04:38:11 +01:00
|
|
|
fn new_transform(&self, service: S) -> T::Future {
|
|
|
|
self.as_ref().new_transform(service)
|
2019-03-02 21:16:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 20:53:08 +01:00
|
|
|
/// `Apply` transform to new service
|
2019-11-18 09:30:04 +01:00
|
|
|
pub struct ApplyTransform<T, S> {
|
2019-03-12 20:53:08 +01:00
|
|
|
s: Rc<S>,
|
2019-03-05 06:24:47 +01:00
|
|
|
t: Rc<T>,
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<T, S> ApplyTransform<T, S>
|
2019-03-05 06:24:47 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
S: ServiceFactory,
|
2019-03-12 20:53:08 +01:00
|
|
|
T: Transform<S::Service, InitError = S::InitError>,
|
2019-03-05 06:24:47 +01:00
|
|
|
{
|
2019-03-12 20:53:08 +01:00
|
|
|
/// Create new `ApplyTransform` new service instance
|
2019-11-14 13:38:24 +01:00
|
|
|
fn new(t: T, service: S) -> Self {
|
2019-03-05 06:24:47 +01:00
|
|
|
Self {
|
2019-03-12 20:53:08 +01:00
|
|
|
s: Rc::new(service),
|
2019-11-14 13:38:24 +01:00
|
|
|
t: Rc::new(t),
|
2019-03-05 06:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<T, S> Clone for ApplyTransform<T, S> {
|
2019-03-12 20:53:08 +01:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ApplyTransform {
|
|
|
|
s: self.s.clone(),
|
|
|
|
t: self.t.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<T, S> ServiceFactory for ApplyTransform<T, S>
|
2019-03-05 06:24:47 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
S: ServiceFactory,
|
2019-03-12 20:53:08 +01:00
|
|
|
T: Transform<S::Service, InitError = S::InitError>,
|
2019-03-05 06:24:47 +01:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = T::Request;
|
2019-03-05 06:24:47 +01:00
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = S::Config;
|
2019-03-05 06:24:47 +01:00
|
|
|
type Service = T::Transform;
|
|
|
|
type InitError = T::InitError;
|
2019-05-12 15:03:50 +02:00
|
|
|
type Future = ApplyTransformFuture<T, S>;
|
2019-03-05 06:24:47 +01:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
fn new_service(&self, cfg: &S::Config) -> Self::Future {
|
2019-03-05 06:24:47 +01:00
|
|
|
ApplyTransformFuture {
|
|
|
|
t_cell: self.t.clone(),
|
2019-11-14 13:38:24 +01:00
|
|
|
fut_a: self.s.new_service(cfg),
|
2019-03-05 06:24:47 +01:00
|
|
|
fut_t: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
#[pin_project::pin_project]
|
2019-11-18 09:30:04 +01:00
|
|
|
pub struct ApplyTransformFuture<T, S>
|
2019-03-05 06:24:47 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
S: ServiceFactory,
|
2019-03-12 20:53:08 +01:00
|
|
|
T: Transform<S::Service, InitError = S::InitError>,
|
2019-03-05 06:24:47 +01:00
|
|
|
{
|
2019-11-19 09:51:40 +01:00
|
|
|
#[pin]
|
2019-03-12 20:53:08 +01:00
|
|
|
fut_a: S::Future,
|
2019-11-19 09:51:40 +01:00
|
|
|
#[pin]
|
2019-11-14 13:38:24 +01:00
|
|
|
fut_t: Option<T::Future>,
|
2019-03-05 06:24:47 +01:00
|
|
|
t_cell: Rc<T>,
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<T, S> Future for ApplyTransformFuture<T, S>
|
2019-03-05 06:24:47 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
S: ServiceFactory,
|
2019-03-12 20:53:08 +01:00
|
|
|
T: Transform<S::Service, InitError = S::InitError>,
|
2019-03-05 06:24:47 +01:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = Result<T::Transform, T::InitError>;
|
2019-03-05 06:24:47 +01:00
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let mut this = self.as_mut().project();
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
if let Some(fut) = this.fut_t.as_pin_mut() {
|
|
|
|
return fut.poll(cx);
|
2019-03-05 06:24:47 +01:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-11-19 09:51:40 +01:00
|
|
|
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
|
|
|
|
let fut = this.t_cell.new_transform(service);
|
|
|
|
this = self.as_mut().project();
|
|
|
|
this.fut_t.set(Some(fut));
|
|
|
|
this.fut_t.as_pin_mut().unwrap().poll(cx)
|
2019-03-05 06:24:47 +01:00
|
|
|
} else {
|
2019-11-19 09:51:40 +01:00
|
|
|
return Poll::Pending;
|
2019-03-05 06:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|