From 82e92cfbd9028205fb63557e0d58ffd1496ec4dc Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 6 Jun 2020 12:23:52 +0900 Subject: [PATCH] Remove uses of pin_project::project attribute pin-project will deprecate the project attribute due to some unfixable limitations. Refs: https://github.com/taiki-e/pin-project/issues/225 --- Cargo.toml | 3 +- core/Cargo.toml | 2 +- core/src/either.rs | 94 ++++++++--------------- misc/multistream-select/Cargo.toml | 2 +- misc/multistream-select/src/negotiated.rs | 49 ++++-------- protocols/pnet/Cargo.toml | 2 +- protocols/secio/Cargo.toml | 2 +- 7 files changed, 55 insertions(+), 99 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d53e8dfd643..59f9db58741 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,7 +75,7 @@ libp2p-wasm-ext = { version = "0.19.0", path = "transports/wasm-ext", optional = libp2p-yamux = { version = "0.19.0", path = "muxers/yamux", optional = true } libp2p-noise = { version = "0.19.0", path = "protocols/noise", optional = true } parking_lot = "0.10.0" -pin-project = "0.4.6" +pin-project = "0.4.17" smallvec = "1.0" wasm-timer = "0.2.4" @@ -115,4 +115,3 @@ members = [ "transports/websocket", "transports/wasm-ext" ] - diff --git a/core/Cargo.toml b/core/Cargo.toml index 87e6b871bb9..f61866875db 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -24,7 +24,7 @@ multiaddr = { package = "parity-multiaddr", version = "0.9.0", path = "../misc/m multihash = "0.11.0" multistream-select = { version = "0.8.1", path = "../misc/multistream-select" } parking_lot = "0.10.0" -pin-project = "0.4.6" +pin-project = "0.4.17" prost = "0.6.1" rand = "0.7" rw-stream-sink = "0.2.0" diff --git a/core/src/either.rs b/core/src/either.rs index 1c3a09cc5b1..e5052b79819 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -25,7 +25,7 @@ use crate::{ Multiaddr }; use futures::{prelude::*, io::{IoSlice, IoSliceMut}}; -use pin_project::{pin_project, project}; +use pin_project::pin_project; use std::{fmt, io::{Error as IoError}, pin::Pin, task::Context, task::Poll}; #[derive(Debug, Copy, Clone)] @@ -62,7 +62,7 @@ where /// Implements `AsyncRead` and `AsyncWrite` and dispatches all method calls to /// either `First` or `Second`. -#[pin_project] +#[pin_project(project = EitherOutputProj)] #[derive(Debug, Copy, Clone)] pub enum EitherOutput { First(#[pin] A), @@ -74,23 +74,19 @@ where A: AsyncRead, B: AsyncRead, { - #[project] fn poll_read(self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll> { - #[project] match self.project() { - EitherOutput::First(a) => AsyncRead::poll_read(a, cx, buf), - EitherOutput::Second(b) => AsyncRead::poll_read(b, cx, buf), + EitherOutputProj::First(a) => AsyncRead::poll_read(a, cx, buf), + EitherOutputProj::Second(b) => AsyncRead::poll_read(b, cx, buf), } } - #[project] fn poll_read_vectored(self: Pin<&mut Self>, cx: &mut Context, bufs: &mut [IoSliceMut]) -> Poll> { - #[project] match self.project() { - EitherOutput::First(a) => AsyncRead::poll_read_vectored(a, cx, bufs), - EitherOutput::Second(b) => AsyncRead::poll_read_vectored(b, cx, bufs), + EitherOutputProj::First(a) => AsyncRead::poll_read_vectored(a, cx, bufs), + EitherOutputProj::Second(b) => AsyncRead::poll_read_vectored(b, cx, bufs), } } } @@ -100,41 +96,33 @@ where A: AsyncWrite, B: AsyncWrite, { - #[project] fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll> { - #[project] match self.project() { - EitherOutput::First(a) => AsyncWrite::poll_write(a, cx, buf), - EitherOutput::Second(b) => AsyncWrite::poll_write(b, cx, buf), + EitherOutputProj::First(a) => AsyncWrite::poll_write(a, cx, buf), + EitherOutputProj::Second(b) => AsyncWrite::poll_write(b, cx, buf), } } - #[project] fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context, bufs: &[IoSlice]) -> Poll> { - #[project] match self.project() { - EitherOutput::First(a) => AsyncWrite::poll_write_vectored(a, cx, bufs), - EitherOutput::Second(b) => AsyncWrite::poll_write_vectored(b, cx, bufs), + EitherOutputProj::First(a) => AsyncWrite::poll_write_vectored(a, cx, bufs), + EitherOutputProj::Second(b) => AsyncWrite::poll_write_vectored(b, cx, bufs), } } - #[project] fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - #[project] match self.project() { - EitherOutput::First(a) => AsyncWrite::poll_flush(a, cx), - EitherOutput::Second(b) => AsyncWrite::poll_flush(b, cx), + EitherOutputProj::First(a) => AsyncWrite::poll_flush(a, cx), + EitherOutputProj::Second(b) => AsyncWrite::poll_flush(b, cx), } } - #[project] fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - #[project] match self.project() { - EitherOutput::First(a) => AsyncWrite::poll_close(a, cx), - EitherOutput::Second(b) => AsyncWrite::poll_close(b, cx), + EitherOutputProj::First(a) => AsyncWrite::poll_close(a, cx), + EitherOutputProj::Second(b) => AsyncWrite::poll_close(b, cx), } } } @@ -146,13 +134,11 @@ where { type Item = Result>; - #[project] fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - #[project] match self.project() { - EitherOutput::First(a) => TryStream::try_poll_next(a, cx) + EitherOutputProj::First(a) => TryStream::try_poll_next(a, cx) .map(|v| v.map(|r| r.map_err(EitherError::A))), - EitherOutput::Second(b) => TryStream::try_poll_next(b, cx) + EitherOutputProj::Second(b) => TryStream::try_poll_next(b, cx) .map(|v| v.map(|r| r.map_err(EitherError::B))), } } @@ -165,39 +151,31 @@ where { type Error = EitherError; - #[project] fn poll_ready(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - #[project] match self.project() { - EitherOutput::First(a) => Sink::poll_ready(a, cx).map_err(EitherError::A), - EitherOutput::Second(b) => Sink::poll_ready(b, cx).map_err(EitherError::B), + EitherOutputProj::First(a) => Sink::poll_ready(a, cx).map_err(EitherError::A), + EitherOutputProj::Second(b) => Sink::poll_ready(b, cx).map_err(EitherError::B), } } - #[project] fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error> { - #[project] match self.project() { - EitherOutput::First(a) => Sink::start_send(a, item).map_err(EitherError::A), - EitherOutput::Second(b) => Sink::start_send(b, item).map_err(EitherError::B), + EitherOutputProj::First(a) => Sink::start_send(a, item).map_err(EitherError::A), + EitherOutputProj::Second(b) => Sink::start_send(b, item).map_err(EitherError::B), } } - #[project] fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - #[project] match self.project() { - EitherOutput::First(a) => Sink::poll_flush(a, cx).map_err(EitherError::A), - EitherOutput::Second(b) => Sink::poll_flush(b, cx).map_err(EitherError::B), + EitherOutputProj::First(a) => Sink::poll_flush(a, cx).map_err(EitherError::A), + EitherOutputProj::Second(b) => Sink::poll_flush(b, cx).map_err(EitherError::B), } } - #[project] fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - #[project] match self.project() { - EitherOutput::First(a) => Sink::poll_close(a, cx).map_err(EitherError::A), - EitherOutput::Second(b) => Sink::poll_close(b, cx).map_err(EitherError::B), + EitherOutputProj::First(a) => Sink::poll_close(a, cx).map_err(EitherError::A), + EitherOutputProj::Second(b) => Sink::poll_close(b, cx).map_err(EitherError::B), } } } @@ -349,7 +327,7 @@ pub enum EitherOutbound { } /// Implements `Stream` and dispatches all method calls to either `First` or `Second`. -#[pin_project] +#[pin_project(project = EitherListenStreamProj)] #[derive(Debug, Copy, Clone)] #[must_use = "futures do nothing unless polled"] pub enum EitherListenStream { @@ -364,17 +342,15 @@ where { type Item = Result, EitherError>, EitherError>; - #[project] fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - #[project] match self.project() { - EitherListenStream::First(a) => match TryStream::try_poll_next(a, cx) { + EitherListenStreamProj::First(a) => match TryStream::try_poll_next(a, cx) { Poll::Pending => Poll::Pending, Poll::Ready(None) => Poll::Ready(None), Poll::Ready(Some(Ok(le))) => Poll::Ready(Some(Ok(le.map(EitherFuture::First).map_err(EitherError::A)))), Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(EitherError::A(err)))), }, - EitherListenStream::Second(a) => match TryStream::try_poll_next(a, cx) { + EitherListenStreamProj::Second(a) => match TryStream::try_poll_next(a, cx) { Poll::Pending => Poll::Pending, Poll::Ready(None) => Poll::Ready(None), Poll::Ready(Some(Ok(le))) => Poll::Ready(Some(Ok(le.map(EitherFuture::Second).map_err(EitherError::B)))), @@ -385,7 +361,7 @@ where } /// Implements `Future` and dispatches all method calls to either `First` or `Second`. -#[pin_project] +#[pin_project(project = EitherFutureProj)] #[derive(Debug, Copy, Clone)] #[must_use = "futures do nothing unless polled"] pub enum EitherFuture { @@ -400,19 +376,17 @@ where { type Output = Result, EitherError>; - #[project] fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { - #[project] match self.project() { - EitherFuture::First(a) => TryFuture::try_poll(a, cx) + EitherFutureProj::First(a) => TryFuture::try_poll(a, cx) .map_ok(EitherOutput::First).map_err(EitherError::A), - EitherFuture::Second(a) => TryFuture::try_poll(a, cx) + EitherFutureProj::Second(a) => TryFuture::try_poll(a, cx) .map_ok(EitherOutput::Second).map_err(EitherError::B), } } } -#[pin_project] +#[pin_project(project = EitherFuture2Proj)] #[derive(Debug, Copy, Clone)] #[must_use = "futures do nothing unless polled"] pub enum EitherFuture2 { A(#[pin] A), B(#[pin] B) } @@ -424,13 +398,11 @@ where { type Output = Result, EitherError>; - #[project] fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { - #[project] match self.project() { - EitherFuture2::A(a) => TryFuture::try_poll(a, cx) + EitherFuture2Proj::A(a) => TryFuture::try_poll(a, cx) .map_ok(EitherOutput::First).map_err(EitherError::A), - EitherFuture2::B(a) => TryFuture::try_poll(a, cx) + EitherFuture2Proj::B(a) => TryFuture::try_poll(a, cx) .map_ok(EitherOutput::Second).map_err(EitherError::B), } } diff --git a/misc/multistream-select/Cargo.toml b/misc/multistream-select/Cargo.toml index f339bc9254e..36828c8b607 100644 --- a/misc/multistream-select/Cargo.toml +++ b/misc/multistream-select/Cargo.toml @@ -13,7 +13,7 @@ edition = "2018" bytes = "0.5" futures = "0.3" log = "0.4" -pin-project = "0.4.8" +pin-project = "0.4.17" smallvec = "1.0" unsigned-varint = "0.3.2" diff --git a/misc/multistream-select/src/negotiated.rs b/misc/multistream-select/src/negotiated.rs index f5b368c63fb..f309ba9e9d6 100644 --- a/misc/multistream-select/src/negotiated.rs +++ b/misc/multistream-select/src/negotiated.rs @@ -22,7 +22,7 @@ use crate::protocol::{Protocol, MessageReader, Message, Version, ProtocolError}; use bytes::{BytesMut, Buf}; use futures::{prelude::*, io::{IoSlice, IoSliceMut}, ready}; -use pin_project::{pin_project, project}; +use pin_project::pin_project; use std::{error::Error, fmt, io, mem, pin::Pin, task::{Context, Poll}}; /// An I/O stream that has settled on an (application-layer) protocol to use. @@ -87,7 +87,6 @@ impl Negotiated { } /// Polls the `Negotiated` for completion. - #[project] fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> where TInner: AsyncRead + AsyncWrite + Unpin @@ -107,9 +106,8 @@ impl Negotiated { let mut this = self.project(); - #[project] match this.state.as_mut().project() { - State::Completed { remaining, .. } => { + StateProj::Completed { remaining, .. } => { debug_assert!(remaining.is_empty()); return Poll::Ready(Ok(())) } @@ -163,7 +161,7 @@ impl Negotiated { } /// The states of a `Negotiated` I/O stream. -#[pin_project] +#[pin_project(project = StateProj)] #[derive(Debug)] enum State { /// In this state, a `Negotiated` is still expecting to @@ -193,14 +191,12 @@ impl AsyncRead for Negotiated where TInner: AsyncRead + AsyncWrite + Unpin { - #[project] fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll> { loop { - #[project] match self.as_mut().project().state.project() { - State::Completed { io, remaining } => { + StateProj::Completed { io, remaining } => { // If protocol negotiation is complete and there is no // remaining data to be flushed, commence with reading. if remaining.is_empty() { @@ -229,14 +225,12 @@ where } }*/ - #[project] fn poll_read_vectored(mut self: Pin<&mut Self>, cx: &mut Context, bufs: &mut [IoSliceMut]) -> Poll> { loop { - #[project] match self.as_mut().project().state.project() { - State::Completed { io, remaining } => { + StateProj::Completed { io, remaining } => { // If protocol negotiation is complete and there is no // remaining data to be flushed, commence with reading. if remaining.is_empty() { @@ -261,11 +255,9 @@ impl AsyncWrite for Negotiated where TInner: AsyncWrite + AsyncRead + Unpin { - #[project] fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll> { - #[project] match self.project().state.project() { - State::Completed { mut io, remaining } => { + StateProj::Completed { mut io, remaining } => { while !remaining.is_empty() { let n = ready!(io.as_mut().poll_write(cx, &remaining)?); if n == 0 { @@ -275,16 +267,14 @@ where } io.poll_write(cx, buf) }, - State::Expecting { io, .. } => io.poll_write(cx, buf), - State::Invalid => panic!("Negotiated: Invalid state"), + StateProj::Expecting { io, .. } => io.poll_write(cx, buf), + StateProj::Invalid => panic!("Negotiated: Invalid state"), } } - #[project] fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - #[project] match self.project().state.project() { - State::Completed { mut io, remaining } => { + StateProj::Completed { mut io, remaining } => { while !remaining.is_empty() { let n = ready!(io.as_mut().poll_write(cx, &remaining)?); if n == 0 { @@ -294,12 +284,11 @@ where } io.poll_flush(cx) }, - State::Expecting { io, .. } => io.poll_flush(cx), - State::Invalid => panic!("Negotiated: Invalid state"), + StateProj::Expecting { io, .. } => io.poll_flush(cx), + StateProj::Invalid => panic!("Negotiated: Invalid state"), } } - #[project] fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { // Ensure all data has been flushed and expected negotiation messages // have been received. @@ -307,21 +296,18 @@ where ready!(self.as_mut().poll_flush(cx).map_err(Into::::into)?); // Continue with the shutdown of the underlying I/O stream. - #[project] match self.project().state.project() { - State::Completed { io, .. } => io.poll_close(cx), - State::Expecting { io, .. } => io.poll_close(cx), - State::Invalid => panic!("Negotiated: Invalid state"), + StateProj::Completed { io, .. } => io.poll_close(cx), + StateProj::Expecting { io, .. } => io.poll_close(cx), + StateProj::Invalid => panic!("Negotiated: Invalid state"), } } - #[project] fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context, bufs: &[IoSlice]) -> Poll> { - #[project] match self.project().state.project() { - State::Completed { mut io, remaining } => { + StateProj::Completed { mut io, remaining } => { while !remaining.is_empty() { let n = ready!(io.as_mut().poll_write(cx, &remaining)?); if n == 0 { @@ -331,8 +317,8 @@ where } io.poll_write_vectored(cx, bufs) }, - State::Expecting { io, .. } => io.poll_write_vectored(cx, bufs), - State::Invalid => panic!("Negotiated: Invalid state"), + StateProj::Expecting { io, .. } => io.poll_write_vectored(cx, bufs), + StateProj::Invalid => panic!("Negotiated: Invalid state"), } } } @@ -460,4 +446,3 @@ mod tests { quickcheck(prop as fn(_,_,_,_) -> _) } } - diff --git a/protocols/pnet/Cargo.toml b/protocols/pnet/Cargo.toml index de5ea5cfd14..a838f03f20e 100644 --- a/protocols/pnet/Cargo.toml +++ b/protocols/pnet/Cargo.toml @@ -15,7 +15,7 @@ log = "0.4.8" salsa20 = "0.3.0" sha3 = "0.8" rand = "0.7" -pin-project = "0.4.6" +pin-project = "0.4.17" [dev-dependencies] quickcheck = "0.9.0" diff --git a/protocols/secio/Cargo.toml b/protocols/secio/Cargo.toml index 88ddf26b58d..7a63fa88c70 100644 --- a/protocols/secio/Cargo.toml +++ b/protocols/secio/Cargo.toml @@ -19,7 +19,7 @@ lazy_static = "1.2.0" libp2p-core = { version = "0.19.0", path = "../../core" } log = "0.4.6" prost = "0.6.1" -pin-project = "0.4.6" +pin-project = "0.4.17" quicksink = "0.1" rand = "0.7" rw-stream-sink = "0.2.0"