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

31 lines
674 B
Rust
Raw Normal View History

use crate::File;
use tokio_io::AsyncWriteExt;
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 mut file = File::create(path).await?;
file.write_all(contents.as_ref()).await?;
2019-02-20 23:38:49 +01:00
Ok(())
2019-02-20 23:38:49 +01:00
}