From 745e96a1856946c9ba9d3fbdf9cc94e97396b10f Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 24 Dec 2021 23:51:33 +0000 Subject: [PATCH] ReadBody does not require Unpin stream --- awc/src/response.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/awc/src/response.rs b/awc/src/response.rs index 78cc339b4..3d78b84fb 100644 --- a/awc/src/response.rs +++ b/awc/src/response.rs @@ -415,10 +415,13 @@ where } } -struct ReadBody { - stream: Payload, - buf: BytesMut, - limit: usize, +pin_project_lite::pin_project! { + struct ReadBody { + #[pin] + stream: Payload, + buf: BytesMut, + limit: usize, + } } impl ReadBody { @@ -433,15 +436,15 @@ impl ReadBody { impl Future for ReadBody where - S: Stream> + Unpin, + S: Stream>, { type Output = Result; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.get_mut(); + let mut this = self.project(); - while let Some(chunk) = ready!(Pin::new(&mut this.stream).poll_next(cx)?) { - if (this.buf.len() + chunk.len()) > this.limit { + while let Some(chunk) = ready!(this.stream.as_mut().poll_next(cx)?) { + if (this.buf.len() + chunk.len()) > *this.limit { return Poll::Ready(Err(PayloadError::Overflow)); } this.buf.extend_from_slice(&chunk); @@ -453,11 +456,14 @@ where #[cfg(test)] mod tests { - use super::*; use serde::{Deserialize, Serialize}; + use static_assertions::assert_impl_all; + use super::*; use crate::{http::header, test::TestResponse}; + assert_impl_all!(ClientResponse: Unpin); + #[actix_rt::test] async fn test_body() { let mut req = TestResponse::with_header((header::CONTENT_LENGTH, "xxxx")).finish();