2019-08-27 12:25:20 -07:00
|
|
|
use crate::asyncify;
|
2019-07-19 13:11:46 -07:00
|
|
|
|
2019-08-04 11:24:30 -07:00
|
|
|
use std::{io, path::Path};
|
2019-02-20 23:38:49 +01:00
|
|
|
|
|
|
|
|
/// Creates a future that will open a file for writing and write the entire
|
|
|
|
|
/// contents of `contents` to it.
|
|
|
|
|
///
|
|
|
|
|
/// This is the async equivalent of `std::fs::write`.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
2019-08-04 11:24:30 -07:00
|
|
|
/// use tokio::fs;
|
2019-05-14 10:27:36 -07:00
|
|
|
///
|
2019-08-04 11:24:30 -07:00
|
|
|
/// # async fn dox() -> std::io::Result<()> {
|
|
|
|
|
/// fs::write("foo.txt", b"Hello world!").await?;
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
/// # }
|
2019-02-20 23:38:49 +01:00
|
|
|
/// ```
|
2019-08-04 11:24:30 -07:00
|
|
|
pub async fn write<P, C: AsRef<[u8]> + Unpin>(path: P, contents: C) -> io::Result<()>
|
2019-02-20 23:38:49 +01:00
|
|
|
where
|
2019-07-11 11:05:49 -05:00
|
|
|
P: AsRef<Path> + Send + Unpin + 'static,
|
2019-02-20 23:38:49 +01:00
|
|
|
{
|
2019-08-27 12:25:20 -07:00
|
|
|
let path = path.as_ref().to_owned();
|
|
|
|
|
let contents = contents.as_ref().to_owned();
|
2019-02-20 23:38:49 +01:00
|
|
|
|
2019-08-27 12:25:20 -07:00
|
|
|
asyncify(move || std::fs::write(path, contents)).await
|
2019-02-20 23:38:49 +01:00
|
|
|
}
|