Files
tokio/tokio-fs/src/write.rs
T

29 lines
694 B
Rust
Raw Normal View History

use crate::asyncify;
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
/// use tokio::fs;
2019-05-14 10:27:36 -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
/// ```
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
{
let path = path.as_ref().to_owned();
let contents = contents.as_ref().to_owned();
2019-02-20 23:38:49 +01:00
asyncify(move || std::fs::write(path, contents)).await
2019-02-20 23:38:49 +01:00
}