Skip to content

Remove uses of pin_project::project attribute #2176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.20"

[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.20"

[dev-dependencies]
futures = { path = "../futures", version = "0.3.5", features = ["async-await", "thread-pool"] }
Expand Down
103 changes: 35 additions & 68 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,24 +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]
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
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};
#[cfg(feature = "sink")]
use futures_sink::Sink;
use futures_core::task::{Context, Poll};
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 @@ -38,21 +38,19 @@ impl<Fut> Future for Flatten<Fut, Fut::Output>
{
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 @@ -76,23 +74,21 @@ impl<Fut> Stream for Flatten<Fut, Fut::Output>
{
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 @@ -107,54 +103,46 @@ 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