Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: use async fn instead of custom futures #1381

Merged
merged 14 commits into from
Aug 4, 2019
15 changes: 8 additions & 7 deletions tokio-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ categories = ["asynchronous", "network-programming", "filesystem"]
publish = false

[dependencies]
futures-core-preview = "0.3.0-alpha.17"
tokio-threadpool = { version = "0.2.0", path = "../tokio-threadpool" }
tokio-io = { version = "0.2.0", path = "../tokio-io" }
tokio-threadpool = { version = "0.2.0", path = "../tokio-threadpool" }

futures-core-preview = "= 0.3.0-alpha.17"
futures-util-preview = "= 0.3.0-alpha.17"

[dev-dependencies]
rand = "0.7"
tempfile = "3"
tokio-codec = { version = "0.2.0", path = "../tokio-codec" }
tokio = { version = "0.2.0", path = "../tokio" }
futures-channel-preview = "0.3.0-alpha.17"
futures-preview = { version = "0.3.0-alpha.17" }
futures-util-preview = "0.3.0-alpha.17"
# tokio-codec = { version = "0.2.0", path = "../tokio-codec" }
tokio = { version = "*", path = "../tokio" }
# futures-preview = { version = "0.3.0-alpha.17" }
# futures-util-preview = "0.3.0-alpha.17"
41 changes: 4 additions & 37 deletions tokio-fs/src/create_dir.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
use std::fs;
use std::future::Future;
use crate::asyncify;

use std::io;
use std::path::Path;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;

/// Creates a new, empty directory at the provided path
///
/// This is an async version of [`std::fs::create_dir`][std]
///
/// [std]: https://doc.rust-lang.org/std/fs/fn.create_dir.html
pub fn create_dir<P: AsRef<Path>>(path: P) -> CreateDirFuture<P> {
CreateDirFuture::new(path)
}

/// Future returned by `create_dir`.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct CreateDirFuture<P>
where
P: AsRef<Path>,
{
path: P,
}

impl<P> CreateDirFuture<P>
where
P: AsRef<Path>,
{
fn new(path: P) -> CreateDirFuture<P> {
CreateDirFuture { path }
}
}

impl<P> Future for CreateDirFuture<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::create_dir(&self.path))
}
pub async fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
asyncify(|| std::fs::create_dir(&path)).await
}
41 changes: 4 additions & 37 deletions tokio-fs/src/create_dir_all.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,14 @@
use std::fs;
use std::future::Future;
use crate::asyncify;

use std::io;
use std::path::Path;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;

/// Recursively create a directory and all of its parent components if they
/// are missing.
///
/// This is an async version of [`std::fs::create_dir_all`][std]
///
/// [std]: https://doc.rust-lang.org/std/fs/fn.create_dir_all.html
pub fn create_dir_all<P: AsRef<Path>>(path: P) -> CreateDirAllFuture<P> {
CreateDirAllFuture::new(path)
}

/// Future returned by `create_dir_all`.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct CreateDirAllFuture<P>
where
P: AsRef<Path>,
{
path: P,
}

impl<P> CreateDirAllFuture<P>
where
P: AsRef<Path>,
{
fn new(path: P) -> CreateDirAllFuture<P> {
CreateDirAllFuture { path }
}
}

impl<P> Future for CreateDirAllFuture<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::create_dir_all(&self.path))
}
pub async fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
asyncify(|| std::fs::create_dir_all(&path)).await
}
Loading