inline convenience map body calls

This commit is contained in:
Rob Ede 2021-12-04 04:25:48 +00:00
parent 3a183811cc
commit 1008598880
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
6 changed files with 23 additions and 6 deletions

View File

@ -4,8 +4,8 @@
### Added ### Added
* Methods on `AcceptLanguage`: `ranked` and `preference`. [#2480] * Methods on `AcceptLanguage`: `ranked` and `preference`. [#2480]
* `AcceptEncoding` typed header. [#2482] * `AcceptEncoding` typed header. [#2482]
* `HttpResponse::map_into_{left,right}_body`. [#2468] * `HttpResponse::map_into_{left,right}_body` and `HttpResponse::map_into_boxed_body`. [#2468]
* `HttpResponse::map_into_boxed_body`. [#2468] * `ServiceResponse::map_into_{left,right}_body` and `HttpResponse::map_into_boxed_body`. [#2468]
### Changed ### Changed
* Rename `Accept::{mime_precedence => ranked}`. [#2480] * Rename `Accept::{mime_precedence => ranked}`. [#2480]

View File

@ -190,6 +190,7 @@ impl<B> Response<B> {
} }
} }
#[inline]
pub fn map_into_boxed_body(self) -> Response<BoxBody> pub fn map_into_boxed_body(self) -> Response<BoxBody>
where where
B: MessageBody + 'static, B: MessageBody + 'static,

View File

@ -150,7 +150,8 @@ where
Either::right(ok(req Either::right(ok(req
.into_response(res) .into_response(res)
.map_body(|_, body| EitherBody::right(BoxBody::new(body))))) .map_into_boxed_body()
.map_into_right_body()))
} }
} }
} }

View File

@ -357,7 +357,7 @@ impl HttpResponseBuilder {
S: Stream<Item = Result<Bytes, E>> + 'static, S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static, E: Into<BoxError> + 'static,
{ {
self.body(BoxBody::new(BodyStream::new(stream))) self.body(BodyStream::new(stream))
} }
/// Set a JSON body and build the `HttpResponse`. /// Set a JSON body and build the `HttpResponse`.
@ -387,7 +387,7 @@ impl HttpResponseBuilder {
/// `HttpResponseBuilder` can not be used after this call. /// `HttpResponseBuilder` can not be used after this call.
#[inline] #[inline]
pub fn finish(&mut self) -> HttpResponse { pub fn finish(&mut self) -> HttpResponse {
self.body(BoxBody::new(())) self.body(())
} }
/// This method construct new `HttpResponseBuilder` /// This method construct new `HttpResponseBuilder`

View File

@ -229,14 +229,17 @@ impl<B> HttpResponse<B> {
// TODO: docs for the body map methods below // TODO: docs for the body map methods below
#[inline]
pub fn map_into_left_body<R>(self) -> HttpResponse<EitherBody<B, R>> { pub fn map_into_left_body<R>(self) -> HttpResponse<EitherBody<B, R>> {
self.map_body(|_, body| EitherBody::left(body)) self.map_body(|_, body| EitherBody::left(body))
} }
#[inline]
pub fn map_into_right_body<L>(self) -> HttpResponse<EitherBody<L, B>> { pub fn map_into_right_body<L>(self) -> HttpResponse<EitherBody<L, B>> {
self.map_body(|_, body| EitherBody::right(body)) self.map_body(|_, body| EitherBody::right(body))
} }
#[inline]
pub fn map_into_boxed_body(self) -> HttpResponse<BoxBody> pub fn map_into_boxed_body(self) -> HttpResponse<BoxBody>
where where
B: MessageBody + 'static, B: MessageBody + 'static,

View File

@ -5,7 +5,7 @@ use std::{
}; };
use actix_http::{ use actix_http::{
body::{BoxBody, MessageBody}, body::{BoxBody, EitherBody, MessageBody},
http::{HeaderMap, Method, StatusCode, Uri, Version}, http::{HeaderMap, Method, StatusCode, Uri, Version},
Extensions, HttpMessage, Payload, PayloadStream, RequestHead, Response, ResponseHead, Extensions, HttpMessage, Payload, PayloadStream, RequestHead, Response, ResponseHead,
}; };
@ -410,6 +410,7 @@ impl<B> ServiceResponse<B> {
impl<B> ServiceResponse<B> { impl<B> ServiceResponse<B> {
/// Set a new body /// Set a new body
#[inline]
pub fn map_body<F, B2>(self, f: F) -> ServiceResponse<B2> pub fn map_body<F, B2>(self, f: F) -> ServiceResponse<B2>
where where
F: FnOnce(&mut ResponseHead, B) -> B2, F: FnOnce(&mut ResponseHead, B) -> B2,
@ -422,6 +423,17 @@ impl<B> ServiceResponse<B> {
} }
} }
#[inline]
pub fn map_into_left_body<R>(self) -> ServiceResponse<EitherBody<B, R>> {
self.map_body(|_, body| EitherBody::left(body))
}
#[inline]
pub fn map_into_right_body<L>(self) -> ServiceResponse<EitherBody<L, B>> {
self.map_body(|_, body| EitherBody::right(body))
}
#[inline]
pub fn map_into_boxed_body(self) -> ServiceResponse<BoxBody> pub fn map_into_boxed_body(self) -> ServiceResponse<BoxBody>
where where
B: MessageBody + 'static, B: MessageBody + 'static,