-
Notifications
You must be signed in to change notification settings - Fork 15
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
Implement Path::try_exists #48
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
extern crate autocfg; | ||
|
||
fn main() { | ||
let ac = autocfg::new(); | ||
// Allows `#[cfg(rustc_1_63)]` to be used in code | ||
ac.emit_rustc_version(1, 63); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ pub(crate) enum ErrorKind { | |
Canonicalize, | ||
ReadLink, | ||
SymlinkMetadata, | ||
#[allow(dead_code)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to put the method behind a feature flag, I needed to get clippy to not complain about this unused variant. Maybe there's a better way or pattern. |
||
FileExists, | ||
|
||
#[cfg(windows)] | ||
SeekRead, | ||
|
@@ -84,6 +86,7 @@ impl fmt::Display for Error { | |
Canonicalize => write!(formatter, "failed to canonicalize path `{}`", path), | ||
ReadLink => write!(formatter, "failed to read symbolic link `{}`", path), | ||
SymlinkMetadata => write!(formatter, "failed to query metadata of symlink `{}`", path), | ||
FileExists => write!(formatter, "failed to check file existance `{}`", path), | ||
|
||
#[cfg(windows)] | ||
SeekRead => write!(formatter, "failed to seek and read from `{}`", path), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#[allow(unused_imports)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import is only needed when In this case I think it affects the whole file, not just this one import. Which is not ideal |
||
use crate::errors::{Error, ErrorKind}; | ||
use std::fs; | ||
use std::io; | ||
use std::path::{Path, PathBuf}; | ||
|
@@ -8,6 +10,9 @@ use std::path::{Path, PathBuf}; | |
// | ||
// Because no one else can implement it, we can add methods backwards-compatibly. | ||
pub trait PathExt: crate::Sealed { | ||
/// Wrapper for [`Path::try_exists`](https://doc.rust-lang.org/std/path/struct.Path.html#method.try_exists). | ||
#[cfg(rustc_1_63)] | ||
fn fs_err_try_exists(&self) -> io::Result<bool>; | ||
/// Wrapper for [`crate::metadata`]. | ||
fn fs_err_metadata(&self) -> io::Result<fs::Metadata>; | ||
/// Wrapper for [`crate::symlink_metadata`]. | ||
|
@@ -21,6 +26,12 @@ pub trait PathExt: crate::Sealed { | |
} | ||
|
||
impl PathExt for Path { | ||
#[cfg(rustc_1_63)] | ||
fn fs_err_try_exists(&self) -> io::Result<bool> { | ||
self.try_exists() | ||
.map_err(|source| Error::build(source, ErrorKind::FileExists, self)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weirdly enough, |
||
} | ||
|
||
fn fs_err_metadata(&self) -> io::Result<fs::Metadata> { | ||
crate::metadata(self) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure best practice on features. Other things I considered include:
Adding an
all
feature that loads all features:Alternatively making a feature named
1_63
that would enable all features on 1.63+.