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

176 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-09 15:36:23 +01:00
/// `Config` is a service factory configuration type.
pub trait Transform<S> {
/// 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
2019-03-09 15:36:23 +01:00
type Transform: Service<
Request = Self::Request,
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-09 15:36:23 +01:00
impl<T, S> Transform<S> for Rc<T>
where
2019-03-09 15:36:23 +01:00
T: Transform<S>,
{
2019-03-09 15:36:23 +01:00
type Request = T::Request;
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-09 15:36:23 +01:00
impl<T, S> Transform<S> for Arc<T>
where
2019-03-09 15:36:23 +01:00
T: Transform<S>,
{
2019-03-09 15:36:23 +01:00
type Request = T::Request;
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-09 15:36:23 +01:00
pub trait IntoTransform<T, S>
where
2019-03-09 15:36:23 +01:00
T: Transform<S>,
{
/// Convert to a `TransformService`
fn into_transform(self) -> T;
}
2019-03-09 15:36:23 +01:00
impl<T, S> IntoTransform<T, S> for T
where
2019-03-09 15:36:23 +01:00
T: Transform<S>,
{
fn into_transform(self) -> T {
self
}
}
2019-03-05 06:24:47 +01:00
/// `Apply` transform new service
#[derive(Clone)]
2019-03-09 15:36:23 +01:00
pub struct ApplyTransform<T, A, C> {
a: A,
2019-03-05 06:24:47 +01:00
t: Rc<T>,
2019-03-09 15:36:23 +01:00
_t: std::marker::PhantomData<C>,
2019-03-05 06:24:47 +01:00
}
2019-03-09 15:36:23 +01:00
impl<T, A, C> ApplyTransform<T, A, C>
2019-03-05 06:24:47 +01:00
where
2019-03-09 15:36:23 +01:00
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
2019-03-05 06:24:47 +01:00
{
/// Create new `ApplyNewService` new service instance
2019-03-09 15:36:23 +01:00
pub fn new<F: IntoTransform<T, A::Service>>(t: F, a: A) -> Self {
2019-03-05 06:24:47 +01:00
Self {
a,
t: Rc::new(t.into_transform()),
_t: std::marker::PhantomData,
}
}
}
2019-03-09 15:36:23 +01:00
impl<T, A, C> NewService<C> for ApplyTransform<T, A, C>
2019-03-05 06:24:47 +01:00
where
2019-03-09 15:36:23 +01:00
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::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;
type Service = T::Transform;
type InitError = T::InitError;
2019-03-09 15:36:23 +01:00
type Future = ApplyTransformFuture<T, A, C>;
2019-03-05 06:24:47 +01:00
2019-03-09 15:36:23 +01:00
fn new_service(&self, cfg: &C) -> 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-09 15:36:23 +01:00
pub struct ApplyTransformFuture<T, A, C>
2019-03-05 06:24:47 +01:00
where
2019-03-09 15:36:23 +01:00
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
2019-03-05 06:24:47 +01:00
{
2019-03-09 15:36:23 +01:00
fut_a: A::Future,
fut_t: Option<<T::Future as IntoFuture>::Future>,
2019-03-05 06:24:47 +01:00
t_cell: Rc<T>,
}
2019-03-09 15:36:23 +01:00
impl<T, A, C> Future for ApplyTransformFuture<T, A, C>
2019-03-05 06:24:47 +01:00
where
2019-03-09 15:36:23 +01:00
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::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)
}
}
}