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

pub trait Transform<S> where
    <Self::Transform as Service>::Request == Self::Request,
    <Self::Transform as Service>::Response == Self::Response,
    <Self::Transform as Service>::Error == Self::Error,
    <Self::Future as Future>::Output == Result<Self::Transform, Self::InitError>, 
{ type Request; type Response; type Error; type Transform: Service; type InitError; type Future: Future; fn new_transform(&self, service: S) -> Self::Future; fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, 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:

This example is not tested
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>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        ready!(self.service.poll_ready(cx)).map_err(TimeoutError::Service)
    }

    fn call(&mut 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:

This example is not tested
pub struct TimeoutTransform {
    timeout: Duration,
}

impl<S> Transform<S> for TimeoutTransform<E>
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 Request

Requests handled by the service.

type Response

Responses given by the service.

type Error

Errors produced by the service.

type Transform: Service

The TransformService value created by this factory

type InitError

Errors produced while building a transform service.

type Future: Future

The future response value.

Loading content...

Required methods

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

Creates and returns a new Transform component, asynchronously

Loading content...

Provided methods

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

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

Loading content...

Implementations on Foreign Types

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

type Request = <T as Transform<S>>::Request

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

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

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

type InitError = E

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

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

type Request = <T as Transform<S>>::Request

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

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

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

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

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

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

type Request = <T as Transform<S>>::Request

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

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

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

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

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

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

type Request = <S as Service>::Request

type Response = <S as Service>::Response

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

type InitError = E

type Transform = TimeoutService<S>

type Future = Ready<Result<<Timeout<E> as Transform<S>>::Transform, <Timeout<E> as Transform<S>>::InitError>>

impl<S> Transform<S> for InFlight where
    S: Service
[src]

type Request = <S as Service>::Request

type Response = <S as Service>::Response

type Error = <S as Service>::Error

type InitError = !

type Transform = InFlightService<S>

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

impl<S> Transform<S> for InOrder<S> where
    S: Service,
    <S as Service>::Response: 'static,
    <S as Service>::Future: 'static,
    <S as Service>::Error: 'static, 
[src]

type Request = <S as Service>::Request

type Response = <S as Service>::Response

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

type InitError = !

type Transform = InOrderService<S>

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

Loading content...

Implementors

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

type Request = ServiceRequest

type Response = ServiceResponse<B>

type Error = Error

type InitError = ()

type Transform = ErrorHandlersMiddleware<S, B>

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

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

type Request = ServiceRequest

type Response = ServiceResponse<Encoder<B>>

type Error = Error

type InitError = ()

type Transform = CompressMiddleware<S>

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

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

type Request = ServiceRequest

type Response = ServiceResponse<B>

type Error = Error

type InitError = ()

type Transform = DefaultHeadersMiddleware<S>

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

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

type Request = ServiceRequest

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> for NormalizePath where
    S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static, 
[src]

type Request = ServiceRequest

type Response = ServiceResponse<B>

type Error = Error

type InitError = ()

type Transform = NormalizePathNormalization<S>

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

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

type Request = S::Request

type Response = S::Response

type Error = S::Error

type InitError = T::InitError

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

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

Loading content...