diff --git a/CHANGES.md b/CHANGES.md index 32f444ec1..0688e7fca 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,11 @@ # Changes ## Unreleased - 2021-xx-xx +### Added +* `Scoped` middleware enabling generic response body/error type of middlewares + like `Logger` and `Compress` to be used in `middleware::Condition` + and `Resource`, `Scope` services. [#1865] + ### Changed * Update `actix-*` dependencies to tokio `1.0` based versions. [#1813] * Bumped `rand` to `0.8`. @@ -9,7 +14,7 @@ * MSRV is now 1.46.0. [#1813]: https://github.com/actix/actix-web/pull/1813 - +[#1865]: https://github.com/actix/actix-web/pull/1865 ### Fixed * added the actual parsing error to `test::read_body_json` [#1812] diff --git a/src/middleware/scoped.rs b/src/middleware/scoped.rs index 54c5b49bb..aa9318769 100644 --- a/src/middleware/scoped.rs +++ b/src/middleware/scoped.rs @@ -41,15 +41,14 @@ impl Scoped { } } -impl Transform for Scoped +impl Transform for Scoped where - S: Service, - T: Transform, + S: Service, + T: Transform, T::Future: 'static, T::Response: MapServiceResponseBody, Error: From, { - type Request = T::Request; type Response = ServiceResponse; type Error = Error; type Transform = ScopedMiddleware; @@ -69,22 +68,21 @@ pub struct ScopedMiddleware { service: S, } -impl Service for ScopedMiddleware +impl Service for ScopedMiddleware where - S: Service, + S: Service, S::Response: MapServiceResponseBody, Error: From, { - type Request = S::Request; type Response = ServiceResponse; type Error = Error; - type Future = ScopedFuture; + type Future = ScopedFuture; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.service.poll_ready(cx).map_err(From::from) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Req) -> Self::Future { let fut = self.service.call(req); ScopedFuture { fut } } @@ -92,19 +90,16 @@ where #[doc(hidden)] #[pin_project::pin_project] -pub struct ScopedFuture -where - S: Service, -{ +pub struct ScopedFuture { #[pin] - fut: S::Future, + fut: Fut, } -impl Future for ScopedFuture +impl Future for ScopedFuture where - S: Service, - S::Response: MapServiceResponseBody, - Error: From, + Fut: Future>, + T: MapServiceResponseBody, + Error: From, { type Output = Result; @@ -114,8 +109,8 @@ where } } -// private trait for convert ServiceResponse's ResponseBody type -// to ResponseBody::Other(Body::Message) +// private trait for convert ServiceResponse's ResponseBody generic type +// to ResponseBody #[doc(hidden)] pub trait MapServiceResponseBody { fn map_body(self) -> ServiceResponse; @@ -137,8 +132,7 @@ mod tests { use crate::http::StatusCode; use crate::middleware::{Compress, Condition, Logger}; use crate::test::{call_service, init_service, TestRequest}; - use crate::App; - use crate::{web, HttpResponse}; + use crate::{web, App, HttpResponse}; #[actix_rt::test] async fn test_scope_middleware() {