Skip to content

Commit

Permalink
io: Add AsyncWriteExt::flush (#1376)
Browse files Browse the repository at this point in the history
* io: Add `AsyncWriteExt::flush`

* fmt

* fix clippy
  • Loading branch information
LucioFranco authored Aug 2, 2019
1 parent 144d980 commit 6b20272
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
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

0 comments on commit 6b20272

Please sign in to comment.