tokio_fs/
rename.rs

1use std::fs;
2use std::io;
3use std::path::Path;
4
5use futures::{Future, Poll};
6
7/// Rename a file or directory to a new name, replacing the original file if
8/// `to` already exists.
9///
10/// This will not work if the new name is on a different mount point.
11///
12/// This is an async version of [`std::fs::rename`][std]
13///
14/// [std]: https://doc.rust-lang.org/std/fs/fn.rename.html
15pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> RenameFuture<P, Q> {
16    RenameFuture::new(from, to)
17}
18
19/// Future returned by `rename`.
20#[derive(Debug)]
21pub struct RenameFuture<P, Q>
22where
23    P: AsRef<Path>,
24    Q: AsRef<Path>,
25{
26    from: P,
27    to: Q,
28}
29
30impl<P, Q> RenameFuture<P, Q>
31where
32    P: AsRef<Path>,
33    Q: AsRef<Path>,
34{
35    fn new(from: P, to: Q) -> RenameFuture<P, Q> {
36        RenameFuture { from: from, to: to }
37    }
38}
39
40impl<P, Q> Future for RenameFuture<P, Q>
41where
42    P: AsRef<Path>,
43    Q: AsRef<Path>,
44{
45    type Item = ();
46    type Error = io::Error;
47
48    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
49        ::blocking_io(|| fs::rename(&self.from, &self.to))
50    }
51}