-
Notifications
You must be signed in to change notification settings - Fork 340
Implement async Path & PathBuf #320
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
e27b578
WIP init Path and PathBuf async stubs
Wassasin 3bd6a9d
Implemented components
Wassasin 930b818
Use std variants of Path and PathBuf internally
Wassasin e690b55
Implemented fs::metadata and Path::exists
Wassasin 6bbfd03
Fixed various tests
Wassasin 6c6106a
Implemented Path::{metadata, symlink_metadata}
Wassasin a57ba7e
Implemented Path::into_path_buf
Wassasin 759e357
Implemented Path::ancestors
Wassasin 5235cd5
Implemented Path::display
Wassasin 4070833
Implemented Path::ends_with
Wassasin a7eaae9
Implemented Path::extension
Wassasin a6e1abe
Implemented Path::file_name
Wassasin 28e936f
Implemented Path::file_stem
Wassasin 3a9597c
Implemented Path::has_root
Wassasin 20f58ea
Implemented Path::is_absolute
Wassasin df9a01f
Implemented Path::is_file
Wassasin 5d87006
Implemented Path::is_relative
Wassasin 0c03b92
Implemented Path::iter
Wassasin cc57db0
Implemented Path::join
Wassasin 141954d
Implemented Path::parent
Wassasin 89f73d3
Implemented Path::read_dir
Wassasin d349333
Implemented Path::read_link
Wassasin 942403c
Implemented Path::starts_with
Wassasin df53a07
Implemented Path::strip_prefix
Wassasin ea43d7f
Implemented Path::to_str
Wassasin a17b017
Implemented Path::to_string_lossy
Wassasin 3c24b18
Implemented Path::with_extension
Wassasin 409a10a
Implemented Path::with_file_name
Wassasin 1bd17f1
Implemented PathBuf::as_path
Wassasin 80eaa28
Implemented PathBuf::into_boxed_path
Wassasin 47ef222
Implemented PathBuf::into_os_string
Wassasin 71125d5
Implemented PathBuf::new
Wassasin 07f9e48
Implemented PathBuf::pop
Wassasin cc417cc
Implemented PathBuf::push
Wassasin 54c94b7
Implemented PathBuf::set_extension
Wassasin 8df55dd
Implemented PathBuf::set_file_name
Wassasin ba87048
Implemented our own Path::ancestors iterator
Wassasin 0adcb50
Add ToOwned and Borrow impls
f9cfee9
Formatting
504f8cb
Use crate::path everywhere
5c1e052
Fix failing tests
aa13ba7
Refactor
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use std::ffi::OsStr; | ||
|
||
use crate::path::PathBuf; | ||
use crate::{fs, io}; | ||
|
||
/// This struct is an async version of [`std::path::Path`]. | ||
/// | ||
/// [`std::path::Path`]: https://doc.rust-lang.org/std/path/struct.Path.html | ||
pub struct Path { | ||
inner: OsStr, | ||
} | ||
|
||
impl Path { | ||
/// Yields the underlying [`OsStr`] slice. | ||
/// | ||
/// [`OsStr`]: https://doc.rust-lang.org/std/ffi/struct.OsStr.html | ||
pub fn as_os_str(&self) -> &OsStr { | ||
&self.inner | ||
} | ||
|
||
/// Returns the canonical, absolute form of the path with all intermediate | ||
/// components normalized and symbolic links resolved. | ||
/// | ||
/// This is an alias to [`fs::canonicalize`]. | ||
/// | ||
/// [`fs::canonicalize`]: ../fs/fn.canonicalize.html | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use crate::path::{Path, PathBuf}; | ||
/// | ||
/// let path = Path::new("/foo/test/../test/bar.rs"); | ||
/// assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs")); | ||
/// ``` | ||
pub async fn canonicalize(&self) -> io::Result<PathBuf> { | ||
fs::canonicalize(self).await | ||
} | ||
|
||
/// Directly wraps a string slice as a `Path` slice. | ||
/// | ||
/// This is a cost-free conversion. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use crate::path::Path; | ||
/// | ||
/// Path::new("foo.txt"); | ||
/// ``` | ||
/// | ||
/// You can create `Path`s from `String`s, or even other `Path`s: | ||
/// | ||
/// ``` | ||
/// use crate::path::Path; | ||
/// | ||
/// let string = String::from("foo.txt"); | ||
/// let from_string = Path::new(&string); | ||
/// let from_path = Path::new(&from_string); | ||
/// assert_eq!(from_string, from_path); | ||
/// ``` | ||
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path { | ||
unsafe { &*(s.as_ref() as *const OsStr as *const Path) } | ||
} | ||
|
||
/// Converts a `Path` to an owned [`PathBuf`]. | ||
/// | ||
/// [`PathBuf`]: struct.PathBuf.html | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use crate::path::{Path, PathBuf}; | ||
/// | ||
/// let path_buf = Path::new("foo.txt").to_path_buf(); | ||
/// assert_eq!(path_buf, PathBuf::from("foo.txt")); | ||
/// ``` | ||
pub fn to_path_buf(&self) -> PathBuf { | ||
PathBuf::from(self.inner.to_os_string()) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a std::path::Path> for &'a Path { | ||
fn from(path: &'a std::path::Path) -> &'a Path { | ||
&Path::new(path.as_os_str()) | ||
} | ||
} | ||
|
||
impl<'a> Into<&'a std::path::Path> for &'a Path { | ||
fn into(self) -> &'a std::path::Path { | ||
std::path::Path::new(&self.inner) | ||
} | ||
} | ||
|
||
impl AsRef<Path> for Path { | ||
fn as_ref(&self) -> &Path { | ||
self | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for Path { | ||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
std::fmt::Debug::fmt(&self.inner, formatter) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::ffi::OsString; | ||
|
||
/// This struct is an async version of [`std::path::PathBuf`]. | ||
/// | ||
/// [`std::path::Path`]: https://doc.rust-lang.org/std/path/struct.PathBuf.html | ||
pub struct PathBuf { | ||
inner: OsString, | ||
} | ||
|
||
impl From<std::path::PathBuf> for PathBuf { | ||
fn from(path: std::path::PathBuf) -> PathBuf { | ||
PathBuf { | ||
inner: path.into_os_string(), | ||
} | ||
} | ||
} | ||
|
||
impl Into<std::path::PathBuf> for PathBuf { | ||
fn into(self) -> std::path::PathBuf { | ||
self.inner.into() | ||
} | ||
} | ||
|
||
impl From<OsString> for PathBuf { | ||
fn from(path: OsString) -> PathBuf { | ||
PathBuf { inner: path } | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for PathBuf { | ||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
std::fmt::Debug::fmt(&self.inner, formatter) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.