mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-29 00:00:11 +02:00
fs: use async fn instead of custom futures (#1381)
Also update all the doc examples.
This commit is contained in:
+16
-71
@@ -1,13 +1,8 @@
|
||||
use crate::{file, File};
|
||||
use crate::File;
|
||||
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_io::AsyncReadExt;
|
||||
|
||||
use futures_core::ready;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::Context;
|
||||
use std::task::Poll;
|
||||
use std::{io, mem, path::Path};
|
||||
use std::{io, path::Path};
|
||||
|
||||
/// Creates a future which will open a file for reading and read the entire
|
||||
/// contents into a buffer and return said buffer.
|
||||
@@ -17,74 +12,24 @@ use std::{io, mem, path::Path};
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use tokio::prelude::Future;
|
||||
/// #![feature(async_await)]
|
||||
///
|
||||
/// let task = tokio::fs::read("foo.txt").map(|data| {
|
||||
/// // do something with the contents of the file ...
|
||||
/// println!("foo.txt contains {} bytes", data.len());
|
||||
/// }).map_err(|e| {
|
||||
/// // handle errors
|
||||
/// eprintln!("IO error: {:?}", e);
|
||||
/// });
|
||||
/// use tokio::fs;
|
||||
///
|
||||
/// tokio::run(task);
|
||||
/// # async fn dox() -> std::io::Result<()> {
|
||||
/// let contents = fs::read("foo.txt").await?;
|
||||
/// println!("foo.txt contains {} bytes", contents.len());
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn read<P>(path: P) -> ReadFile<P>
|
||||
pub async fn read<P>(path: P) -> io::Result<Vec<u8>>
|
||||
where
|
||||
P: AsRef<Path> + Send + Unpin + 'static,
|
||||
{
|
||||
ReadFile {
|
||||
state: State::Open(File::open(path)),
|
||||
}
|
||||
}
|
||||
let mut file = File::open(path).await?;
|
||||
let metadata = file.metadata().await?;
|
||||
|
||||
/// A future used to open a file and read its entire contents into a buffer.
|
||||
#[derive(Debug)]
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct ReadFile<P: AsRef<Path> + Send + Unpin + 'static> {
|
||||
state: State<P>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State<P: AsRef<Path> + Send + Unpin + 'static> {
|
||||
Open(file::OpenFuture<P>),
|
||||
Metadata(file::MetadataFuture),
|
||||
Reading(Vec<u8>, usize, File),
|
||||
Empty,
|
||||
}
|
||||
|
||||
impl<P: AsRef<Path> + Send + Unpin + 'static> Future for ReadFile<P> {
|
||||
type Output = io::Result<Vec<u8>>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let inner = Pin::get_mut(self);
|
||||
match &mut inner.state {
|
||||
State::Open(ref mut open_file) => {
|
||||
let file = ready!(Pin::new(open_file).poll(cx))?;
|
||||
let new_state = State::Metadata(file.metadata());
|
||||
mem::replace(&mut inner.state, new_state);
|
||||
Pin::new(inner).poll(cx)
|
||||
}
|
||||
State::Metadata(read_metadata) => {
|
||||
let (file, metadata) = ready!(Pin::new(read_metadata).poll(cx))?;
|
||||
let buf = Vec::with_capacity(metadata.len() as usize + 1);
|
||||
let new_state = State::Reading(buf, 0, file);
|
||||
mem::replace(&mut inner.state, new_state);
|
||||
Pin::new(inner).poll(cx)
|
||||
}
|
||||
State::Reading(buf, ref mut pos, file) => {
|
||||
let n = ready!(Pin::new(file).poll_read_buf(cx, buf))?;
|
||||
*pos += n;
|
||||
if *pos >= buf.len() {
|
||||
match mem::replace(&mut inner.state, State::Empty) {
|
||||
State::Reading(buf, _, _) => Poll::Ready(Ok(buf)),
|
||||
_ => panic!(),
|
||||
}
|
||||
} else {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
State::Empty => panic!("poll a WriteFile after it's done"),
|
||||
}
|
||||
}
|
||||
let mut contents = Vec::with_capacity(metadata.len() as usize + 1);
|
||||
file.read_to_end(&mut contents).await?;
|
||||
Ok(contents)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user