1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-30 08:24:28 +02:00

body ergo v4 using any body

This commit is contained in:
Rob Ede
2021-12-08 22:45:54 +00:00
parent fb5b4734a4
commit f12f62ba73
25 changed files with 427 additions and 208 deletions

View File

@ -19,7 +19,7 @@ use super::BodySize;
pub trait MessageBody {
// TODO: consider this bound to only fmt::Display since the error type is not really used
// and there is an impl for Into<Box<StdError>> on String
type Error: Into<Box<dyn StdError>>;
type Error: Into<Box<dyn StdError>> + 'static;
/// Body size hint.
fn size(&self) -> BodySize;
@ -272,7 +272,7 @@ impl<B, F, E> MessageBody for MessageBodyMapErr<B, F>
where
B: MessageBody,
F: FnOnce(B::Error) -> E,
E: Into<Box<dyn StdError>>,
E: Into<Box<dyn StdError>> + 'static,
{
type Error = E;
@ -306,6 +306,8 @@ mod tests {
use super::*;
// static_assertions::assert_obj_safe!(MessageBody<()>);
macro_rules! assert_poll_next {
($pin:expr, $exp:expr) => {
assert_eq!(

View File

@ -1,5 +1,6 @@
//! Traits and structures to aid consuming and writing HTTP payloads.
// mod any;
mod body_stream;
mod boxed;
mod either;
@ -9,6 +10,7 @@ mod size;
mod sized_stream;
mod utils;
// pub use self::any::AnyBody;
pub use self::body_stream::BodyStream;
pub use self::boxed::BoxBody;
pub use self::either::EitherBody;

View File

@ -53,6 +53,15 @@ impl<B: MessageBody> Encoder<B> {
}
}
pub fn not_acceptable(body: Bytes) -> Self {
Encoder {
body: EncoderBody::Bytes { body },
encoder: None,
fut: None,
eof: false,
}
}
pub fn response(encoding: ContentEncoding, head: &mut ResponseHead, body: B) -> Self {
let can_encode = !(head.headers().contains_key(&CONTENT_ENCODING)
|| head.status == StatusCode::SWITCHING_PROTOCOLS
@ -99,6 +108,7 @@ pin_project! {
#[project = EncoderBodyProj]
enum EncoderBody<B> {
None,
Bytes { body: Bytes },
Stream { #[pin] body: B },
}
}
@ -112,6 +122,7 @@ where
fn size(&self) -> BodySize {
match self {
EncoderBody::None => BodySize::None,
EncoderBody::Bytes { body } => body.size(),
EncoderBody::Stream { body } => body.size(),
}
}
@ -122,7 +133,9 @@ where
) -> Poll<Option<Result<Bytes, Self::Error>>> {
match self.project() {
EncoderBodyProj::None => Poll::Ready(None),
EncoderBodyProj::Bytes { body } => {
Pin::new(body).poll_next(cx).map_err(|err| match err {})
}
EncoderBodyProj::Stream { body } => body
.poll_next(cx)
.map_err(|err| EncoderError::Body(err.into())),

View File

@ -8,7 +8,7 @@ mod decoder;
mod encoder;
pub use self::decoder::Decoder;
pub use self::encoder::Encoder;
pub use self::encoder::{Encoder, EncoderError};
/// Special-purpose writer for streaming (de-)compression.
///

View File

@ -9,6 +9,8 @@ use crate::{body::BoxBody, ws, Response};
pub use http::Error as HttpError;
pub use crate::encoding::EncoderError;
pub struct Error {
inner: Box<ErrorInner>,
}

View File

@ -2,7 +2,7 @@
use std::{
cell::{Ref, RefMut},
fmt, str,
fmt, mem, str,
};
use bytes::{Bytes, BytesMut};
@ -203,6 +203,12 @@ impl<B> Response<B> {
}
}
impl<B: Default> Response<B> {
pub fn take_body(&mut self) -> B {
mem::take(&mut self.body)
}
}
impl<B> fmt::Debug for Response<B>
where
B: MessageBody,