Service trait — common pattern for request/response.
Definition:
1trait Service<Request> {2 type Response;3 type Error;4 type Future: Future<Output = Result<Self::Response, Self::Error>>;56 fn call(&self, req: Request) -> Self::Future;7}
Implementation:
1struct MyService;23impl Service<Request> for MyService {4 type Response = Response;5 type Error = Error;6 type Future = Pin<Box<dyn Future<Output = Result<Response, Error>>>>;78 fn call(&self, req: Request) -> Self::Future {9 Box::pin(async move {10 Ok(Response::new())11 })12 }13}
Key: Service trait for middleware/layers.