From 06322bd6273a7fa55690e717c60543e5ebf3f5c7 Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Mon, 28 Sep 2020 01:15:38 +0800 Subject: [PATCH] remove MessageBody bound generic type from more types --- CHANGES.md | 8 ++++- actix-http/src/encoding/encoder.rs | 12 ++++---- actix-http/src/response.rs | 5 ++- src/app.rs | 31 ++++++++----------- src/app_service.rs | 49 +++++++++--------------------- src/middleware/compress.rs | 12 ++++---- src/middleware/logger.rs | 17 ++++++----- 7 files changed, 60 insertions(+), 74 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 93a285bfe..7ee62626e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,9 +2,15 @@ ## Unreleased - 2020-xx-xx ### Changed -* remove generic type `B` for `app::App` and it's not bound to `actix_http::body::MessageBody` trait anymore. [#1692] +* Remove generic type `B` for `app::App`, `app_service::{AppInit, AppInitResult, AppInitService}`. + Change `middleware::{Compressed, Logger}` to return `ServiceResponse` directly. [#1692] +* Add `TrailingSlash::MergeOnly` behaviour to `NormalizePath`, which allow `NormalizePath` + to keep the trailing slash's existance as it is. [#1695] +* Fix `ResourceMap` recursive references when printing/debugging. [#1708] [#1692]: https://github.com/actix/actix-web/pull/1692 +[#1695]: https://github.com/actix/actix-web/pull/1695 +[#1708]: https://github.com/actix/actix-web/pull/1708 ## 3.0.2 - 2020-09-15 diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index eb1821285..2d8cf519e 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -29,12 +29,12 @@ pub struct Encoder { fut: Option>, } -impl Encoder { +impl Encoder { pub fn response( encoding: ContentEncoding, head: &mut ResponseHead, body: ResponseBody, - ) -> ResponseBody> { + ) -> ResponseBody { let can_encode = !(head.headers().contains_key(&CONTENT_ENCODING) || head.status == StatusCode::SWITCHING_PROTOCOLS || head.status == StatusCode::NO_CONTENT @@ -62,20 +62,20 @@ impl Encoder { if let Some(enc) = ContentEncoder::encoder(encoding) { update_head(encoding, head); head.no_chunking(false); - return ResponseBody::Body(Encoder { + return ResponseBody::Other(Body::from_message(Encoder { body, eof: false, fut: None, encoder: Some(enc), - }); + })); } } - ResponseBody::Body(Encoder { + ResponseBody::Other(Body::from_message(Encoder { body, eof: false, fut: None, encoder: None, - }) + })) } } diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index 2def67168..b96293a0d 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -232,7 +232,10 @@ impl Response { } /// Set a body and return previous body value - pub(crate) fn replace_body(self, body: B2) -> (Response, ResponseBody) { + pub(crate) fn replace_body( + self, + body: B2, + ) -> (Response, ResponseBody) { ( Response { head: self.head, diff --git a/src/app.rs b/src/app.rs index cefd36869..13a25326d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,7 +3,6 @@ use std::fmt; use std::future::Future; use std::rc::Rc; -use actix_http::body::MessageBody; use actix_http::Extensions; use actix_service::boxed::{self, BoxServiceFactory}; use actix_service::{ @@ -27,7 +26,7 @@ type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Err /// Application builder - structure that follows the builder pattern /// for building application instances. -pub struct App { +pub struct App { endpoint: T, services: Vec>, default: Option>, @@ -38,7 +37,7 @@ pub struct App { extensions: Extensions, } -impl App { +impl App { /// Create application builder. Application can be configured with a builder-like pattern. #[allow(clippy::new_without_default)] pub fn new() -> Self { @@ -56,13 +55,12 @@ impl App { } } -impl App +impl App where - B: MessageBody, T: ServiceFactory< Config = (), Request = ServiceRequest, - Response = ServiceResponse, + Response = ServiceResponse, Error = Error, InitError = (), >, @@ -344,14 +342,14 @@ where /// .route("/index.html", web::get().to(index)); /// } /// ``` - pub fn wrap( + pub fn wrap( self, mw: M, ) -> App< impl ServiceFactory< Config = (), Request = ServiceRequest, - Response = ServiceResponse, + Response = ServiceResponse, Error = Error, InitError = (), >, @@ -360,11 +358,10 @@ where M: Transform< T::Service, Request = ServiceRequest, - Response = ServiceResponse, + Response = ServiceResponse, Error = Error, InitError = (), >, - B1: MessageBody, { App { endpoint: apply(mw, self.endpoint), @@ -409,22 +406,21 @@ where /// .route("/index.html", web::get().to(index)); /// } /// ``` - pub fn wrap_fn( + pub fn wrap_fn( self, mw: F, ) -> App< impl ServiceFactory< Config = (), Request = ServiceRequest, - Response = ServiceResponse, + Response = ServiceResponse, Error = Error, InitError = (), >, > where - B1: MessageBody, F: FnMut(ServiceRequest, &mut T::Service) -> R + Clone, - R: Future, Error>>, + R: Future>, { App { endpoint: apply_fn_factory(self.endpoint, mw), @@ -439,18 +435,17 @@ where } } -impl IntoServiceFactory> for App +impl IntoServiceFactory> for App where - B: MessageBody, T: ServiceFactory< Config = (), Request = ServiceRequest, - Response = ServiceResponse, + Response = ServiceResponse, Error = Error, InitError = (), >, { - fn into_factory(self) -> AppInit { + fn into_factory(self) -> AppInit { AppInit { data: Rc::new(self.data), data_factories: Rc::new(self.data_factories), diff --git a/src/app_service.rs b/src/app_service.rs index 98d8c8a8d..65ad6bf47 100644 --- a/src/app_service.rs +++ b/src/app_service.rs @@ -1,6 +1,5 @@ use std::cell::RefCell; use std::future::Future; -use std::marker::PhantomData; use std::pin::Pin; use std::rc::Rc; use std::task::{Context, Poll}; @@ -27,16 +26,7 @@ type BoxResponse = LocalBoxFuture<'static, Result>; /// Service factory to convert `Request` to a `ServiceRequest`. /// It also executes data factories. -pub struct AppInit -where - T: ServiceFactory< - Config = (), - Request = ServiceRequest, - Response = ServiceResponse, - Error = Error, - InitError = (), - >, -{ +pub struct AppInit { pub(crate) endpoint: T, pub(crate) extensions: RefCell>, pub(crate) data: Rc>>, @@ -47,23 +37,23 @@ where pub(crate) external: RefCell>, } -impl ServiceFactory for AppInit +impl ServiceFactory for AppInit where T: ServiceFactory< Config = (), Request = ServiceRequest, - Response = ServiceResponse, + Response = ServiceResponse, Error = Error, InitError = (), >, { type Config = AppConfig; type Request = Request; - type Response = ServiceResponse; + type Response = ServiceResponse; type Error = T::Error; type InitError = T::InitError; - type Service = AppInitService; - type Future = AppInitResult; + type Service = AppInitService; + type Future = AppInitResult; fn new_service(&self, config: AppConfig) -> Self::Future { // update resource default service @@ -125,13 +115,12 @@ where ), config, rmap, - _t: PhantomData, } } } #[pin_project::pin_project] -pub struct AppInitResult +pub struct AppInitResult where T: ServiceFactory, { @@ -149,21 +138,19 @@ where config: AppConfig, data: Rc>>, extensions: Option, - - _t: PhantomData, } -impl Future for AppInitResult +impl Future for AppInitResult where T: ServiceFactory< Config = (), Request = ServiceRequest, - Response = ServiceResponse, + Response = ServiceResponse, Error = Error, InitError = (), >, { - type Output = Result, ()>; + type Output = Result, ()>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); @@ -213,10 +200,7 @@ where } /// Service to convert `Request` to a `ServiceRequest` -pub struct AppInitService -where - T: Service, Error = Error>, -{ +pub struct AppInitService { service: T, rmap: Rc, config: AppConfig, @@ -224,12 +208,12 @@ where pool: &'static HttpRequestPool, } -impl Service for AppInitService +impl Service for AppInitService where - T: Service, Error = Error>, + T: Service, { type Request = Request; - type Response = ServiceResponse; + type Response = ServiceResponse; type Error = T::Error; type Future = T::Future; @@ -263,10 +247,7 @@ where } } -impl Drop for AppInitService -where - T: Service, Error = Error>, -{ +impl Drop for AppInitService { fn drop(&mut self) { self.pool.clear(); } diff --git a/src/middleware/compress.rs b/src/middleware/compress.rs index fe3ba841c..ee5beacea 100644 --- a/src/middleware/compress.rs +++ b/src/middleware/compress.rs @@ -53,11 +53,11 @@ impl Default for Compress { impl Transform for Compress where - B: MessageBody, + B: MessageBody + Unpin + 'static, S: Service, Error = Error>, { type Request = ServiceRequest; - type Response = ServiceResponse>; + type Response = ServiceResponse; type Error = Error; type InitError = (); type Transform = CompressMiddleware; @@ -78,11 +78,11 @@ pub struct CompressMiddleware { impl Service for CompressMiddleware where - B: MessageBody, + B: MessageBody + Unpin + 'static, S: Service, Error = Error>, { type Request = ServiceRequest; - type Response = ServiceResponse>; + type Response = ServiceResponse; type Error = Error; type Future = CompressResponse; @@ -126,10 +126,10 @@ where impl Future for CompressResponse where - B: MessageBody, + B: MessageBody + Unpin + 'static, S: Service, Error = Error>, { - type Output = Result>, Error>; + type Output = Result, Error>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index 51d4722d7..9abb86875 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -130,10 +130,10 @@ impl Default for Logger { impl Transform for Logger where S: Service, Error = Error>, - B: MessageBody, + B: MessageBody + Unpin + 'static, { type Request = ServiceRequest; - type Response = ServiceResponse>; + type Response = ServiceResponse; type Error = Error; type InitError = (); type Transform = LoggerMiddleware; @@ -156,10 +156,10 @@ pub struct LoggerMiddleware { impl Service for LoggerMiddleware where S: Service, Error = Error>, - B: MessageBody, + B: MessageBody + Unpin + 'static, { type Request = ServiceRequest; - type Response = ServiceResponse>; + type Response = ServiceResponse; type Error = Error; type Future = LoggerResponse; @@ -208,10 +208,10 @@ where impl Future for LoggerResponse where - B: MessageBody, + B: MessageBody + Unpin + 'static, S: Service, Error = Error>, { - type Output = Result>, Error>; + type Output = Result, Error>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); @@ -237,16 +237,17 @@ where let format = this.format.take(); Poll::Ready(Ok(res.map_body(move |_, body| { - ResponseBody::Body(StreamLog { + ResponseBody::Other(Body::from_message(StreamLog { body, time, format, size: 0, - }) + })) }))) } } +use actix_http::body::Body; use pin_project::{pin_project, pinned_drop}; #[pin_project(PinnedDrop)]