Skip to content

Commit ee31f68

Browse files
bors[bot]Stjepan Glavinayoshuawuyts
authored
Merge #190 #200
190: Clean up the fs module and a few other places r=stjepang a=stjepang Just a cleanup for various pieces of documentation, mainly around the `lib.rs` docs, the prelude, and the `fs` module. Some small bugs are also fixed along the way. This PR is the first one in a series of review PRs that I will be submitting before the upcoming 1.0 release. 200: expose IoSlice, IoSliceMut r=stjepang a=yoshuawuyts Exposes `io::IoSlice` and `io::IoSliceMut`. Given we're returning these from `read_vectored` and `write_vectored` it might make sense to just include them as part of our re-exports. Thanks! Ref #131. Co-authored-by: Stjepan Glavina <[email protected]> Co-authored-by: Yoshua Wuyts <[email protected]>
3 parents 36c437b + 7f71af9 + ab112e9 commit ee31f68

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+845
-413
lines changed

Diff for: Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ name = "async-std"
33
version = "0.99.5"
44
authors = [
55
"Stjepan Glavina <[email protected]>",
6-
"The async-std Project Developers",
6+
"Yoshua Wuyts <[email protected]>",
7+
"Contributors to async-std",
78
]
89
edition = "2018"
910
license = "Apache-2.0/MIT"

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Async version of Rust's standard library
1+
# Async version of the Rust standard library
22

