async_std/fs/remove_dir_all.rs
1use crate::io;
2use crate::path::Path;
3use crate::task::spawn_blocking;
4use crate::utils::Context as _;
5
6/// Removes a directory and all of its contents.
7///
8/// This function is an async version of [`std::fs::remove_dir_all`].
9///
10/// [`std::fs::remove_dir_all`]: https://doc.rust-lang.org/std/fs/fn.remove_dir_all.html
11///
12/// # Errors
13///
14/// An error will be returned in the following situations:
15///
16/// * `path` is not an existing and empty directory.
17/// * The current process lacks permissions to remove the directory.
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::remove_dir_all("./some/directory").await?;
28/// #
29/// # Ok(()) }) }
30/// ```
31pub async fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
32 let path = path.as_ref().to_owned();
33 spawn_blocking(move || {
34 std::fs::remove_dir_all(&path)
35 .context(|| format!("could not remove directory `{}`", path.display()))
36 })
37 .await
38}