1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-02-20 03:14:21 +01:00

implement parts as assoc method

This commit is contained in:
Rob Ede 2021-04-14 02:57:28 +01:00
parent 5202bf03c1
commit 1bfdfd1f41
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933

View File

@ -80,7 +80,7 @@ impl ResponseBuilder {
/// ``` /// ```
#[inline] #[inline]
pub fn status(&mut self, status: StatusCode) -> &mut Self { pub fn status(&mut self, status: StatusCode) -> &mut Self {
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = self.inner() {
parts.status = status; parts.status = status;
} }
self self
@ -104,7 +104,7 @@ impl ResponseBuilder {
where where
H: IntoHeaderPair, H: IntoHeaderPair,
{ {
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = self.inner() {
match header.try_into_header_pair() { match header.try_into_header_pair() {
Ok((key, value)) => { Ok((key, value)) => {
parts.headers.insert(key, value); parts.headers.insert(key, value);
@ -135,7 +135,7 @@ impl ResponseBuilder {
where where
H: IntoHeaderPair, H: IntoHeaderPair,
{ {
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = self.inner() {
match header.try_into_header_pair() { match header.try_into_header_pair() {
Ok((key, value)) => parts.headers.append(key, value), Ok((key, value)) => parts.headers.append(key, value),
Err(e) => self.err = Some(e.into()), Err(e) => self.err = Some(e.into()),
@ -148,7 +148,7 @@ impl ResponseBuilder {
/// Set the custom reason for the response. /// Set the custom reason for the response.
#[inline] #[inline]
pub fn reason(&mut self, reason: &'static str) -> &mut Self { pub fn reason(&mut self, reason: &'static str) -> &mut Self {
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = self.inner() {
parts.reason = Some(reason); parts.reason = Some(reason);
} }
self self
@ -157,7 +157,7 @@ impl ResponseBuilder {
/// Set connection type to KeepAlive /// Set connection type to KeepAlive
#[inline] #[inline]
pub fn keep_alive(&mut self) -> &mut Self { pub fn keep_alive(&mut self) -> &mut Self {
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = self.inner() {
parts.set_connection_type(ConnectionType::KeepAlive); parts.set_connection_type(ConnectionType::KeepAlive);
} }
self self
@ -169,7 +169,7 @@ impl ResponseBuilder {
where where
V: IntoHeaderValue, V: IntoHeaderValue,
{ {
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = self.inner() {
parts.set_connection_type(ConnectionType::Upgrade); parts.set_connection_type(ConnectionType::Upgrade);
} }
@ -183,7 +183,7 @@ impl ResponseBuilder {
/// Force close connection, even if it is marked as keep-alive /// Force close connection, even if it is marked as keep-alive
#[inline] #[inline]
pub fn force_close(&mut self) -> &mut Self { pub fn force_close(&mut self) -> &mut Self {
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = self.inner() {
parts.set_connection_type(ConnectionType::Close); parts.set_connection_type(ConnectionType::Close);
} }
self self
@ -195,7 +195,7 @@ impl ResponseBuilder {
let mut buf = itoa::Buffer::new(); let mut buf = itoa::Buffer::new();
self.insert_header((header::CONTENT_LENGTH, buf.format(len))); self.insert_header((header::CONTENT_LENGTH, buf.format(len)));
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = self.inner() {
parts.no_chunking(true); parts.no_chunking(true);
} }
self self
@ -207,7 +207,7 @@ impl ResponseBuilder {
where where
V: IntoHeaderValue, V: IntoHeaderValue,
{ {
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = self.inner() {
match value.try_into_value() { match value.try_into_value() {
Ok(value) => { Ok(value) => {
parts.headers.insert(header::CONTENT_TYPE, value); parts.headers.insert(header::CONTENT_TYPE, value);
@ -232,17 +232,17 @@ impl ResponseBuilder {
head.extensions.borrow_mut() head.extensions.borrow_mut()
} }
/// Set a body and generate `Response`. /// Generate response with a wrapped body.
/// ///
/// `ResponseBuilder` can not be used after this call. /// This `ResponseBuilder` will be left in a useless state.
#[inline] #[inline]
pub fn body<B: Into<Body>>(&mut self, body: B) -> Response<Body> { pub fn body<B: Into<Body>>(&mut self, body: B) -> Response<Body> {
self.message_body(body.into()) self.message_body(body.into())
} }
/// Set a body and generate `Response`. /// Generate response with a body.
/// ///
/// `ResponseBuilder` can not be used after this call. /// This `ResponseBuilder` will be left in a useless state.
pub fn message_body<B>(&mut self, body: B) -> Response<B> { pub fn message_body<B>(&mut self, body: B) -> Response<B> {
if let Some(e) = self.err.take() { if let Some(e) = self.err.take() {
return Response::from(Error::from(e)).into_body(); return Response::from(Error::from(e)).into_body();
@ -257,9 +257,9 @@ impl ResponseBuilder {
} }
} }
/// Set a streaming body and generate `Response`. /// Generate response with a streaming body.
/// ///
/// `ResponseBuilder` can not be used after this call. /// This `ResponseBuilder` will be left in a useless state.
#[inline] #[inline]
pub fn streaming<S, E>(&mut self, stream: S) -> Response<Body> pub fn streaming<S, E>(&mut self, stream: S) -> Response<Body>
where where
@ -269,32 +269,30 @@ impl ResponseBuilder {
self.body(Body::from_message(BodyStream::new(stream))) self.body(Body::from_message(BodyStream::new(stream)))
} }
/// Set an empty body and generate `Response` /// Generate response with an empty body.
/// ///
/// `ResponseBuilder` can not be used after this call. /// This `ResponseBuilder` will be left in a useless state.
#[inline] #[inline]
pub fn finish(&mut self) -> Response<Body> { pub fn finish(&mut self) -> Response<Body> {
self.body(Body::Empty) self.body(Body::Empty)
} }
/// This method construct new `ResponseBuilder` /// Create an owned `ResponseBuilder`, leaving the original in a useless state.
pub fn take(&mut self) -> ResponseBuilder { pub fn take(&mut self) -> ResponseBuilder {
ResponseBuilder { ResponseBuilder {
head: self.head.take(), head: self.head.take(),
err: self.err.take(), err: self.err.take(),
} }
} }
}
#[inline] /// Get access to the inner response head if there has been no error.
fn parts<'a>( fn inner(&mut self) -> Option<&mut ResponseHead> {
parts: &'a mut Option<BoxedResponseHead>, if self.err.is_some() {
err: &Option<HttpError>, return None;
) -> Option<&'a mut ResponseHead> { }
if err.is_some() {
return None; self.head.as_mut().map(|r| &mut **r)
} }
parts.as_mut().map(|r| &mut **r)
} }
impl Default for ResponseBuilder { impl Default for ResponseBuilder {