io: Add AsyncWriteExt::flush (#1376)

* io: Add `AsyncWriteExt::flush`

* fmt

* fix clippy
This commit is contained in:
Lucio Franco
2019-08-02 13:53:49 -04:00
committed by GitHub
parent 144d980e5c
commit 6b202722ea
4 changed files with 55 additions and 2 deletions
+15
View File
@@ -1,3 +1,4 @@
use crate::io::flush::{flush, Flush};
use crate::io::write::{write, Write};
use crate::io::write_all::{write_all, WriteAll};
@@ -32,6 +33,20 @@ pub trait AsyncWriteExt: AsyncWrite {
{
write_all(self, src)
}
/// Flush the contents of this writer.
///
/// # Examples
///
/// ```
/// unimplemented!();
/// ```
fn flush(&mut self) -> Flush<'_, Self>
where
Self: Unpin,
{
flush(self)
}
}
impl<W: AsyncWrite + ?Sized> AsyncWriteExt for W {}
+38
View File
@@ -0,0 +1,38 @@
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio_io::AsyncWrite;
/// A future used to fully flush an I/O object.
///
/// Created by the [`AsyncWriteExt::flush`] function.
///
/// [`flush`]: fn.flush.html
#[derive(Debug)]
pub struct Flush<'a, A: ?Sized> {
a: &'a mut A,
}
/// Creates a future which will entirely flush an I/O object.
pub(super) fn flush<A>(a: &mut A) -> Flush<'_, A>
where
A: AsyncWrite + Unpin + ?Sized,
{
Flush { a }
}
impl<'a, A> Unpin for Flush<'a, A> where A: Unpin + ?Sized {}
impl<A> Future for Flush<'_, A>
where
A: AsyncWrite + Unpin + ?Sized,
{
type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let me = &mut *self;
Pin::new(&mut *me.a).poll_flush(cx)
}
}
+1
View File
@@ -40,6 +40,7 @@ mod async_buf_read_ext;
mod async_read_ext;
mod async_write_ext;
mod copy;
mod flush;
mod lines;
mod read;
mod read_exact;
+1 -2
View File
@@ -1,6 +1,5 @@
use std::future::Future;
use std::io;
use std::marker::Unpin;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio_io::AsyncWrite;
@@ -23,7 +22,7 @@ where
}
// forward Unpin
impl<'a, W: Unpin + ?Sized> Unpin for Write<'_, W> {}
impl<'a, W: Unpin + ?Sized> Unpin for Write<'a, W> {}
impl<W> Future for Write<'_, W>
where