summaryrefslogtreecommitdiffstats
path: root/tokio-fs
diff options
context:
space:
mode:
authorDylan Frankland <github@frankland.io>2019-07-19 12:09:53 -0700
committerCarl Lerche <me@carllerche.com>2019-07-19 12:09:53 -0700
commit12ce75f08832254bcbd1fb55f4f59f57a482f569 (patch)
treef569e2f9708f34f4223f06892aa8a4fe0f7e3810 /tokio-fs
parenta88308ed9f608e7225e9803f2dd69ac250c926bf (diff)
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.
Diffstat (limited to 'tokio-fs')
-rw-r--r--tokio-fs/src/lib.rs2
-rw-r--r--tokio-fs/src/remove_dir_all.rs46
2 files changed, 48 insertions, 0 deletions
diff --git a/tokio-fs/src/lib.rs b/tokio-fs/src/lib.rs
index 2070059d..540106ad 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 00000000..d2eab649
--- /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<P: AsRef<Path>>(path: P) -> RemoveDirAllFuture<P> {
+ 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<P>
+where
+ P: AsRef<Path>,
+{
+ path: P,
+}
+
+impl<P> RemoveDirAllFuture<P>
+where
+ P: AsRef<Path>,
+{
+ fn new(path: P) -> RemoveDirAllFuture<P> {
+ RemoveDirAllFuture { path }
+ }
+}
+
+impl<P> Future for RemoveDirAllFuture<P>
+where
+ P: AsRef<Path>,
+{
+ type Output = io::Result<()>;
+
+ fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
+ crate::blocking_io(|| fs::remove_dir_all(&self.path))
+ }
+}