Skip to content

Commit

Permalink
Remove uses of pin_project::project attribute
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 committed Jun 5, 2020
1 parent 8f7087d commit c9be6d3
Show file tree
Hide file tree
Showing 65 changed files with 532 additions and 737 deletions.
2 changes: 1 addition & 1 deletion futures-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ futures-util = { version = "0.3.5", path = "../futures-util", default-features =
futures-executor = { version = "0.3.5", path = "../futures-executor", default-features = false }
pin-utils = { version = "0.1.0", default-features = false }
once_cell = { version = "1.3.1", default-features = false, features = ["std"], optional = true }
pin-project = "0.4.15"
pin-project = "0.4.17"

[dev-dependencies]
futures = { version = "0.3.5", path = "../futures", default-features = false, features = ["std", "executor"] }
Expand Down
2 changes: 1 addition & 1 deletion futures-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ memchr = { version = "2.2", optional = true }
futures_01 = { version = "0.1.25", optional = true, package = "futures" }
tokio-io = { version = "0.1.9", optional = true }
pin-utils = "0.1.0"
pin-project = "0.4.15"
pin-project = "0.4.17"

[dev-dependencies]
futures = { path = "../futures", version = "0.3.5", features = ["async-await", "thread-pool"] }
Expand Down
98 changes: 34 additions & 64 deletions futures-util/src/future/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use futures_core::future::{FusedFuture, Future};
use futures_core::stream::{FusedStream, Stream};
#[cfg(feature = "sink")]
use futures_sink::Sink;
use pin_project::{pin_project, project};
use pin_project::pin_project;

/// Combines two different futures, streams, or sinks having the same associated types into a single
/// type.
#[pin_project]
#[pin_project(project = EitherProj)]
#[derive(Debug, Clone)]
pub enum Either<A, B> {
/// First branch of the type
Expand Down Expand Up @@ -58,12 +58,10 @@ where
{
type Output = A::Output;

#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<A::Output> {
#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project() {
Either::Left(x) => x.poll(cx),
Either::Right(x) => x.poll(cx),
EitherProj::Left(x) => x.poll(cx),
EitherProj::Right(x) => x.poll(cx),
}
}
}
Expand All @@ -88,12 +86,10 @@ where
{
type Item = A::Item;

#[project]
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<A::Item>> {
#[project]
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.project() {
Either::Left(x) => x.poll_next(cx),
Either::Right(x) => x.poll_next(cx),
EitherProj::Left(x) => x.poll_next(cx),
EitherProj::Right(x) => x.poll_next(cx),
}
}
}
Expand All @@ -119,39 +115,31 @@ where
{
type Error = A::Error;

#[project]
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_ready(cx),
Either::Right(x) => x.poll_ready(cx),
EitherProj::Left(x) => x.poll_ready(cx),
EitherProj::Right(x) => x.poll_ready(cx),
}
}

#[project]
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> {
#[project]
match self.project() {
Either::Left(x) => x.start_send(item),
Either::Right(x) => x.start_send(item),
EitherProj::Left(x) => x.start_send(item),
EitherProj::Right(x) => x.start_send(item),
}
}

#[project]
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_flush(cx),
Either::Right(x) => x.poll_flush(cx),
EitherProj::Left(x) => x.poll_flush(cx),
EitherProj::Right(x) => x.poll_flush(cx),
}
}

#[project]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_close(cx),
Either::Right(x) => x.poll_close(cx),
EitherProj::Left(x) => x.poll_close(cx),
EitherProj::Right(x) => x.poll_close(cx),
}
}
}
Expand Down Expand Up @@ -182,29 +170,25 @@ mod if_std {
}
}

#[project]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_read(cx, buf),
Either::Right(x) => x.poll_read(cx, buf),
EitherProj::Left(x) => x.poll_read(cx, buf),
EitherProj::Right(x) => x.poll_read(cx, buf),
}
}

#[project]
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>],
) -> Poll<Result<usize>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_read_vectored(cx, bufs),
Either::Right(x) => x.poll_read_vectored(cx, bufs),
EitherProj::Left(x) => x.poll_read_vectored(cx, bufs),
EitherProj::Right(x) => x.poll_read_vectored(cx, bufs),
}
}
}
Expand All @@ -214,47 +198,39 @@ mod if_std {
A: AsyncWrite,
B: AsyncWrite,
{
#[project]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_write(cx, buf),
Either::Right(x) => x.poll_write(cx, buf),
EitherProj::Left(x) => x.poll_write(cx, buf),
EitherProj::Right(x) => x.poll_write(cx, buf),
}
}

#[project]
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<Result<usize>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_write_vectored(cx, bufs),
Either::Right(x) => x.poll_write_vectored(cx, bufs),
EitherProj::Left(x) => x.poll_write_vectored(cx, bufs),
EitherProj::Right(x) => x.poll_write_vectored(cx, bufs),
}
}

#[project]
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_flush(cx),
Either::Right(x) => x.poll_flush(cx),
EitherProj::Left(x) => x.poll_flush(cx),
EitherProj::Right(x) => x.poll_flush(cx),
}
}

