From 77058ef779449ad53a2ed9dd5b62b8c718b2bed3 Mon Sep 17 00:00:00 2001 From: Maksym Vorobiov Date: Tue, 18 Feb 2020 20:48:37 +0200 Subject: [PATCH] adopt MessageBody Pin changes to actix-web root --- src/middleware/logger.rs | 17 ++++++++++++----- src/test.rs | 6 +++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index d692132c..e40fe648 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -238,15 +238,20 @@ where } } +use pin_project::{pin_project, pinned_drop}; + +#[pin_project(PinnedDrop)] pub struct StreamLog { + #[pin] body: ResponseBody, format: Option, size: usize, time: OffsetDateTime, } -impl Drop for StreamLog { - fn drop(&mut self) { +#[pinned_drop] +impl PinnedDrop for StreamLog { + fn drop(self: Pin<&mut Self>) { if let Some(ref format) = self.format { let render = |fmt: &mut Formatter<'_>| { for unit in &format.0 { @@ -259,15 +264,17 @@ impl Drop for StreamLog { } } + impl MessageBody for StreamLog { fn size(&self) -> BodySize { self.body.size() } - fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll>> { - match self.body.poll_next(cx) { + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll>> { + let this = self.project(); + match this.body.poll_next(cx) { Poll::Ready(Some(Ok(chunk))) => { - self.size += chunk.len(); + *this.size += chunk.len(); Poll::Ready(Some(Ok(chunk))) } val => val, diff --git a/src/test.rs b/src/test.rs index 6a6ef27c..0eb02ff7 100644 --- a/src/test.rs +++ b/src/test.rs @@ -150,7 +150,7 @@ where pub async fn read_response(app: &mut S, req: Request) -> Bytes where S: Service, Error = Error>, - B: MessageBody, + B: MessageBody + Unpin, { let mut resp = app .call(req) @@ -193,7 +193,7 @@ where /// ``` pub async fn read_body(mut res: ServiceResponse) -> Bytes where - B: MessageBody, + B: MessageBody + Unpin, { let mut body = res.take_body(); let mut bytes = BytesMut::new(); @@ -251,7 +251,7 @@ where pub async fn read_response_json(app: &mut S, req: Request) -> T where S: Service, Error = Error>, - B: MessageBody, + B: MessageBody + Unpin, T: DeserializeOwned, { let body = read_response(app, req).await;