fs: use async fn instead of custom futures (#1381)

Also update all the doc examples.
This commit is contained in:
Carl Lerche
2019-08-04 11:24:30 -07:00
committed by GitHub
parent 337646b97f
commit 6cbe3d4f82
36 changed files with 691 additions and 1668 deletions
+8 -22
View File
@@ -1,6 +1,8 @@
use crate::blocking_io;
use tokio_io::AsyncWrite;
use std::io::{self, Stdout as StdStdout, Write};
use std::io::{self, Write};
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
@@ -16,7 +18,7 @@ use std::task::Poll;
/// [`AsyncWrite`]: trait.AsyncWrite.html
#[derive(Debug)]
pub struct Stdout {
std: StdStdout,
std: std::io::Stdout,
}
/// Constructs a new handle to the standard output of the current process.
@@ -28,33 +30,17 @@ pub fn stdout() -> Stdout {
Stdout { std }
}
impl Write for Stdout {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
crate::would_block(|| self.std.write(buf))
}
fn flush(&mut self) -> io::Result<()> {
crate::would_block(|| self.std.flush())
}
}
impl AsyncWrite for Stdout {
fn poll_write(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
match Pin::get_mut(self).write(buf) {
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => Poll::Pending,
other => Poll::Ready(other),
}
blocking_io(|| (&mut self.std).write(buf))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
match Pin::get_mut(self).flush() {
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => Poll::Pending,
other => Poll::Ready(other),
}
fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
blocking_io(|| (&mut self.std).flush())
}
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {