1use crate::fs::asyncify;
2
3use std::{io, path::Path};
4
5/// Creates a future which will open a file for reading and read the entire
6/// contents into a string and return said string.
7///
8/// This is the async equivalent of [`std::fs::read_to_string`][std].
9///
10/// This operation is implemented by running the equivalent blocking operation
11/// on a separate thread pool using [`spawn_blocking`].
12///
13/// [`spawn_blocking`]: crate::task::spawn_blocking
14/// [std]: fn@std::fs::read_to_string
15///
16/// # Examples
17///
18/// ```no_run
19/// use tokio::fs;
20///
21/// # async fn dox() -> std::io::Result<()> {
22/// let contents = fs::read_to_string("foo.txt").await?;
23/// println!("foo.txt contains {} bytes", contents.len());
24/// # Ok(())
25/// # }
26/// ```
27pub async fn read_to_string(path: impl AsRef<Path>) -> io::Result<String> {
28 let path: PathBuf = path.as_ref().to_owned();
29 asyncify(move || std::fs::read_to_string(path)).await
30}
31