1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-03 20:52:13 +01:00
actix-net/actix-service/src/transform.rs

168 lines
4.3 KiB
Rust
Raw Normal View History

use std::rc::Rc;
use std::sync::Arc;
2019-03-05 06:24:47 +01:00
use futures::{Async, Future, IntoFuture, Poll};
use crate::transform_map_init_err::TransformMapInitErr;
2019-03-05 06:24:47 +01:00
use crate::{NewService, Service};
2019-03-05 04:38:11 +01:00
/// `Transform` service factory.
///
2019-03-05 04:38:11 +01:00
/// Transform factory creates service that wraps other services.
2019-03-05 16:35:26 +01:00
///
/// * `S` is a wrapped service.
/// * `R` requests handled by this transform service.
pub trait Transform<R, S> {
/// Responses given by the service.
type Response;
/// Errors produced by the service.
type Error;
/// The `TransformService` value created by this factory
2019-03-05 16:35:26 +01:00
type Transform: Service<R, Response = Self::Response, Error = Self::Error>;
/// Errors produced while building a service.
type InitError;
/// The future response value.
2019-03-05 06:35:47 +01:00
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
/// Create and return a new service value asynchronously.
2019-03-05 04:38:11 +01:00
fn new_transform(&self, service: S) -> Self::Future;
/// Map this service's factory init error to a different error,
/// returning a new transform service factory.
2019-03-05 04:38:11 +01:00
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
where
Self: Sized,
F: Fn(Self::InitError) -> E,
{
TransformMapInitErr::new(self, f)
}
}
2019-03-05 16:35:26 +01:00
impl<T, R, S> Transform<R, S> for Rc<T>
where
2019-03-05 16:35:26 +01:00
T: Transform<R, S>,
{
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
2019-03-05 04:38:11 +01:00
type Transform = T::Transform;
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-05 16:35:26 +01:00
impl<T, R, S> Transform<R, S> for Arc<T>
where
2019-03-05 16:35:26 +01:00
T: Transform<R, S>,
{
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
2019-03-05 04:38:11 +01:00
type Transform = T::Transform;
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-05 04:38:11 +01:00
/// Trait for types that can be converted to a *transform service*
2019-03-05 16:35:26 +01:00
pub trait IntoTransform<T, R, S>
where
2019-03-05 16:35:26 +01:00
T: Transform<R, S>,
{
/// Convert to a `TransformService`
fn into_transform(self) -> T;
}
2019-03-05 16:35:26 +01:00
impl<T, S, R> IntoTransform<T, S, R> for T
where
2019-03-05 16:35:26 +01:00
T: Transform<S, R>,
{
fn into_transform(self) -> T {
self
}
}
2019-03-05 06:24:47 +01:00
/// `Apply` transform new service
#[derive(Clone)]
2019-03-05 16:35:26 +01:00
pub struct ApplyTransform<T, R, S, Req, Cfg> {
a: S,
2019-03-05 06:24:47 +01:00
t: Rc<T>,
2019-03-05 16:35:26 +01:00
_t: std::marker::PhantomData<(R, Req, Cfg)>,
2019-03-05 06:24:47 +01:00
}
2019-03-05 16:35:26 +01:00
impl<T, R, S, Req, Cfg> ApplyTransform<T, R, S, Req, Cfg>
2019-03-05 06:24:47 +01:00
where
2019-03-05 16:35:26 +01:00
S: NewService<Req, Cfg>,
T: Transform<R, S::Service, Error = S::Error, InitError = S::InitError>,
2019-03-05 06:24:47 +01:00
{
/// Create new `ApplyNewService` new service instance
2019-03-05 16:35:26 +01:00
pub fn new<F: IntoTransform<T, R, S::Service>>(t: F, a: S) -> Self {
2019-03-05 06:24:47 +01:00
Self {
a,
t: Rc::new(t.into_transform()),
_t: std::marker::PhantomData,
}
}
}
2019-03-05 16:35:26 +01:00
impl<T, R, S, Req, Cfg> NewService<R, Cfg> for ApplyTransform<T, R, S, Req, Cfg>
2019-03-05 06:24:47 +01:00
where
2019-03-05 16:35:26 +01:00
S: NewService<Req, Cfg>,
T: Transform<R, S::Service, Error = S::Error, InitError = S::InitError>,
2019-03-05 06:24:47 +01:00
{
type Response = T::Response;
type Error = T::Error;
type Service = T::Transform;
type InitError = T::InitError;
2019-03-05 16:35:26 +01:00
type Future = ApplyTransformFuture<T, R, S, Req, Cfg>;
2019-03-05 06:24:47 +01:00
2019-03-05 16:35:26 +01:00
fn new_service(&self, cfg: &Cfg) -> Self::Future {
2019-03-05 06:24:47 +01:00
ApplyTransformFuture {
t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg).into_future(),
fut_t: None,
}
}
}
2019-03-05 16:35:26 +01:00
pub struct ApplyTransformFuture<T, R, S, Req, Cfg>
2019-03-05 06:24:47 +01:00
where
2019-03-05 16:35:26 +01:00
S: NewService<Req, Cfg>,
T: Transform<R, S::Service, Error = S::Error, InitError = S::InitError>,
2019-03-05 06:24:47 +01:00
{
2019-03-05 16:35:26 +01:00
fut_a: S::Future,
fut_t: Option<T::Future>,
2019-03-05 06:24:47 +01:00
t_cell: Rc<T>,
}
2019-03-05 16:35:26 +01:00
impl<T, R, S, Req, Cfg> Future for ApplyTransformFuture<T, R, S, Req, Cfg>
2019-03-05 06:24:47 +01:00
where
2019-03-05 16:35:26 +01:00
S: NewService<Req, Cfg>,
T: Transform<R, S::Service, Error = S::Error, InitError = S::InitError>,
2019-03-05 06:24:47 +01:00
{
type Item = T::Transform;
type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if self.fut_t.is_none() {
if let Async::Ready(service) = self.fut_a.poll()? {
self.fut_t = Some(self.t_cell.new_transform(service).into_future());
}
}
if let Some(ref mut fut) = self.fut_t {
fut.poll()
} else {
Ok(Async::NotReady)
}
}
}