broker_tokio/fs/write.rs
1use crate::fs::asyncify;
2
3use std::{io, path::Path};
4
5/// Creates a future that will open a file for writing and write the entire
6/// contents of `contents` to it.
7///
8/// This is the async equivalent of `std::fs::write`.
9///
10/// # Examples
11///
12/// ```no_run
13/// use tokio::fs;
14///
15/// # async fn dox() -> std::io::Result<()> {
16/// fs::write("foo.txt", b"Hello world!").await?;
17/// # Ok(())
18/// # }
19/// ```
20pub async fn write<C: AsRef<[u8]> + Unpin>(path: impl AsRef<Path>, contents: C) -> io::Result<()> {
21 let path = path.as_ref().to_owned();
22 let contents = contents.as_ref().to_owned();
23
24 asyncify(move || std::fs::write(path, contents)).await
25}