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