Remove uses of pin_project::project attribute (#1604)

pin-project will deprecate the project attribute due to some unfixable
limitations.

Refs: https://github.com/taiki-e/pin-project/issues/225
This commit is contained in:
Taiki Endo
2020-06-10 01:01:57 +09:00
committed by GitHub
parent cacd7300de
commit 9aaf042410
7 changed files with 55 additions and 99 deletions

View File

@ -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"
]

View File

@ -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"

View File

@ -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<A, B> {
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<Result<usize, IoError>> {
#[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<Result<usize, IoError>>
{
#[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<Result<usize, IoError>> {
#[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<Result<usize, IoError>>
{
#[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<Result<(), IoError>> {
#[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<Result<(), IoError>> {
#[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<I, EitherError<A::Error, B::Error>>;
#[project]
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
#[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<A::Error, B::Error>;
#[project]
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
#[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<Result<(), Self::Error>> {
#[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<Result<(), Self::Error>> {
#[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<A: StreamMuxer, B: StreamMuxer> {
}
/// 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<A, B> {
@ -364,17 +342,15 @@ where
{
type Item = Result<ListenerEvent<EitherFuture<AInner, BInner>, EitherError<AError, BError>>, EitherError<AError, BError>>;
#[project]
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
#[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<A, B> {
@ -400,19 +376,17 @@ where
{
type Output = Result<EitherOutput<AInner, BInner>, EitherError<AFuture::Error, BFuture::Error>>;
#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
#[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, B> { A(#[pin] A), B(#[pin] B) }
@ -424,13 +398,11 @@ where
{
type Output = Result<EitherOutput<AItem, BItem>, EitherError<AError, BError>>;
#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
#[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),
}
}

View File

@ -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"

View File

@ -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<TInner> Negotiated<TInner> {
}
/// Polls the `Negotiated` for completion.
#[project]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), NegotiationError>>
where
TInner: AsyncRead + AsyncWrite + Unpin
@ -107,9 +106,8 @@ impl<TInner> Negotiated<TInner> {
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<TInner> Negotiated<TInner> {
}
/// The states of a `Negotiated` I/O stream.
#[pin_project]
#[pin_project(project = StateProj)]
#[derive(Debug)]
enum State<R> {
/// In this state, a `Negotiated` is still expecting to
@ -193,14 +191,12 @@ impl<TInner> AsyncRead for Negotiated<TInner>
where
TInner: AsyncRead + AsyncWrite + Unpin
{
#[project]
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8])
-> Poll<Result<usize, io::Error>>
{
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<Result<usize, io::Error>>
{
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<TInner> AsyncWrite for Negotiated<TInner>
where
TInner: AsyncWrite + AsyncRead + Unpin
{
#[project]
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<Result<usize, io::Error>> {
#[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<Result<(), io::Error>> {
#[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<Result<(), io::Error>> {
// 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::<io::Error>::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<Result<usize, io::Error>>
{
#[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(_,_,_,_) -> _)
}
}

View File

@ -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"

View File

@ -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"