Trait actix_web::dev::Transform[][src]

pub trait Transform<S, Req> {
    type Response;
    type Error;
    type Transform: Service<Req>;
    type InitError;
    type Future: Future;
    pub fn new_transform(&self, service: S) -> Self::Future;

    pub fn map_init_err<F, E>(
        self,
        f: F
    ) -> TransformMapInitErr<Self, S, Req, F, E>
    where
        F: Fn(Self::InitError) -> E + Clone
, { ... } }

The Transform trait defines the interface of a service factory that wraps inner service during construction.

Transform(middleware) wraps inner service and runs during inbound and/or outbound processing in the request/response lifecycle. It may modify request and/or response.

For example, timeout transform:

pub struct Timeout<S> {
    service: S,
    timeout: Duration,
}

impl<S> Service for Timeout<S>
where
    S: Service,
{
    type Request = S::Request;
    type Response = S::Response;
    type Error = TimeoutError<S::Error>;
    type Future = TimeoutServiceResponse<S>;

    actix_service::forward_ready!(service);

    fn call(&self, req: S::Request) -> Self::Future {
        TimeoutServiceResponse {
            fut: self.service.call(req),
            sleep: Delay::new(clock::now() + self.timeout),
        }
    }
}

Timeout service in above example is decoupled from underlying service implementation and could be applied to any service.

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 the Service that follows it during execution as a parameter, assuming ownership of the next Service.

Factory for Timeout middleware from the above example could look like this:

pub struct TimeoutTransform {
    timeout: Duration,
}

impl<S> Transform<S> for TimeoutTransform
where
    S: Service,
{
    type Request = S::Request;
    type Response = S::Response;
    type Error = TimeoutError<S::Error>;
    type InitError = S::Error;
    type Transform = Timeout<S>;
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ok(TimeoutService {
            service,
            timeout: self.timeout,
        })
    }
}

Associated Types

type Response[src]

Responses given by the service.

type Error[src]

Errors produced by the service.

type Transform: Service<Req>[src]

The TransformService value created by this factory

type InitError[src]

Errors produced while building a transform service.

type Future: Future[src]

The future response value.

Loading content...

Required methods

pub fn new_transform(&self, service: S) -> Self::Future[src]

Creates and returns a new Transform component, asynchronously

Loading content...

Provided methods

pub fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, Req, F, E> where
    F: Fn(Self::InitError) -> E + Clone
[src]

Map this transform's factory error to a different error, returning a new transform service factory.

Loading content...

Implementations on Foreign Types

impl<T, S, Req> Transform<S, Req> for Arc<T> where
    T: Transform<S, Req>, 
[src]

type Response = <T as Transform<S, Req>>::Response

type Error = <T as Transform<S, Req>>::Error

type Transform = <T as Transform<S, Req>>::Transform

type InitError = <T as Transform<S, Req>>::InitError

type Future = <T as Transform<S, Req>>::Future

impl<T, S, F, E, Req> Transform<S, Req> for TransformMapInitErr<T, S, Req, F, E> where
    T: Transform<S, Req>,
    F: Fn(<T as Transform<S, Req>>::InitError) -> E + Clone
[src]

type Response = <T as Transform<S, Req>>::Response

type Error = <T as Transform<S, Req>>::Error

type Transform = <T as Transform<S, Req>>::Transform

type InitError = E

type Future = TransformMapInitErrFuture<T, S, F, E, Req>

impl<T, S, Req> Transform<S, Req> for Rc<T> where
    T: Transform<S, Req>, 
[src]

type Response = <T as Transform<S, Req>>::Response

type Error = <T as Transform<S, Req>>::Error

type Transform = <T as Transform<S, Req>>::Transform

type InitError = <T as Transform<S, Req>>::InitError

type Future = <T as Transform<S, Req>>::Future

impl<S, E, Req> Transform<S, Req> for Timeout<E> where
    S: Service<Req>, 
[src]

type Response = <S as Service<Req>>::Response

type Error = TimeoutError<<S as Service<Req>>::Error>

type Transform = TimeoutService<S, Req>

type InitError = E

type Future = TimeoutFuture<<Timeout<E> as Transform<S, Req>>::Transform, <Timeout<E> as Transform<S, Req>>::InitError>

Loading content...

Implementors

impl<S, B> Transform<S, ServiceRequest> for Compress where
    B: MessageBody,
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, 
[src]

type Response = ServiceResponse<Encoder<B>>

type Error = Error

type Transform = CompressMiddleware<S>

type InitError = ()

type Future = Ready<Result<Self::Transform, Self::InitError>>

impl<S, B> Transform<S, ServiceRequest> for DefaultHeaders where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static, 
[src]

type Response = ServiceResponse<B>

type Error = Error

type Transform = DefaultHeadersMiddleware<S>

type InitError = ()

type Future = Ready<Result<Self::Transform, Self::InitError>>

impl<S, B> Transform<S, ServiceRequest> for ErrorHandlers<B> where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
    S::Future: 'static,
    B: 'static, 
[src]

type Response = ServiceResponse<B>

type Error = Error

type Transform = ErrorHandlersMiddleware<S, B>

type InitError = ()

type Future = LocalBoxFuture<'static, Result<Self::Transform, Self::InitError>>

impl<S, B> Transform<S, ServiceRequest> for Logger where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    B: MessageBody
[src]

type Response = ServiceResponse<StreamLog<B>>

type Error = Error

type InitError = ()

type Transform = LoggerMiddleware<S>

type Future = Ready<Result<Self::Transform, Self::InitError>>

impl<S, B> Transform<S, ServiceRequest> for NormalizePath where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static, 
[src]

type Response = ServiceResponse<B>

type Error = Error

type Transform = NormalizePathNormalization<S>

type InitError = ()

type Future = Ready<Result<Self::Transform, Self::InitError>>

impl<S, T, Req> Transform<S, Req> for Compat<T> where
    S: Service<Req>,
    T: Transform<S, Req>,
    T::Future: 'static,
    T::Response: MapServiceResponseBody,
    Error: From<T::Error>, 
[src]

type Response = ServiceResponse

type Error = Error

type Transform = CompatMiddleware<T::Transform>

type InitError = T::InitError

type Future = LocalBoxFuture<'static, Result<Self::Transform, Self::InitError>>

impl<S, T, Req> Transform<S, Req> for Condition<T> where
    S: Service<Req> + 'static,
    T: Transform<S, Req, Response = S::Response, Error = S::Error>,
    T::Future: 'static,
    T::InitError: 'static,
    T::Transform: 'static, 
[src]

type Response = S::Response

type Error = S::Error

type Transform = ConditionMiddleware<T::Transform, S>

type InitError = T::InitError

type Future = LocalBoxFuture<'static, Result<Self::Transform, Self::InitError>>

Loading content...