update service trait and change log

This commit is contained in:
fakeshadow 2021-01-04 13:20:45 +08:00
parent 589abb76f6
commit dd598f7b28
2 changed files with 22 additions and 23 deletions

View File

@ -1,6 +1,11 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## 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 ### Changed
* Update `actix-*` dependencies to tokio `1.0` based versions. [#1813] * Update `actix-*` dependencies to tokio `1.0` based versions. [#1813]
* Bumped `rand` to `0.8`. * Bumped `rand` to `0.8`.
@ -9,7 +14,7 @@
* MSRV is now 1.46.0. * MSRV is now 1.46.0.
[#1813]: https://github.com/actix/actix-web/pull/1813 [#1813]: https://github.com/actix/actix-web/pull/1813
[#1865]: https://github.com/actix/actix-web/pull/1865
### Fixed ### Fixed
* added the actual parsing error to `test::read_body_json` [#1812] * added the actual parsing error to `test::read_body_json` [#1812]

View File

@ -41,15 +41,14 @@ impl<T> Scoped<T> {
} }
} }
impl<S, T> Transform<S> for Scoped<T> impl<S, T, Req> Transform<S, Req> for Scoped<T>
where where
S: Service, S: Service<Req>,
T: Transform<S>, T: Transform<S, Req>,
T::Future: 'static, T::Future: 'static,
T::Response: MapServiceResponseBody, T::Response: MapServiceResponseBody,
Error: From<T::Error>, Error: From<T::Error>,
{ {
type Request = T::Request;
type Response = ServiceResponse; type Response = ServiceResponse;
type Error = Error; type Error = Error;
type Transform = ScopedMiddleware<T::Transform>; type Transform = ScopedMiddleware<T::Transform>;
@ -69,22 +68,21 @@ pub struct ScopedMiddleware<S> {
service: S, service: S,
} }
impl<S> Service for ScopedMiddleware<S> impl<S, Req> Service<Req> for ScopedMiddleware<S>
where where
S: Service, S: Service<Req>,
S::Response: MapServiceResponseBody, S::Response: MapServiceResponseBody,
Error: From<S::Error>, Error: From<S::Error>,
{ {
type Request = S::Request;
type Response = ServiceResponse; type Response = ServiceResponse;
type Error = Error; type Error = Error;
type Future = ScopedFuture<S>; type Future = ScopedFuture<S::Future>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx).map_err(From::from) 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); let fut = self.service.call(req);
ScopedFuture { fut } ScopedFuture { fut }
} }
@ -92,19 +90,16 @@ where
#[doc(hidden)] #[doc(hidden)]
#[pin_project::pin_project] #[pin_project::pin_project]
pub struct ScopedFuture<S> pub struct ScopedFuture<Fut> {
where
S: Service,
{
#[pin] #[pin]
fut: S::Future, fut: Fut,
} }
impl<S> Future for ScopedFuture<S> impl<Fut, T, E> Future for ScopedFuture<Fut>
where where
S: Service, Fut: Future<Output = Result<T, E>>,
S::Response: MapServiceResponseBody, T: MapServiceResponseBody,
Error: From<S::Error>, Error: From<E>,
{ {
type Output = Result<ServiceResponse, Error>; type Output = Result<ServiceResponse, Error>;
@ -114,8 +109,8 @@ where
} }
} }
// private trait for convert ServiceResponse's ResponseBody<B> type // private trait for convert ServiceResponse's ResponseBody<B> generic type
// to ResponseBody::Other(Body::Message) // to ResponseBody<Body>
#[doc(hidden)] #[doc(hidden)]
pub trait MapServiceResponseBody { pub trait MapServiceResponseBody {
fn map_body(self) -> ServiceResponse; fn map_body(self) -> ServiceResponse;
@ -137,8 +132,7 @@ mod tests {
use crate::http::StatusCode; use crate::http::StatusCode;
use crate::middleware::{Compress, Condition, Logger}; use crate::middleware::{Compress, Condition, Logger};
use crate::test::{call_service, init_service, TestRequest}; use crate::test::{call_service, init_service, TestRequest};
use crate::App; use crate::{web, App, HttpResponse};
use crate::{web, HttpResponse};
#[actix_rt::test] #[actix_rt::test]
async fn test_scope_middleware() { async fn test_scope_middleware() {