From 10085988808f5fa621d1e75e45a24d062c1eb049 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 4 Dec 2021 04:25:48 +0000 Subject: [PATCH] inline convenience map body calls --- CHANGES.md | 4 ++-- actix-http/src/response.rs | 1 + src/middleware/compress.rs | 3 ++- src/response/builder.rs | 4 ++-- src/response/response.rs | 3 +++ src/service.rs | 14 +++++++++++++- 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index f19df4897..2dc45c3ed 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,8 +4,8 @@ ### Added * Methods on `AcceptLanguage`: `ranked` and `preference`. [#2480] * `AcceptEncoding` typed header. [#2482] -* `HttpResponse::map_into_{left,right}_body`. [#2468] -* `HttpResponse::map_into_boxed_body`. [#2468] +* `HttpResponse::map_into_{left,right}_body` and `HttpResponse::map_into_boxed_body`. [#2468] +* `ServiceResponse::map_into_{left,right}_body` and `HttpResponse::map_into_boxed_body`. [#2468] ### Changed * Rename `Accept::{mime_precedence => ranked}`. [#2480] diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index 282118b21..ad41094ae 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -190,6 +190,7 @@ impl Response { } } + #[inline] pub fn map_into_boxed_body(self) -> Response where B: MessageBody + 'static, diff --git a/src/middleware/compress.rs b/src/middleware/compress.rs index 06af6b6fc..da8087868 100644 --- a/src/middleware/compress.rs +++ b/src/middleware/compress.rs @@ -150,7 +150,8 @@ where Either::right(ok(req .into_response(res) - .map_body(|_, body| EitherBody::right(BoxBody::new(body))))) + .map_into_boxed_body() + .map_into_right_body())) } } } diff --git a/src/response/builder.rs b/src/response/builder.rs index 4d0c0a7f7..b5bef2e99 100644 --- a/src/response/builder.rs +++ b/src/response/builder.rs @@ -357,7 +357,7 @@ impl HttpResponseBuilder { S: Stream> + 'static, E: Into + 'static, { - self.body(BoxBody::new(BodyStream::new(stream))) + self.body(BodyStream::new(stream)) } /// Set a JSON body and build the `HttpResponse`. @@ -387,7 +387,7 @@ impl HttpResponseBuilder { /// `HttpResponseBuilder` can not be used after this call. #[inline] pub fn finish(&mut self) -> HttpResponse { - self.body(BoxBody::new(())) + self.body(()) } /// This method construct new `HttpResponseBuilder` diff --git a/src/response/response.rs b/src/response/response.rs index a0bf248db..97de21e42 100644 --- a/src/response/response.rs +++ b/src/response/response.rs @@ -229,14 +229,17 @@ impl HttpResponse { // TODO: docs for the body map methods below + #[inline] pub fn map_into_left_body(self) -> HttpResponse> { self.map_body(|_, body| EitherBody::left(body)) } + #[inline] pub fn map_into_right_body(self) -> HttpResponse> { self.map_body(|_, body| EitherBody::right(body)) } + #[inline] pub fn map_into_boxed_body(self) -> HttpResponse where B: MessageBody + 'static, diff --git a/src/service.rs b/src/service.rs index 2c6acb182..df9e809e4 100644 --- a/src/service.rs +++ b/src/service.rs @@ -5,7 +5,7 @@ use std::{ }; use actix_http::{ - body::{BoxBody, MessageBody}, + body::{BoxBody, EitherBody, MessageBody}, http::{HeaderMap, Method, StatusCode, Uri, Version}, Extensions, HttpMessage, Payload, PayloadStream, RequestHead, Response, ResponseHead, }; @@ -410,6 +410,7 @@ impl ServiceResponse { impl ServiceResponse { /// Set a new body + #[inline] pub fn map_body(self, f: F) -> ServiceResponse where F: FnOnce(&mut ResponseHead, B) -> B2, @@ -422,6 +423,17 @@ impl ServiceResponse { } } + #[inline] + pub fn map_into_left_body(self) -> ServiceResponse> { + self.map_body(|_, body| EitherBody::left(body)) + } + + #[inline] + pub fn map_into_right_body(self) -> ServiceResponse> { + self.map_body(|_, body| EitherBody::right(body)) + } + + #[inline] pub fn map_into_boxed_body(self) -> ServiceResponse where B: MessageBody + 'static,