diff --git a/tokio/src/io/async_write_ext.rs b/tokio/src/io/async_write_ext.rs index 1972238f3..8b6a4844b 100644 --- a/tokio/src/io/async_write_ext.rs +++ b/tokio/src/io/async_write_ext.rs @@ -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 AsyncWriteExt for W {} diff --git a/tokio/src/io/flush.rs b/tokio/src/io/flush.rs new file mode 100644 index 000000000..e9b9dde89 --- /dev/null +++ b/tokio/src/io/flush.rs @@ -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: &mut A) -> Flush<'_, A> +where + A: AsyncWrite + Unpin + ?Sized, +{ + Flush { a } +} + +impl<'a, A> Unpin for Flush<'a, A> where A: Unpin + ?Sized {} + +impl Future for Flush<'_, A> +where + A: AsyncWrite + Unpin + ?Sized, +{ + type Output = io::Result<()>; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let me = &mut *self; + Pin::new(&mut *me.a).poll_flush(cx) + } +} diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index d1fce45ef..cfa30e1e2 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -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; diff --git a/tokio/src/io/write.rs b/tokio/src/io/write.rs index d52885272..f31b1796a 100644 --- a/tokio/src/io/write.rs +++ b/tokio/src/io/write.rs @@ -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 Future for Write<'_, W> where