Merge branch 'master' into master

This commit is contained in:
Yuki Okushi 2020-01-28 12:36:26 +09:00 committed by GitHub
commit 42690fd525
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 61 deletions

View File

@ -488,10 +488,12 @@ where
} }
} }
#[pin_project::pin_project(PinnedDrop)]
struct OpenWaitingConnection<F, Io> struct OpenWaitingConnection<F, Io>
where where
Io: AsyncRead + AsyncWrite + Unpin + 'static, Io: AsyncRead + AsyncWrite + Unpin + 'static,
{ {
#[pin]
fut: F, fut: F,
key: Key, key: Key,
h2: Option< h2: Option<
@ -525,12 +527,13 @@ where
} }
} }
impl<F, Io> Drop for OpenWaitingConnection<F, Io> #[pin_project::pinned_drop]
impl<F, Io> PinnedDrop for OpenWaitingConnection<F, Io>
where where
Io: AsyncRead + AsyncWrite + Unpin + 'static, Io: AsyncRead + AsyncWrite + Unpin + 'static,
{ {
fn drop(&mut self) { fn drop(self: Pin<&mut Self>) {
if let Some(inner) = self.inner.take() { if let Some(inner) = self.project().inner.take() {
let mut inner = inner.as_ref().borrow_mut(); let mut inner = inner.as_ref().borrow_mut();
inner.release(); inner.release();
inner.check_availibility(); inner.check_availibility();
@ -545,8 +548,8 @@ where
{ {
type Output = (); type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() }; let this = self.as_mut().project();
if let Some(ref mut h2) = this.h2 { if let Some(ref mut h2) = this.h2 {
return match Pin::new(h2).poll(cx) { return match Pin::new(h2).poll(cx) {
@ -571,7 +574,7 @@ where
}; };
} }
match unsafe { Pin::new_unchecked(&mut this.fut) }.poll(cx) { match this.fut.poll(cx) {
Poll::Ready(Err(err)) => { Poll::Ready(Err(err)) => {
let _ = this.inner.take(); let _ = this.inner.take();
if let Some(rx) = this.rx.take() { if let Some(rx) = this.rx.take() {
@ -589,8 +592,8 @@ where
))); )));
Poll::Ready(()) Poll::Ready(())
} else { } else {
this.h2 = Some(handshake(io).boxed_local()); *this.h2 = Some(handshake(io).boxed_local());
unsafe { Pin::new_unchecked(this) }.poll(cx) self.poll(cx)
} }
} }
Poll::Pending => Poll::Pending, Poll::Pending => Poll::Pending,

View File

@ -158,14 +158,16 @@ where
#[pin_project::pin_project] #[pin_project::pin_project]
struct ServiceResponse<F, I, E, B> { struct ServiceResponse<F, I, E, B> {
#[pin]
state: ServiceResponseState<F, B>, state: ServiceResponseState<F, B>,
config: ServiceConfig, config: ServiceConfig,
buffer: Option<Bytes>, buffer: Option<Bytes>,
_t: PhantomData<(I, E)>, _t: PhantomData<(I, E)>,
} }
#[pin_project::pin_project]
enum ServiceResponseState<F, B> { enum ServiceResponseState<F, B> {
ServiceCall(F, Option<SendResponse<Bytes>>), ServiceCall(#[pin] F, Option<SendResponse<Bytes>>),
SendPayload(SendStream<Bytes>, ResponseBody<B>), SendPayload(SendStream<Bytes>, ResponseBody<B>),
} }
@ -247,12 +249,14 @@ where
{ {
type Output = (); type Output = ();
#[pin_project::project]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project(); let mut this = self.as_mut().project();
match this.state { #[project]
ServiceResponseState::ServiceCall(ref mut call, ref mut send) => { match this.state.project() {
match unsafe { Pin::new_unchecked(call) }.poll(cx) { ServiceResponseState::ServiceCall(call, send) => {
match call.poll(cx) {
Poll::Ready(Ok(res)) => { Poll::Ready(Ok(res)) => {
let (res, body) = res.into().replace_body(()); let (res, body) = res.into().replace_body(());
@ -273,8 +277,7 @@ where
if size.is_eof() { if size.is_eof() {
Poll::Ready(()) Poll::Ready(())
} else { } else {
*this.state = this.state.set(ServiceResponseState::SendPayload(stream, body));
ServiceResponseState::SendPayload(stream, body);
self.poll(cx) self.poll(cx)
} }
} }
@ -300,10 +303,10 @@ where
if size.is_eof() { if size.is_eof() {
Poll::Ready(()) Poll::Ready(())
} else { } else {
*this.state = ServiceResponseState::SendPayload( this.state.set(ServiceResponseState::SendPayload(
stream, stream,
body.into_body(), body.into_body(),
); ));
self.poll(cx) self.poll(cx)
} }
} }

View File

@ -193,6 +193,30 @@ impl FromRequest for () {
macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => { macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
// This module is a trick to get around the inability of
// `macro_rules!` macros to make new idents. We want to make
// a new `FutWrapper` struct for each distinct invocation of
// this macro. Ideally, we would name it something like
// `FutWrapper_$fut_type`, but this can't be done in a macro_rules
// macro.
//
// Instead, we put everything in a module named `$fut_type`, thus allowing
// us to use the name `FutWrapper` without worrying about conflicts.
// This macro only exists to generate trait impls for tuples - these
// are inherently global, so users don't have to care about this
// weird trick.
#[allow(non_snake_case)]
mod $fut_type {
// Bring everything into scope, so we don't need
// redundant imports
use super::*;
/// A helper struct to allow us to pin-project through
/// to individual fields
#[pin_project::pin_project]
struct FutWrapper<$($T: FromRequest),+>($(#[pin] $T::Future),+);
/// FromRequest implementation for tuple /// FromRequest implementation for tuple
#[doc(hidden)] #[doc(hidden)]
#[allow(unused_parens)] #[allow(unused_parens)]
@ -205,7 +229,7 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
$fut_type { $fut_type {
items: <($(Option<$T>,)+)>::default(), items: <($(Option<$T>,)+)>::default(),
futs: ($($T::from_request(req, payload),)+), futs: FutWrapper($($T::from_request(req, payload),)+),
} }
} }
} }
@ -214,7 +238,8 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
#[pin_project::pin_project] #[pin_project::pin_project]
pub struct $fut_type<$($T: FromRequest),+> { pub struct $fut_type<$($T: FromRequest),+> {
items: ($(Option<$T>,)+), items: ($(Option<$T>,)+),
futs: ($($T::Future,)+), #[pin]
futs: FutWrapper<$($T,)+>,
} }
impl<$($T: FromRequest),+> Future for $fut_type<$($T),+> impl<$($T: FromRequest),+> Future for $fut_type<$($T),+>
@ -222,12 +247,12 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
type Output = Result<($($T,)+), Error>; type Output = Result<($($T,)+), Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let mut this = self.project();
let mut ready = true; let mut ready = true;
$( $(
if this.items.$n.is_none() { if this.items.$n.is_none() {
match unsafe { Pin::new_unchecked(&mut this.futs.$n) }.poll(cx) { match this.futs.as_mut().project().$n.poll(cx) {
Poll::Ready(Ok(item)) => { Poll::Ready(Ok(item)) => {
this.items.$n = Some(item); this.items.$n = Some(item);
} }
@ -246,6 +271,7 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
} }
} }
} }
}
}); });
#[rustfmt::skip] #[rustfmt::skip]

View File

@ -95,11 +95,10 @@ where
/// Calls service and waits for response future completion. /// Calls service and waits for response future completion.
/// ///
/// ```rust /// ```rust
/// use actix_web::{test, App, HttpResponse, http::StatusCode}; /// use actix_web::{test, web, App, HttpResponse, http::StatusCode};
/// use actix_service::Service;
/// ///
/// #[test] /// #[actix_rt::test]
/// fn test_response() { /// async fn test_response() {
/// let mut app = test::init_service( /// let mut app = test::init_service(
/// App::new() /// App::new()
/// .service(web::resource("/test").to(|| async { /// .service(web::resource("/test").to(|| async {