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