tokio_fs/
read.rs

1use futures::{Async, Future, Poll};
2use std::{io, mem, path::Path};
3use tokio_io;
4use {file, File};
5
6/// Creates a future which will open a file for reading and read the entire
7/// contents into a buffer and return said buffer.
8///
9/// This is the async equivalent of `std::fs::read`.
10///
11/// # Examples
12///
13/// ```no_run
14/// # extern crate tokio;
15/// use tokio::prelude::Future;
16/// fn main() {
17///     let task = tokio::fs::read("foo.txt").map(|data| {
18///         // do something with the contents of the file ...
19///         println!("foo.txt contains {} bytes", data.len());
20///     }).map_err(|e| {
21///         // handle errors
22///         eprintln!("IO error: {:?}", e);
23///     });
24///     tokio::run(task);
25/// }
26/// ```
27pub fn read<P>(path: P) -> ReadFile<P>
28where
29    P: AsRef<Path> + Send + 'static,
30{
31    ReadFile {
32        state: State::Open(File::open(path)),
33    }
34}
35
36/// A future used to open a file and read its entire contents into a buffer.
37#[derive(Debug)]
38pub struct ReadFile<P: AsRef<Path> + Send + 'static> {
39    state: State<P>,
40}
41
42#[derive(Debug)]
43enum State<P: AsRef<Path> + Send + 'static> {
44    Open(file::OpenFuture<P>),
45    Metadata(file::MetadataFuture),
46    Read(tokio_io::io::ReadToEnd<File>),
47}
48
49impl<P: AsRef<Path> + Send + 'static> Future for ReadFile<P> {
50    type Item = Vec<u8>;
51    type Error = io::Error;
52
53    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
54        let new_state = match &mut self.state {
55            State::Open(ref mut open_file) => {
56                let file = try_ready!(open_file.poll());
57                State::Metadata(file.metadata())
58            }
59            State::Metadata(read_metadata) => {
60                let (file, metadata) = try_ready!(read_metadata.poll());
61                let buf = Vec::with_capacity(metadata.len() as usize + 1);
62                let read = tokio_io::io::read_to_end(file, buf);
63                State::Read(read)
64            }
65            State::Read(ref mut read) => {
66                let (_, buf) = try_ready!(read.poll());
67                return Ok(Async::Ready(buf));
68            }
69        };
70
71        mem::replace(&mut self.state, new_state);
72        // Getting here means we transitionsed state. Must poll the new state.
73        self.poll()
74    }
75}