33
[![Build Status](https://travis-ci.com/async-rs/async-std.svg?branch=master)](https://travis-ci.com/async-rs/async-std)
44
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](https://github.com/async-rs/async-std)

Diff for: examples/list-dir.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ fn main() -> io::Result<()> {
1313
task::block_on(async {
1414
let mut dir = fs::read_dir(&path).await?;
1515

16-
while let Some(entry) = dir.next().await {
17-
println!("{}", entry?.file_name().to_string_lossy());
16+
while let Some(res) = dir.next().await {
17+
let entry = res?;
18+
println!("{}", entry.file_name().to_string_lossy());
1819
}
1920

2021
Ok(())

Diff for: src/fs/canonicalize.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::fs;
21
use std::path::{Path, PathBuf};
32

43
use crate::io;
@@ -15,10 +14,11 @@ use crate::task::blocking;
1514
///
1615
/// # Errors
1716
///
18-
/// An error will be returned in the following situations (not an exhaustive list):
17+
/// An error will be returned in the following situations:
1918
///
20-
/// * `path` does not exist.
21-
/// * A non-final component in path is not a directory.
19+
/// * `path` does not point to an existing file or directory.
20+
/// * A non-final component in `path` is not a directory.
21+
/// * Some other I/O error occurred.
2222
///
2323
/// # Examples
2424
///
@@ -33,5 +33,5 @@ use crate::task::blocking;
3333
/// ```
3434
pub async fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
3535
let path = path.as_ref().to_owned();
36-
blocking::spawn(async move { fs::canonicalize(path) }).await
36+
blocking::spawn(async move { std::fs::canonicalize(path) }).await
3737
}

Diff for: src/fs/copy.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
use std::fs;
21
use std::path::Path;
32

43
use crate::io;
54
use crate::task::blocking;
65

7-
/// Copies the contents and permissions of one file to another.
6+
/// Copies the contents and permissions of a file to a new location.
87
///
9-
/// On success, the total number of bytes copied is returned and equals the length of the `from`
10-
/// file.
8+
/// On success, the total number of bytes copied is returned and equals the length of the `to` file
9+
/// after this operation.
1110
///
1211
/// The old contents of `to` will be overwritten. If `from` and `to` both point to the same file,
13-
/// then the file will likely get truncated by this operation.
12+
/// then the file will likely get truncated as a result of this operation.
13+
///
14+
/// If you're working with open [`File`]s and want to copy contents through those types, use the
15+
/// [`io::copy`] function.
1416
///
1517
/// This function is an async version of [`std::fs::copy`].
1618
///
19+
/// [`File`]: struct.File.html
20+
/// [`io::copy`]: ../io/fn.copy.html
1721
/// [`std::fs::copy`]: https://doc.rust-lang.org/std/fs/fn.copy.html
1822
///
1923
/// # Errors
2024
///
21-
/// An error will be returned in the following situations (not an exhaustive list):
25+
/// An error will be returned in the following situations:
2226
///
23-
/// * The `from` path is not a file.
24-
/// * The `from` file does not exist.
25-
/// * The current process lacks permissions to access `from` or write `to`.
27+
/// * `from` does not point to an existing file.
28+
/// * The current process lacks permissions to read `from` or write `to`.
29+
/// * Some other I/O error occurred.
2630
///
2731
/// # Examples
2832
///
@@ -31,12 +35,12 @@ use crate::task::blocking;
3135
/// #
3236
/// use async_std::fs;
3337
///
34-
/// let bytes_copied = fs::copy("a.txt", "b.txt").await?;
38+
/// let num_bytes = fs::copy("a.txt", "b.txt").await?;
3539
/// #
3640
/// # Ok(()) }) }
3741
/// ```
3842
pub async fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
3943
let from = from.as_ref().to_owned();
4044
let to = to.as_ref().to_owned();
41-
blocking::spawn(async move { fs::copy(&from, &to) }).await
45+
blocking::spawn(async move { std::fs::copy(&from, &to) }).await
4246
}

Diff for: src/fs/create_dir.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
use std::fs;
21
use std::path::Path;
32

43
use crate::io;
54
use crate::task::blocking;
65

7-
/// Creates a new, empty directory.
6+
/// Creates a new directory.
7+
///
8+
/// Note that this function will only create the final directory in `path`. If you want to create
9+
/// all of its missing parent directories too, use the [`create_dir_all`] function instead.
810
///
911
/// This function is an async version of [`std::fs::create_dir`].
1012
///
13+
/// [`create_dir_all`]: fn.create_dir_all.html
1114
/// [`std::fs::create_dir`]: https://doc.rust-lang.org/std/fs/fn.create_dir.html
1215
///
1316
/// # Errors
1417
///
15-
/// An error will be returned in the following situations (not an exhaustive list):
18+
/// An error will be returned in the following situations:
1619
///
17-
/// * `path` already exists.
18-
/// * A parent of the given path does not exist.
19-
/// * The current process lacks permissions to create directory at `path`.
20+
/// * `path` already points to an existing file or directory.
21+
/// * A parent directory in `path` does not exist.
22+
/// * The current process lacks permissions to create the directory.
23+
/// * Some other I/O error occurred.
2024
///
2125
/// # Examples
2226
///
@@ -25,11 +29,11 @@ use crate::task::blocking;
2529
/// #
2630
/// use async_std::fs;
2731
///
28-
/// fs::create_dir("./some/dir").await?;
32+
/// fs::create_dir("./some/directory").await?;
2933
/// #
3034
/// # Ok(()) }) }
3135
/// ```
3236
pub async fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
3337
let path = path.as_ref().to_owned();
34-
blocking::spawn(async move { fs::create_dir(path) }).await
38+
blocking::spawn(async move { std::fs::create_dir(path) }).await
3539
}

Diff for: src/fs/create_dir_all.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
use std::fs;
21
use std::path::Path;
32

43
use crate::io;
54
use crate::task::blocking;
65

7-
/// Creates a new, empty directory and all of its parents if they are missing.
6+
/// Creates a new directory and all of its parents if they are missing.
87
///
98
/// This function is an async version of [`std::fs::create_dir_all`].
109
///
1110
/// [`std::fs::create_dir_all`]: https://doc.rust-lang.org/std/fs/fn.create_dir_all.html
1211
///
1312
/// # Errors
1413
///
15-
/// An error will be returned in the following situations (not an exhaustive list):
14+
/// An error will be returned in the following situations:
1615
///
17-
/// * The parent directories do not exists and couldn't be created.
18-
/// * The current process lacks permissions to create directory at `path`.
16+
/// * `path` already points to an existing file or directory.
17+
/// * The current process lacks permissions to create the directory or its missing parents.
18+
/// * Some other I/O error occurred.
1919
///
2020
/// # Examples
2121
///
@@ -24,11 +24,11 @@ use crate::task::blocking;
2424
/// #
2525
/// use async_std::fs;
2626
///
27-
/// fs::create_dir_all("./some/dir").await?;
27+
/// fs::create_dir_all("./some/directory").await?;
2828
/// #
2929
/// # Ok(()) }) }
3030
/// ```
3131
pub async fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
3232
let path = path.as_ref().to_owned();
33-
blocking::spawn(async move { fs::create_dir_all(path) }).await
33+
blocking::spawn(async move { std::fs::create_dir_all(path) }).await
3434
}

Diff for: src/fs/dir_builder.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::fs;
21
use std::path::Path;
32

43
use cfg_if::cfg_if;
@@ -7,21 +6,28 @@ use crate::future::Future;
76
use crate::io;
87
use crate::task::blocking;
98

10-
/// A builder for creating directories in various manners.
9+
/// A builder for creating directories with configurable options.
10+
///
11+
/// For Unix-specific options, import the [`os::unix::fs::DirBuilderExt`] trait.
1112
///
1213
/// This type is an async version of [`std::fs::DirBuilder`].
1314
///
15+
/// [`os::unix::fs::DirBuilderExt`]: ../os/unix/fs/trait.DirBuilderExt.html
1416
/// [`std::fs::DirBuilder`]: https://doc.rust-lang.org/std/fs/struct.DirBuilder.html
1517
#[derive(Debug)]
1618
pub struct DirBuilder {
19+
/// Set to `true` if non-existent parent directories should be created.
1720
recursive: bool,
1821

22+
/// Unix mode for newly created directories.
1923
#[cfg(unix)]
2024
mode: Option<u32>,
2125
}
2226

2327
impl DirBuilder {
24-
/// Creates a new builder with [`recursive`] set to `false`.
28+
/// Creates a blank set of options.
29+
///
30+
/// The [`recursive`] option is initially set to `false`.
2531
///
2632
/// [`recursive`]: #method.recursive
2733
///
@@ -33,25 +39,24 @@ impl DirBuilder {
3339
/// let builder = DirBuilder::new();
3440
/// ```
3541
pub fn new() -> DirBuilder {
42+
#[cfg(not(unix))]
43+
let builder = DirBuilder { recursive: false };
44+
3645
#[cfg(unix)]
3746
let builder = DirBuilder {
3847
recursive: false,
3948
mode: None,
4049
};
4150

42-
#[cfg(windows)]
43-
let builder = DirBuilder { recursive: false };
44-
4551
builder
4652
}
4753

4854
/// Sets the option for recursive mode.
4955
///
50-
/// This option, when `true`, means that all parent directories should be created recursively
51-
/// if they don't exist. Parents are created with the same security settings and permissions as
52-
/// the final directory.
56+
/// When set to `true`, this option means all parent directories should be created recursively
57+
/// if they don't exist. Parents are created with the same permissions as the final directory.
5358
///
54-
/// This option defaults to `false`.
59+
/// This option is initially set to `false`.
5560
///
5661
/// # Examples
5762
///
@@ -70,6 +75,14 @@ impl DirBuilder {
7075
///
7176
/// It is considered an error if the directory already exists unless recursive mode is enabled.
7277
///
78+
/// # Errors
79+
///
80+
/// An error will be returned in the following situations:
81+
///
82+
/// * `path` already points to an existing file or directory.
83+
/// * The current process lacks permissions to create the directory or its missing parents.
84+
/// * Some other I/O error occurred.
85+
///
7386
/// # Examples
7487
///
7588
/// ```no_run
@@ -79,13 +92,13 @@ impl DirBuilder {
7992
///
8093
/// DirBuilder::new()
8194
/// .recursive(true)
82-
/// .create("/tmp/foo/bar/baz")
95+
/// .create("./some/directory")
8396
/// .await?;
8497
/// #
8598
/// # Ok(()) }) }
8699
/// ```
87100
pub fn create<P: AsRef<Path>>(&self, path: P) -> impl Future<Output = io::Result<()>> {
88-
let mut builder = fs::DirBuilder::new();
101+
let mut builder = std::fs::DirBuilder::new();
89102
builder.recursive(self.recursive);
90103

91104
#[cfg(unix)]

0 commit comments

Comments
 (0)