fs: use async fn instead of custom futures (#1381)

Also update all the doc examples.
This commit is contained in:
Carl Lerche
2019-08-04 11:24:30 -07:00
committed by GitHub
parent 337646b97f
commit 6cbe3d4f82
36 changed files with 691 additions and 1668 deletions
+1
View File
@@ -2,5 +2,6 @@
#[cfg(unix)]
pub mod unix;
#[cfg(windows)]
pub mod windows;
-53
View File
@@ -1,53 +0,0 @@
//! Unix-specific extensions to primitives in the `tokio_fs` module.
use std::future::Future;
use std::io;
use std::os::unix::fs;
use std::path::Path;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
/// Creates a new symbolic link on the filesystem.
///
/// The `dst` path will be a symbolic link pointing to the `src` path.
///
/// This is an async version of [`std::os::unix::fs::symlink`][std]
///
/// [std]: https://doc.rust-lang.org/std/os/unix/fs/fn.symlink.html
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> SymlinkFuture<P, Q> {
SymlinkFuture::new(src, dst)
}
/// Future returned by `symlink`.
#[derive(Debug)]
pub struct SymlinkFuture<P, Q>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
src: P,
dst: Q,
}
impl<P, Q> SymlinkFuture<P, Q>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
fn new(src: P, dst: Q) -> SymlinkFuture<P, Q> {
SymlinkFuture { src, dst }
}
}
impl<P, Q> Future for SymlinkFuture<P, Q>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
type Output = io::Result<()>;
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
crate::blocking_io(|| fs::symlink(&self.src, &self.dst))
}
}
+5
View File
@@ -0,0 +1,5 @@
//! Unix-specific extensions to primitives in the `tokio_fs` module.
mod symlink;
pub use self::symlink::symlink;
+15
View File
@@ -0,0 +1,15 @@
use crate::asyncify;
use std::io;
use std::path::Path;
/// Creates a new symbolic link on the filesystem.
///
/// The `dst` path will be a symbolic link pointing to the `src` path.
///
/// This is an async version of [`std::os::unix::fs::symlink`][std]
///
/// [std]: https://doc.rust-lang.org/std/os/unix/fs/fn.symlink.html
pub async fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
asyncify(|| std::os::unix::fs::symlink(&src, &dst)).await
}
+2 -2
View File
@@ -3,5 +3,5 @@
mod symlink_dir;
mod symlink_file;
pub use self::symlink_dir::{symlink_dir, SymlinkDirFuture};
pub use self::symlink_file::{symlink_file, SymlinkFileFuture};
pub use self::symlink_dir::symlink_dir;
pub use self::symlink_file::symlink_file;
+4 -40
View File
@@ -1,10 +1,7 @@
use std::future::Future;
use crate::asyncify;
use std::io;
use std::os::windows::fs;
use std::path::Path;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
/// Creates a new directory symlink on the filesystem.
///
@@ -14,39 +11,6 @@ use std::task::Poll;
/// This is an async version of [`std::os::windows::fs::symlink_dir`][std]
///
/// [std]: https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_dir.html
pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> SymlinkDirFuture<P, Q> {
SymlinkDirFuture::new(src, dst)
}
/// Future returned by `symlink_dir`.
#[derive(Debug)]
pub struct SymlinkDirFuture<P, Q>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
src: P,
dst: Q,
}
impl<P, Q> SymlinkDirFuture<P, Q>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
fn new(src: P, dst: Q) -> SymlinkDirFuture<P, Q> {
SymlinkDirFuture { src, dst }
}
}
impl<P, Q> Future for SymlinkDirFuture<P, Q>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
type Output = io::Result<()>;
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
crate::blocking_io(|| fs::symlink_dir(&self.src, &self.dst))
}
pub async fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
asyncify(|| std::os::windows::fs::symlink_dir(&src, &dst)).await
}
+4 -40
View File
@@ -1,10 +1,7 @@
use std::future::Future;
use crate::asyncify;
use std::io;
use std::os::windows::fs;
use std::path::Path;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
/// Creates a new file symbolic link on the filesystem.
///
@@ -14,39 +11,6 @@ use std::task::Poll;
/// This is an async version of [`std::os::windows::fs::symlink_file`][std]
///
/// [std]: https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_file.html
pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> SymlinkFileFuture<P, Q> {
SymlinkFileFuture::new(src, dst)
}
/// Future returned by `symlink_file`.
#[derive(Debug)]
pub struct SymlinkFileFuture<P, Q>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
src: P,
dst: Q,
}
impl<P, Q> SymlinkFileFuture<P, Q>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
fn new(src: P, dst: Q) -> SymlinkFileFuture<P, Q> {
SymlinkFileFuture { src, dst }
}
}
impl<P, Q> Future for SymlinkFileFuture<P, Q>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
type Output = io::Result<()>;
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
crate::blocking_io(|| fs::symlink_file(&self.src, &self.dst))
}
pub async fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
asyncify(|| std::os::windows::fs::symlink_file(&src, &dst)).await
}