Trait actix_service::Transform [−][src]
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: S::Request) -> 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, })) } }
Associated Types
type Response
[src]
Responses produced by the service.
type Error
[src]
Errors produced by the service.
type Transform: Service<Req, Response = Self::Response, Error = Self::Error>
[src]
The TransformService
value created by this factory
type InitError
[src]
Errors produced while building a transform service.
type Future: Future<Output = Result<Self::Transform, Self::InitError>>
[src]
The future response value.
Required methods
fn new_transform(&self, service: S) -> Self::Future
[src]
Creates and returns a new Transform component, asynchronously
Implementations on Foreign Types
impl<T, S, Req> Transform<S, Req> for Rc<T> where
T: Transform<S, Req>,
[src]
T: Transform<S, Req>,
type Response = T::Response
type Error = T::Error
type Transform = T::Transform
type InitError = T::InitError
type Future = T::Future
fn new_transform(&self, service: S) -> T::Future
[src]
impl<T, S, Req> Transform<S, Req> for Arc<T> where
T: Transform<S, Req>,
[src]
T: Transform<S, Req>,