Trait ntex::Transform[][src]

pub trait Transform<S> {
    type Service;
    fn new_transform(&self, service: S) -> Self::Service;
}
Expand description

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>;

    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:

pub struct TimeoutTransform {
    timeout: Duration,
}

impl<S> Transform<S> for TimeoutTransform<E>
where
    S: Service,
{
    type Transform = Timeout<S>;

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

Associated Types

The TransformService value created by this factory

Required methods

Creates and returns a new Transform component, asynchronously

Implementations on Foreign Types

Implementors