mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-07 00:00:08 +02:00
fs: add read_to_string (#1664)
This commit is contained in:
committed by
Carl Lerche
parent
4c97e9dc28
commit
2a181320b7
@@ -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<P>(path: P) -> io::Result<String>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let path = path.as_ref().to_owned();
|
||||
asyncify(move || std::fs::read_to_string(path)).await
|
||||
}
|
||||
Reference in New Issue
Block a user