summaryrefslogtreecommitdiffstats
path: root/tokio-fs/src/rename.rs
diff options
context:
space:
mode:
authorBrian Olsen <brian@maven-group.org>2018-08-01 06:39:27 +0200
committerCarl Lerche <me@carllerche.com>2018-07-31 21:39:27 -0700
commit0490280d662f000aff674593cc9a4f69a1cd1171 (patch)
tree321f63a9aac19afc2bb49b44baa152e939f53fe8 /tokio-fs/src/rename.rs
parent0f76470172d7f7c50d854d4475ce66d8b2e3e5d0 (diff)
tokio-fs: Add async versions of most of std::fs (#494)
* create_dir * create_dir_all * hard_link * read_dir * read_link * remove_dir * remove_file * rename * set_permissions that works with path * symlink_metadata * symlink on unix * symlink_dir on windows * symlink_file on windows
Diffstat (limited to 'tokio-fs/src/rename.rs')
-rw-r--r--tokio-fs/src/rename.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/tokio-fs/src/rename.rs b/tokio-fs/src/rename.rs
new file mode 100644
index 00000000..210f53bb
--- /dev/null
+++ b/tokio-fs/src/rename.rs
@@ -0,0 +1,54 @@
+use std::fs;
+use std::io;
+use std::path::Path;
+
+use futures::{Future, Poll};
+
+/// Rename a file or directory to a new name, replacing the original file if
+/// `to` already exists.
+///
+/// This will not work if the new name is on a different mount point.
+///
+/// This is an async version of [`std::fs::rename`][std]
+///
+/// [std]: https://doc.rust-lang.org/std/fs/fn.rename.html
+pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> RenameFuture<P, Q> {
+ RenameFuture::new(from, to)
+}
+
+/// Future returned by `rename`.
+#[derive(Debug)]
+pub struct RenameFuture<P, Q>
+where
+ P: AsRef<Path>,
+ Q: AsRef<Path>
+{
+ from: P,
+ to: Q,
+}
+
+impl<P, Q> RenameFuture<P, Q>
+where
+ P: AsRef<Path>,
+ Q: AsRef<Path>
+{
+ fn new(from: P, to: Q) -> RenameFuture<P, Q> {
+ RenameFuture {
+ from: from,
+ to: to,
+ }
+ }
+}
+
+impl<P, Q> Future for RenameFuture<P, Q>
+where
+ P: AsRef<Path>,
+ Q: AsRef<Path>
+{
+ type Item = ();
+ type Error = io::Error;
+
+ fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
+ ::blocking_io(|| fs::rename(&self.from, &self.to) )
+ }
+}