#[project]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_close(cx),
Either::Right(x) => x.poll_close(cx),
EitherProj::Left(x) => x.poll_close(cx),
EitherProj::Right(x) => x.poll_close(cx),
}
}
}
Expand All @@ -264,16 +240,14 @@ mod if_std {
A: AsyncSeek,
B: AsyncSeek,
{
#[project]
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
pos: SeekFrom,
) -> Poll<Result<u64>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_seek(cx, pos),
Either::Right(x) => x.poll_seek(cx, pos),
EitherProj::Left(x) => x.poll_seek(cx, pos),
EitherProj::Right(x) => x.poll_seek(cx, pos),
}
}
}
Expand All @@ -283,21 +257,17 @@ mod if_std {
A: AsyncBufRead,
B: AsyncBufRead,
{
#[project]
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
#[project]
match self.project() {
Either::Left(x) => x.poll_fill_buf(cx),
Either::Right(x) => x.poll_fill_buf(cx),
EitherProj::Left(x) => x.poll_fill_buf(cx),
EitherProj::Right(x) => x.poll_fill_buf(cx),
}
}

#[project]
fn consume(self: Pin<&mut Self>, amt: usize) {
#[project]
match self.project() {
Either::Left(x) => x.consume(amt),
Either::Right(x) => x.consume(amt),
EitherProj::Left(x) => x.consume(amt),
EitherProj::Right(x) => x.consume(amt),
}
}
}
Expand Down
48 changes: 18 additions & 30 deletions futures-util/src/future/future/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use futures_core::stream::{FusedStream, Stream};
use futures_core::task::{Context, Poll};
#[cfg(feature = "sink")]
use futures_sink::Sink;
use pin_project::{pin_project, project};
use pin_project::pin_project;

#[pin_project]
#[pin_project(project = FlattenProj)]
#[derive(Debug)]
pub enum Flatten<Fut1, Fut2> {
First(#[pin] Fut1),
Expand Down Expand Up @@ -40,21 +40,19 @@ where
{
type Output = <Fut::Output as Future>::Output;

#[project]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(loop {
#[project]
match self.as_mut().project() {
Flatten::First(f) => {
FlattenProj::First(f) => {
let f = ready!(f.poll(cx));
self.set(Flatten::Second(f));
}
Flatten::Second(f) => {
FlattenProj::Second(f) => {
let output = ready!(f.poll(cx));
self.set(Flatten::Empty);
break output;
}
Flatten::Empty => panic!("Flatten polled after completion"),
FlattenProj::Empty => panic!("Flatten polled after completion"),
}
})
}
Expand All @@ -80,23 +78,21 @@ where
{
type Item = <Fut::Output as Stream>::Item;

#[project]
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(loop {
#[project]
match self.as_mut().project() {
Flatten::First(f) => {
FlattenProj::First(f) => {
let f = ready!(f.poll(cx));
self.set(Flatten::Second(f));
}
Flatten::Second(f) => {
FlattenProj::Second(f) => {
let output = ready!(f.poll_next(cx));
if output.is_none() {
self.set(Flatten::Empty);
}
break output;
}
Flatten::Empty => break None,
FlattenProj::Empty => break None,
}
})
}
Expand All @@ -110,48 +106,40 @@ where
{
type Error = <Fut::Output as Sink<Item>>::Error;

#[project]
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(loop {
#[project]
match self.as_mut().project() {
Flatten::First(f) => {
FlattenProj::First(f) => {
let f = ready!(f.poll(cx));
self.set(Flatten::Second(f));
}
Flatten::Second(f) => {
FlattenProj::Second(f) => {
break ready!(f.poll_ready(cx));
}
Flatten::Empty => panic!("poll_ready called after eof"),
FlattenProj::Empty => panic!("poll_ready called after eof"),
}
})
}

#[project]
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> {
#[project]
match self.project() {
Flatten::First(_) => panic!("poll_ready not called first"),
Flatten::Second(f) => f.start_send(item),
Flatten::Empty => panic!("start_send called after eof"),
FlattenProj::First(_) => panic!("poll_ready not called first"),
FlattenProj::Second(f) => f.start_send(item),
FlattenProj::Empty => panic!("start_send called after eof"),
}
}

#[project]
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
#[project]
match self.project() {
Flatten::First(_) => Poll::Ready(Ok(())),
Flatten::Second(f) => f.poll_flush(cx),
Flatten::Empty => panic!("poll_flush called after eof"),
FlattenProj::First(_) => Poll::Ready(Ok(())),
FlattenProj::Second(f) => f.poll_flush(cx),
FlattenProj::Empty => panic!("poll_flush called after eof"),
}
}

#[project]
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
#[project]
let res = match self.as_mut().project() {
Flatten::Second(f) => f.poll_close(cx),
FlattenProj::Second(f) => f.poll_close(cx),
_ => Poll::Ready(Ok(())),
};
if res.is_ready() {
Expand Down
Loading

0 comments on commit c9be6d3

Please sign in to comment.