From 2b818be18f2c286d4e1e020789dec3acb500aeca Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 21 Apr 2021 16:05:59 +0100 Subject: [PATCH] message body error type downstream --- actix-test/src/lib.rs | 4 ++-- src/middleware/compat.rs | 4 +++- src/middleware/logger.rs | 7 ++++--- src/response/response.rs | 2 +- src/server.rs | 2 +- src/service.rs | 5 ++++- src/test.rs | 8 ++++---- 7 files changed, 19 insertions(+), 13 deletions(-) diff --git a/actix-test/src/lib.rs b/actix-test/src/lib.rs index 8fab33289..e0aec7eea 100644 --- a/actix-test/src/lib.rs +++ b/actix-test/src/lib.rs @@ -85,7 +85,7 @@ where S::InitError: fmt::Debug, S::Response: Into> + 'static, >::Future: 'static, - B: MessageBody + 'static, + B: MessageBody + 'static, { start_with(TestServerConfig::default(), factory) } @@ -124,7 +124,7 @@ where S::InitError: fmt::Debug, S::Response: Into> + 'static, >::Future: 'static, - B: MessageBody + 'static, + B: MessageBody + 'static, { let (tx, rx) = mpsc::channel(); diff --git a/src/middleware/compat.rs b/src/middleware/compat.rs index 0e3a4f2b7..3143f3c95 100644 --- a/src/middleware/compat.rs +++ b/src/middleware/compat.rs @@ -113,7 +113,9 @@ pub trait MapServiceResponseBody { fn map_body(self) -> ServiceResponse; } -impl MapServiceResponseBody for ServiceResponse { +impl + Unpin + 'static> MapServiceResponseBody + for ServiceResponse +{ fn map_body(self) -> ServiceResponse { self.map_body(|_, body| ResponseBody::Other(Body::from_message(body))) } diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index 40ed9258f..a1ea8a71a 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -22,10 +22,9 @@ use time::OffsetDateTime; use crate::{ dev::{BodySize, MessageBody, ResponseBody}, - error::{Error, Result}, http::{HeaderName, StatusCode}, service::{ServiceRequest, ServiceResponse}, - HttpResponse, + Error, HttpResponse, Result, }; /// Middleware for logging request and response summaries to the terminal. @@ -327,7 +326,9 @@ impl PinnedDrop for StreamLog { } } -impl MessageBody for StreamLog { +impl> MessageBody for StreamLog { + type Error = Error; + fn size(&self) -> BodySize { self.body.size() } diff --git a/src/response/response.rs b/src/response/response.rs index 31868fe0b..b1b49f820 100644 --- a/src/response/response.rs +++ b/src/response/response.rs @@ -243,7 +243,7 @@ impl HttpResponse { } } -impl fmt::Debug for HttpResponse { +impl> fmt::Debug for HttpResponse { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("HttpResponse") .field("error", &self.error) diff --git a/src/server.rs b/src/server.rs index 6577f4d1f..0d2ea4d08 100644 --- a/src/server.rs +++ b/src/server.rs @@ -80,7 +80,7 @@ where >::Future: 'static, S::Service: 'static, // S::Service: 'static, - B: MessageBody + 'static, + B: MessageBody + 'static, { /// Create new HTTP server with application factory pub fn new(factory: F) -> Self { diff --git a/src/service.rs b/src/service.rs index f6d1f9ebf..2318ce5c2 100644 --- a/src/service.rs +++ b/src/service.rs @@ -443,7 +443,10 @@ impl From> for Response { } } -impl fmt::Debug for ServiceResponse { +impl fmt::Debug for ServiceResponse +where + B: MessageBody, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let res = writeln!( f, diff --git a/src/test.rs b/src/test.rs index c2e456e58..2d02bd395 100644 --- a/src/test.rs +++ b/src/test.rs @@ -150,7 +150,7 @@ where pub async fn read_response(app: &S, req: Request) -> Bytes where S: Service, Error = Error>, - B: MessageBody + Unpin, + B: MessageBody + Unpin, { let mut resp = app .call(req) @@ -195,7 +195,7 @@ where /// ``` pub async fn read_body(mut res: ServiceResponse) -> Bytes where - B: MessageBody + Unpin, + B: MessageBody + Unpin, { let mut body = res.take_body(); let mut bytes = BytesMut::new(); @@ -244,7 +244,7 @@ where /// ``` pub async fn read_body_json(res: ServiceResponse) -> T where - B: MessageBody + Unpin, + B: MessageBody + Unpin, T: DeserializeOwned, { let body = read_body(res).await; @@ -305,7 +305,7 @@ where pub async fn read_response_json(app: &S, req: Request) -> T where S: Service, Error = Error>, - B: MessageBody + Unpin, + B: MessageBody + Unpin, T: DeserializeOwned, { let body = read_response(app, req).await;