Skip to content

Commit

Permalink
Add AsyncWriteExt::shutdown (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler authored and LucioFranco committed Aug 3, 2019
1 parent 878503f commit 63377e2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tokio-io/src/io/async_write_ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::io::flush::{flush, Flush};
use crate::io::shutdown::{shutdown, Shutdown};
use crate::io::write::{write, Write};
use crate::io::write_all::{write_all, WriteAll};
use crate::AsyncWrite;
Expand Down Expand Up @@ -28,6 +29,14 @@ pub trait AsyncWriteExt: AsyncWrite {
{
flush(self)
}

/// Shutdown this writer.
fn shutdown(&mut self) -> Shutdown<'_, Self>
where
Self: Unpin,
{
shutdown(self)
}
}

impl<W: AsyncWrite + ?Sized> AsyncWriteExt for W {}
1 change: 1 addition & 0 deletions tokio-io/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ mod read_line;
mod read_to_end;
mod read_to_string;
mod read_until;
mod shutdown;
mod write;
mod write_all;

Expand Down
37 changes: 37 additions & 0 deletions tokio-io/src/io/shutdown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::AsyncWrite;
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};

/// A future used to shutdown an I/O object.
///
/// Created by the [`AsyncWriteExt::shutdown`] function.
///
/// [`shutdown`]: fn.shutdown.html
#[derive(Debug)]
pub struct Shutdown<'a, A: ?Sized> {
a: &'a mut A,
}

/// Creates a future which will shutdown an I/O object.
pub(super) fn shutdown<A>(a: &mut A) -> Shutdown<'_, A>
where
A: AsyncWrite + Unpin + ?Sized,
{
Shutdown { a }
}

impl<'a, A> Unpin for Shutdown<'a, A> where A: Unpin + ?Sized {}

impl<A> Future for Shutdown<'_, 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_shutdown(cx)
}
}

0 comments on commit 63377e2

Please sign in to comment.