mirror of https://github.com/fafhrd91/actix-web
address review comments
This commit is contained in:
parent
f2cc57c563
commit
27c77ecfa8
|
@ -8,12 +8,13 @@ use std::{
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|
||||||
use super::{BodySize, MessageBody, MessageBodyMapErr};
|
use super::{BodySize, MessageBody, MessageBodyMapErr};
|
||||||
|
use crate::body;
|
||||||
|
|
||||||
/// A boxed message body with boxed errors.
|
/// A boxed message body with boxed errors.
|
||||||
pub struct BoxBody(BoxBodyInner);
|
pub struct BoxBody(BoxBodyInner);
|
||||||
|
|
||||||
enum BoxBodyInner {
|
enum BoxBodyInner {
|
||||||
None,
|
None(body::None),
|
||||||
Bytes(Bytes),
|
Bytes(Bytes),
|
||||||
Stream(Pin<Box<dyn MessageBody<Error = Box<dyn StdError>>>>),
|
Stream(Pin<Box<dyn MessageBody<Error = Box<dyn StdError>>>>),
|
||||||
}
|
}
|
||||||
|
@ -29,7 +30,7 @@ impl BoxBody {
|
||||||
B: MessageBody + 'static,
|
B: MessageBody + 'static,
|
||||||
{
|
{
|
||||||
match body.size() {
|
match body.size() {
|
||||||
BodySize::None => Self(BoxBodyInner::None),
|
BodySize::None => Self(BoxBodyInner::None(body::None)),
|
||||||
_ => match body.try_into_bytes() {
|
_ => match body.try_into_bytes() {
|
||||||
Ok(bytes) => Self(BoxBodyInner::Bytes(bytes)),
|
Ok(bytes) => Self(BoxBodyInner::Bytes(bytes)),
|
||||||
Err(body) => {
|
Err(body) => {
|
||||||
|
@ -60,8 +61,8 @@ impl MessageBody for BoxBody {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn size(&self) -> BodySize {
|
fn size(&self) -> BodySize {
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
BoxBodyInner::None => BodySize::None,
|
BoxBodyInner::None(none) => none.size(),
|
||||||
BoxBodyInner::Bytes(bytes) => BodySize::Sized(bytes.len() as u64),
|
BoxBodyInner::Bytes(bytes) => bytes.size(),
|
||||||
BoxBodyInner::Stream(stream) => stream.size(),
|
BoxBodyInner::Stream(stream) => stream.size(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,7 +73,7 @@ impl MessageBody for BoxBody {
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
||||||
match &mut self.0 {
|
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::Bytes(bytes) => Pin::new(bytes).poll_next(cx).map_err(Into::into),
|
||||||
BoxBodyInner::Stream(stream) => Pin::new(stream).poll_next(cx),
|
BoxBodyInner::Stream(stream) => Pin::new(stream).poll_next(cx),
|
||||||
}
|
}
|
||||||
|
@ -81,7 +82,8 @@ impl MessageBody for BoxBody {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn try_into_bytes(self) -> Result<Bytes, Self> {
|
fn try_into_bytes(self) -> Result<Bytes, Self> {
|
||||||
match self.0 {
|
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),
|
_ => Err(self),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,9 @@ pub trait MessageBody {
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Result<Bytes, Self::Error>>>;
|
) -> 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>
|
fn try_into_bytes(self) -> Result<Bytes, Self>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
|
@ -369,7 +372,7 @@ mod tests {
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::body::{BoxBody, EitherBody};
|
use crate::body::{self, EitherBody};
|
||||||
|
|
||||||
macro_rules! assert_poll_next {
|
macro_rules! assert_poll_next {
|
||||||
($pin:expr, $exp:expr) => {
|
($pin:expr, $exp:expr) => {
|
||||||
|
@ -497,6 +500,21 @@ mod tests {
|
||||||
assert_poll_next_none!(Pin::new(&mut body));
|
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
|
// down-casting used to be done with a method on MessageBody trait
|
||||||
// test is kept to demonstrate equivalence of Any trait
|
// test is kept to demonstrate equivalence of Any trait
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
|
|
@ -40,4 +40,9 @@ impl MessageBody for None {
|
||||||
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
||||||
Poll::Ready(Option::None)
|
Poll::Ready(Option::None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn try_into_bytes(self) -> Result<Bytes, Self> {
|
||||||
|
Ok(Bytes::new())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -136,6 +136,7 @@ where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
|
EncoderBody::None => Ok(Bytes::new()),
|
||||||
EncoderBody::Full { body } => Ok(body),
|
EncoderBody::Full { body } => Ok(body),
|
||||||
_ => Err(self),
|
_ => Err(self),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue