diff --git a/actix-http/src/body/body.rs b/actix-http/src/body/body.rs index 1a709f19e..c705ba3b2 100644 --- a/actix-http/src/body/body.rs +++ b/actix-http/src/body/body.rs @@ -8,7 +8,7 @@ use std::{ use bytes::{Bytes, BytesMut}; use futures_core::Stream; -use pin_project::pin_project; +use pin_project_lite::pin_project; use super::{BodySize, BodyStream, BoxBody, MessageBody, SizedStream}; use crate::error::Error; @@ -17,18 +17,20 @@ use crate::error::Error; #[deprecated(since = "4.0.0", note = "Renamed to `AnyBody`.")] pub type Body = AnyBody; -/// Represents various types of HTTP message body. -#[pin_project(project = AnyBodyProj)] -#[derive(Clone)] -pub enum AnyBody { - /// Empty response. `Content-Length` header is not set. - None, +pin_project! { + /// Represents various types of HTTP message body. + #[derive(Clone)] + #[project = AnyBodyProj] + pub enum AnyBody { + /// Empty response. `Content-Length` header is not set. + None, - /// Complete, in-memory response body. - Bytes(Bytes), + /// Complete, in-memory response body. + Bytes { body: Bytes }, - /// Generic / Other message body. - Body(#[pin] B), + /// Generic / Other message body. + Body { #[pin] body: B }, + } } impl AnyBody { @@ -39,7 +41,7 @@ impl AnyBody { /// Constructs a new, 0-length body. pub fn empty() -> Self { - Self::Bytes(Bytes::new()) + Self::Bytes { body: Bytes::new() } } /// Create boxed body from generic message body. @@ -48,27 +50,33 @@ impl AnyBody { B: MessageBody + 'static, B::Error: Into>, { - Self::Body(BoxBody::new(body)) + Self::Body { + body: BoxBody::new(body), + } } /// Constructs new `AnyBody` instance from a slice of bytes by copying it. /// /// If your bytes container is owned, it may be cheaper to use a `From` impl. pub fn copy_from_slice(s: &[u8]) -> Self { - Self::Bytes(Bytes::copy_from_slice(s)) + Self::Bytes { + body: Bytes::copy_from_slice(s), + } } #[doc(hidden)] #[deprecated(since = "4.0.0", note = "Renamed to `copy_from_slice`.")] pub fn from_slice(s: &[u8]) -> Self { - Self::Bytes(Bytes::copy_from_slice(s)) + Self::Bytes { + body: Bytes::copy_from_slice(s), + } } } impl AnyBody { /// Create body from generic message body. pub fn new(body: B) -> Self { - Self::Body(body) + Self::Body { body } } } @@ -80,8 +88,8 @@ where pub fn into_boxed(self) -> AnyBody { match self { Self::None => AnyBody::None, - Self::Bytes(bytes) => AnyBody::Bytes(bytes), - Self::Body(body) => AnyBody::new_boxed(body), + Self::Bytes { body: bytes } => AnyBody::Bytes { body: bytes }, + Self::Body { body } => AnyBody::new_boxed(body), } } } @@ -96,8 +104,8 @@ where fn size(&self) -> BodySize { match self { AnyBody::None => BodySize::None, - AnyBody::Bytes(ref bin) => BodySize::Sized(bin.len() as u64), - AnyBody::Body(ref body) => body.size(), + AnyBody::Bytes { ref body } => BodySize::Sized(body.len() as u64), + AnyBody::Body { ref body } => body.size(), } } @@ -107,16 +115,16 @@ where ) -> Poll>> { match self.project() { AnyBodyProj::None => Poll::Ready(None), - AnyBodyProj::Bytes(bin) => { - let len = bin.len(); + AnyBodyProj::Bytes { body } => { + let len = body.len(); if len == 0 { Poll::Ready(None) } else { - Poll::Ready(Some(Ok(mem::take(bin)))) + Poll::Ready(Some(Ok(mem::take(body)))) } } - AnyBodyProj::Body(body) => body + AnyBodyProj::Body { body } => body .poll_next(cx) .map_err(|err| Error::new_body().with_cause(err)), } @@ -125,13 +133,13 @@ where impl PartialEq for AnyBody { fn eq(&self, other: &AnyBody) -> bool { - match *self { + match self { AnyBody::None => matches!(*other, AnyBody::None), - AnyBody::Bytes(ref b) => match *other { - AnyBody::Bytes(ref b2) => b == b2, + AnyBody::Bytes { body } => match other { + AnyBody::Bytes { body: b2 } => body == b2, _ => false, }, - AnyBody::Body(_) => false, + AnyBody::Body { .. } => false, } } } @@ -140,39 +148,49 @@ impl fmt::Debug for AnyBody { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { AnyBody::None => write!(f, "AnyBody::None"), - AnyBody::Bytes(ref bytes) => write!(f, "AnyBody::Bytes({:?})", bytes), - AnyBody::Body(ref stream) => write!(f, "AnyBody::Message({:?})", stream), + AnyBody::Bytes { ref body } => write!(f, "AnyBody::Bytes({:?})", body), + AnyBody::Body { ref body } => write!(f, "AnyBody::Message({:?})", body), } } } impl From<&'static str> for AnyBody { fn from(string: &'static str) -> Self { - Self::Bytes(Bytes::from_static(string.as_ref())) + Self::Bytes { + body: Bytes::from_static(string.as_ref()), + } } } impl From<&'static [u8]> for AnyBody { fn from(bytes: &'static [u8]) -> Self { - Self::Bytes(Bytes::from_static(bytes)) + Self::Bytes { + body: Bytes::from_static(bytes), + } } } impl From> for AnyBody { fn from(vec: Vec) -> Self { - Self::Bytes(Bytes::from(vec)) + Self::Bytes { + body: Bytes::from(vec), + } } } impl From for AnyBody { fn from(string: String) -> Self { - Self::Bytes(Bytes::from(string)) + Self::Bytes { + body: Bytes::from(string), + } } } impl From<&'_ String> for AnyBody { fn from(string: &String) -> Self { - Self::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&string))) + Self::Bytes { + body: Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&string)), + } } } @@ -180,22 +198,24 @@ impl From> for AnyBody { fn from(string: Cow<'_, str>) -> Self { match string { Cow::Owned(s) => Self::from(s), - Cow::Borrowed(s) => { - Self::Bytes(Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(s))) - } + Cow::Borrowed(s) => Self::Bytes { + body: Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(s)), + }, } } } impl From for AnyBody { fn from(bytes: Bytes) -> Self { - Self::Bytes(bytes) + Self::Bytes { body: bytes } } } impl From for AnyBody { fn from(bytes: BytesMut) -> Self { - Self::Bytes(bytes.freeze()) + Self::Bytes { + body: bytes.freeze(), + } } } diff --git a/actix-http/src/body/mod.rs b/actix-http/src/body/mod.rs index 004318e23..e4ce89a83 100644 --- a/actix-http/src/body/mod.rs +++ b/actix-http/src/body/mod.rs @@ -37,7 +37,7 @@ mod tests { impl TestAnyBody { pub(crate) fn get_ref(&self) -> &[u8] { match *self { - AnyBody::Bytes(ref bin) => bin, + AnyBody::Bytes { ref body } => body, _ => panic!(), } } @@ -168,10 +168,18 @@ mod tests { #[actix_rt::test] async fn test_body_eq() { assert!( - AnyBody::Bytes(Bytes::from_static(b"1")) - == AnyBody::Bytes(Bytes::from_static(b"1")) + AnyBody::Bytes { + body: Bytes::from_static(b"1") + } == AnyBody::Bytes { + body: Bytes::from_static(b"1") + } + ); + + assert!( + AnyBody::Bytes { + body: Bytes::from_static(b"1") + } != AnyBody::None ); - assert!(AnyBody::Bytes(Bytes::from_static(b"1")) != AnyBody::None); } #[actix_rt::test] diff --git a/awc/src/middleware/redirect.rs b/awc/src/middleware/redirect.rs index 12a71f7cb..f32b6f77f 100644 --- a/awc/src/middleware/redirect.rs +++ b/awc/src/middleware/redirect.rs @@ -95,7 +95,7 @@ where }; let body_opt = match body { - AnyBody::Bytes(ref b) => Some(b.clone()), + AnyBody::Bytes { ref body } => Some(body.clone()), _ => None, }; @@ -192,7 +192,9 @@ where let body_new = if is_redirect { // try to reuse body match body { - Some(ref bytes) => AnyBody::Bytes(bytes.clone()), + Some(ref bytes) => AnyBody::Bytes { + body: bytes.clone(), + }, // TODO: should this be AnyBody::Empty or AnyBody::None. _ => AnyBody::empty(), } diff --git a/awc/src/sender.rs b/awc/src/sender.rs index 7e1bcd646..bad72941c 100644 --- a/awc/src/sender.rs +++ b/awc/src/sender.rs @@ -236,7 +236,9 @@ impl RequestSender { response_decompress, timeout, config, - AnyBody::Bytes(Bytes::from(body)), + AnyBody::Bytes { + body: Bytes::from(body), + }, ) } @@ -265,7 +267,9 @@ impl RequestSender { response_decompress, timeout, config, - AnyBody::Bytes(Bytes::from(body)), + AnyBody::Bytes { + body: Bytes::from(body), + }, ) }