mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
Fix perf drop in HttpResponseBuilder (#2174)
This commit is contained in:
parent
52bb2b5daf
commit
2aa674c1fd
@ -32,7 +32,7 @@ use crate::{
|
|||||||
///
|
///
|
||||||
/// This type can be used to construct an instance of `Response` through a builder-like pattern.
|
/// This type can be used to construct an instance of `Response` through a builder-like pattern.
|
||||||
pub struct HttpResponseBuilder {
|
pub struct HttpResponseBuilder {
|
||||||
head: Option<ResponseHead>,
|
res: Option<Response<Body>>,
|
||||||
err: Option<HttpError>,
|
err: Option<HttpError>,
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
cookies: Option<CookieJar>,
|
cookies: Option<CookieJar>,
|
||||||
@ -43,7 +43,7 @@ impl HttpResponseBuilder {
|
|||||||
/// Create response builder
|
/// Create response builder
|
||||||
pub fn new(status: StatusCode) -> Self {
|
pub fn new(status: StatusCode) -> Self {
|
||||||
Self {
|
Self {
|
||||||
head: Some(ResponseHead::new(status)),
|
res: Some(Response::new(status)),
|
||||||
err: None,
|
err: None,
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
cookies: None,
|
cookies: None,
|
||||||
@ -291,15 +291,19 @@ impl HttpResponseBuilder {
|
|||||||
/// Responses extensions
|
/// Responses extensions
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
||||||
let head = self.head.as_ref().expect("cannot reuse response builder");
|
self.res
|
||||||
head.extensions()
|
.as_ref()
|
||||||
|
.expect("cannot reuse response builder")
|
||||||
|
.extensions()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Mutable reference to a the response's extensions
|
/// Mutable reference to a the response's extensions
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions> {
|
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions> {
|
||||||
let head = self.head.as_ref().expect("cannot reuse response builder");
|
self.res
|
||||||
head.extensions_mut()
|
.as_mut()
|
||||||
|
.expect("cannot reuse response builder")
|
||||||
|
.extensions_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a body and generate `Response`.
|
/// Set a body and generate `Response`.
|
||||||
@ -318,12 +322,14 @@ impl HttpResponseBuilder {
|
|||||||
return HttpResponse::from_error(Error::from(err)).into_body();
|
return HttpResponse::from_error(Error::from(err)).into_body();
|
||||||
}
|
}
|
||||||
|
|
||||||
// allow unused mut when cookies feature is disabled
|
let res = self
|
||||||
#[allow(unused_mut)]
|
.res
|
||||||
let mut head = self.head.take().expect("cannot reuse response builder");
|
.take()
|
||||||
|
.expect("cannot reuse response builder")
|
||||||
|
.set_body(body);
|
||||||
|
|
||||||
let mut res = HttpResponse::with_body(StatusCode::OK, body);
|
#[allow(unused_mut)]
|
||||||
*res.head_mut() = head;
|
let mut res = HttpResponse::from(res);
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
if let Some(ref jar) = self.cookies {
|
if let Some(ref jar) = self.cookies {
|
||||||
@ -383,7 +389,7 @@ impl HttpResponseBuilder {
|
|||||||
/// This method construct new `HttpResponseBuilder`
|
/// This method construct new `HttpResponseBuilder`
|
||||||
pub fn take(&mut self) -> Self {
|
pub fn take(&mut self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
head: self.head.take(),
|
res: self.res.take(),
|
||||||
err: self.err.take(),
|
err: self.err.take(),
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
cookies: self.cookies.take(),
|
cookies: self.cookies.take(),
|
||||||
@ -396,7 +402,7 @@ impl HttpResponseBuilder {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.head.as_mut()
|
self.res.as_mut().map(|res| res.head_mut())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user