Skip to content

Commit

Permalink
Remove uses of pin_project::project attribute (#1604)
Browse files Browse the repository at this point in the history
pin-project will deprecate the project attribute due to some unfixable
limitations.

Refs: taiki-e/pin-project#225
  • Loading branch information
taiki-e authored Jun 9, 2020
1 parent cacd730 commit 9aaf042
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 99 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -115,4 +115,3 @@ members = [
"transports/websocket",
"transports/wasm-ext"
]

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
94 changes: 33 additions & 61 deletions core/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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),
Expand All @@ -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),
}
}
}
Expand All @@ -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),
}
}
}
Expand All @@ -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))),
}
}
Expand All @@ -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),
}
}
}
Expand Down Expand Up @@ -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> {
Expand All @@ -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)))),
Expand All @@ -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> {
Expand All @@ -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) }
Expand All @@ -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),
}
}
Expand Down
2 changes: 1 addition & 1 deletion misc/multistream-select/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Loading

0 comments on commit 9aaf042

Please sign in to comment.