Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: Add AsyncWriteExt::flush #1376

Merged
merged 3 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tokio/src/io/async_write_ext.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::io::flush::{flush, Flush};
use crate::io::write::{write, Write};
use crate::io::write_all::{write_all, WriteAll};

Expand Down Expand Up @@ -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 changes: 38 additions & 0 deletions tokio/src/io/flush.rs
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions tokio/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions tokio/src/io/write.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down