1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-28 07:47:49 +02:00

MessageBody::boxed (#2520)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Ali MJ Al-Nasrawy
2021-12-17 03:43:40 +03:00
committed by GitHub
parent 44b7302845
commit 3c0d059d92
8 changed files with 40 additions and 10 deletions

View File

@@ -14,7 +14,11 @@ use crate::Error;
pub struct BoxBody(Pin<Box<dyn MessageBody<Error = Box<dyn StdError>>>>);
impl BoxBody {
/// Boxes a `MessageBody` and any errors it generates.
/// Same as `MessageBody::boxed`.
///
/// If the body type to wrap is unknown or generic it is better to use [`MessageBody::boxed`] to
/// avoid double boxing.
#[inline]
pub fn new<B>(body: B) -> Self
where
B: MessageBody + 'static,
@@ -24,6 +28,7 @@ impl BoxBody {
}
/// Returns a mutable pinned reference to the inner message body type.
#[inline]
pub fn as_pin_mut(&mut self) -> Pin<&mut (dyn MessageBody<Error = Box<dyn StdError>>)> {
self.0.as_mut()
}
@@ -38,10 +43,12 @@ impl fmt::Debug for BoxBody {
impl MessageBody for BoxBody {
type Error = Error;
#[inline]
fn size(&self) -> BodySize {
self.0.size()
}
#[inline]
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
@@ -52,13 +59,20 @@ impl MessageBody for BoxBody {
.map_err(|err| Error::new_body().with_cause(err))
}
#[inline]
fn is_complete_body(&self) -> bool {
self.0.is_complete_body()
}
#[inline]
fn take_complete_body(&mut self) -> Bytes {
self.0.take_complete_body()
}
#[inline]
fn boxed(self) -> BoxBody {
self
}
}
#[cfg(test)]