Skip to content

Commit e27b578

Browse files
committed
WIP init Path and PathBuf async stubs
1 parent 30b5ca5 commit e27b578

File tree

5 files changed

+151
-5
lines changed

5 files changed

+151
-5
lines changed

src/fs/canonicalize.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::{Path, PathBuf};
1+
use crate::path::{Path, PathBuf};
22

33
use crate::io;
44
use crate::task::blocking;
@@ -32,6 +32,8 @@ use crate::task::blocking;
3232
/// # Ok(()) }) }
3333
/// ```
3434
pub async fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
35-
let path = path.as_ref().to_owned();
36-
blocking::spawn(async move { std::fs::canonicalize(path) }).await
35+
let path: std::path::PathBuf = path.as_ref().to_path_buf().into();
36+
Ok(blocking::spawn(async move { std::fs::canonicalize(&path) })
37+
.await?
38+
.into())
3739
}

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

src/path/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
//!
55
//! [`std::path`]: https://doc.rust-lang.org/std/path/index.html
66
7+
mod path;
8+
mod pathbuf;
9+
710
// Structs re-export
811
#[doc(inline)]
912
pub use std::path::{Ancestors, Components, Display, Iter, PrefixComponent, StripPrefixError};
@@ -19,3 +22,6 @@ pub use std::path::MAIN_SEPARATOR;
1922
// Functions re-export
2023
#[doc(inline)]
2124
pub use std::path::is_separator;
25+
26+
pub use path::Path;
27+
pub use pathbuf::PathBuf;

src/path/path.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use std::ffi::OsStr;
2+
3+
use crate::path::PathBuf;
4+
use crate::{fs, io};
5+
6+
/// This struct is an async version of [`std::path::Path`].
7+
///
8+
/// [`std::path::Path`]: https://doc.rust-lang.org/std/path/struct.Path.html
9+
pub struct Path {
10+
inner: OsStr,
11+
}
12+
13+
impl Path {
14+
/// Yields the underlying [`OsStr`] slice.
15+
///
16+
/// [`OsStr`]: https://doc.rust-lang.org/std/ffi/struct.OsStr.html
17+
pub fn as_os_str(&self) -> &OsStr {
18+
&self.inner
19+
}
20+
21+
/// Returns the canonical, absolute form of the path with all intermediate
22+
/// components normalized and symbolic links resolved.
23+
///
24+
/// This is an alias to [`fs::canonicalize`].
25+
///
26+
/// [`fs::canonicalize`]: ../fs/fn.canonicalize.html
27+
///
28+
/// # Examples
29+
///
30+
/// ```no_run
31+
/// use crate::path::{Path, PathBuf};
32+
///
33+
/// let path = Path::new("/foo/test/../test/bar.rs");
34+
/// assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
35+
/// ```
36+
pub async fn canonicalize(&self) -> io::Result<PathBuf> {
37+
fs::canonicalize(self).await
38+
}
39+
40+
/// Directly wraps a string slice as a `Path` slice.
41+
///
42+
/// This is a cost-free conversion.
43+
///
44+
/// # Examples
45+
///
46+
/// ```
47+
/// use crate::path::Path;
48+
///
49+
/// Path::new("foo.txt");
50+
/// ```
51+
///
52+
/// You can create `Path`s from `String`s, or even other `Path`s:
53+
///
54+
/// ```
55+
/// use crate::path::Path;
56+
///
57+
/// let string = String::from("foo.txt");
58+
/// let from_string = Path::new(&string);
59+
/// let from_path = Path::new(&from_string);
60+
/// assert_eq!(from_string, from_path);
61+
/// ```
62+
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
63+
unsafe { &*(s.as_ref() as *const OsStr as *const Path) }
64+
}
65+
66+
/// Converts a `Path` to an owned [`PathBuf`].
67+
///
68+
/// [`PathBuf`]: struct.PathBuf.html
69+
///
70+
/// # Examples
71+
///
72+
/// ```
73+
/// use crate::path::{Path, PathBuf};
74+
///
75+
/// let path_buf = Path::new("foo.txt").to_path_buf();
76+
/// assert_eq!(path_buf, PathBuf::from("foo.txt"));
77+
/// ```
78+
pub fn to_path_buf(&self) -> PathBuf {
79+
PathBuf::from(self.inner.to_os_string())
80+
}
81+
}
82+
83+
impl<'a> From<&'a std::path::Path> for &'a Path {
84+
fn from(path: &'a std::path::Path) -> &'a Path {
85+
&Path::new(path.as_os_str())
86+
}
87+
}
88+
89+
impl<'a> Into<&'a std::path::Path> for &'a Path {
90+
fn into(self) -> &'a std::path::Path {
91+
std::path::Path::new(&self.inner)
92+
}
93+
}
94+
95+
impl AsRef<Path> for Path {
96+
fn as_ref(&self) -> &Path {
97+
self
98+
}
99+
}
100+
101+
impl std::fmt::Debug for Path {
102+
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103+
std::fmt::Debug::fmt(&self.inner, formatter)
104+
}
105+
}

src/path/pathbuf.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::ffi::OsString;
2+
3+
/// This struct is an async version of [`std::path::PathBuf`].
4+
///
5+
/// [`std::path::Path`]: https://doc.rust-lang.org/std/path/struct.PathBuf.html
6+
pub struct PathBuf {
7+
inner: OsString,
8+
}
9+
10+
impl From<std::path::PathBuf> for PathBuf {
11+
fn from(path: std::path::PathBuf) -> PathBuf {
12+
PathBuf {
13+
inner: path.into_os_string(),
14+
}
15+
}
16+
}
17+
18+
impl Into<std::path::PathBuf> for PathBuf {
19+
fn into(self) -> std::path::PathBuf {
20+
self.inner.into()
21+
}
22+
}
23+
24+
impl From<OsString> for PathBuf {
25+
fn from(path: OsString) -> PathBuf {
26+
PathBuf { inner: path }
27+
}
28+
}
29+
30+
impl std::fmt::Debug for PathBuf {
31+
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32+
std::fmt::Debug::fmt(&self.inner, formatter)
33+
}
34+
}

0 commit comments

Comments
 (0)