actix_web::dev

Trait Transform

source
pub trait Transform<S, Req> {
    type Response;
    type Error;
    type Transform: Service<Req, Response = Self::Response, Error = Self::Error>;
    type InitError;
    type Future: Future<Output = Result<Self::Transform, Self::InitError>>;

    // Required method
    fn new_transform(&self, service: S) -> Self::Future;
}
Expand description

Defines the interface of a service factory that wraps inner service during construction.

Transformers wrap an inner service and runs during inbound and/or outbound processing in the service lifecycle. It may modify request and/or response.

For example, a timeout service wrapper:

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

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

    actix_service::forward_ready!(service);

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

This wrapper service is decoupled from the underlying service implementation and could be applied to any service.

The Transform trait defines the interface of a service wrapper. 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.

A transform for the Timeout middleware could look like this:

pub struct TimeoutTransform {
    timeout: Duration,
}

impl<S: Service<Req>, Req> Transform<S, Req> for TimeoutTransform {
    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 {
        ready(Ok(Timeout {
            service,
            timeout: self.timeout,
        }))
    }
}

Required Associated Types§

source

type Response

Responses produced by the service.

source

type Error

Errors produced by the service.

source

type Transform: Service<Req, Response = Self::Response, Error = Self::Error>

The TransformService value created by this factory

source

type InitError

Errors produced while building a transform service.

source

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

The future response value.

Required Methods§

source

fn new_transform(&self, service: S) -> Self::Future

Creates and returns a new Transform component, asynchronously

Implementations on Foreign Types§

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

fn new_transform(&self, service: S) -> <T as Transform<S, Req>>::Future

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

fn new_transform(&self, service: S) -> <T as Transform<S, Req>>::Future

Implementors§

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl<S, T, Req> Transform<S, Req> for Compat<T>
where S: Service<Req>, T: Transform<S, Req>, T::Future: 'static, T::Response: MapServiceResponseBody, T::Error: Into<Error>,

source§

type Response = ServiceResponse

source§

type Error = Error

source§

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

source§

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

source§

type Future = Pin<Box<dyn Future<Output = Result<<Compat<T> as Transform<S, Req>>::Transform, <Compat<T> as Transform<S, Req>>::InitError>>>>

source§

impl<S, T, Req, BE, BD, Err> Transform<S, Req> for Condition<T>
where S: Service<Req, Response = ServiceResponse<BD>, Error = Err> + 'static, T: Transform<S, Req, Response = ServiceResponse<BE>, Error = Err>, T::Future: 'static, T::InitError: 'static, T::Transform: 'static,

source§

type Response = ServiceResponse<EitherBody<BE, BD>>

source§

type Error = Err

source§

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

source§

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

source§

type Future = Pin<Box<dyn Future<Output = Result<<Condition<T> as Transform<S, Req>>::Transform, <Condition<T> as Transform<S, Req>>::InitError>>>>

source§

impl<S: Service<Req>, Req> Transform<S, Req> for Identity

source§

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

source§

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

source§

type Transform = IdentityMiddleware<S>

source§

type InitError = ()

source§

type Future = Ready<Result<<Identity as Transform<S, Req>>::Transform, <Identity as Transform<S, Req>>::InitError>>