async_std/fs/
create_dir_all.rs

1use crate::io;
2use crate::path::Path;
3use crate::task::spawn_blocking;
4use crate::utils::Context as _;
5
6/// Creates a new directory and all of its parents if they are missing.
7///
8/// This function is an async version of [`std::fs::create_dir_all`].
9///
10/// [`std::fs::create_dir_all`]: https://doc.rust-lang.org/std/fs/fn.create_dir_all.html
11///
12/// # Errors
13///
14/// An error will be returned in the following situations:
15///
16/// * `path` already points to an existing file or directory.
17/// * The current process lacks permissions to create the directory or its missing parents.
18/// * Some other I/O error occurred.
19///
20/// # Examples
21///
22/// ```no_run
23/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
24/// #
25/// use async_std::fs;
26///
27/// fs::create_dir_all("./some/directory").await?;
28/// #
29/// # Ok(()) }) }
30/// ```
31pub async fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
32    let path = path.as_ref().to_owned();
33    spawn_blocking(move || {
34        std::fs::create_dir_all(&path)
35            .context(|| format!("could not create directory path `{}`", path.display()))
36    })
37    .await
38}