From 5167bf16a2011fcd3f3a38d0bd4900255806e69e Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 16 Nov 2021 16:13:45 +0000 Subject: [PATCH] rename Stream variant to Body --- CHANGES.md | 4 +++ actix-http/CHANGES.md | 5 +-- actix-http/src/body/body.rs | 49 +++++++++++++++++++++--------- actix-http/src/body/mod.rs | 4 +-- actix-http/src/encoding/encoder.rs | 2 +- actix-http/src/h1/dispatcher.rs | 2 +- 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 0cb5ccb23..0861ca1b6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,10 @@ # Changes ## Unreleased - 2021-xx-xx +### Fixed +* Relax `Unpin` bound on `S` (stream) parameter of `HttpResponseBuilder::streaming`. [#????] + +[#2423]: https://github.com/actix/actix-web/pull/2423 ## 4.0.0-beta.11 - 2021-11-15 diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index c3dc1de0f..a7918f0ee 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -7,10 +7,11 @@ * `AnyBody::into_boxed` for quickly converting to a type-erased, boxed body type. [#????] ### Changed -* Rename `AnyBody::{Message => Stream}`. [#2446] +* Rename `AnyBody::{Message => Body}`. [#2446] * Rename `AnyBody::{from_message => new_boxed}`. [#????] +* Rename `AnyBody::{from_slice => copy_from_slice}`. [#????] * Rename `BoxAnyBody` to `BoxBody` [#????] -* Change representation of `AnyBody` to include a type parameter in `Stream` variant. Defaults to `BoxBody`. [#????] +* Change representation of `AnyBody` to include a type parameter in `Body` variant. Defaults to `BoxBody`. [#????] ### Removed * `AnyBody::Empty`; an empty body can now only be represented as a zero-length `Bytes` variant. [#2446] diff --git a/actix-http/src/body/body.rs b/actix-http/src/body/body.rs index 6b6263889..26dd5a492 100644 --- a/actix-http/src/body/body.rs +++ b/actix-http/src/body/body.rs @@ -17,15 +17,15 @@ pub type Body = AnyBody; /// Represents various types of HTTP message body. #[derive(Clone)] -pub enum AnyBody { +pub enum AnyBody { /// Empty response. `Content-Length` header is not set. None, /// Specific response body. Bytes(Bytes), - /// Generic message body. - Stream(S), + /// Generic / Other message body. + Body(B), } impl AnyBody { @@ -40,10 +40,18 @@ impl AnyBody { B: MessageBody + 'static, B::Error: Into>, { - Self::Stream(BoxBody::from_body(body)) + Self::Body(BoxBody::from_body(body)) } - /// Create body from slice (copy) + /// 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)) + } + + #[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)) } @@ -56,22 +64,22 @@ where { /// Create body from generic message body. pub fn new(body: B) -> Self { - Self::Stream(body) + Self::Body(body) } pub fn into_boxed(self) -> AnyBody { match self { AnyBody::None => AnyBody::new_boxed(()), AnyBody::Bytes(body) => AnyBody::new_boxed(body), - AnyBody::Stream(body) => AnyBody::new_boxed(body), + AnyBody::Body(body) => AnyBody::new_boxed(body), } } } -impl MessageBody for AnyBody +impl MessageBody for AnyBody where - S: MessageBody + Unpin, - S::Error: StdError + 'static, + B: MessageBody + Unpin, + B::Error: StdError + 'static, { type Error = Error; @@ -79,7 +87,7 @@ where match self { AnyBody::None => BodySize::None, AnyBody::Bytes(ref bin) => BodySize::Sized(bin.len() as u64), - AnyBody::Stream(ref body) => body.size(), + AnyBody::Body(ref body) => body.size(), } } @@ -98,7 +106,7 @@ where } } - AnyBody::Stream(body) => Pin::new(body) + AnyBody::Body(body) => Pin::new(body) .poll_next(cx) .map_err(|err| Error::new_body().with_cause(err)), } @@ -113,7 +121,7 @@ impl PartialEq for AnyBody { AnyBody::Bytes(ref b2) => b == b2, _ => false, }, - AnyBody::Stream(_) => false, + AnyBody::Body(_) => false, } } } @@ -123,7 +131,7 @@ impl fmt::Debug for AnyBody { match *self { AnyBody::None => write!(f, "AnyBody::None"), AnyBody::Bytes(ref bytes) => write!(f, "AnyBody::Bytes({:?})", bytes), - AnyBody::Stream(ref stream) => write!(f, "AnyBody::Message({:?})", stream), + AnyBody::Body(ref stream) => write!(f, "AnyBody::Message({:?})", stream), } } } @@ -252,12 +260,25 @@ mod tests { use static_assertions::{assert_impl_all, assert_not_impl_all}; use super::*; + use crate::body::to_bytes; assert_impl_all!(AnyBody<()>: MessageBody, fmt::Debug, Send, Sync); + assert_impl_all!(AnyBody>: MessageBody, fmt::Debug, Send, Sync); assert_impl_all!(AnyBody: MessageBody, fmt::Debug, Send, Sync); assert_impl_all!(AnyBody: MessageBody, fmt::Debug); assert_impl_all!(BoxBody: MessageBody, fmt::Debug); assert_not_impl_all!(AnyBody: Send, Sync); assert_not_impl_all!(BoxBody: Send, Sync); + + #[actix_rt::test] + async fn nested_boxed_body() { + let body = AnyBody::copy_from_slice(&[1, 2, 3]); + let boxed_body = BoxBody::from_body(BoxBody::from_body(body)); + + assert_eq!( + to_bytes(boxed_body).await.unwrap(), + Bytes::from(vec![1, 2, 3]), + ); + } } diff --git a/actix-http/src/body/mod.rs b/actix-http/src/body/mod.rs index 59c0eaf79..a6a189c09 100644 --- a/actix-http/src/body/mod.rs +++ b/actix-http/src/body/mod.rs @@ -108,10 +108,10 @@ mod tests { assert_eq!(Body::from(b"test".as_ref()).size(), BodySize::Sized(4)); assert_eq!(Body::from(b"test".as_ref()).get_ref(), b"test"); assert_eq!( - Body::from_slice(b"test".as_ref()).size(), + Body::copy_from_slice(b"test".as_ref()).size(), BodySize::Sized(4) ); - assert_eq!(Body::from_slice(b"test".as_ref()).get_ref(), b"test"); + assert_eq!(Body::copy_from_slice(b"test".as_ref()).get_ref(), b"test"); let sb = Bytes::from(&b"test"[..]); pin!(sb); diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index b145cc26a..4d4534c85 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -68,7 +68,7 @@ impl Encoder { return ResponseBody::Other(Body::Bytes(buf)); } } - Body::Stream(stream) => EncoderBody::BoxedStream(stream), + Body::Body(stream) => EncoderBody::BoxedStream(stream), }, ResponseBody::Body(stream) => EncoderBody::Stream(stream), }; diff --git a/actix-http/src/h1/dispatcher.rs b/actix-http/src/h1/dispatcher.rs index 844bc61ea..163d84f5b 100644 --- a/actix-http/src/h1/dispatcher.rs +++ b/actix-http/src/h1/dispatcher.rs @@ -1077,7 +1077,7 @@ mod tests { fn_service(|req: Request| { let path = req.path().as_bytes(); ready(Ok::<_, Error>( - Response::ok().set_body(AnyBody::from_slice(path)), + Response::ok().set_body(AnyBody::copy_from_slice(path)), )) }) }