1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-09-02 01:31:57 +02:00

use sync method on Responder trait (#1891)

This commit is contained in:
fakeshadow
2021-01-09 06:17:19 +08:00
committed by GitHub
parent 2204614134
commit d40ae8c8ca
10 changed files with 221 additions and 300 deletions

View File

@@ -1,13 +1,6 @@
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
use actix_http::{Error, Response};
use bytes::Bytes;
use futures_util::{future::LocalBoxFuture, ready, FutureExt, TryFutureExt};
use pin_project::pin_project;
use futures_util::{future::LocalBoxFuture, FutureExt, TryFutureExt};
use crate::{dev, request::HttpRequest, FromRequest, Responder};
@@ -68,42 +61,10 @@ where
A: Responder,
B: Responder,
{
type Error = Error;
type Future = EitherResponder<A, B>;
fn respond_to(self, req: &HttpRequest) -> Self::Future {
fn respond_to(self, req: &HttpRequest) -> Response {
match self {
Either::A(a) => EitherResponder::A(a.respond_to(req)),
Either::B(b) => EitherResponder::B(b.respond_to(req)),
}
}
}
#[pin_project(project = EitherResponderProj)]
pub enum EitherResponder<A, B>
where
A: Responder,
B: Responder,
{
A(#[pin] A::Future),
B(#[pin] B::Future),
}
impl<A, B> Future for EitherResponder<A, B>
where
A: Responder,
B: Responder,
{
type Output = Result<Response, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project() {
EitherResponderProj::A(fut) => {
Poll::Ready(ready!(fut.poll(cx)).map_err(|e| e.into()))
}
EitherResponderProj::B(fut) => {
Poll::Ready(ready!(fut.poll(cx).map_err(|e| e.into())))
}
Either::A(a) => a.respond_to(req),
Either::B(b) => b.respond_to(req),
}
}
}

View File

@@ -9,7 +9,7 @@ use std::{fmt, ops};
use actix_http::{Error, HttpMessage, Payload, Response};
use bytes::BytesMut;
use encoding_rs::{Encoding, UTF_8};
use futures_util::future::{err, ok, FutureExt, LocalBoxFuture, Ready};
use futures_util::future::{FutureExt, LocalBoxFuture};
use futures_util::StreamExt;
use serde::de::DeserializeOwned;
use serde::Serialize;
@@ -158,18 +158,13 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
}
impl<T: Serialize> Responder for Form<T> {
type Error = Error;
type Future = Ready<Result<Response, Error>>;
fn respond_to(self, _: &HttpRequest) -> Self::Future {
let body = match serde_urlencoded::to_string(&self.0) {
Ok(body) => body,
Err(e) => return err(e.into()),
};
ok(Response::build(StatusCode::OK)
.set(ContentType::form_url_encoded())
.body(body))
fn respond_to(self, _: &HttpRequest) -> Response {
match serde_urlencoded::to_string(&self.0) {
Ok(body) => Response::build(StatusCode::OK)
.set(ContentType::form_url_encoded())
.body(body),
Err(e) => Response::from_error(e.into()),
}
}
}
@@ -493,7 +488,7 @@ mod tests {
hello: "world".to_string(),
counter: 123,
});
let resp = form.respond_to(&req).await.unwrap();
let resp = form.respond_to(&req);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(CONTENT_TYPE).unwrap(),

View File

@@ -8,7 +8,6 @@ use std::task::{Context, Poll};
use std::{fmt, ops};
use bytes::BytesMut;
use futures_util::future::{ready, Ready};
use futures_util::ready;
use futures_util::stream::Stream;
use serde::de::DeserializeOwned;
@@ -123,18 +122,13 @@ where
}
impl<T: Serialize> Responder for Json<T> {
type Error = Error;
type Future = Ready<Result<Response, Error>>;
fn respond_to(self, _: &HttpRequest) -> Self::Future {
let body = match serde_json::to_string(&self.0) {
Ok(body) => body,
Err(e) => return ready(Err(e.into())),
};
ready(Ok(Response::build(StatusCode::OK)
.content_type("application/json")
.body(body)))
fn respond_to(self, _: &HttpRequest) -> Response {
match serde_json::to_string(&self.0) {
Ok(body) => Response::build(StatusCode::OK)
.content_type("application/json")
.body(body),
Err(e) => Response::from_error(e.into()),
}
}
}
@@ -498,7 +492,7 @@ mod tests {
let j = Json(MyObject {
name: "test".to_string(),
});
let resp = j.respond_to(&req).await.unwrap();
let resp = j.respond_to(&req);
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),