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

add associated error type to MessageBody (#2183)

This commit is contained in:
Rob Ede
2021-05-05 18:36:02 +01:00
committed by GitHub
parent dd1a3e7675
commit ddaf8c3e43
27 changed files with 447 additions and 74 deletions

View File

@ -113,7 +113,11 @@ pub trait MapServiceResponseBody {
fn map_body(self) -> ServiceResponse;
}
impl<B: MessageBody + Unpin + 'static> MapServiceResponseBody for ServiceResponse<B> {
impl<B> MapServiceResponseBody for ServiceResponse<B>
where
B: MessageBody + Unpin + 'static,
B::Error: Into<Error>,
{
fn map_body(self) -> ServiceResponse {
self.map_body(|_, body| ResponseBody::Other(Body::from_message(body)))
}

View File

@ -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,13 @@ impl<B> PinnedDrop for StreamLog<B> {
}
}
impl<B: MessageBody> MessageBody for StreamLog<B> {
impl<B> MessageBody for StreamLog<B>
where
B: MessageBody,
B::Error: Into<Error>,
{
type Error = Error;
fn size(&self) -> BodySize {
self.body.size()
}
@ -335,7 +340,7 @@ impl<B: MessageBody> MessageBody for StreamLog<B> {
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Error>>> {
) -> Poll<Option<Result<Bytes, Self::Error>>> {
let this = self.project();
match this.body.poll_next(cx) {
Poll::Ready(Some(Ok(chunk))) => {

View File

@ -243,7 +243,11 @@ impl<B> HttpResponse<B> {
}
}
impl<B: MessageBody> fmt::Debug for HttpResponse<B> {
impl<B> fmt::Debug for HttpResponse<B>
where
B: MessageBody,
B::Error: Into<Error>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("HttpResponse")
.field("error", &self.error)

View File

@ -81,6 +81,7 @@ where
S::Service: 'static,
// S::Service: 'static,
B: MessageBody + 'static,
B::Error: Into<Error>,
{
/// Create new HTTP server with application factory
pub fn new(factory: F) -> Self {

View File

@ -443,7 +443,11 @@ impl<B> From<ServiceResponse<B>> for Response<B> {
}
}
impl<B: MessageBody> fmt::Debug for ServiceResponse<B> {
impl<B> fmt::Debug for ServiceResponse<B>
where
B: MessageBody,
B::Error: Into<Error>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let res = writeln!(
f,

View File

@ -151,6 +151,7 @@ pub async fn read_response<S, B>(app: &S, req: Request) -> Bytes
where
S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody + Unpin,
B::Error: Into<Error>,
{
let mut resp = app
.call(req)
@ -196,6 +197,7 @@ where
pub async fn read_body<B>(mut res: ServiceResponse<B>) -> Bytes
where
B: MessageBody + Unpin,
B::Error: Into<Error>,
{
let mut body = res.take_body();
let mut bytes = BytesMut::new();
@ -245,6 +247,7 @@ where
pub async fn read_body_json<T, B>(res: ServiceResponse<B>) -> T
where
B: MessageBody + Unpin,
B::Error: Into<Error>,
T: DeserializeOwned,
{
let body = read_body(res).await;
@ -306,6 +309,7 @@ pub async fn read_response_json<S, B, T>(app: &S, req: Request) -> T
where
S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody + Unpin,
B::Error: Into<Error>,
T: DeserializeOwned,
{
let body = read_response(app, req).await;