Add AsyncWriteExt::shutdown (#1382)

This commit is contained in:
Steven Fackler
2019-08-03 00:51:24 -04:00
committed by Lucio Franco
parent 878503f965
commit 63377e2110
3 changed files with 47 additions and 0 deletions
+9
View File
@@ -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;
@@ -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
View File
@@ -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;
+37
View File
@@ -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)
}
}