Skip to content

Commit 304978c

Browse files
authored
Merge pull request #747 from rust-lang-nursery/rename-poll-complete
Rename `Sink::poll_complete` to `flush`
2 parents 669980b + 9ca313a commit 304978c

40 files changed

+174
-126
lines changed

futures-channel/benches/sync_mpsc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl Stream for TestSender {
103103
Err(_) => panic!(),
104104
Ok(Ok(())) => {
105105
self.last += 1;
106-
assert_eq!(Ok(Async::Ready(())), self.tx.poll_complete());
106+
assert_eq!(Ok(Async::Ready(())), self.tx.flush());
107107
Ok(Async::Ready(Some(self.last)))
108108
}
109109
Ok(Err(_)) => {

futures-channel/tests/mpsc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn send_recv_no_buffer() {
3434

3535
// Run on a task context
3636
let f = lazy(move || {
37-
assert!(tx.poll_complete().unwrap().is_ready());
37+
assert!(tx.flush().unwrap().is_ready());
3838
assert!(tx.poll_ready().unwrap().is_ready());
3939

4040
// Send first message

futures-sink/src/channel_impls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<T> Sink for Sender<T> {
1616
self.start_send(msg).map(res_to_async_sink)
1717
}
1818

19-
fn poll_complete(&mut self) -> Poll<(), SendError<T>> {
19+
fn flush(&mut self) -> Poll<(), SendError<T>> {
2020
Ok(Async::Ready(()))
2121
}
2222

@@ -33,7 +33,7 @@ impl<T> Sink for UnboundedSender<T> {
3333
self.start_send(msg).map(res_to_async_sink)
3434
}
3535

36-
fn poll_complete(&mut self) -> Poll<(), SendError<T>> {
36+
fn flush(&mut self) -> Poll<(), SendError<T>> {
3737
Ok(Async::Ready(()))
3838
}
3939

@@ -51,7 +51,7 @@ impl<'a, T> Sink for &'a UnboundedSender<T> {
5151
Ok(AsyncSink::Ready)
5252
}
5353

54-
fn poll_complete(&mut self) -> Poll<(), SendError<T>> {
54+
fn flush(&mut self) -> Poll<(), SendError<T>> {
5555
Ok(Async::Ready(()))
5656
}
5757

futures-sink/src/lib.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use futures_core::Poll;
2727
#[derive(Copy, Clone, Debug, PartialEq)]
2828
pub enum AsyncSink<T> {
2929
/// The `start_send` attempt succeeded, so the sending process has
30-
/// *started*; you must use `Sink::poll_complete` to drive the send
30+
/// *started*; you must use `Sink::flush` to drive the send
3131
/// to completion.
3232
Ready,
3333

@@ -83,7 +83,7 @@ if_std! {
8383
Ok(::AsyncSink::Ready)
8484
}
8585

86-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
86+
fn flush(&mut self) -> Poll<(), Self::SinkError> {
8787
Ok(::Async::Ready(()))
8888
}
8989

@@ -105,8 +105,8 @@ if_std! {
105105
(**self).start_send(item)
106106
}
107107

108-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
109-
(**self).poll_complete()
108+
fn flush(&mut self) -> Poll<(), Self::SinkError> {
109+
(**self).flush()
110110
}
111111

112112
fn close(&mut self) -> Poll<(), Self::SinkError> {
@@ -160,21 +160,21 @@ pub trait Sink {
160160
/// until the buffer is fully flushed. Since sinks are designed to work with
161161
/// asynchronous I/O, the process of actually writing out the data to an
162162
/// underlying object takes place asynchronously. **You *must* use
163-
/// `poll_complete` in order to drive completion of a send**. In particular,
163+
/// `flush` in order to drive completion of a send**. In particular,
164164
/// `start_send` does not begin the flushing process
165165
///
166166
/// # Return value
167167
///
168168
/// This method returns `AsyncSink::Ready` if the sink was able to start
169169
/// sending `item`. In that case, you *must* ensure that you call
170-
/// `poll_complete` to process the sent item to completion. Note, however,
170+
/// `flush` to process the sent item to completion. Note, however,
171171
/// that several calls to `start_send` can be made prior to calling
172-
/// `poll_complete`, which will work on completing all pending items.
172+
/// `flush`, which will work on completing all pending items.
173173
///
174174
/// The method returns `AsyncSink::Pending` if the sink was unable to begin
175175
/// sending, usually due to being full. The sink must have attempted to
176176
/// complete processing any outstanding requests (equivalent to
177-
/// `poll_complete`) before yielding this result. The current task will be
177+
/// `flush`) before yielding this result. The current task will be
178178
/// automatically scheduled for notification when the sink may be ready to
179179
/// receive new values.
180180
///
@@ -190,7 +190,7 @@ pub trait Sink {
190190
/// sink:
191191
///
192192
/// - It is called outside of the context of a task.
193-
/// - A previous call to `start_send` or `poll_complete` yielded an error.
193+
/// - A previous call to `start_send` or `flush` yielded an error.
194194
fn start_send(&mut self, item: Self::SinkItem)
195195
-> StartSend<Self::SinkItem, Self::SinkError>;
196196

@@ -230,7 +230,7 @@ pub trait Sink {
230230
/// This method may panic in a few situations, depending on the specific sink:
231231
///
232232
/// - It is called outside of the context of a task.
233-
/// - A previous call to `start_send` or `poll_complete` yielded an error.
233+
/// - A previous call to `start_send` or `flush` yielded an error.
234234
///
235235
/// # Compatibility nodes
236236
///
@@ -240,34 +240,34 @@ pub trait Sink {
240240
/// this method to what it's always done, just flushing.
241241
///
242242
/// In the 0.2 release series of futures this method will be renamed to
243-
/// `poll_flush`. For 0.1, however, the breaking change is not happening
243+
/// `flush`. For 0.1, however, the breaking change is not happening
244244
/// yet.
245-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError>;
245+
fn flush(&mut self) -> Poll<(), Self::SinkError>;
246246

247247
/// A method to indicate that no more values will ever be pushed into this
248248
/// sink.
249249
///
250250
/// This method is used to indicate that a sink will no longer even be given
251251
/// another value by the caller. That is, the `start_send` method above will
252-
/// be called no longer (nor `poll_complete`). This method is intended to
252+
/// be called no longer (nor `flush`). This method is intended to
253253
/// model "graceful shutdown" in various protocols where the intent to shut
254254
/// down is followed by a little more blocking work.
255255
///
256256
/// Callers of this function should work it it in a similar fashion to
257-
/// `poll_complete`. Once called it may return `Pending` which indicates
257+
/// `flush`. Once called it may return `Pending` which indicates
258258
/// that more external work needs to happen to make progress. The current
259259
/// task will be scheduled to receive a notification in such an event,
260260
/// however.
261261
///
262-
/// Note that this function will imply `poll_complete` above. That is, if a
262+
/// Note that this function will imply `flush` above. That is, if a
263263
/// sink has buffered data, then it'll be flushed out during a `close`
264-
/// operation. It is not necessary to have `poll_complete` return `Ready`
264+
/// operation. It is not necessary to have `flush` return `Ready`
265265
/// before a `close` is called. Once a `close` is called, though,
266-
/// `poll_complete` cannot be called.
266+
/// `flush` cannot be called.
267267
///
268268
/// # Return value
269269
///
270-
/// This function, like `poll_complete`, returns a `Poll`. The value is
270+
/// This function, like `flush`, returns a `Poll`. The value is
271271
/// `Ready` once the close operation has completed. At that point it should
272272
/// be safe to drop the sink and deallocate associated resources.
273273
///
@@ -286,7 +286,7 @@ pub trait Sink {
286286
/// always be true.
287287
///
288288
/// Note that it's also typically an error to call `start_send` or
289-
/// `poll_complete` after the `close` function is called. This method will
289+
/// `flush` after the `close` function is called. This method will
290290
/// *initiate* a close, and continuing to send values after that (or attempt
291291
/// to flush) may result in strange behavior, panics, errors, etc. Once this
292292
/// method is called, it must be the only method called on this `Sink`.
@@ -296,12 +296,12 @@ pub trait Sink {
296296
/// This method may panic or cause panics if:
297297
///
298298
/// * It is called outside the context of a future's task
299-
/// * It is called and then `start_send` or `poll_complete` is called
299+
/// * It is called and then `start_send` or `flush` is called
300300
///
301301
/// # Compatibility notes
302302
///
303303
/// Note that this function is currently by default a provided function,
304-
/// defaulted to calling `poll_complete` above. This function was added
304+
/// defaulted to calling `flush` above. This function was added
305305
/// in the 0.1 series of the crate as a backwards-compatible addition. It
306306
/// is intended that in the 0.2 series the method will no longer be a
307307
/// default method.
@@ -321,8 +321,8 @@ impl<'a, S: ?Sized + Sink> Sink for &'a mut S {
321321
(**self).start_send(item)
322322
}
323323

324-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
325-
(**self).poll_complete()
324+
fn flush(&mut self) -> Poll<(), Self::SinkError> {
325+
(**self).flush()
326326
}
327327

328328
fn close(&mut self) -> Poll<(), Self::SinkError> {

futures-util/src/sink/buffer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<S: Sink> Buffer<S> {
4848
self.buf.push_front(item);
4949

5050
// ensure that we attempt to complete any pushes we've started
51-
self.sink.poll_complete()?;
51+
self.sink.flush()?;
5252

5353
return Ok(Async::Pending);
5454
}
@@ -85,14 +85,14 @@ impl<S: Sink> Sink for Buffer<S> {
8585
Ok(AsyncSink::Ready)
8686
}
8787

88-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
88+
fn flush(&mut self) -> Poll<(), Self::SinkError> {
8989
if self.cap == 0 {
90-
return self.sink.poll_complete();
90+
return self.sink.flush();
9191
}
9292

9393
try_ready!(self.try_empty_buffer());
9494
debug_assert!(self.buf.is_empty());
95-
self.sink.poll_complete()
95+
self.sink.flush()
9696
}
9797

9898
fn close(&mut self) -> Poll<(), Self::SinkError> {

futures-util/src/sink/close.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use futures_core::{Poll, Async, Future};
2+
use futures_sink::Sink;
3+
4+
/// Future for the `close` combinator, which polls the sink until all data has
5+
/// been closeed.
6+
#[derive(Debug)]
7+
#[must_use = "futures do nothing unless polled"]
8+
pub struct Close<S> {
9+
sink: Option<S>,
10+
}
11+
12+
/// A future that completes when the sink has finished closing.
13+
///
14+
/// The sink itself is returned after closeing is complete.
15+
pub fn close<S: Sink>(sink: S) -> Close<S> {
16+
Close { sink: Some(sink) }
17+
}
18+
19+
impl<S: Sink> Close<S> {
20+
/// Get a shared reference to the inner sink.
21+
/// Returns `None` if the sink has already been closeed.
22+
pub fn get_ref(&self) -> Option<&S> {
23+
self.sink.as_ref()
24+
}
25+
26+
/// Get a mutable reference to the inner sink.
27+
/// Returns `None` if the sink has already been closeed.
28+
pub fn get_mut(&mut self) -> Option<&mut S> {
29+
self.sink.as_mut()
30+
}
31+
32+
/// Consume the `Close` and return the inner sink.
33+
/// Returns `None` if the sink has already been closeed.
34+
pub fn into_inner(self) -> Option<S> {
35+
self.sink
36+
}
37+
}
38+
39+
impl<S: Sink> Future for Close<S> {
40+
type Item = S;
41+
type Error = S::SinkError;
42+
43+
fn poll(&mut self) -> Poll<S, S::SinkError> {
44+
let mut sink = self.sink.take().expect("Attempted to poll Close after it completed");
45+
if sink.close()?.is_ready() {
46+
Ok(Async::Ready(sink))
47+
} else {
48+
self.sink = Some(sink);
49+
Ok(Async::Pending)
50+
}
51+
}
52+
}

futures-util/src/sink/fanout.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ impl<A, B> Sink for Fanout<A, B>
6767
}
6868
}
6969

70-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
71-
let left_async = self.left.poll_complete()?;
72-
let right_async = self.right.poll_complete()?;
70+
fn flush(&mut self) -> Poll<(), Self::SinkError> {
71+
let left_async = self.left.flush()?;
72+
let right_async = self.right.flush()?;
7373
// Only if both downstream sinks are ready, signal readiness.
7474
if left_async.is_ready() && right_async.is_ready() {
7575
Ok(Async::Ready(()))
@@ -112,9 +112,9 @@ impl<S: Sink> Downstream<S> {
112112
Ok(())
113113
}
114114

115-
fn poll_complete(&mut self) -> Poll<(), S::SinkError> {
115+
fn flush(&mut self) -> Poll<(), S::SinkError> {
116116
self.keep_flushing()?;
117-
let async = self.sink.poll_complete()?;
117+
let async = self.sink.flush()?;
118118
// Only if all values have been sent _and_ the underlying
119119
// sink is completely flushed, signal readiness.
120120
if self.state.is_ready() && async.is_ready() {

futures-util/src/sink/flush.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
use futures_core::{Poll, Async, Future};
22
use futures_sink::Sink;
33

4-
/// Future for the `Sink::flush` combinator, which polls the sink until all data
4+
/// Future for the `flush` combinator, which polls the sink until all data
55
/// has been flushed.
66
#[derive(Debug)]
77
#[must_use = "futures do nothing unless polled"]
88
pub struct Flush<S> {
99
sink: Option<S>,
1010
}
1111

12-
pub fn new<S: Sink>(sink: S) -> Flush<S> {
12+
/// A future that completes when the sink has finished processing all
13+
/// pending requests.
14+
///
15+
/// The sink itself is returned after flushing is complete; this adapter is
16+
/// intended to be used when you want to stop sending to the sink until
17+
/// all current requests are processed.
18+
pub fn flush<S: Sink>(sink: S) -> Flush<S> {
1319
Flush { sink: Some(sink) }
1420
}
1521

@@ -39,7 +45,7 @@ impl<S: Sink> Future for Flush<S> {
3945

4046
fn poll(&mut self) -> Poll<S, S::SinkError> {
4147
let mut sink = self.sink.take().expect("Attempted to poll Flush after it completed");
42-
if sink.poll_complete()?.is_ready() {
48+
if sink.flush()?.is_ready() {
4349
Ok(Async::Ready(sink))
4450
} else {
4551
self.sink = Some(sink);

futures-util/src/sink/from_err.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ impl<S, E> Sink for SinkFromErr<S, E>
5353
self.sink.start_send(item).map_err(|e| e.into())
5454
}
5555

56-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
57-
self.sink.poll_complete().map_err(|e| e.into())
56+
fn flush(&mut self) -> Poll<(), Self::SinkError> {
57+
self.sink.flush().map_err(|e| e.into())
5858
}
5959

6060
fn close(&mut self) -> Poll<(), Self::SinkError> {

futures-util/src/sink/map_err.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ impl<S, F, E> Sink for SinkMapErr<S, F>
4444
self.sink.start_send(item).map_err(|e| self.f.take().expect("cannot use MapErr after an error")(e))
4545
}
4646

47-
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
48-
self.sink.poll_complete().map_err(|e| self.f.take().expect("cannot use MapErr after an error")(e))
47+
fn flush(&mut self) -> Poll<(), Self::SinkError> {
48+
self.sink.flush().map_err(|e| self.f.take().expect("cannot use MapErr after an error")(e))
4949
}
5050

5151
fn close(&mut self) -> Poll<(), Self::SinkError> {

0 commit comments

Comments
 (0)