tokio_io/io/flush.rs
1use std::io;
2
3use futures::{Async, Future, Poll};
4
5use AsyncWrite;
6
7/// A future used to fully flush an I/O object.
8///
9/// Resolves to the underlying I/O object once the flush operation is complete.
10///
11/// Created by the [`flush`] function.
12///
13/// [`flush`]: fn.flush.html
14#[derive(Debug)]
15pub struct Flush<A> {
16 a: Option<A>,
17}
18
19/// Creates a future which will entirely flush an I/O object and then yield the
20/// object itself.
21///
22/// This function will consume the object provided if an error happens, and
23/// otherwise it will repeatedly call `flush` until it sees `Ok(())`, scheduling
24/// a retry if `WouldBlock` is seen along the way.
25pub fn flush<A>(a: A) -> Flush<A>
26where
27 A: AsyncWrite,
28{
29 Flush { a: Some(a) }
30}
31
32impl<A> Future for Flush<A>
33where
34 A: AsyncWrite,
35{
36 type Item = A;
37 type Error = io::Error;
38
39 fn poll(&mut self) -> Poll<A, io::Error> {
40 try_ready!(self.a.as_mut().unwrap().poll_flush());
41 Ok(Async::Ready(self.a.take().unwrap()))
42 }
43}