Skip to content

Commit 00a8433

Browse files
authored
Merge pull request #320 from Wassasin/183-async-path
Implement async Path & PathBuf
2 parents 612a94b + aa13ba7 commit 00a8433

31 files changed

+1127
-53
lines changed

src/fs/canonicalize.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::{Path, PathBuf};
2-
31
use crate::io;
2+
use crate::path::{Path, PathBuf};
43
use crate::task::blocking;
54

65
/// Returns the canonical form of a path.
@@ -33,5 +32,5 @@ use crate::task::blocking;
3332
/// ```
3433
pub async fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
3534
let path = path.as_ref().to_owned();
36-
blocking::spawn(async move { std::fs::canonicalize(path) }).await
35+
blocking::spawn(async move { std::fs::canonicalize(&path).map(Into::into) }).await
3736
}

src/fs/copy.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Copies the contents and permissions of a file to a new location.

src/fs/create_dir.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Creates a new directory.

src/fs/create_dir_all.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Creates a new directory and all of its parents if they are missing.

src/fs/dir_builder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use std::path::Path;
2-
31
use cfg_if::cfg_if;
42

53
use crate::future::Future;
64
use crate::io;
5+
use crate::path::Path;
76
use crate::task::blocking;
87

98
/// A builder for creating directories with configurable options.

src/fs/dir_entry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::ffi::OsString;
22
use std::fmt;
3-
use std::path::PathBuf;
43
use std::sync::Arc;
54

65
use cfg_if::cfg_if;
76

87
use crate::fs::{FileType, Metadata};
98
use crate::io;
9+
use crate::path::PathBuf;
1010
use crate::task::blocking;
1111

1212
/// An entry in a directory.
@@ -50,7 +50,7 @@ impl DirEntry {
5050
/// # Ok(()) }) }
5151
/// ```
5252
pub fn path(&self) -> PathBuf {
53-
self.0.path()
53+
self.0.path().into()
5454
}
5555

5656
/// Reads the metadata for this entry.

src/fs/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::cmp;
33
use std::fmt;
44
use std::io::{Read as _, Seek as _, Write as _};
55
use std::ops::{Deref, DerefMut};
6-
use std::path::Path;
76
use std::pin::Pin;
87
use std::sync::atomic::{AtomicBool, Ordering};
98
use std::sync::{Arc, Mutex};
@@ -13,6 +12,7 @@ use cfg_if::cfg_if;
1312
use crate::fs::{Metadata, Permissions};
1413
use crate::future;
1514
use crate::io::{self, Read, Seek, SeekFrom, Write};
15+
use crate::path::Path;
1616
use crate::prelude::*;
1717
use crate::task::{self, blocking, Context, Poll, Waker};
1818

src/fs/hard_link.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Creates a hard link on the filesystem.

src/fs/metadata.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::path::Path;
2-
31
use cfg_if::cfg_if;
42

53
use crate::io;
4+
use crate::path::Path;
65
use crate::task::blocking;
76

87
/// Reads metadata for a path.

src/fs/open_options.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::path::Path;
2-
31
use cfg_if::cfg_if;
42

53
use crate::fs::File;
64
use crate::future::Future;
75
use crate::io;
6+
use crate::path::Path;
87
use crate::task::blocking;
98

109
/// A builder for opening files with configurable options.

src/fs/read.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Reads the entire contents of a file as raw bytes.

src/fs/read_dir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::path::Path;
21
use std::pin::Pin;
32

43
use crate::fs::DirEntry;
54
use crate::future::Future;
65
use crate::io;
6+
use crate::path::Path;
77
use crate::stream::Stream;
88
use crate::task::{blocking, Context, JoinHandle, Poll};
99

src/fs/read_link.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::{Path, PathBuf};
2-
31
use crate::io;
2+
use crate::path::{Path, PathBuf};
43
use crate::task::blocking;
54

65
/// Reads a symbolic link and returns the path it points to.
@@ -29,5 +28,5 @@ use crate::task::blocking;
2928
/// ```
3029
pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
3130
let path = path.as_ref().to_owned();
32-
blocking::spawn(async move { std::fs::read_link(path) }).await
31+
blocking::spawn(async move { std::fs::read_link(path).map(Into::into) }).await
3332
}

src/fs/read_to_string.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Reads the entire contents of a file as a string.

src/fs/remove_dir.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Removes an empty directory.

src/fs/remove_dir_all.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Removes a directory and all of its contents.

src/fs/remove_file.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Removes a file.

src/fs/rename.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Renames a file or directory to a new location.

src/fs/set_permissions.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::path::Path;
2-
31
use crate::fs::Permissions;
42
use crate::io;
3+
use crate::path::Path;
54
use crate::task::blocking;
65

76
/// Changes the permissions of a file or directory.

src/fs/symlink_metadata.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::path::Path;
2-
31
use crate::fs::Metadata;
42
use crate::io;
3+
use crate::path::Path;
54
use crate::task::blocking;
65

76
/// Reads metadata for a path without following symbolic links.

src/fs/write.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::Path;
2-
31
use crate::io;
2+
use crate::path::Path;
43
use crate::task::blocking;
54

