1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-21 23:56:35 +00:00

Merge pull request #1553 from taiki-e/pin-project

Remove uses of pin_project::project attribute
This commit is contained in:
Yuki Okushi 2020-06-07 02:00:01 +09:00 committed by GitHub
commit 4f9a1ac3b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 55 additions and 83 deletions

View file

@ -90,7 +90,7 @@ fxhash = "0.2.1"
log = "0.4"
mime = "0.3"
socket2 = "0.3"
pin-project = "0.4.6"
pin-project = "0.4.17"
regex = "1.3"
serde = { version = "1.0", features=["derive"] }
serde_json = "1.0"

View file

@ -70,7 +70,7 @@ language-tags = "0.2"
log = "0.4"
mime = "0.3"
percent-encoding = "2.1"
pin-project = "0.4.6"
pin-project = "0.4.17"
rand = "0.7"
regex = "1.3"
serde = "1.0"

View file

@ -6,7 +6,7 @@ use std::{fmt, mem};
use bytes::{Bytes, BytesMut};
use futures_core::Stream;
use futures_util::ready;
use pin_project::{pin_project, project};
use pin_project::pin_project;
use crate::error::Error;
@ -70,7 +70,7 @@ impl<T: MessageBody + Unpin> MessageBody for Box<T> {
}
}
#[pin_project]
#[pin_project(project = ResponseBodyProj)]
pub enum ResponseBody<B> {
Body(#[pin] B),
Other(#[pin] Body),
@ -109,15 +109,13 @@ impl<B: MessageBody> MessageBody for ResponseBody<B> {
}
}
#[project]
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Error>>> {
#[project]
match self.project() {
ResponseBody::Body(body) => body.poll_next(cx),
ResponseBody::Other(body) => body.poll_next(cx),
ResponseBodyProj::Body(body) => body.poll_next(cx),
ResponseBodyProj::Other(body) => body.poll_next(cx),
}
}
}
@ -125,20 +123,18 @@ impl<B: MessageBody> MessageBody for ResponseBody<B> {
impl<B: MessageBody> Stream for ResponseBody<B> {
type Item = Result<Bytes, Error>;
#[project]
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
#[project]
match self.project() {
ResponseBody::Body(body) => body.poll_next(cx),
ResponseBody::Other(body) => body.poll_next(cx),
ResponseBodyProj::Body(body) => body.poll_next(cx),
ResponseBodyProj::Other(body) => body.poll_next(cx),
}
}
}
#[pin_project]
#[pin_project(project = BodyProj)]
/// Represents various types of http message body.
pub enum Body {
/// Empty response. `Content-Length` header is not set.
@ -173,16 +169,14 @@ impl MessageBody for Body {
}
}
#[project]
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Error>>> {
#[project]
match self.project() {
Body::None => Poll::Ready(None),
Body::Empty => Poll::Ready(None),
Body::Bytes(ref mut bin) => {
BodyProj::None => Poll::Ready(None),
BodyProj::Empty => Poll::Ready(None),
BodyProj::Bytes(ref mut bin) => {
let len = bin.len();
if len == 0 {
Poll::Ready(None)
@ -190,7 +184,7 @@ impl MessageBody for Body {
Poll::Ready(Some(Ok(mem::take(bin))))
}
}
Body::Message(ref mut body) => Pin::new(body.as_mut()).poll_next(cx),
BodyProj::Message(ref mut body) => Pin::new(body.as_mut()).poll_next(cx),
}
}
}

View file

@ -7,7 +7,7 @@ use actix_codec::{AsyncRead, AsyncWrite, Framed};
use bytes::{Buf, Bytes};
use futures_util::future::{err, Either, FutureExt, LocalBoxFuture, Ready};
use h2::client::SendRequest;
use pin_project::{pin_project, project};
use pin_project::pin_project;
use crate::body::MessageBody;
use crate::h1::ClientCodec;
@ -205,7 +205,7 @@ where
}
}
#[pin_project]
#[pin_project(project = EitherIoProj)]
pub enum EitherIo<A, B> {
A(#[pin] A),
B(#[pin] B),
@ -216,16 +216,14 @@ where
A: AsyncRead,
B: AsyncRead,
{
#[project]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
#[project]
match self.project() {
EitherIo::A(val) => val.poll_read(cx, buf),
EitherIo::B(val) => val.poll_read(cx, buf),
EitherIoProj::A(val) => val.poll_read(cx, buf),
EitherIoProj::B(val) => val.poll_read(cx, buf),
}
}
@ -245,41 +243,34 @@ where
A: AsyncWrite,
B: AsyncWrite,
{
#[project]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
#[project]
match self.project() {
EitherIo::A(val) => val.poll_write(cx, buf),
EitherIo::B(val) => val.poll_write(cx, buf),
EitherIoProj::A(val) => val.poll_write(cx, buf),
EitherIoProj::B(val) => val.poll_write(cx, buf),
}
}
#[project]
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
#[project]
match self.project() {
EitherIo::A(val) => val.poll_flush(cx),
EitherIo::B(val) => val.poll_flush(cx),
EitherIoProj::A(val) => val.poll_flush(cx),
EitherIoProj::B(val) => val.poll_flush(cx),
}
}
#[project]
fn poll_shutdown(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<io::Result<()>> {
#[project]
match self.project() {
EitherIo::A(val) => val.poll_shutdown(cx),
EitherIo::B(val) => val.poll_shutdown(cx),
EitherIoProj::A(val) => val.poll_shutdown(cx),
EitherIoProj::B(val) => val.poll_shutdown(cx),
}
}
#[project]
fn poll_write_buf<U: Buf>(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
@ -288,10 +279,9 @@ where
where
Self: Sized,
{
#[project]
match self.project() {
EitherIo::A(val) => val.poll_write_buf(cx, buf),
EitherIo::B(val) => val.poll_write_buf(cx, buf),
EitherIoProj::A(val) => val.poll_write_buf(cx, buf),
EitherIoProj::B(val) => val.poll_write_buf(cx, buf),
}
}
}

View file

@ -9,7 +9,7 @@ use brotli2::write::BrotliEncoder;
use bytes::Bytes;
use flate2::write::{GzEncoder, ZlibEncoder};
use futures_core::ready;
use pin_project::{pin_project, project};
use pin_project::pin_project;
use crate::body::{Body, BodySize, MessageBody, ResponseBody};
use crate::http::header::{ContentEncoding, CONTENT_ENCODING};
@ -79,7 +79,7 @@ impl<B: MessageBody> Encoder<B> {
}
}
#[pin_project]
#[pin_project(project = EncoderBodyProj)]
enum EncoderBody<B> {
Bytes(Bytes),
Stream(#[pin] B),
@ -95,22 +95,22 @@ impl<B: MessageBody> MessageBody for EncoderBody<B> {
}
}
#[project]
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Error>>> {
#[project]
match self.project() {
EncoderBody::Bytes(b) => {
EncoderBodyProj::Bytes(b) => {
if b.is_empty() {
Poll::Ready(None)
} else {
Poll::Ready(Some(Ok(std::mem::take(b))))
}
}
EncoderBody::Stream(b) => b.poll_next(cx),
EncoderBody::BoxedStream(ref mut b) => Pin::new(b.as_mut()).poll_next(cx),
EncoderBodyProj::Stream(b) => b.poll_next(cx),
EncoderBodyProj::BoxedStream(ref mut b) => {
Pin::new(b.as_mut()).poll_next(cx)
}
}
}
}

View file

@ -58,7 +58,7 @@ where
inner: DispatcherState<T, S, B, X, U>,
}
#[pin_project]
#[pin_project(project = DispatcherStateProj)]
enum DispatcherState<T, S, B, X, U>
where
S: Service<Request = Request>,
@ -73,7 +73,7 @@ where
Upgrade(Pin<Box<U::Future>>),
}
#[pin_project]
#[pin_project(project = InnerDispatcherProj)]
struct InnerDispatcher<T, S, B, X, U>
where
S: Service<Request = Request>,
@ -112,7 +112,7 @@ enum DispatcherMessage {
Error(Response<()>),
}
#[pin_project]
#[pin_project(project = StateProj)]
enum State<S, B, X>
where
S: Service<Request = Request>,
@ -296,7 +296,6 @@ where
///
/// true - got WouldBlock
/// false - didn't get WouldBlock
#[pin_project::project]
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
@ -307,8 +306,7 @@ where
let len = self.write_buf.len();
let mut written = 0;
#[project]
let InnerDispatcher { io, write_buf, .. } = self.project();
let InnerDispatcherProj { io, write_buf, .. } = self.project();
let mut io = Pin::new(io.as_mut().unwrap());
while written < len {
match io.as_mut().poll_write(cx, &write_buf[written..]) {
@ -366,16 +364,14 @@ where
.extend_from_slice(b"HTTP/1.1 100 Continue\r\n\r\n");
}
#[pin_project::project]
fn poll_response(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Result<PollResponse, DispatchError> {
loop {
let mut this = self.as_mut().project();
#[project]
let state = match this.state.project() {
State::None => match this.messages.pop_front() {
StateProj::None => match this.messages.pop_front() {
Some(DispatcherMessage::Item(req)) => {
Some(self.as_mut().handle_request(req, cx)?)
}
@ -388,7 +384,7 @@ where
}
None => None,
},
State::ExpectCall(fut) => match fut.as_mut().poll(cx) {
StateProj::ExpectCall(fut) => match fut.as_mut().poll(cx) {
Poll::Ready(Ok(req)) => {
self.as_mut().send_continue();
this = self.as_mut().project();
@ -403,7 +399,7 @@ where
}
Poll::Pending => None,
},
State::ServiceCall(fut) => match fut.as_mut().poll(cx) {
StateProj::ServiceCall(fut) => match fut.as_mut().poll(cx) {
Poll::Ready(Ok(res)) => {
let (res, body) = res.into().replace_body(());
let state = self.as_mut().send_response(res, body)?;
@ -418,7 +414,7 @@ where
}
Poll::Pending => None,
},
State::SendPayload(mut stream) => {
StateProj::SendPayload(mut stream) => {
loop {
if this.write_buf.len() < HW_BUFFER_SIZE {
match stream.as_mut().poll_next(cx) {
@ -724,13 +720,11 @@ where
{
type Output = Result<(), DispatchError>;
#[pin_project::project]
#[inline]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.as_mut().project();
#[project]
match this.inner.project() {
DispatcherState::Normal(mut inner) => {
DispatcherStateProj::Normal(mut inner) => {
inner.as_mut().poll_keepalive(cx)?;
if inner.flags.contains(Flags::SHUTDOWN) {
@ -850,7 +844,7 @@ where
}
}
}
DispatcherState::Upgrade(fut) => fut.as_mut().poll(cx).map_err(|e| {
DispatcherStateProj::Upgrade(fut) => fut.as_mut().poll(cx).map_err(|e| {
error!("Upgrade handler error: {}", e);
DispatchError::Upgrade
}),

View file

@ -165,7 +165,7 @@ struct ServiceResponse<F, I, E, B> {
_t: PhantomData<(I, E)>,
}
#[pin_project::pin_project]
#[pin_project::pin_project(project = ServiceResponseStateProj)]
enum ServiceResponseState<F, B> {
ServiceCall(#[pin] F, Option<SendResponse<Bytes>>),
SendPayload(SendStream<Bytes>, #[pin] ResponseBody<B>),
@ -245,13 +245,11 @@ where
{
type Output = ();
#[pin_project::project]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
#[project]
match this.state.project() {
ServiceResponseState::ServiceCall(call, send) => match call.poll(cx) {
ServiceResponseStateProj::ServiceCall(call, send) => match call.poll(cx) {
Poll::Ready(Ok(res)) => {
let (res, body) = res.into().replace_body(());
@ -305,7 +303,7 @@ where
}
}
},
ServiceResponseState::SendPayload(ref mut stream, ref mut body) => loop {
ServiceResponseStateProj::SendPayload(ref mut stream, ref mut body) => loop {
loop {
if let Some(ref mut buffer) = this.buffer {
match stream.poll_capacity(cx) {

View file

@ -10,7 +10,7 @@ use bytes::Bytes;
use futures_core::{ready, Future};
use futures_util::future::ok;
use h2::server::{self, Handshake};
use pin_project::{pin_project, project};
use pin_project::pin_project;
use crate::body::MessageBody;
use crate::builder::HttpServiceBuilder;
@ -574,7 +574,7 @@ where
}
}
#[pin_project]
#[pin_project(project = StateProj)]
enum State<T, S, B, X, U>
where
S: Service<Request = Request>,
@ -650,16 +650,14 @@ where
U: Service<Request = (Request, Framed<T, h1::Codec>), Response = ()>,
U::Error: fmt::Display,
{
#[project]
fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), DispatchError>> {
#[project]
match self.as_mut().project() {
State::H1(disp) => disp.poll(cx),
State::H2(disp) => disp.poll(cx),
State::H2Handshake(ref mut data) => {
StateProj::H1(disp) => disp.poll(cx),
StateProj::H2(disp) => disp.poll(cx),
StateProj::H2Handshake(ref mut data) => {
let conn = if let Some(ref mut item) = data {
match Pin::new(&mut item.0).poll(cx) {
Poll::Ready(Ok(conn)) => conn,

View file

@ -23,7 +23,7 @@ actix-codec = "0.2.0"
bytes = "0.5.2"
futures-channel = { version = "0.3.5", default-features = false }
futures-core = { version = "0.3.5", default-features = false }
pin-project = "0.4.6"
pin-project = "0.4.17"
[dev-dependencies]
actix-rt = "1.0.0"

View file

@ -12,7 +12,7 @@ use actix_http::{Error, Response, ResponseBuilder};
use bytes::{Bytes, BytesMut};
use futures_util::future::{err, ok, Either as EitherFuture, Ready};
use futures_util::ready;
use pin_project::{pin_project, project};
use pin_project::pin_project;
use crate::request::HttpRequest;
@ -379,7 +379,7 @@ where
}
}
#[pin_project]
#[pin_project(project = EitherResponderProj)]
pub enum EitherResponder<A, B>
where
A: Responder,
@ -396,14 +396,12 @@ where
{
type Output = Result<Response, Error>;
#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
#[project]
match self.project() {
EitherResponder::A(fut) => {
EitherResponderProj::A(fut) => {
Poll::Ready(ready!(fut.poll(cx)).map_err(|e| e.into()))
}
EitherResponder::B(fut) => {
EitherResponderProj::B(fut) => {
Poll::Ready(ready!(fut.poll(cx).map_err(|e| e.into())))
}
}