Skip to content

Commit bcb61a7

Browse files
taiki-ecramertj
authored andcommitted
Use (try_)poll_(next_)unpin more
1 parent 428ce77 commit bcb61a7

15 files changed

+32
-29
lines changed

futures-util/src/async_await/poll.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::future::FutureExt;
12
use core::pin::Pin;
23
use futures_core::future::Future;
34
use futures_core::task::{Context, Poll};
@@ -29,6 +30,6 @@ pub struct PollOnce<F: Future + Unpin> {
2930
impl<F: Future + Unpin> Future for PollOnce<F> {
3031
type Output = Poll<F::Output>;
3132
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
32-
Poll::Ready(Pin::new(&mut self.future).poll(cx))
33+
Poll::Ready(self.future.poll_unpin(cx))
3334
}
3435
}

futures-util/src/future/select.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::pin::Pin;
22
use futures_core::future::Future;
33
use futures_core::task::{Context, Poll};
4-
use crate::future::Either;
4+
use crate::future::{Either, FutureExt};
55

66
/// Future for the [`select()`] function.
77
#[must_use = "futures do nothing unless you `.await` or poll them"]
@@ -55,9 +55,9 @@ impl<A: Unpin, B: Unpin> Future for Select<A, B> where A: Future, B: Future {
5555

5656
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
5757
let (mut a, mut b) = self.inner.take().expect("cannot poll Select twice");
58-
match Pin::new(&mut a).poll(cx) {
58+
match a.poll_unpin(cx) {
5959
Poll::Ready(x) => Poll::Ready(Either::Left((x, b))),
60-
Poll::Pending => match Pin::new(&mut b).poll(cx) {
60+
Poll::Pending => match b.poll_unpin(cx) {
6161
Poll::Ready(x) => Poll::Ready(Either::Right((x, a))),
6262
Poll::Pending => {
6363
self.inner = Some((a, b));

futures-util/src/future/select_all.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::future::FutureExt;
12
use core::iter::FromIterator;
23
use core::mem;
34
use core::pin::Pin;
@@ -42,7 +43,7 @@ impl<Fut: Future + Unpin> Future for SelectAll<Fut> {
4243

4344
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
4445
let item = self.inner.iter_mut().enumerate().find_map(|(i, f)| {
45-
match Pin::new(f).poll(cx) {
46+
match f.poll_unpin(cx) {
4647
Poll::Pending => None,
4748
Poll::Ready(e) => Some((i, e)),
4849
}

futures-util/src/stream/buffer_unordered.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::stream::{Fuse, FuturesUnordered};
1+
use crate::stream::{Fuse, FuturesUnordered, StreamExt};
22
use futures_core::future::Future;
33
use futures_core::stream::{Stream, FusedStream};
44
use futures_core::task::{Context, Poll};
@@ -114,7 +114,7 @@ where
114114
}
115115

116116
// Attempt to pull the next value from the in_progress_queue
117-
match Pin::new(self.as_mut().in_progress_queue()).poll_next(cx) {
117+
match self.as_mut().in_progress_queue().poll_next_unpin(cx) {
118118
x @ Poll::Pending | x @ Poll::Ready(Some(_)) => return x,
119119
Poll::Ready(None) => {}
120120
}

futures-util/src/stream/buffered.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::stream::{Fuse, FuturesOrdered};
1+
use crate::stream::{Fuse, FuturesOrdered, StreamExt};
22
use futures_core::future::Future;
33
use futures_core::stream::Stream;
44
use futures_core::task::{Context, Poll};
@@ -109,7 +109,7 @@ where
109109
}
110110

111111
// Attempt to pull the next value from the in_progress_queue
112-
let res = Pin::new(self.as_mut().in_progress_queue()).poll_next(cx);
112+
let res = self.as_mut().in_progress_queue().poll_next_unpin(cx);
113113
if let Some(val) = ready!(res) {
114114
return Poll::Ready(Some(val))
115115
}

futures-util/src/stream/into_future.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::stream::StreamExt;
12
use core::pin::Pin;
23
use futures_core::future::{FusedFuture, Future};
34
use futures_core::stream::Stream;
@@ -85,7 +86,7 @@ impl<St: Stream + Unpin> Future for StreamFuture<St> {
8586
) -> Poll<Self::Output> {
8687
let item = {
8788
let s = self.stream.as_mut().expect("polling StreamFuture twice");
88-
ready!(Pin::new(s).poll_next(cx))
89+
ready!(s.poll_next_unpin(cx))
8990
};
9091
let stream = self.stream.take().unwrap();
9192
Poll::Ready((item, stream))

futures-util/src/try_future/select_ok.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::try_future::TryFutureExt;
12
use core::iter::FromIterator;
23
use core::mem;
34
use core::pin::Pin;
@@ -45,7 +46,7 @@ impl<Fut: TryFuture + Unpin> Future for SelectOk<Fut> {
4546
// loop until we've either exhausted all errors, a success was hit, or nothing is ready
4647
loop {
4748
let item = self.inner.iter_mut().enumerate().find_map(|(i, f)| {
48-
match Pin::new(f).try_poll(cx) {
49+
match f.try_poll_unpin(cx) {
4950
Poll::Pending => None,
5051
Poll::Ready(e) => Some((i, e)),
5152
}

futures-util/src/try_future/try_select.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::pin::Pin;
22
use futures_core::future::{Future, TryFuture};
33
use futures_core::task::{Context, Poll};
44
use crate::future::Either;
5+
use crate::try_future::TryFutureExt;
56

67
/// Future for the [`try_select()`] function.
78
#[must_use = "futures do nothing unless you `.await` or poll them"]
@@ -64,10 +65,10 @@ impl<A: Unpin, B: Unpin> Future for TrySelect<A, B>
6465

6566
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
6667
let (mut a, mut b) = self.inner.take().expect("cannot poll Select twice");
67-
match Pin::new(&mut a).try_poll(cx) {
68+
match a.try_poll_unpin(cx) {
6869
Poll::Ready(Err(x)) => Poll::Ready(Err(Either::Left((x, b)))),
6970
Poll::Ready(Ok(x)) => Poll::Ready(Ok(Either::Left((x, b)))),
70-
Poll::Pending => match Pin::new(&mut b).try_poll(cx) {
71+
Poll::Pending => match b.try_poll_unpin(cx) {
7172
Poll::Ready(Err(x)) => Poll::Ready(Err(Either::Right((x, a)))),
7273
Poll::Ready(Ok(x)) => Poll::Ready(Ok(Either::Right((x, a)))),
7374
Poll::Pending => {

futures-util/src/try_stream/into_async_read.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::try_stream::TryStreamExt;
12
use core::pin::Pin;
23
use futures_core::stream::TryStream;
34
use futures_core::task::{Context, Poll};
@@ -72,7 +73,7 @@ where
7273
return Poll::Ready(Ok(len));
7374
}
7475
ReadState::PendingChunk => {
75-
match ready!(Pin::new(&mut self.stream).try_poll_next(cx)) {
76+
match ready!(self.stream.try_poll_next_unpin(cx)) {
7677
Some(Ok(chunk)) => {
7778
if !chunk.as_ref().is_empty() {
7879
self.state = ReadState::Ready {
@@ -109,7 +110,7 @@ where
109110
cx: &mut Context<'_>,
110111
) -> Poll<Result<&'a [u8]>> {
111112
while let ReadState::PendingChunk = self.state {
112-
match ready!(Pin::new(&mut self.stream).try_poll_next(cx)) {
113+
match ready!(self.stream.try_poll_next_unpin(cx)) {
113114
Some(Ok(chunk)) => {
114115
if !chunk.as_ref().is_empty() {
115116
self.state = ReadState::Ready {

futures-util/src/try_stream/try_buffer_unordered.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<St> Stream for TryBufferUnordered<St>
9292
}
9393

9494
// Attempt to pull the next value from the in_progress_queue
95-
match Pin::new(self.as_mut().in_progress_queue()).poll_next(cx) {
95+
match self.as_mut().in_progress_queue().poll_next_unpin(cx) {
9696
x @ Poll::Pending | x @ Poll::Ready(Some(_)) => return x,
9797
Poll::Ready(None) => {}
9898
}

futures/tests/io_buf_reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use futures::executor::block_on;
2-
use futures::future::Future;
2+
use futures::future::{Future, FutureExt};
33
use futures::io::{
44
AsyncSeek, AsyncSeekExt, AsyncBufRead, AsyncBufReadExt, AsyncRead, AsyncReadExt,
55
AllowStdIo, BufReader, SeekFrom,
@@ -259,7 +259,7 @@ impl AsyncBufRead for MaybePending<'_> {
259259
fn run<F: Future + Unpin>(mut f: F) -> F::Output {
260260
let mut cx = noop_context();
261261
loop {
262-
if let Poll::Ready(x) = Pin::new(&mut f).poll(&mut cx) {
262+
if let Poll::Ready(x) = f.poll_unpin(&mut cx) {
263263
return x;
264264
}
265265
}

futures/tests/io_buf_writer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use futures::executor::block_on;
2-
use futures::future::Future;
2+
use futures::future::{Future, FutureExt};
33
use futures::io::{AsyncSeek, AsyncSeekExt, AsyncWrite, AsyncWriteExt, BufWriter, SeekFrom};
44
use futures::task::{Context, Poll};
55
use futures_test::task::noop_context;
@@ -111,7 +111,7 @@ impl AsyncWrite for MaybePending {
111111
fn run<F: Future + Unpin>(mut f: F) -> F::Output {
112112
let mut cx = noop_context();
113113
loop {
114-
if let Poll::Ready(x) = Pin::new(&mut f).poll(&mut cx) {
114+
if let Poll::Ready(x) = f.poll_unpin(&mut cx) {
115115
return x;
116116
}
117117
}

futures/tests/io_lines.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use futures::executor::block_on;
2-
use futures::future::Future;
2+
use futures::future::{Future, FutureExt};
33
use futures::stream::{self, StreamExt, TryStreamExt};
44
use futures::io::AsyncBufReadExt;
55
use futures::task::Poll;
66
use futures_test::io::AsyncReadTestExt;
77
use futures_test::task::noop_context;
88
use std::io::Cursor;
9-
use std::pin::Pin;
109

1110
macro_rules! block_on_next {
1211
($expr:expr) => {
@@ -31,7 +30,7 @@ fn lines() {
3130
fn run<F: Future + Unpin>(mut f: F) -> F::Output {
3231
let mut cx = noop_context();
3332
loop {
34-
if let Poll::Ready(x) = Pin::new(&mut f).poll(&mut cx) {
33+
if let Poll::Ready(x) = f.poll_unpin(&mut cx) {
3534
return x;
3635
}
3736
}

futures/tests/io_read_line.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use futures::executor::block_on;
2-
use futures::future::Future;
2+
use futures::future::{Future, FutureExt};
33
use futures::stream::{self, StreamExt, TryStreamExt};
44
use futures::io::AsyncBufReadExt;
55
use futures::task::Poll;
66
use futures_test::io::AsyncReadTestExt;
77
use futures_test::task::noop_context;
88
use std::io::Cursor;
9-
use std::pin::Pin;
109

1110
#[test]
1211
fn read_line() {
@@ -30,7 +29,7 @@ fn read_line() {
3029
fn run<F: Future + Unpin>(mut f: F) -> F::Output {
3130
let mut cx = noop_context();
3231
loop {
33-
if let Poll::Ready(x) = Pin::new(&mut f).poll(&mut cx) {
32+
if let Poll::Ready(x) = f.poll_unpin(&mut cx) {
3433
return x;
3534
}
3635
}

futures/tests/io_read_until.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use futures::executor::block_on;
2-
use futures::future::Future;
2+
use futures::future::{Future, FutureExt};
33
use futures::stream::{self, StreamExt, TryStreamExt};
44
use futures::io::AsyncBufReadExt;
55
use futures::task::Poll;
66
use futures_test::io::AsyncReadTestExt;
77
use futures_test::task::noop_context;
88
use std::io::Cursor;
9-
use std::pin::Pin;
109

1110
#[test]
1211
fn read_until() {
@@ -30,7 +29,7 @@ fn read_until() {
3029
fn run<F: Future + Unpin>(mut f: F) -> F::Output {
3130
let mut cx = noop_context();
3231
loop {
33-
if let Poll::Ready(x) = Pin::new(&mut f).poll(&mut cx) {
32+
if let Poll::Ready(x) = f.poll_unpin(&mut cx) {
3433
return x;
3534
}
3635
}

0 commit comments

Comments
 (0)