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

99 lines
2.3 KiB
Rust
Raw Normal View History

use std::rc::Rc;
use std::sync::Arc;
2019-03-05 05:29:35 +01:00
use futures::IntoFuture;
use crate::transform_map_init_err::TransformMapInitErr;
use crate::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.
/// `Config` is a service factory configuration type.
2019-03-05 04:38:11 +01:00
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-05 04:38:11 +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 05:29:35 +01:00
type Future: IntoFuture<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 04:38:11 +01:00
impl<T, S> Transform<S> for Rc<T>
where
2019-03-05 04:38:11 +01:00
T: Transform<S>,
{
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
impl<T, S> Transform<S> for Arc<T>
where
2019-03-05 04:38:11 +01:00
T: Transform<S>,
{
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*
pub trait IntoTransform<T, S>
where
T: Transform<S>,
{
/// Convert to a `TransformService`
fn into_transform(self) -> T;
}
impl<T, S> IntoTransform<T, S> for T
where
T: Transform<S>,
{
fn into_transform(self) -> T {
self
}
}