1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-01 04:51:51 +02:00

provide optimisation path for single-chunk body types (#2497)

This commit is contained in:
Rob Ede
2021-12-09 13:52:35 +00:00
committed by GitHub
parent 69fa17f66f
commit 774ac7fec4
7 changed files with 328 additions and 30 deletions

View File

@@ -51,6 +51,34 @@ impl MessageBody for BoxBody {
.poll_next(cx)
.map_err(|err| Error::new_body().with_cause(err))
}
fn is_complete_body(&self) -> bool {
self.0.is_complete_body()
}
fn take_complete_body(&mut self) -> Bytes {
debug_assert!(
self.is_complete_body(),
"boxed type does not allow taking complete body; caller should make sure to \
call `is_complete_body` first",
);
// we do not have DerefMut access to call take_complete_body directly but since
// is_complete_body is true we should expect the entire bytes chunk in one poll_next
let waker = futures_util::task::noop_waker();
let mut cx = Context::from_waker(&waker);
match self.as_pin_mut().poll_next(&mut cx) {
Poll::Ready(Some(Ok(data))) => data,
_ => {
panic!(
"boxed type indicated it allows taking complete body but failed to \
return Bytes when polled",
);
}
}
}
}
#[cfg(test)]