Skip to content

Commit f57acc7

Browse files
aturoncramertj
authored andcommitted
Update with review comments
1 parent 108fc90 commit f57acc7

38 files changed

+135
-76
lines changed

futures-core/src/poll.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
///
33
/// This macro bakes in propagation of `Pending` signals by returning early.
44
#[macro_export]
5-
macro_rules! try_ready {
5+
macro_rules! ready {
66
($e:expr) => (match $e {
77
$crate::Poll::Ready(t) => t,
88
$crate::Poll::Pending => return $crate::Poll::Pending,

futures-util/src/future/flatten_sink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<F> Sink for FlattenSink<F> where F: Future, <F as Future>::Item: Sink<SinkE
6666

6767
fn poll_close(&mut self, cx: &mut task::Context) -> Result<Async<()>, Self::SinkError> {
6868
if let State::Ready(ref mut s) = self.st {
69-
try_ready!(s.poll_close(cx));
69+
ready!(s.poll_close(cx));
7070
}
7171
self.st = State::Closed;
7272
return Ok(Async::Ready(()));

futures-util/src/io/lines.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<A> Stream for Lines<A>
4646
type Error = io::Error;
4747

4848
fn poll(&mut self, cx: &mut task::Context) -> Poll<Option<String>, io::Error> {
49-
let n = try_ready!(self.io.read_line(&mut self.line));
49+
let n = ready!(self.io.read_line(&mut self.line));
5050
if n == 0 && self.line.len() == 0 {
5151
return Ok(None.into())
5252
}

futures-util/src/io/read_until.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<A> Future for ReadUntil<A>
6262
// and just return it, as we are finished.
6363
// If we hit "would block" then all the read data so far
6464
// is in our buffer, and otherwise we propagate errors.
65-
try_ready!(a.read_until(byte, buf));
65+
ready!(a.read_until(byte, buf));
6666
},
6767
State::Empty => panic!("poll ReadUntil after it's done"),
6868
}

futures-util/src/sink/buffer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ impl<S: Sink> Buffer<S> {
4444
}
4545

4646
fn try_empty_buffer(&mut self, cx: &mut task::Context) -> Poll<(), S::SinkError> {
47-
try_ready!(self.sink.poll_ready(cx));
47+
ready!(self.sink.poll_ready(cx));
4848
while let Some(item) = self.buf.pop_front() {
4949
self.sink.start_send(item)?;
5050
if self.buf.len() != 0 {
51-
try_ready!(self.sink.poll_ready(cx));
51+
ready!(self.sink.poll_ready(cx));
5252
}
5353
}
5454
Ok(Async::Ready(()))
@@ -93,13 +93,13 @@ impl<S: Sink> Sink for Buffer<S> {
9393
}
9494

9595
fn poll_flush(&mut self, cx: &mut task::Context) -> Poll<(), Self::SinkError> {
96-
try_ready!(self.try_empty_buffer(cx));
96+
ready!(self.try_empty_buffer(cx));
9797
debug_assert!(self.buf.is_empty());
9898
self.sink.poll_flush(cx)
9999
}
100100

101101
fn poll_close(&mut self, cx: &mut task::Context) -> Poll<(), Self::SinkError> {
102-
try_ready!(self.try_empty_buffer(cx));
102+
ready!(self.try_empty_buffer(cx));
103103
debug_assert!(self.buf.is_empty());
104104
self.sink.poll_close(cx)
105105
}

futures-util/src/sink/send.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<S: Sink> Future for Send<S> {
6161

6262
// we're done sending the item, but want to block on flushing the
6363
// sink
64-
try_ready!(self.sink_mut().poll_flush(cx));
64+
ready!(self.sink_mut().poll_flush(cx));
6565

6666
// now everything's emptied, so return the sink for further use
6767
Ok(Async::Ready(self.take_sink()))

futures-util/src/sink/send_all.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ impl<T, U> Future for SendAll<T, U>
7575
// If we've got an item buffered already, we need to write it to the
7676
// sink before we can do anything else
7777
if let Some(item) = self.buffered.take() {
78-
try_ready!(self.try_start_send(cx, item))
78+
ready!(self.try_start_send(cx, item))
7979
}
8080

8181
loop {
8282
match self.stream_mut().poll_next(cx)? {
83-
Async::Ready(Some(item)) => try_ready!(self.try_start_send(cx, item)),
83+
Async::Ready(Some(item)) => ready!(self.try_start_send(cx, item)),
8484
Async::Ready(None) => {
85-
try_ready!(self.sink_mut().poll_flush(cx));
85+
ready!(self.sink_mut().poll_flush(cx));
8686
return Ok(Async::Ready(self.take_result()))
8787
}
8888
Async::Pending => {
89-
try_ready!(self.sink_mut().poll_flush(cx));
89+
ready!(self.sink_mut().poll_flush(cx));
9090
return Ok(Async::Pending)
9191
}
9292
}

futures-util/src/sink/with.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ impl<S, U, Fut, F> Sink for With<S, U, Fut, F>
143143
}
144144

145145
fn poll_flush(&mut self, cx: &mut task::Context) -> Poll<(), Self::SinkError> {
146-
try_ready!(self.poll(cx));
146+
ready!(self.poll(cx));
147147
self.sink.poll_flush(cx).map_err(Into::into)
148148
}
149149

150150
fn poll_close(&mut self, cx: &mut task::Context) -> Poll<(), Self::SinkError> {
151-
try_ready!(self.poll(cx));
151+
ready!(self.poll(cx));
152152
self.sink.poll_close(cx).map_err(Into::into)
153153
}
154154
}

futures-util/src/sink/with_flat_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ where
7171
}
7272
}
7373
if let Some(mut stream) = self.stream.take() {
74-
while let Some(x) = try_ready!(stream.poll_next(cx)) {
74+
while let Some(x) = ready!(stream.poll_next(cx)) {
7575
match self.sink.poll_ready(cx)? {
7676
Async::Ready(()) => self.sink.start_send(x)?,
7777
Async::Pending => {

futures-util/src/stream/buffer_unordered.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<S> Stream for BufferUnordered<S>
9696
}
9797

9898
// Try polling a new future
99-
if let Some(val) = try_ready!(self.queue.poll_next(cx)) {
99+
if let Some(val) = ready!(self.queue.poll_next(cx)) {
100100
return Ok(Async::Ready(Some(val)));
101101
}
102102

futures-util/src/stream/buffered.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<S> Stream for Buffered<S>
106106
}
107107

108108
// Try polling a new future
109-
if let Some(val) = try_ready!(self.queue.poll_next(cx)) {
109+
if let Some(val) = ready!(self.queue.poll_next(cx)) {
110110
return Ok(Async::Ready(Some(val)));
111111
}
112112

futures-util/src/stream/chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<S1, S2> Stream for Chain<S1, S2>
3838

3939
fn poll_next(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
4040
if let Some(first) = self.first().as_pin_mut() {
41-
if let Some(item) = try_ready!(first.poll_next(cx)) {
41+
if let Some(item) = ready!(first.poll_next(cx)) {
4242
return Poll::Ready(Some(item))
4343
}
4444
}

futures-util/src/stream/chunks.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::mem::{self, PinMut};
2+
use std::marker::Unpin;
23
use std::prelude::v1::*;
34

45
use futures_core::{Poll, Stream};
@@ -76,6 +77,8 @@ impl<S> Chunks<S> where S: Stream {
7677
unsafe_pinned!(stream -> Fuse<S>);
7778
}
7879

80+
unsafe impl<S: Unpin + Stream> Unpin for Chunks<S> {}
81+
7982
impl<S> Stream for Chunks<S>
8083
where S: Stream
8184
{
@@ -84,7 +87,7 @@ impl<S> Stream for Chunks<S>
8487
fn poll_next(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
8588
let cap = self.items.capacity();
8689
loop {
87-
match try_ready!(self.stream().poll_next(cx)) {
90+
match ready!(self.stream().poll_next(cx)) {
8891
// Push the item into the buffer and check whether it is full.
8992
// If so, replace our buffer with a new and empty one and return
9093
// the full one.

futures-util/src/stream/collect.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::prelude::v1::*;
2-
2+
use std::marker::Unpin;
33
use std::mem::{self, PinMut};
44

55
use futures_core::{Future, Poll, Stream};
@@ -33,14 +33,16 @@ impl<S: Stream, C: Default> Collect<S, C> {
3333
unsafe_unpinned!(items -> C);
3434
}
3535

36+
unsafe impl<S: Unpin + Stream, C> Unpin for Collect<S, C> {}
37+
3638
impl<S, C> Future for Collect<S, C>
3739
where S: Stream, C: Default + Extend<S:: Item>
3840
{
3941
type Output = C;
4042

4143
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<C> {
4244
loop {
43-
match try_ready!(self.stream().poll_next(cx)) {
45+
match ready!(self.stream().poll_next(cx)) {
4446
Some(e) => self.items().extend(Some(e)),
4547
None => return Poll::Ready(self.finish()),
4648
}

futures-util/src/stream/concat.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::mem::PinMut;
22
use core::fmt::{Debug, Formatter, Result as FmtResult};
33
use core::default::Default;
4+
use core::marker::Unpin;
45

56
use futures_core::{Future, Poll, Stream};
67
use futures_core::task;
@@ -41,6 +42,8 @@ impl<S: Stream> Concat<S> {
4142
unsafe_unpinned!(accum -> Option<S::Item>);
4243
}
4344

45+
unsafe impl<S: Stream + Unpin> Unpin for Concat<S> {}
46+
4447
impl<S> Future for Concat<S>
4548
where S: Stream,
4649
S::Item: Extend<<<S as Stream>::Item as IntoIterator>::Item> + IntoIterator + Default,

futures-util/src/stream/empty.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::mem::PinMut;
2-
use core::marker::PhantomData;
2+
use core::marker::{Unpin, PhantomData};
33

44
use futures_core::{Stream, Poll};
55
use futures_core::task;
@@ -22,6 +22,8 @@ pub fn empty<T>() -> Empty<T> {
2222
}
2323
}
2424

25+
unsafe impl<T> Unpin for Empty<T> {}
26+
2527
impl<T> Stream for Empty<T> {
2628
type Item = T;
2729

futures-util/src/stream/filter.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::mem::PinMut;
2+
use core::marker::Unpin;
23

34
use {PinMutExt, OptionExt};
45

@@ -69,6 +70,12 @@ impl<S, R, P> Filter<S, R, P>
6970
unsafe_unpinned!(pending_item -> Option<S::Item>);
7071
}
7172

73+
unsafe impl<S, R, P> Unpin for Filter<S, R, P>
74+
where S: Stream + Unpin,
75+
P: FnMut(&S::Item) -> R,
76+
R: Future<Output = bool> + Unpin,
77+
{}
78+
7279
/* TODO
7380
// Forwarding impl of Sink from the underlying stream
7481
impl<S, R, P> Sink for Filter<S, R, P>
@@ -94,7 +101,7 @@ impl<S, R, P> Stream for Filter<S, R, P>
94101
fn poll_next(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Option<S::Item>> {
95102
loop {
96103
if self.pending_fut().as_pin_mut().is_none() {
97-
let item = match try_ready!(self.stream().poll_next(cx)) {
104+
let item = match ready!(self.stream().poll_next(cx)) {
98105
Some(e) => e,
99106
None => return Poll::Ready(None),
100107
};
@@ -103,7 +110,7 @@ impl<S, R, P> Stream for Filter<S, R, P>
103110
*self.pending_item() = Some(item);
104111
}
105112

106-
let yield_item = try_ready!(self.pending_fut().as_pin_mut().unwrap().poll(cx));
113+
let yield_item = ready!(self.pending_fut().as_pin_mut().unwrap().poll(cx));
107114
self.pending_fut().assign(None);
108115
let item = self.pending_item().take().unwrap();
109116

futures-util/src/stream/filter_map.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::mem::PinMut;
2+
use core::marker::Unpin;
23

34
use {PinMutExt, OptionExt};
45

@@ -66,6 +67,12 @@ impl<S, R, F> FilterMap<S, R, F>
6667
unsafe_pinned!(pending -> Option<R>);
6768
}
6869

70+
unsafe impl<S, R, F> Unpin for FilterMap<S, R, F>
71+
where S: Stream + Unpin,
72+
F: FnMut(S::Item) -> R,
73+
R: Future + Unpin,
74+
{}
75+
6976
/* TODO
7077
// Forwarding impl of Sink from the underlying stream
7178
impl<S, R, F> Sink for FilterMap<S, R, F>
@@ -90,15 +97,15 @@ impl<S, R, F, B> Stream for FilterMap<S, R, F>
9097
fn poll_next(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Option<B>> {
9198
loop {
9299
if self.pending().as_pin_mut().is_none() {
93-
let item = match try_ready!(self.stream().poll_next(cx)) {
100+
let item = match ready!(self.stream().poll_next(cx)) {
94101
Some(e) => e,
95102
None => return Poll::Ready(None),
96103
};
97104
let fut = (self.f())(item);
98105
self.pending().assign(Some(fut));
99106
}
100107

101-
let item = try_ready!(self.pending().as_pin_mut().unwrap().poll(cx));
108+
let item = ready!(self.pending().as_pin_mut().unwrap().poll(cx));
102109
self.pending().assign(None);
103110
if item.is_some() {
104111
return Poll::Ready(item);

futures-util/src/stream/flatten.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ impl<S> Stream for Flatten<S>
7777
fn poll_next(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
7878
loop {
7979
if self.next().as_pin_mut().is_none() {
80-
match try_ready!(self.stream().poll_next(cx)) {
80+
match ready!(self.stream().poll_next(cx)) {
8181
Some(e) => self.next().assign(Some(e)),
8282
None => return Poll::Ready(None),
8383
}
8484
}
85-
let item = try_ready!(self.next().as_pin_mut().unwrap().poll_next(cx));
85+
let item = ready!(self.next().as_pin_mut().unwrap().poll_next(cx));
8686
if item.is_some() {
8787
return Poll::Ready(item);
8888
} else {

futures-util/src/stream/fold.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::mem::PinMut;
2+
use core::marker::Unpin;
23

34
use {PinMutExt, OptionExt};
45

@@ -37,6 +38,8 @@ impl<S, Fut, T, F> Fold<S, Fut, T, F> {
3738
unsafe_pinned!(fut -> Option<Fut>);
3839
}
3940

41+
unsafe impl<S: Unpin, Fut: Unpin, T, F> Unpin for Fold<S, Fut, T, F> {}
42+
4043
impl<S, Fut, T, F> Future for Fold<S, Fut, T, F>
4144
where S: Stream,
4245
F: FnMut(T, S::Item) -> Fut,
@@ -46,21 +49,23 @@ impl<S, Fut, T, F> Future for Fold<S, Fut, T, F>
4649

4750
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<T> {
4851
loop {
49-
if self.accum().is_some() {
50-
let item = try_ready!(self.stream().poll_next(cx));
51-
let accum = self.accum().take().unwrap();
52-
53-
if let Some(e) = item {
54-
let fut = (self.f())(accum, e);
55-
self.fut().assign(Some(fut));
56-
} else {
57-
return Poll::Ready(accum)
58-
}
59-
} else {
60-
let accum = try_ready!(self.fut().as_pin_mut().unwrap().poll(cx));
52+
// we're currently processing a future to produce a new accum value
53+
if self.accum().is_none() {
54+
let accum = ready!(self.fut().as_pin_mut().unwrap().poll(cx));
6155
*self.accum() = Some(accum);
6256
self.fut().assign(None);
6357
}
58+
59+
let item = ready!(self.stream().poll_next(cx));
60+
let accum = self.accum().take()
61+
.expect("Fold polled after completion");
62+
63+
if let Some(e) = item {
64+
let fut = (self.f())(accum, e);
65+
self.fut().assign(Some(fut));
66+
} else {
67+
return Poll::Ready(accum)
68+
}
6469
}
6570
}
6671
}

futures-util/src/stream/for_each.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::mem::PinMut;
2+
use core::marker::Unpin;
23

34
use {PinMutExt, OptionExt};
45

@@ -35,6 +36,8 @@ impl<S, U, F> ForEach<S, U, F> {
3536
unsafe_pinned!(fut -> Option<U>);
3637
}
3738

39+
unsafe impl<S, U, F> Unpin for ForEach<S, U, F> {}
40+
3841
impl<S, U, F> Future for ForEach<S, U, F>
3942
where S: Stream,
4043
F: FnMut(S::Item) -> U,
@@ -45,11 +48,11 @@ impl<S, U, F> Future for ForEach<S, U, F>
4548
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<()> {
4649
loop {
4750
if let Some(fut) = self.fut().as_pin_mut() {
48-
try_ready!(fut.poll(cx));
51+
ready!(fut.poll(cx));
4952
}
5053
self.fut().assign(None);
5154

52-
match try_ready!(self.stream().poll_next(cx)) {
55+
match ready!(self.stream().poll_next(cx)) {
5356
Some(e) => {
5457
let fut = (self.f())(e);
5558
self.fut().assign(Some(fut));

0 commit comments

Comments
 (0)