mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-29 00:00:11 +02:00
fs: use async fn instead of custom futures (#1381)
Also update all the doc examples.
This commit is contained in:
@@ -2,5 +2,6 @@
|
||||
|
||||
#[cfg(unix)]
|
||||
pub mod unix;
|
||||
|
||||
#[cfg(windows)]
|
||||
pub mod windows;
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
//! Unix-specific extensions to primitives in the `tokio_fs` module.
|
||||
|
||||
mod symlink;
|
||||
|
||||
pub use self::symlink::symlink;
|
||||
@@ -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
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user