From 12ce75f08832254bcbd1fb55f4f59f57a482f569 Mon Sep 17 00:00:00 2001 From: Dylan Frankland Date: Fri, 19 Jul 2019 12:09:53 -0700 Subject: [PATCH] fs: add `remove_dir_all` and `RemoveDirAllFuture` (#1325) Adds the sister function to `remove_dir` and mirrors the `create_dir_all` that's already exposed. --- tokio-fs/src/lib.rs | 2 ++ tokio-fs/src/remove_dir_all.rs | 46 ++++++++++++++++++++++++++++++++++ tokio/src/fs.rs | 4 ++- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tokio-fs/src/remove_dir_all.rs diff --git a/tokio-fs/src/lib.rs b/tokio-fs/src/lib.rs index 2070059da..540106ad6 100644 --- a/tokio-fs/src/lib.rs +++ b/tokio-fs/src/lib.rs @@ -43,6 +43,7 @@ mod read; mod read_dir; mod read_link; mod remove_dir; +mod remove_dir_all; mod remove_file; mod rename; mod set_permissions; @@ -62,6 +63,7 @@ pub use crate::read::{read, ReadFile}; pub use crate::read_dir::{read_dir, DirEntry, ReadDir, ReadDirFuture}; pub use crate::read_link::{read_link, ReadLinkFuture}; pub use crate::remove_dir::{remove_dir, RemoveDirFuture}; +pub use crate::remove_dir_all::{remove_dir_all, RemoveDirAllFuture}; pub use crate::remove_file::{remove_file, RemoveFileFuture}; pub use crate::rename::{rename, RenameFuture}; pub use crate::set_permissions::{set_permissions, SetPermissionsFuture}; diff --git a/tokio-fs/src/remove_dir_all.rs b/tokio-fs/src/remove_dir_all.rs new file mode 100644 index 000000000..d2eab6491 --- /dev/null +++ b/tokio-fs/src/remove_dir_all.rs @@ -0,0 +1,46 @@ +use std::fs; +use std::future::Future; +use std::io; +use std::path::Path; +use std::pin::Pin; +use std::task::Context; +use std::task::Poll; + +/// Removes a directory at this path, after removing all its contents. Use carefully! +/// +/// This is an async version of [`std::fs::remove_dir_all`][std] +/// +/// [std]: https://doc.rust-lang.org/std/fs/fn.remove_dir_all.html +pub fn remove_dir_all>(path: P) -> RemoveDirAllFuture

{ + RemoveDirAllFuture::new(path) +} + +/// Future returned by `remove_dir_all`. +#[derive(Debug)] +#[must_use = "futures do nothing unless you `.await` or poll them"] +pub struct RemoveDirAllFuture

+where + P: AsRef, +{ + path: P, +} + +impl

RemoveDirAllFuture

+where + P: AsRef, +{ + fn new(path: P) -> RemoveDirAllFuture

{ + RemoveDirAllFuture { path } + } +} + +impl

Future for RemoveDirAllFuture

+where + P: AsRef, +{ + type Output = io::Result<()>; + + fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll { + crate::blocking_io(|| fs::remove_dir_all(&self.path)) + } +} diff --git a/tokio/src/fs.rs b/tokio/src/fs.rs index 5d185cd0f..4831b93e2 100644 --- a/tokio/src/fs.rs +++ b/tokio/src/fs.rs @@ -12,4 +12,6 @@ pub use tokio_fs::{ create_dir, create_dir_all, file, hard_link, metadata, os, read_dir, read_link, }; pub use tokio_fs::{read, write, ReadFile, WriteFile}; -pub use tokio_fs::{remove_dir, remove_file, rename, set_permissions, symlink_metadata, File}; +pub use tokio_fs::{ + remove_dir, remove_dir_all, remove_file, rename, set_permissions, symlink_metadata, File, +};