remove responsebody enum

This commit is contained in:
Rob Ede 2021-11-16 17:06:32 +00:00
parent 0b13d27b03
commit 5b27827f69
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
6 changed files with 17 additions and 120 deletions

View File

@ -7,6 +7,9 @@
### Fixed
* Relax `Unpin` bound on `S` (stream) parameter of `HttpResponseBuilder::streaming`. [#2448]
### Removed
* `dev::ResponseBody` re-export; is function is replaced by the new `dev::AnyBody` enum. [#2446]
[#2423]: https://github.com/actix/actix-web/pull/2423

View File

@ -2,22 +2,23 @@
## Unreleased - 2021-xx-xx
### Added
* `AnyBody::empty` for quickly creating an empty body. [#2446]
* `impl Clone` for `AnyBody<S> where S: Clone`. [#2448]
* `AnyBody::into_boxed` for quickly converting to a type-erased, boxed body type. [#2448]
* `body::AnyBody::empty` for quickly creating an empty body. [#2446]
* `impl Clone` for `body::AnyBody<S> where S: Clone`. [#2448]
* `body::AnyBody::into_boxed` for quickly converting to a type-erased, boxed body type. [#2448]
### Changed
* Rename `AnyBody::{Message => Body}`. [#2446]
* Rename `AnyBody::{from_message => new_boxed}`. [#2448]
* Rename `AnyBody::{from_slice => copy_from_slice}`. [#2448]
* Rename `BoxAnyBody` to `BoxBody` [#2448]
* Rename `body::AnyBody::{Message => Body}`. [#2446]
* Rename `body::AnyBody::{from_message => new_boxed}`. [#2448]
* Rename `body::AnyBody::{from_slice => copy_from_slice}`. [#2448]
* Rename `body::{BoxAnyBody => BoxBody}`. [#2448]
* Change representation of `AnyBody` to include a type parameter in `Body` variant. Defaults to `BoxBody`. [#2448]
* `Encoder::response` now returns `AnyBody<Encoder<B>>`. [#2448]
### Removed
* `AnyBody::Empty`; an empty body can now only be represented as a zero-length `Bytes` variant. [#2446]
* `BodySize::Empty`; an empty body can now only be represented as a `Sized(0)` variant. [#2446]
* `body::AnyBody::Empty`; an empty body can now only be represented as a zero-length `Bytes` variant. [#2446]
* `body::BodySize::Empty`; an empty body can now only be represented as a `Sized(0)` variant. [#2446]
* `EncoderError::Boxed`; it is no longer required. [#2446]
* `body::ResponseBody`; is function is replaced by the new `body::AnyBody` enum. [#2446]
[#2446]: https://github.com/actix/actix-web/pull/2446

View File

@ -11,7 +11,6 @@ use futures_core::ready;
mod body;
mod body_stream;
mod message_body;
mod response_body;
mod size;
mod sized_stream;
@ -19,7 +18,6 @@ pub use self::body::{AnyBody, Body, BoxBody};
pub use self::body_stream::BodyStream;
pub use self::message_body::MessageBody;
pub(crate) use self::message_body::MessageBodyMapErr;
pub use self::response_body::ResponseBody;
pub use self::size::BodySize;
pub use self::sized_stream::SizedStream;

View File

@ -1,84 +0,0 @@
use std::{
mem,
pin::Pin,
task::{Context, Poll},
};
use bytes::Bytes;
use futures_core::Stream;
use pin_project::pin_project;
use crate::error::Error;
use super::{Body, BodySize, MessageBody};
#[pin_project(project = ResponseBodyProj)]
pub enum ResponseBody<B> {
Body(#[pin] B),
Other(Body),
}
impl ResponseBody<Body> {
pub fn into_body<B>(self) -> ResponseBody<B> {
match self {
ResponseBody::Body(b) => ResponseBody::Other(b),
ResponseBody::Other(b) => ResponseBody::Other(b),
}
}
}
impl<B> ResponseBody<B> {
pub fn take_body(&mut self) -> ResponseBody<B> {
mem::replace(self, ResponseBody::Other(Body::None))
}
}
impl<B: MessageBody> ResponseBody<B> {
pub fn as_ref(&self) -> Option<&B> {
if let ResponseBody::Body(ref b) = self {
Some(b)
} else {
None
}
}
}
impl<B> MessageBody for ResponseBody<B>
where
B: MessageBody,
B::Error: Into<Error>,
{
type Error = Error;
fn size(&self) -> BodySize {
match self {
ResponseBody::Body(ref body) => body.size(),
ResponseBody::Other(ref body) => body.size(),
}
}
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Self::Error>>> {
Stream::poll_next(self, cx)
}
}
impl<B> Stream for ResponseBody<B>
where
B: MessageBody,
B::Error: Into<Error>,
{
type Item = Result<Bytes, Error>;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
match self.project() {
ResponseBodyProj::Body(body) => body.poll_next(cx).map_err(Into::into),
ResponseBodyProj::Other(body) => Pin::new(body).poll_next(cx),
}
}
}

View File

@ -14,7 +14,7 @@ pub use crate::types::form::UrlEncoded;
pub use crate::types::json::JsonBody;
pub use crate::types::readlines::Readlines;
pub use actix_http::body::{AnyBody, Body, BodySize, MessageBody, ResponseBody, SizedStream};
pub use actix_http::body::{AnyBody, Body, BodySize, MessageBody, SizedStream};
#[cfg(feature = "__compress")]
pub use actix_http::encoding::Decoder as Decompress;

View File

@ -232,7 +232,7 @@ pub(crate) mod tests {
use bytes::{Bytes, BytesMut};
use super::*;
use crate::dev::{Body, ResponseBody};
use crate::dev::AnyBody;
use crate::http::{header::CONTENT_TYPE, HeaderValue, StatusCode};
use crate::test::{init_service, TestRequest};
use crate::{error, web, App};
@ -264,13 +264,13 @@ pub(crate) mod tests {
pub(crate) trait BodyTest {
fn bin_ref(&self) -> &[u8];
fn body(&self) -> &Body;
fn body(&self) -> &AnyBody;
}
impl BodyTest for Body {
fn bin_ref(&self) -> &[u8] {
match self {
Body::Bytes(ref bin) => bin,
AnyBody::Bytes(ref bin) => bin,
_ => unreachable!("bug in test impl"),
}
}
@ -279,27 +279,6 @@ pub(crate) mod tests {
}
}
impl BodyTest for ResponseBody<Body> {
fn bin_ref(&self) -> &[u8] {
match self {
ResponseBody::Body(ref b) => match b {
Body::Bytes(ref bin) => bin,
_ => unreachable!("bug in test impl"),
},
ResponseBody::Other(ref b) => match b {
Body::Bytes(ref bin) => bin,
_ => unreachable!("bug in test impl"),
},
}
}
fn body(&self) -> &Body {
match self {
ResponseBody::Body(ref b) => b,
ResponseBody::Other(ref b) => b,
}
}
}
#[actix_rt::test]
async fn test_responder() {
let req = TestRequest::default().to_http_request();