diff --git a/tokio-fs/src/lib.rs b/tokio-fs/src/lib.rs index a1cc0f74b..5e01a72ff 100644 --- a/tokio-fs/src/lib.rs +++ b/tokio-fs/src/lib.rs @@ -49,6 +49,7 @@ pub mod os; mod read; mod read_dir; mod read_link; +mod read_to_string; mod remove_dir; mod remove_dir_all; mod remove_file; @@ -69,6 +70,7 @@ pub use crate::open_options::OpenOptions; pub use crate::read::read; pub use crate::read_dir::{read_dir, DirEntry, ReadDir}; pub use crate::read_link::read_link; +pub use crate::read_to_string::read_to_string; pub use crate::remove_dir::remove_dir; pub use crate::remove_dir_all::remove_dir_all; pub use crate::remove_file::remove_file; diff --git a/tokio-fs/src/read_to_string.rs b/tokio-fs/src/read_to_string.rs new file mode 100644 index 000000000..9676a15cd --- /dev/null +++ b/tokio-fs/src/read_to_string.rs @@ -0,0 +1,27 @@ +use crate::asyncify; + +use std::{io, path::Path}; + +/// Creates a future which will open a file for reading and read the entire +/// contents into a string and return said string. +/// +/// This is the async equivalent of `std::fs::read_to_string`. +/// +/// # Examples +/// +/// ```no_run +/// use tokio::fs; +/// +/// # async fn dox() -> std::io::Result<()> { +/// let contents = fs::read_to_string("foo.txt").await?; +/// println!("foo.txt contains {} bytes", contents.len()); +/// # Ok(()) +/// # } +/// ``` +pub async fn read_to_string

(path: P) -> io::Result +where + P: AsRef, +{ + let path = path.as_ref().to_owned(); + asyncify(move || std::fs::read_to_string(path)).await +} diff --git a/tokio/src/fs.rs b/tokio/src/fs.rs index 75f493aae..cf902f8ec 100644 --- a/tokio/src/fs.rs +++ b/tokio/src/fs.rs @@ -8,7 +8,7 @@ //! function. pub use tokio_fs::{ - create_dir, create_dir_all, hard_link, metadata, os, read, read_dir, read_link, remove_dir, - remove_dir_all, remove_file, rename, set_permissions, symlink_metadata, write, File, - OpenOptions, + create_dir, create_dir_all, hard_link, metadata, os, read, read_dir, read_link, read_to_string, + remove_dir, remove_dir_all, remove_file, rename, set_permissions, symlink_metadata, write, + File, OpenOptions, };