address review comments

This commit is contained in:
Ali MJ Al-Nasrawy 2021-12-17 20:51:18 +03:00
parent f2cc57c563
commit 27c77ecfa8
4 changed files with 33 additions and 7 deletions

View File

@ -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<Box<dyn MessageBody<Error = Box<dyn StdError>>>>),
}
@ -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<Option<Result<Bytes, Self::Error>>> {
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<Bytes, Self> {
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),
}
}

View File

@ -31,6 +31,9 @@ pub trait MessageBody {
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Self::Error>>>;
/// Convert this body into `Bytes`.
///
/// Bodies with `BodySize::None` are allowed to return empty `Bytes`.
fn try_into_bytes(self) -> Result<Bytes, Self>
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]

View File

@ -40,4 +40,9 @@ impl MessageBody for None {
) -> Poll<Option<Result<Bytes, Self::Error>>> {
Poll::Ready(Option::None)
}
#[inline]
fn try_into_bytes(self) -> Result<Bytes, Self> {
Ok(Bytes::new())
}
}

View File

@ -136,6 +136,7 @@ where
Self: Sized,
{
match self {
EncoderBody::None => Ok(Bytes::new()),
EncoderBody::Full { body } => Ok(body),
_ => Err(self),
}