From 27c77ecfa8f92fd21a14f3c6583976df354b6b0b Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Fri, 17 Dec 2021 20:51:18 +0300 Subject: [PATCH] address review comments --- actix-http/src/body/boxed.rs | 14 ++++++++------ actix-http/src/body/message_body.rs | 20 +++++++++++++++++++- actix-http/src/body/none.rs | 5 +++++ actix-http/src/encoding/encoder.rs | 1 + 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/actix-http/src/body/boxed.rs b/actix-http/src/body/boxed.rs index 47294bc3d..a2d7540c4 100644 --- a/actix-http/src/body/boxed.rs +++ b/actix-http/src/body/boxed.rs @@ -8,12 +8,13 @@ use std::{ use bytes::Bytes; use super::{BodySize, MessageBody, MessageBodyMapErr}; +use crate::body; /// A boxed message body with boxed errors. pub struct BoxBody(BoxBodyInner); enum BoxBodyInner { - None, + None(body::None), Bytes(Bytes), Stream(Pin>>>), } @@ -29,7 +30,7 @@ impl BoxBody { B: MessageBody + 'static, { match body.size() { - BodySize::None => Self(BoxBodyInner::None), + BodySize::None => Self(BoxBodyInner::None(body::None)), _ => match body.try_into_bytes() { Ok(bytes) => Self(BoxBodyInner::Bytes(bytes)), Err(body) => { @@ -60,8 +61,8 @@ impl MessageBody for BoxBody { #[inline] fn size(&self) -> BodySize { match &self.0 { - BoxBodyInner::None => BodySize::None, - BoxBodyInner::Bytes(bytes) => BodySize::Sized(bytes.len() as u64), + BoxBodyInner::None(none) => none.size(), + BoxBodyInner::Bytes(bytes) => bytes.size(), BoxBodyInner::Stream(stream) => stream.size(), } } @@ -72,7 +73,7 @@ impl MessageBody for BoxBody { cx: &mut Context<'_>, ) -> Poll>> { match &mut self.0 { - BoxBodyInner::None => Poll::Ready(None), + BoxBodyInner::None(_) => Poll::Ready(None), BoxBodyInner::Bytes(bytes) => Pin::new(bytes).poll_next(cx).map_err(Into::into), BoxBodyInner::Stream(stream) => Pin::new(stream).poll_next(cx), } @@ -81,7 +82,8 @@ impl MessageBody for BoxBody { #[inline] fn try_into_bytes(self) -> Result { match self.0 { - BoxBodyInner::Bytes(bytes) => Ok(bytes), + BoxBodyInner::None(none) => Ok(none.try_into_bytes().unwrap()), + BoxBodyInner::Bytes(bytes) => Ok(bytes.try_into_bytes().unwrap()), _ => Err(self), } } diff --git a/actix-http/src/body/message_body.rs b/actix-http/src/body/message_body.rs index 3544a5369..bd13e75ec 100644 --- a/actix-http/src/body/message_body.rs +++ b/actix-http/src/body/message_body.rs @@ -31,6 +31,9 @@ pub trait MessageBody { cx: &mut Context<'_>, ) -> Poll>>; + /// Convert this body into `Bytes`. + /// + /// Bodies with `BodySize::None` are allowed to return empty `Bytes`. fn try_into_bytes(self) -> Result where Self: Sized, @@ -369,7 +372,7 @@ mod tests { use bytes::{Bytes, BytesMut}; use super::*; - use crate::body::{BoxBody, EitherBody}; + use crate::body::{self, EitherBody}; macro_rules! assert_poll_next { ($pin:expr, $exp:expr) => { @@ -497,6 +500,21 @@ mod tests { assert_poll_next_none!(Pin::new(&mut body)); } + #[actix_rt::test] + async fn none_body_combinators() { + fn none_body() -> BoxBody { + let body = body::None; + let body = BoxBody::new(body); + let body = EitherBody::<_, ()>::left(body); + let body = EitherBody::<(), _>::right(body); + body.boxed() + } + + assert_eq!(none_body().size(), BodySize::None); + assert_eq!(none_body().try_into_bytes().unwrap(), Bytes::new()); + assert_poll_next_none!(Pin::new(&mut none_body())); + } + // down-casting used to be done with a method on MessageBody trait // test is kept to demonstrate equivalence of Any trait #[actix_rt::test] diff --git a/actix-http/src/body/none.rs b/actix-http/src/body/none.rs index 0fc7c8c9f..0e7bbe5a9 100644 --- a/actix-http/src/body/none.rs +++ b/actix-http/src/body/none.rs @@ -40,4 +40,9 @@ impl MessageBody for None { ) -> Poll>> { Poll::Ready(Option::None) } + + #[inline] + fn try_into_bytes(self) -> Result { + Ok(Bytes::new()) + } } diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index 55ddc8d8c..70448a115 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -136,6 +136,7 @@ where Self: Sized, { match self { + EncoderBody::None => Ok(Bytes::new()), EncoderBody::Full { body } => Ok(body), _ => Err(self), }