65
/// Writes a slice of bytes as the new contents of a file.

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,14 @@ pub mod future;
5555
pub mod io;
5656
pub mod net;
5757
pub mod os;
58+
pub mod path;
5859
pub mod prelude;
5960
pub mod stream;
6061
pub mod sync;
6162
pub mod task;
6263

6364
cfg_if! {
6465
if #[cfg(any(feature = "unstable", feature = "docs"))] {
65-
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
66-
pub mod path;
6766
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
6867
pub mod pin;
6968
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]

src/os/unix/fs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
//! Unix-specific filesystem extensions.
22
3-
use std::path::Path;
4-
53
use cfg_if::cfg_if;
64

75
use crate::io;
6+
use crate::path::Path;
87
use crate::task::blocking;
98

109
/// Creates a new symbolic link on the filesystem.

src/os/unix/net/datagram.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::fmt;
44
use std::net::Shutdown;
5-
use std::path::Path;
65

76
use mio_uds;
87

@@ -11,6 +10,7 @@ use crate::future;
1110
use crate::io;
1211
use crate::net::driver::Watcher;
1312
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
13+
use crate::path::Path;
1414
use crate::task::blocking;
1515

1616
/// A Unix datagram socket.

src/os/unix/net/listener.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Unix-specific networking extensions.
22
33
use std::fmt;
4-
use std::path::Path;
54
use std::pin::Pin;
65

76
use mio_uds;
@@ -12,6 +11,7 @@ use crate::future::{self, Future};
1211
use crate::io;
1312
use crate::net::driver::Watcher;
1413
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
14+
use crate::path::Path;
1515
use crate::stream::Stream;
1616
use crate::task::{blocking, Context, Poll};
1717

src/os/unix/net/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ mod stream;
1313
cfg_if! {
1414
if #[cfg(feature = "docs")] {
1515
use std::fmt;
16-
use std::path::Path;
16+
17+
use crate::path::Path;
1718

1819
/// An address associated with a Unix socket.
1920
///
@@ -65,9 +66,8 @@ cfg_if! {
6566
/// With a pathname:
6667
///
6768
/// ```no_run
68-
/// use std::path::Path;
69-
///
7069
/// use async_std::os::unix::net::UnixListener;
70+
/// use async_std::path::Path;
7171
///
7272
/// let socket = UnixListener::bind("/tmp/socket").await?;
7373
/// let addr = socket.local_addr()?;

src/os/unix/net/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::fmt;
44
use std::io::{Read as _, Write as _};
55
use std::net::Shutdown;
6-
use std::path::Path;
76
use std::pin::Pin;
87

98
use mio_uds;
@@ -12,6 +11,7 @@ use super::SocketAddr;
1211
use crate::io::{self, Read, Write};
1312
use crate::net::driver::Watcher;
1413
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
14+
use crate::path::Path;
1515
use crate::task::{blocking, Context, Poll};
1616

1717
/// A Unix stream socket.

src/path/ancestors.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::iter::FusedIterator;
2+
3+
use crate::path::Path;
4+
5+
/// An iterator over [`Path`] and its ancestors.
6+
///
7+
/// This `struct` is created by the [`ancestors`] method on [`Path`].
8+
/// See its documentation for more.
9+
///
10+
/// # Examples
11+
///
12+
/// ```
13+
/// use async_std::path::Path;
14+
///
15+
/// let path = Path::new("/foo/bar");
16+
///
17+
/// for ancestor in path.ancestors() {
18+
/// println!("{}", ancestor.display());
19+
/// }
20+
/// ```
21+
///
22+
/// [`ancestors`]: struct.Path.html#method.ancestors
23+
/// [`Path`]: struct.Path.html
24+
#[derive(Copy, Clone, Debug)]
25+
pub struct Ancestors<'a> {
26+
pub(crate) next: Option<&'a Path>,
27+
}
28+
29+
impl<'a> Iterator for Ancestors<'a> {
30+
type Item = &'a Path;
31+
32+
fn next(&mut self) -> Option<Self::Item> {
33+
let next = self.next;
34+
self.next = next.and_then(Path::parent);
35+
next
36+
}
37+
}
38+
39+
impl FusedIterator for Ancestors<'_> {}

src/path/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
//!
55
//! [`std::path`]: https://doc.rust-lang.org/std/path/index.html
66
7+
mod ancestors;
8+
mod path;
9+
mod pathbuf;
10+
711
// Structs re-export
812
#[doc(inline)]
9-
pub use std::path::{Ancestors, Components, Display, Iter, PrefixComponent, StripPrefixError};
13+
pub use std::path::{Components, Display, Iter, PrefixComponent, StripPrefixError};
1014

1115
// Enums re-export
1216
#[doc(inline)]
@@ -19,3 +23,7 @@ pub use std::path::MAIN_SEPARATOR;
1923
// Functions re-export
2024
#[doc(inline)]
2125
pub use std::path::is_separator;
26+
27+
use ancestors::Ancestors;
28+
pub use path::Path;
29+
pub use pathbuf::PathBuf;

0 commit comments

Comments
 (0)